Search in sources :

Example 91 with ConfigurationException

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;
    }
}
Also used : Configuration(org.apache.commons.configuration.Configuration) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) SystemConfiguration(org.apache.commons.configuration.SystemConfiguration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) SystemConfiguration(org.apache.commons.configuration.SystemConfiguration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration)

Example 92 with ConfigurationException

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);
    }
}
Also used : Pattern(java.util.regex.Pattern) ConfigurationException(org.apache.commons.configuration.ConfigurationException) CommonsConfiguration(com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) File(java.io.File)

Example 93 with ConfigurationException

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);
}
Also used : ConfigElement(com.thinkaurelius.titan.diskstorage.configuration.ConfigElement) CommonsConfiguration(com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration) IOException(java.io.IOException) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) FileInputStream(java.io.FileInputStream) ConfigurationException(org.apache.commons.configuration.ConfigurationException)

Example 94 with ConfigurationException

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;
}
Also used : ConversionException(org.apache.commons.configuration.ConversionException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) ArrayList(java.util.ArrayList) ZapXmlConfiguration(org.zaproxy.zap.utils.ZapXmlConfiguration)

Example 95 with ConfigurationException

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;
    }
}
Also used : XMLConfiguration(org.apache.commons.configuration.XMLConfiguration) Configuration(org.apache.commons.configuration.Configuration) AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) XMLConfiguration(org.apache.commons.configuration.XMLConfiguration) CombinedConfiguration(org.apache.commons.configuration.CombinedConfiguration) ConfigurationException(org.apache.commons.configuration.ConfigurationException) ArrayList(java.util.ArrayList) Resource(org.springframework.core.io.Resource) CombinedConfiguration(org.apache.commons.configuration.CombinedConfiguration) ConfigurationException(org.apache.commons.configuration.ConfigurationException) OverrideCombiner(org.apache.commons.configuration.tree.OverrideCombiner)

Aggregations

ConfigurationException (org.apache.commons.configuration.ConfigurationException)168 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)64 IOException (java.io.IOException)53 File (java.io.File)40 URL (java.net.URL)17 Configuration (org.apache.commons.configuration.Configuration)14 MalformedURLException (java.net.MalformedURLException)13 ZapXmlConfiguration (org.zaproxy.zap.utils.ZapXmlConfiguration)13 BeforeClass (org.junit.BeforeClass)10 ArrayList (java.util.ArrayList)9 ActionEvent (java.awt.event.ActionEvent)8 CompositeConfiguration (org.apache.commons.configuration.CompositeConfiguration)8 ActionListener (java.awt.event.ActionListener)7 FileInputStream (java.io.FileInputStream)6 FileNotFoundException (java.io.FileNotFoundException)5 HashMap (java.util.HashMap)5 Properties (java.util.Properties)5 DistributedLogConfiguration (com.twitter.distributedlog.DistributedLogConfiguration)4 Iterator (java.util.Iterator)4 JCommander (com.beust.jcommander.JCommander)3