use of org.apache.commons.configuration.ConfigurationException in project es6draft by anba.
the class Resources method loadConfiguration.
/**
* Loads the configuration file.
*/
public static Configuration loadConfiguration(Class<?> clazz) {
TestConfiguration config = clazz.getAnnotation(TestConfiguration.class);
String file = config.file();
String name = config.name();
try {
PropertiesConfiguration properties = new PropertiesConfiguration();
// entries are mandatory unless an explicit default value was given
properties.setThrowExceptionOnMissing(true);
properties.getInterpolator().setParentInterpolator(MISSING_VAR);
properties.load(resource(file), "UTF-8");
Configuration configuration = new CompositeConfiguration(Arrays.asList(new SystemConfiguration(), properties));
return configuration.subset(name);
} catch (ConfigurationException | IOException e) {
throw new RuntimeException(e);
} catch (NoSuchElementException e) {
throw e;
}
}
use of org.apache.commons.configuration.ConfigurationException 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.ConfigurationException 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.ConfigurationException in project zaproxy by zaproxy.
the class VulnerabilitiesLoader method loadVulnerabilitiesFile.
private List<Vulnerability> loadVulnerabilitiesFile(Path file) {
ZapXmlConfiguration config;
try {
config = new ZapXmlConfiguration(file.toFile());
} catch (ConfigurationException e) {
logger.error(e.getMessage(), e);
return null;
}
String[] test;
try {
test = config.getStringArray("vuln_items");
} catch (ConversionException e) {
logger.error(e.getMessage(), e);
return null;
}
final int numberOfVulns = test.length;
List<Vulnerability> tempVulns = new ArrayList<>(numberOfVulns);
String name;
List<String> references;
for (String item : test) {
name = "vuln_item_" + item;
try {
references = new ArrayList<>(Arrays.asList(config.getStringArray(name + ".reference")));
} catch (ConversionException e) {
logger.error(e.getMessage(), e);
references = new ArrayList<>(0);
}
Vulnerability v = new Vulnerability(item, config.getString(name + ".alert"), config.getString(name + ".desc"), config.getString(name + ".solution"), references);
tempVulns.add(v);
}
return tempVulns;
}
use of org.apache.commons.configuration.ConfigurationException in project engine by craftercms.
the class MultiConfigurationBuilder method getConfiguration.
@Override
public Configuration getConfiguration() throws ConfigurationException {
List<Configuration> configs = new ArrayList<>();
// Last configurations should be loaded and added first so that they have greater priority.
logger.info("Loading XML configurations in the order in which the properties will be resolved");
for (int i = configPaths.length - 1; i >= 0; i--) {
try {
Resource resource = resourceLoader.getResource(configPaths[i]);
if (resource.exists()) {
XMLConfiguration config = new XMLConfiguration();
config.load(resource.getInputStream());
logger.info("XML configuration loaded from " + resource);
configs.add(config);
}
} catch (Exception e) {
throw new ConfigurationException("Unable to load configuration at " + configPaths[i], e);
}
}
if (configs.size() > 1) {
CombinedConfiguration combinedConfig = new CombinedConfiguration(new OverrideCombiner());
for (Configuration config : configs) {
combinedConfig.addConfiguration((AbstractConfiguration) config);
}
return combinedConfig;
} else if (configs.size() == 1) {
return configs.get(0);
} else {
return null;
}
}
Aggregations