org.cinchapi.concourse.config.AbstractPreferences Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of concourse-config Show documentation
Show all versions of concourse-config Show documentation
A framework for reading and editing Concourse configuration files
The newest version!
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Jeff Nelson, Cinchapi Software Collective
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.cinchapi.concourse.config;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.commons.configuration.ConfigurationException;
import com.google.common.base.Throwables;
/**
* An abstract wrapper around a Concourse prefs file that provides the caller
* with a seamless way to get variables in a configured or default state. This
* class should be extended to handle specific variables that are defined in a
* specific prefs file.
*
* @author jnelson
*/
public abstract class AbstractPreferences {
/**
* The preferences file that contains the system variables to configure.
*/
private final String file;
/**
* The parser of and handler to the underlying preferences file. The
* subclass should use this object to access variables from the preferences
* file.
*/
private ConcourseConfiguration handler;
/**
* Construct a new instance.
*
* @param file
*/
protected AbstractPreferences(String file) {
this.file = file;
reload();
}
/**
* Return the handler to the underlying preferences file. The subclass
* should use the handler to read/write data.
*
* @return the preferences file handler
*/
protected final ConcourseConfiguration getHandler() {
return handler;
}
/**
* Set the value associated with {@code key} and persist it to the
* underlying preferences file.
*
* @param key
* @param value
*/
protected final void set(String key, Object value) {
handler.setProperty(key, value);
try {
handler.save();
}
catch (ConfigurationException e) {
throw Throwables.propagate(e);
}
}
/**
* Reload the data from the configuration file, in case anything has
* changed.
*/
protected final void reload() {
// Create an empty file if necessary. This means that the subclass can
// handle defaults in a seamless manner.
Path path = Paths.get(file);
if(!Files.exists(path)) {
try {
if(path.getParent() != null) {
Files.createDirectories(path.getParent());
}
Files.createFile(path);
}
catch (IOException e) {
throw Throwables.propagate(e);
}
}
handler = ConcourseConfiguration.loadConfig(file);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy