Example usage for org.apache.commons.configuration FileConfiguration load

List of usage examples for org.apache.commons.configuration FileConfiguration load

Introduction

In this page you can find the example usage for org.apache.commons.configuration FileConfiguration load.

Prototype

void load(Reader in) throws ConfigurationException;

Source Link

Document

Load the configuration from the specified reader.

Usage

From source file:de.devsurf.injection.guice.integrations.commons.configuration.CommonsConfigurationFeature.java

@SuppressWarnings("unchecked")
@Override/*from  w  w w.java2 s  .  com*/
public void process(Class<Object> annotatedClass, Map<String, Annotation> annotations) {
    Configuration config = (Configuration) annotations.get(Configuration.class.getName());
    Named name = config.name();

    // TODO Implement Location overriding
    URL url;
    switch (config.location().type()) {
    case FILE:
        File file = new File(config.location().value());
        if (!file.exists()) {
            _logger.log(Level.WARNING, "Ignoring Configuration " + name + " in " + config.location()
                    + ". In the Path " + file.getAbsolutePath() + " no Configuration was found.");
            return;
        }
        try {
            url = file.toURI().toURL();
        } catch (MalformedURLException e) {
            _logger.log(Level.WARNING, "Ignoring Configuration " + name + " in " + config.location()
                    + ". It has an illegal URL-Format.", e);
            return;
        }
        break;
    case URL:
        try {
            url = new URL(config.location().value());
        } catch (MalformedURLException e) {
            _logger.log(Level.WARNING, "Ignoring Configuration " + name + " in " + config.location()
                    + ". It has an illegal URL-Format.", e);
            return;
        }
        break;
    case CLASSPATH:
    default:
        url = this.getClass().getResource(config.location().value());
        break;
    }

    if (url == null) {
        _logger.log(Level.WARNING, "Ignoring Configuration " + name + " in " + config.location()
                + ", because is couldn't be found in the Classpath.");
        // TODO Throw an exception if config doesn't exist?
        return;
    }

    Named named = null;
    if (name.value().length() > 0) {
        named = name;
    }

    FileConfiguration configuration;
    try {
        // Class<? extends FileConfiguration> interf =
        // config.to().asSubclass(FileConfiguration.class);
        Class<FileConfiguration> interf = (Class<FileConfiguration>) config.to();
        configuration = (FileConfiguration) injector.getInstance(interf);
        configuration.load(url);

        bindInstance(configuration, interf, named, null);
        bindInstance(configuration, FileConfiguration.class, named, null);
        bindInstance(configuration, org.apache.commons.configuration.Configuration.class, named, null);
    } catch (ConfigurationException e) {
        _logger.log(Level.WARNING, "Configuration " + name + " couldn't be loaded/bound: " + e.getMessage(), e);
    }
}

From source file:openlr.properties.OpenLRPropertiesReader.java

/**
 * Load properties from stream.// w w w  .  j a  v  a  2s.  c om
 * 
 * @param iStream
 *            the i stream
 * @param isXML
 *            the is xml
 * @return the configuration
 * @throws OpenLRPropertyException
 *             the open lr property exception
 */
public static FileConfiguration loadPropertiesFromStream(final InputStream iStream, final boolean isXML)
        throws OpenLRPropertyException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("load OpenLR properties file");
    }
    if (iStream == null) {
        LOG.error("Cannot find the OpenLR properties file.");
        throw new OpenLRPropertyException(PropertyError.PROPERTIES_FILE_NOT_FOUND);
    }
    try {
        FileConfiguration conf;
        if (isXML) {
            conf = new XMLConfiguration();
        } else {
            conf = new PropertiesConfiguration();
        }
        conf.load(iStream);
        return conf;
    } catch (ConfigurationException e) {
        throw new OpenLRPropertyException(PropertyError.MISSING_PROPERTY, e);
    }
}

From source file:org.apache.tinkerpop.gremlin.hadoop.groovy.plugin.HadoopRemoteAcceptor.java

@Override
public Object connect(final List<String> args) throws RemoteException {
    if (args.size() == 0) {
        this.hadoopGraph = HadoopGraph.open(new BaseConfiguration());
        this.shell.getInterp().getContext().setProperty("g", this.hadoopGraph);
    }//from w w  w .  ja v a 2  s .co  m
    if (args.size() == 1) {
        try {
            final FileConfiguration configuration = new PropertiesConfiguration();
            configuration.load(new File(args.get(0)));
            this.hadoopGraph = HadoopGraph.open(configuration);
            this.shell.getInterp().getContext().setProperty("g", this.hadoopGraph);
        } catch (final Exception e) {
            throw new RemoteException(e.getMessage(), e);
        }
    } else if (args.size() == 2) {
        try {
            final FileConfiguration configuration = new PropertiesConfiguration();
            configuration.load(new File(args.get(0)));
            this.hadoopGraph = HadoopGraph.open(configuration);
            this.graphVariable = args.get(1);
            this.shell.getInterp().getContext().setProperty(args.get(1), this.hadoopGraph);
        } catch (final Exception e) {
            throw new RemoteException(e.getMessage(), e);
        }
    }

    return this.hadoopGraph;
}