Example usage for org.apache.commons.configuration CompositeConfiguration CompositeConfiguration

List of usage examples for org.apache.commons.configuration CompositeConfiguration CompositeConfiguration

Introduction

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

Prototype

public CompositeConfiguration() 

Source Link

Document

Creates an empty CompositeConfiguration object which can then be added some other Configuration files

Usage

From source file:com.strandls.alchemy.inject.AlchemyModuleFilterConfiguration.java

/**
 * @return configuration read from the config file.
 *///from   ww  w  .  j a  v a2  s .  c o m
private Configuration readConfiguration() {
    final CompositeConfiguration config = new CompositeConfiguration();
    config.addConfiguration(new SystemConfiguration());
    try {
        config.addConfiguration(new HierarchicalINIConfiguration(MODULE_CONFIGURATION_NAME));
    } catch (final ConfigurationException e) {
        // ignore if the configuration file is missing. This means no
        // filtering intended.
        log.warn("Error loading configuration file {}", e);
    }
    return config;
}

From source file:com.caricah.iotracah.bootstrap.system.handler.impl.DefaultConfigHandler.java

/**
 * All system configurations providers are loaded via spi
 * and are given the configurations the system already has.
 * They are further expected to provide their configurations in
 * and additive way. Since the order of loading the configs is not
 * guranteed all the setting keys should be uniquely identified
 * and that task is left to the implementations to enforce.
 *
 * @param configuration/*from w  ww. j a v a2s.co  m*/
 * @return
 * @throws UnRetriableException
 */
@Override
public Configuration populateConfiguration(Configuration configuration) throws UnRetriableException {

    try {
        String configDirectory = getConfigurationDirectory();

        log.debug(" populateConfiguration : obtained config directory - {}", configDirectory);

        Path configurationFile = getConfigurationFileInDirectory(configDirectory);

        if (null == configurationFile) {

            configurationFile = ResourceFileUtil.getFileFromResource(getClass(), getConfigurationFileName())
                    .toPath();

        }

        if (configuration instanceof CompositeConfiguration) {
            ((CompositeConfiguration) configuration)
                    .addConfiguration(new PropertiesConfiguration(configurationFile.toFile()));
            return configuration;
        } else {
            CompositeConfiguration compositeConfiguration = new CompositeConfiguration();

            if (null != configuration) {
                compositeConfiguration.addConfiguration(configuration);
            }

            compositeConfiguration.addConfiguration(new PropertiesConfiguration(configurationFile.toFile()));
            return compositeConfiguration;
        }

    } catch (IOException | ConfigurationException e) {
        log.error(" getConfiguration : ", e);
        throw new UnRetriableException(e);
    }

}

From source file:fr.in2p3.cc.storage.treqs.tools.Configurator.java

/**
 * Constructor of the configurator where it defines the name of the
 * configuration file./*from w  ww .jav a  2  s  . c  om*/
 *
 * @throws ProblematicConfiguationFileException
 *             If there is a problem reading the configuration file.
 */
private Configurator() throws ProblematicConfiguationFileException {
    LOGGER.trace("> Create instance");

    this.properties = new CompositeConfiguration();
    String name = null;
    this.properties.addConfiguration(new SystemConfiguration());
    try {
        name = System.getProperty(Constants.CONFIGURATION_FILE);
        if (name == null) {
            name = DefaultProperties.CONFIGURATION_PROPERTIES;
            LOGGER.debug("No given file in System property");
        }
        // TODO v2.0 Try to show the complete path of the configuration
        // file to use in a logger. This permits to know which is being used
        this.properties.addConfiguration(new HierarchicalINIConfiguration(name));
    } catch (final ConfigurationException e) {
        throw new ProblematicConfiguationFileException(name, e);
    }

    LOGGER.trace("< Create instance");
}

From source file:edu.cornell.med.icb.goby.config.GobyConfiguration.java

/**
 * Load the Goby configuration.//from w w  w. ja v  a 2  s  . co m
 *
 * @param defaultConfigFileLocations locations for configurations to check if one
 *                                   was not explicitly defined in a system property.
 */
private GobyConfiguration(final String... defaultConfigFileLocations) {
    super();
    configuration = new CompositeConfiguration();

    // by loading the system configuration first, any values set on the java command line
    // with "-Dproperty.name=property.value" will take precedence
    configuration.addConfiguration(new SystemConfiguration());

    // check to see if the user specified a configuration in the style of log4j
    if (configuration.containsKey("goby.configuration")) {
        final String configurationURLString = configuration.getString("goby.configuration");
        try {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Attempting to load " + configurationURLString);
            }
            final URL configurationURL = new URL(configurationURLString);
            configuration.addConfiguration(new PropertiesConfiguration(configurationURL));
            LOG.info("Goby configured with " + configurationURL);
        } catch (MalformedURLException e) {
            LOG.error("Invalid Goby configuration", e);
        } catch (ConfigurationException e) {
            LOG.error("Could not configure Goby from " + configurationURLString, e);
        }
    } else {
        // no configuration file location specified so check the default locations
        for (final String configFile : defaultConfigFileLocations) {
            try {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Attempting to load " + configFile);
                }
                configuration.addConfiguration(new PropertiesConfiguration(configFile));
            } catch (ConfigurationException e) {
                continue;
            }

            // if we got here the file was loaded and we don't search any further
            LOG.info("Goby configured with " + new File(configFile).getAbsolutePath());
            break;
        }
    }

    // load "default" configurations for any properties not found elsewhere
    // it's important that this be added LAST so the user can override any settings
    final Configuration defaultConfiguration = getDefaultConfiguration();
    configuration.addConfiguration(defaultConfiguration);

    if (LOG.isDebugEnabled()) {
        LOG.debug("Goby configuration: ");
        final Iterator keys = configuration.getKeys();
        while (keys.hasNext()) {
            final String key = (String) keys.next();
            LOG.debug(key + " = " + configuration.getProperty(key));
        }
    }
}

From source file:com.github.blacklocus.rdsecho.EchoCfg.java

EchoCfg(String propertiesFilename) {
    this.cfg = new CompositeConfiguration();
    this.cfg.addConfiguration(new SystemConfiguration());
    try {//from w  w  w .j  av  a 2  s  .com
        this.cfg.addConfiguration(new PropertiesConfiguration(propertiesFilename));
        LOG.info("Reading configuration from {}", propertiesFilename);

    } catch (ConfigurationException e) {
        LOG.info("{} will not be read because {}", propertiesFilename, e.getMessage());
    }
    validate();
}

From source file:cz.mzk.editor.server.config.EditorConfigurationImpl.java

public EditorConfigurationImpl() {
    File dir = new File(WORKING_DIR);
    if (!dir.exists()) {
        boolean mkdirs = dir.mkdirs();
        if (!mkdirs) {
            LOGGER.error("cannot create directory '" + dir.getAbsolutePath() + "'");
            throw new RuntimeException("cannot create directory '" + dir.getAbsolutePath() + "'");
        }/* w  ww.  j a v  a 2  s  .  com*/
    }
    File confFile = new File(CONFIGURATION);
    if (!confFile.exists()) {
        try {
            confFile.createNewFile();
        } catch (IOException e) {
            LOGGER.error("cannot create configuration file '" + confFile.getAbsolutePath() + "'", e);
            throw new RuntimeException("cannot create configuration file '" + confFile.getAbsolutePath() + "'");
        }
        FileOutputStream confFos;
        try {
            confFos = new FileOutputStream(confFile);
        } catch (FileNotFoundException e) {
            LOGGER.error("cannot create configuration file '" + confFile.getAbsolutePath() + "'", e);
            throw new RuntimeException("cannot create configuration file '" + confFile.getAbsolutePath() + "'");
        }
        try {
            try {
                new Properties().store(confFos, "configuration file for module metadata editor");
            } catch (IOException e) {
                LOGGER.error("cannot create configuration file '" + confFile.getAbsolutePath() + "'", e);
                throw new RuntimeException(
                        "cannot create configuration file '" + confFile.getAbsolutePath() + "'");
            }
        } finally {
            try {
                if (confFos != null)
                    confFos.close();
            } catch (IOException e) {
                LOGGER.error("cannot create configuration file '" + confFile.getAbsolutePath() + "'", e);
                throw new RuntimeException(
                        "cannot create configuration file '" + confFile.getAbsolutePath() + "'");
            } finally {
                confFos = null;
            }
        }
    }

    CompositeConfiguration constconf = new CompositeConfiguration();
    PropertiesConfiguration file = null;
    try {
        file = new PropertiesConfiguration(confFile);
    } catch (ConfigurationException e) {
        LOGGER.error(e.getMessage(), e);
        new RuntimeException("cannot read configuration");
    }
    file.setReloadingStrategy(new FileChangedReloadingStrategy());
    constconf.addConfiguration(file);
    constconf.setProperty(ServerConstants.EDITOR_HOME, WORKING_DIR);
    this.configuration = constconf;

    String hostname = configuration.getString(EditorClientConfiguration.Constants.HOSTNAME, "editor.mzk.cz");
    if (hostname.contains("://")) {
        hostname = hostname.substring(hostname.indexOf("://") + "://".length());
    }
    File imagesDir = new File(ServerConstants.DEFAULT_IMAGES_LOCATION + hostname + File.separator);
    if (!imagesDir.exists()) {
        boolean mkdirs = imagesDir.mkdirs();
        if (!mkdirs) {
            LOGGER.error("cannot create directory '" + imagesDir.getAbsolutePath() + "'");
            throw new RuntimeException("cannot create directory '" + imagesDir.getAbsolutePath() + "'");
        }
    }
    constconf.setProperty(ServerConstants.IMAGES_LOCATION,
            ServerConstants.DEFAULT_IMAGES_LOCATION + hostname + File.separator);
    EnvironmentConfiguration environmentConfiguration = new EnvironmentConfiguration();
    for (Iterator it = environmentConfiguration.getKeys(); it.hasNext();) {
        String key = (String) it.next();
        Object value = environmentConfiguration.getProperty(key);
        key = key.replaceAll("_", ".");
        key = key.replaceAll("\\.\\.", "__");
        constconf.addProperty(key, value);
    }
}

From source file:com.runwaysdk.configuration.CommonsConfigurationResolver.java

public ConfigurationReaderIF getReader(ConfigGroupIF configGroup, String config) {
    CompositeConfiguration _cconfig = new CompositeConfiguration();
    _cconfig.addConfiguration(this.cconfig);

    return new CommonsConfigurationReader(configGroup, config, _cconfig);
}

From source file:ffx.utilities.Keyword.java

/**
 * This method sets up configuration properties in the following precedence
 * * order://from  w  w  w.  ja v a2s.c  o  m
 *
 * 1.) Structure specific properties (for example pdbname.properties)
 *
 * 2.) Java system properties a.) -Dkey=value from the Java command line b.)
 * System.setProperty("key","value") within Java code.
 *
 * 3.) User specific properties (~/.ffx/ffx.properties)
 *
 * 4.) System wide properties (file defined by environment variable
 * FFX_PROPERTIES)
 *
 * 5.) Internal force field definition.
 *
 * @since 1.0
 * @param file a {@link java.io.File} object.
 * @return a {@link org.apache.commons.configuration.CompositeConfiguration}
 * object.
 */
public static CompositeConfiguration loadProperties(File file) {
    /**
     * Command line options take precedence.
     */
    CompositeConfiguration properties = new CompositeConfiguration();

    /**
     * Structure specific options are first.
     */
    if (file != null) {
        String filename = file.getAbsolutePath();
        filename = org.apache.commons.io.FilenameUtils.removeExtension(filename);
        String propertyFilename = filename + ".properties";
        File structurePropFile = new File(propertyFilename);
        if (structurePropFile.exists() && structurePropFile.canRead()) {
            try {
                properties.addConfiguration(new PropertiesConfiguration(structurePropFile));
                properties.addProperty("propertyFile", structurePropFile.getCanonicalPath());
            } catch (ConfigurationException | IOException e) {
                logger.log(Level.INFO, " Error loading {0}.", filename);
            }
        } else {
            propertyFilename = filename + ".key";
            structurePropFile = new File(propertyFilename);
            if (structurePropFile.exists() && structurePropFile.canRead()) {
                try {
                    properties.addConfiguration(new PropertiesConfiguration(structurePropFile));
                    properties.addProperty("propertyFile", structurePropFile.getCanonicalPath());
                } catch (ConfigurationException | IOException e) {
                    logger.log(Level.INFO, " Error loading {0}.", filename);
                }
            }
        }
    }

    /**
     * Java system properties
     *
     * a.) -Dkey=value from the Java command line
     *
     * b.) System.setProperty("key","value") within Java code.
     */
    properties.addConfiguration(new SystemConfiguration());

    /**
     * User specific options are 3rd.
     */
    String filename = System.getProperty("user.home") + File.separator + ".ffx/ffx.properties";
    File userPropFile = new File(filename);
    if (userPropFile.exists() && userPropFile.canRead()) {
        try {
            properties.addConfiguration(new PropertiesConfiguration(userPropFile));
        } catch (ConfigurationException e) {
            logger.log(Level.INFO, " Error loading {0}.", filename);
        }
    }

    /**
     * System wide options are 2nd to last.
     */
    filename = System.getenv("FFX_PROPERTIES");
    if (filename != null) {
        File systemPropFile = new File(filename);
        if (systemPropFile.exists() && systemPropFile.canRead()) {
            try {
                properties.addConfiguration(new PropertiesConfiguration(systemPropFile));
            } catch (ConfigurationException e) {
                logger.log(Level.INFO, " Error loading {0}.", filename);
            }
        }
    }

    /**
     * Echo the interpolated configuration.
     */
    if (logger.isLoggable(Level.FINE)) {
        //Configuration config = properties.interpolatedConfiguration();
        Iterator<String> i = properties.getKeys();
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("\n %-30s %s\n", "Property", "Value"));
        while (i.hasNext()) {
            String s = i.next();
            //sb.append(String.format(" %-30s %s\n", s, Arrays.toString(config.getList(s).toArray())));
            sb.append(String.format(" %-30s %s\n", s, Arrays.toString(properties.getList(s).toArray())));
        }
        logger.fine(sb.toString());
    }

    return properties;
}

From source file:ffx.autoparm.Keyword_poltype.java

/**
 * This method sets up configuration properties in the following precedence
 * order: 1.) Java system properties a.) -Dkey=value from the Java command
 * line b.) System.setProperty("key","value") within Java code.
 *
 * 2.) Structure specific properties (for example pdbname.properties)
 *
 * 3.) User specific properties (~/.ffx/ffx.properties)
 *
 * 4.) System wide properties (file defined by environment variable
 * FFX_PROPERTIES)//www  . j a v a2 s.com
 *
 * 5.) Internal force field definition.
 *
 * @since 1.0
 * @param file a {@link java.io.File} object.
 * @return a {@link org.apache.commons.configuration.CompositeConfiguration}
 * object.
 */
public static CompositeConfiguration loadProperties(File file) {
    /**
     * Command line options take precedences.
     */
    CompositeConfiguration properties = new CompositeConfiguration();
    properties.addConfiguration(new SystemConfiguration());

    /**
     * Structure specific options are 2nd.
     */
    if (file != null && file.exists() && file.canRead()) {

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String prmfilename = br.readLine().split(" +")[1];
            File prmfile = new File(prmfilename);
            if (prmfile.exists() && prmfile.canRead()) {
                properties.addConfiguration(new PropertiesConfiguration(prmfile));
                properties.addProperty("propertyFile", prmfile.getCanonicalPath());
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    //      /**
    //       * User specific options are 3rd.
    //       */
    //      String filename = System.getProperty("user.home") + File.separator
    //            + ".ffx/ffx.properties";
    //      File userPropFile = new File(filename);
    //      if (userPropFile.exists() && userPropFile.canRead()) {
    //         try {
    //            properties.addConfiguration(new PropertiesConfiguration(
    //                  userPropFile));
    //         } catch (Exception e) {
    //            logger.info("Error loading " + filename + ".");
    //         }
    //      }
    //
    //      /**
    //       * System wide options are 2nd to last.
    //       */
    //      filename = System.getenv("FFX_PROPERTIES");
    //      if (filename != null) {
    //         File systemPropFile = new File(filename);
    //         if (systemPropFile.exists() && systemPropFile.canRead()) {
    //            try {
    //               properties.addConfiguration(new PropertiesConfiguration(
    //                     systemPropFile));
    //            } catch (Exception e) {
    //               logger.info("Error loading " + filename + ".");
    //            }
    //         }
    //      }
    /**
     * Echo the interpolated configuration.
     */
    if (logger.isLoggable(Level.FINE)) {
        Configuration config = properties.interpolatedConfiguration();
        Iterator<String> i = config.getKeys();
        while (i.hasNext()) {
            String s = i.next();
            logger.fine("Key: " + s + ", Value: " + Arrays.toString(config.getList(s).toArray()));
        }
    }

    return properties;
}

From source file:cz.fi.muni.xkremser.editor.server.config.EditorConfigurationImpl.java

public EditorConfigurationImpl() {
    File dir = new File(WORKING_DIR);
    if (!dir.exists()) {
        boolean mkdirs = dir.mkdirs();
        if (!mkdirs) {
            LOGGER.error("cannot create directory '" + dir.getAbsolutePath() + "'");
            throw new RuntimeException("cannot create directory '" + dir.getAbsolutePath() + "'");
        }//from www.j a  v a  2s. c o  m
    }
    File confFile = new File(CONFIGURATION);
    if (!confFile.exists()) {
        try {
            confFile.createNewFile();
        } catch (IOException e) {
            LOGGER.error("cannot create configuration file '" + confFile.getAbsolutePath() + "'", e);
            throw new RuntimeException("cannot create configuration file '" + confFile.getAbsolutePath() + "'");
        }
        FileOutputStream confFos;
        try {
            confFos = new FileOutputStream(confFile);
        } catch (FileNotFoundException e) {
            LOGGER.error("cannot create configuration file '" + confFile.getAbsolutePath() + "'", e);
            throw new RuntimeException("cannot create configuration file '" + confFile.getAbsolutePath() + "'");
        }
        try {
            try {
                new Properties().store(confFos, "configuration file for module metadata editor");
            } catch (IOException e) {
                LOGGER.error("cannot create configuration file '" + confFile.getAbsolutePath() + "'", e);
                throw new RuntimeException(
                        "cannot create configuration file '" + confFile.getAbsolutePath() + "'");
            }
        } finally {
            try {
                if (confFos != null)
                    confFos.close();
            } catch (IOException e) {
                LOGGER.error("cannot create configuration file '" + confFile.getAbsolutePath() + "'", e);
                throw new RuntimeException(
                        "cannot create configuration file '" + confFile.getAbsolutePath() + "'");
            } finally {
                confFos = null;
            }
        }
    }
    File imagesDir = new File(DEFAULT_IMAGES_LOCATION);
    if (!imagesDir.exists()) {
        boolean mkdirs = imagesDir.mkdirs();
        if (!mkdirs) {
            LOGGER.error("cannot create directory '" + imagesDir.getAbsolutePath() + "'");
            throw new RuntimeException("cannot create directory '" + imagesDir.getAbsolutePath() + "'");
        }
    }

    CompositeConfiguration constconf = new CompositeConfiguration();
    PropertiesConfiguration file = null;
    try {
        file = new PropertiesConfiguration(confFile);
    } catch (ConfigurationException e) {
        LOGGER.error(e.getMessage(), e);
        new RuntimeException("cannot read configuration");
    }
    file.setReloadingStrategy(new FileChangedReloadingStrategy());
    constconf.addConfiguration(file);
    constconf.setProperty(ServerConstants.EDITOR_HOME, WORKING_DIR);
    this.configuration = constconf;
}