use of org.apache.commons.configuration.PropertiesConfiguration in project ninja by ninjaframework.
the class MessagesImpl method loadLanguageConfiguration.
/**
* Attempts to load a message file and sets the file changed reloading
* strategy on the configuration if the runtime mode is Dev.
*/
private PropertiesConfiguration loadLanguageConfiguration(String fileOrUrl) {
PropertiesConfiguration configuration = SwissKnife.loadConfigurationInUtf8(fileOrUrl);
if (configuration != null && ninjaProperties.isDev()) {
// enable runtime reloading of translations in dev mode
FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
configuration.setReloadingStrategy(strategy);
}
return configuration;
}
use of org.apache.commons.configuration.PropertiesConfiguration in project ninja by ninjaframework.
the class SwissKnife method loadConfigurationInUtf8.
/**
* This is important: We load stuff as UTF-8.
*
* We are using in the default Apache Commons loading mechanism.
*
* With two little tweaks: 1. We don't accept any delimimter by default 2.
* We are reading in UTF-8
*
* More about that:
* http://commons.apache.org/configuration/userguide/howto_filebased
* .html#Loading
*
* From the docs: - If the combination from base path and file name is a
* full URL that points to an existing file, this URL will be used to load
* the file. - If the combination from base path and file name is an
* absolute file name and this file exists, it will be loaded. - If the
* combination from base path and file name is a relative file path that
* points to an existing file, this file will be loaded. - If a file with
* the specified name exists in the user's home directory, this file will be
* loaded. - Otherwise the file name is interpreted as a resource name, and
* it is checked whether the data file can be loaded from the classpath.
*
* @param fileOrUrlOrClasspathUrl Location of the file. Can be on file
* system, or on the classpath. Will both work.
* @return A PropertiesConfiguration or null if there were problems getting
* it.
*/
public static PropertiesConfiguration loadConfigurationInUtf8(String fileOrUrlOrClasspathUrl) {
PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
propertiesConfiguration.setEncoding(NinjaConstant.UTF_8);
propertiesConfiguration.setDelimiterParsingDisabled(true);
propertiesConfiguration.setFileName(fileOrUrlOrClasspathUrl);
propertiesConfiguration.getLayout().setSingleLine(NinjaConstant.applicationSecret, true);
try {
propertiesConfiguration.load(fileOrUrlOrClasspathUrl);
} catch (ConfigurationException e) {
logger.info("Could not load file " + fileOrUrlOrClasspathUrl + " (not a bad thing necessarily, but I am returing null)");
return null;
}
return propertiesConfiguration;
}
use of org.apache.commons.configuration.PropertiesConfiguration in project titan by thinkaurelius.
the class TitanFactory method getLocalConfiguration.
/**
* Load a properties file containing a Titan graph configuration.
* <p/>
* <ol>
* <li>Load the file contents into a {@link org.apache.commons.configuration.PropertiesConfiguration}</li>
* <li>For each key that points to a configuration object that is either a directory
* or local file, check
* whether the associated value is a non-null, non-absolute path. If so,
* then prepend the absolute path of the parent directory of the provided configuration {@code file}.
* This has the effect of making non-absolute backend
* paths relative to the config file's directory rather than the JVM's
* working directory.
* <li>Return the {@link ReadConfiguration} for the prepared configuration file</li>
* </ol>
* <p/>
*
* @param file A properties file to load
* @return A configuration derived from {@code file}
*/
@SuppressWarnings("unchecked")
private static ReadConfiguration getLocalConfiguration(File file) {
Preconditions.checkArgument(file != null && file.exists() && file.isFile() && file.canRead(), "Need to specify a readable configuration file, but was given: %s", file.toString());
try {
PropertiesConfiguration configuration = new PropertiesConfiguration(file);
final File tmpParent = file.getParentFile();
final File configParent;
if (null == tmpParent) {
/*
* null usually means we were given a Titan config file path
* string like "foo.properties" that refers to the current
* working directory of the process.
*/
configParent = new File(System.getProperty("user.dir"));
} else {
configParent = tmpParent;
}
Preconditions.checkNotNull(configParent);
Preconditions.checkArgument(configParent.isDirectory());
// TODO this mangling logic is a relic from the hardcoded string days; it should be deleted and rewritten as a setting on ConfigOption
final Pattern p = Pattern.compile("(" + Pattern.quote(STORAGE_NS.getName()) + "\\..*" + "(" + Pattern.quote(STORAGE_DIRECTORY.getName()) + "|" + Pattern.quote(STORAGE_CONF_FILE.getName()) + ")" + "|" + Pattern.quote(INDEX_NS.getName()) + "\\..*" + "(" + Pattern.quote(INDEX_DIRECTORY.getName()) + "|" + Pattern.quote(INDEX_CONF_FILE.getName()) + ")" + ")");
final Iterator<String> keysToMangle = Iterators.filter(configuration.getKeys(), new Predicate<String>() {
@Override
public boolean apply(String key) {
if (null == key)
return false;
return p.matcher(key).matches();
}
});
while (keysToMangle.hasNext()) {
String k = keysToMangle.next();
Preconditions.checkNotNull(k);
String s = configuration.getString(k);
Preconditions.checkArgument(StringUtils.isNotBlank(s), "Invalid Configuration: key %s has null empty value", k);
configuration.setProperty(k, getAbsolutePath(configParent, s));
}
return new CommonsConfiguration(configuration);
} catch (ConfigurationException e) {
throw new IllegalArgumentException("Could not load configuration at: " + file, e);
}
}
use of org.apache.commons.configuration.PropertiesConfiguration in project titan by thinkaurelius.
the class ConfigurationLint method validate.
public static Status validate(String filename) throws IOException {
Properties p = new Properties();
FileInputStream fis = new FileInputStream(filename);
p.load(fis);
fis.close();
final PropertiesConfiguration apc;
try {
apc = new PropertiesConfiguration(filename);
} catch (ConfigurationException e) {
throw new IOException(e);
}
// new ModifiableConfiguration(GraphDatabaseConfiguration.ROOT_NS,
// , BasicConfiguration.Restriction.NONE);
Iterator<String> iter = apc.getKeys();
int totalKeys = 0;
int keysVerified = 0;
while (iter.hasNext()) {
totalKeys++;
String key = iter.next();
String value = apc.getString(key);
try {
ConfigElement.PathIdentifier pid = ConfigElement.parse(GraphDatabaseConfiguration.ROOT_NS, key);
// ConfigElement shouldn't return null; failure here probably relates to titan-core, not the file
Preconditions.checkState(null != pid);
Preconditions.checkState(null != pid.element);
if (!pid.element.isOption()) {
log.warn("Config key {} is a namespace (only options can be keys)", key);
continue;
}
final ConfigOption<?> opt;
try {
opt = (ConfigOption<?>) pid.element;
} catch (RuntimeException re) {
// This shouldn't happen given the preceding check, but catch it anyway
log.warn("Config key {} maps to the element {}, but it could not be cast to an option", key, pid.element, re);
continue;
}
try {
Object o = new CommonsConfiguration(apc).get(key, opt.getDatatype());
opt.verify(o);
keysVerified++;
} catch (RuntimeException re) {
log.warn("Config key {} is recognized, but its value {} could not be validated", key, value);
log.debug("Validation exception on {}={} follows", key, value, re);
}
} catch (RuntimeException re) {
log.warn("Unknown config key {}", key);
}
}
return new Status(totalKeys, totalKeys - keysVerified);
}
use of org.apache.commons.configuration.PropertiesConfiguration in project distributedlog by twitter.
the class DistributedLogConfiguration method loadConf.
/**
* You can load configurations in precedence order. The first one takes
* precedence over any loaded later.
*
* @param confURL Configuration URL
*/
public void loadConf(URL confURL) throws ConfigurationException {
Configuration loadedConf = new PropertiesConfiguration(confURL);
addConfiguration(loadedConf);
}
Aggregations