use of org.apache.commons.configuration2.builder.fluent.Parameters in project selenium-tests by Wikia.
the class XMLReader method getValue.
/**
* method used to get credentials from configuration xml
*/
public static String getValue(File file, String key) {
if (!file.exists() || file.isDirectory()) {
throw new ConfigurationRuntimeException("Cannot find a file with credentials");
}
try {
Parameters params = new Parameters();
FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<>(XMLConfiguration.class).configure(params.fileBased().setFile(file));
org.apache.commons.configuration2.Configuration config = builder.getConfiguration();
return config.getString(key);
} catch (ConfigurationException e) {
throw new ConfigurationRuntimeException(e);
}
}
use of org.apache.commons.configuration2.builder.fluent.Parameters in project hadoop by apache.
the class MetricsConfig method loadFirst.
/**
* Load configuration from a list of files until the first successful load
* @param conf the configuration object
* @param files the list of filenames to try
* @return the configuration object
*/
static MetricsConfig loadFirst(String prefix, String... fileNames) {
for (String fname : fileNames) {
try {
Configuration cf = new Configurations().propertiesBuilder(fname).configure(new Parameters().properties().setFileName(fname).setListDelimiterHandler(new DefaultListDelimiterHandler(','))).getConfiguration().interpolatedConfiguration();
LOG.info("loaded properties from " + fname);
LOG.debug(toString(cf));
MetricsConfig mc = new MetricsConfig(cf, prefix);
LOG.debug(mc);
return mc;
} catch (ConfigurationException e) {
// Commons Configuration defines the message text when file not found
if (e.getMessage().startsWith("Could not locate")) {
continue;
}
throw new MetricsConfigException(e);
}
}
LOG.warn("Cannot locate configuration: tried " + Joiner.on(",").join(fileNames));
// default to an empty configuration
return new MetricsConfig(new PropertiesConfiguration(), prefix);
}
use of org.apache.commons.configuration2.builder.fluent.Parameters in project BRFS by zhangnianli.
the class ConfigurationLoader method load.
/**
* 加载配置文件,如果指定不使用缓存,每次都会重新加载
*
* @param fileName 配置文件路径
* @param useCache 是否使用缓存功能
* @return
*/
public static ConfigObj load(String fileName, boolean useCache) {
if (fileName == null) {
throw new NullPointerException("config file name is null.");
}
ConfigObj config = null;
if (useCache) {
config = CONFIG_CACHE.get(fileName);
}
if (config != null) {
return config;
}
try {
File configFile = new File(fileName);
if (!configFile.exists() || !configFile.isFile()) {
throw new RuntimeException("configuration file[" + fileName + "] is not valid.");
}
Parameters params = new Parameters();
CONFIGURATION_BUILDER.resetResult();
CONFIGURATION_BUILDER.configure(params.properties().setFileName(fileName).setEncoding(Charsets.UTF_8.name()).setThrowExceptionOnMissing(true));
config = new ConfigObj(CONFIGURATION_BUILDER.getConfiguration());
return config;
} catch (ConfigurationException e) {
throw new RuntimeException("load configuration file error", e);
} finally {
if (config != null) {
CONFIG_CACHE.put(fileName, config);
}
}
}
use of org.apache.commons.configuration2.builder.fluent.Parameters in project sponge by softelnet.
the class DefaultConfigurationManager method createXmlConfiguration.
protected Pair<XMLConfiguration, URL> createXmlConfiguration(String fileName) {
List<Lookup> lookups = Arrays.asList(new SystemPropertiesLookup(), new HomeLookup(), new ConfigLookup());
Parameters params = new Parameters();
FallbackBasePathLocationStrategy locationStrategy = new FallbackBasePathLocationStrategy(FileLocatorUtils.DEFAULT_LOCATION_STRATEGY, home);
FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<>(XMLConfiguration.class).configure(params.xml().setDefaultLookups(lookups).setLocationStrategy(locationStrategy).setFileName(fileName).setSchemaValidation(true).setEntityResolver(new ResourceSchemaResolver()));
try {
XMLConfiguration xmlConfiguration = builder.getConfiguration();
return new ImmutablePair<>(xmlConfiguration, locationStrategy.getLocatedUrl());
} catch (ConfigurationException e) {
throw new ConfigException("Error reading configuration file " + fileName, e);
}
}
use of org.apache.commons.configuration2.builder.fluent.Parameters in project midpoint by Evolveum.
the class StartupConfiguration method createXmlConfiguration.
private void createXmlConfiguration(String filename) throws ConfigurationException {
Map<String, Lookup> lookups = new HashMap<>(ConfigurationInterpolator.getDefaultPrefixLookups());
lookups.put(RandomLookup.PREFIX, new RandomLookup());
lookups.put(HostnameLookup.PREFIX, new HostnameLookup());
FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<>(XMLConfiguration.class).configure(new Parameters().xml().setFileName(filename).setPrefixLookups(lookups));
/*
On debug level this shows stacktrace for:
DEBUG org.apache.commons.beanutils.FluentPropertyBeanIntrospector - Exception is:
java.beans.IntrospectionException: bad write method arg count:
public final void org.apache.commons.configuration2.AbstractConfiguration.setProperty
This is reportedly beanutils over-strictness issue but is nowhere close to be fixed.
Jira for commons-configuration can be also found, but they rely on beanutils fix.
*/
config = builder.getConfiguration();
config.addProperty(MIDPOINT_HOME_PROPERTY, midPointHomePath);
applyEnvironmentProperties();
resolveFileReferences();
}
Aggregations