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

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

Introduction

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

Prototype

public XMLConfiguration(URL url) throws ConfigurationException 

Source Link

Document

Creates a new instance of XMLConfiguration.

Usage

From source file:org.ambraproject.search.service.IndexingServiceTest.java

@BeforeGroups(groups = { "badConfig" })
private void setBadConfig() throws ConfigurationException {
    String fileName = getClass().getClassLoader().getResource(badConfigFile).getFile();
    Configuration tempConfiguration = new XMLConfiguration(fileName);
    indexingService.setAmbraConfiguration(tempConfiguration);
}

From source file:org.apache.accumulo.server.metrics.MetricsConfiguration.java

private void loadConfiguration() {
    // Check to see if ACCUMULO_HOME environment variable is set.
    String ACUHOME = getEnvironmentConfiguration().getString("ACCUMULO_CONF_DIR");
    if (null != ACUHOME) {
        // Try to load the metrics properties file
        File mFile = new File(ACUHOME, metricsFileName);
        if (mFile.exists()) {
            if (log.isDebugEnabled())
                log.debug("Loading config file: " + mFile.getAbsolutePath());
            try {
                xConfig = new XMLConfiguration(mFile);
                xConfig.append(getEnvironmentConfiguration());
                xConfig.addConfigurationListener(new MetricsConfigListener());
                xConfig.setReloadingStrategy(new FileChangedReloadingStrategy());

                // Start a background Thread that checks a property from the XMLConfiguration
                // every so often to force the FileChangedReloadingStrategy to fire.
                if (null == watcher || !watcher.isAlive()) {
                    watcher = new MetricsConfigWatcher();
                    watcher.start();//from  ww  w .j a  v a2 s.c  om
                }
                notFound = false;
                alreadyWarned = false;
            } catch (ConfigurationException ce) {
                log.error("Error reading accumulo-metrics.xml file.");
                notFound = true;
                return;
            }
        } else {
            if (!alreadyWarned)
                log.warn("Unable to find metrics file: " + mFile.getAbsolutePath());
            alreadyWarned = true;
            notFound = true;
            return;
        }
    } else {
        if (!alreadyWarned)
            log.warn(
                    "ACCUMULO_CONF_DIR variable not found in environment. Metrics collection will be disabled.");
        alreadyWarned = true;
        notFound = true;
        return;
    }
    if (xConfig != null) {
        config = xConfig.interpolatedConfiguration();
        // set the enabled boolean from the configuration
        enabled = config.getBoolean(enabledName);
        if (log.isDebugEnabled())
            log.debug("Metrics collection enabled=" + enabled);
    } else {
        enabled = false;
    }

}

From source file:org.apache.hawq.pxf.api.utilities.ProfilesConf.java

private void loadConf(String fileName, boolean isMandatory) {
    URL url = getClassLoader().getResource(fileName);
    if (url == null) {
        LOG.warn(fileName + " not found in the classpath");
        if (isMandatory) {
            throw new ProfileConfException(PROFILES_FILE_NOT_FOUND, fileName);
        }//from  ww w. java 2s  .  c o m
        return;
    }
    try {
        XMLConfiguration conf = new XMLConfiguration(url);
        loadMap(conf);
    } catch (ConfigurationException e) {
        throw new ProfileConfException(PROFILES_FILE_LOAD_ERR, url.getFile(), String.valueOf(e.getCause()));
    }
}

From source file:org.apache.james.postage.Main.java

public static void main(String... args) {
    if (args == null || args.length == 0) {
        System.out.println("Please provide the configuration file");
        return;/*ww  w. j  av a 2  s .  co m*/
    }

    String filename = args[0];

    List<String> scenariosToRun = new ArrayList<String>();
    for (int i = 1; i < args.length; i++) {
        scenariosToRun.add(args[i]);
    }

    // load all scenarios from configuration file
    ConfigurationLoader configurationLoader = new ConfigurationLoader();
    Map<String, PostageConfiguration> configurations;
    try {
        // TODO allow different (non-xml) configs - as Common-Configuration supports it
        XMLConfiguration xmlConfiguration = new XMLConfiguration(filename);
        //xmlConfiguration.setThrowExceptionOnMissing(false);
        configurations = configurationLoader.create(xmlConfiguration);
    } catch (ConfigurationException e) {
        e.printStackTrace();
        return;
    }

    // register shutdown hook if this app is terminated from outside
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            shutdown();
        }
    });

    // run all scenarios
    runScenarios(configurations, scenariosToRun);
}

From source file:org.apache.juddi.config.AppConfig.java

/**
 * Does the actual work of reading the configuration from System
 * Properties and/or juddiv3.properties file. When the juddiv3.properties
 * file is updated the file will be reloaded. By default the reloadDelay is
 * set to 1 second to prevent excessive date stamp checking.
 *//*from  w  w w  . j  av  a  2  s  .c  o m*/
private void loadConfiguration() throws ConfigurationException {
    //Properties from system properties
    CompositeConfiguration compositeConfig = new CompositeConfiguration();
    compositeConfig.addConfiguration(new SystemConfiguration());
    //Properties from file
    //changed 7-19-2013 AO for JUDDI-627
    XMLConfiguration propConfig = null;
    final String filename = System.getProperty(JUDDI_CONFIGURATION_FILE_SYSTEM_PROPERTY);
    if (filename != null) {
        propConfig = new XMLConfiguration(filename);
        try {
            loadedFrom = new File(filename).toURI().toURL();
            //   propConfig = new PropertiesConfiguration(filename);
        } catch (MalformedURLException ex) {
            try {
                loadedFrom = new URL("file://" + filename);
            } catch (MalformedURLException ex1) {
                log.warn("unable to get an absolute path to " + filename
                        + ". This may be ignorable if everything works properly.", ex1);
            }
        }
    } else {
        //propConfig = new PropertiesConfiguration(JUDDI_PROPERTIES);
        propConfig = new XMLConfiguration(JUDDI_PROPERTIES);
        loadedFrom = ClassUtil.getResource(JUDDI_PROPERTIES, this.getClass());
    }
    //Hey! this may break things
    propConfig.setAutoSave(true);

    log.info("Reading from properties file:  " + loadedFrom);
    long refreshDelay = propConfig.getLong(Property.JUDDI_CONFIGURATION_RELOAD_DELAY, 1000l);
    log.debug("Setting refreshDelay to " + refreshDelay);
    FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
    fileChangedReloadingStrategy.setRefreshDelay(refreshDelay);
    propConfig.setReloadingStrategy(fileChangedReloadingStrategy);
    compositeConfig.addConfiguration(propConfig);

    Properties properties = new Properties();
    if ("Hibernate".equals(propConfig.getString(Property.PERSISTENCE_PROVIDER))) {
        if (propConfig.containsKey(Property.DATASOURCE))
            properties.put("hibernate.connection.datasource", propConfig.getString(Property.DATASOURCE));
        if (propConfig.containsKey(Property.HBM_DDL_AUTO))
            properties.put("hibernate.hbm2ddl.auto", propConfig.getString(Property.HBM_DDL_AUTO));
        if (propConfig.containsKey(Property.DEFAULT_SCHEMA))
            properties.put("hibernate.default_schema", propConfig.getString(Property.DEFAULT_SCHEMA));
        if (propConfig.containsKey(Property.HIBERNATE_DIALECT))
            properties.put("hibernate.dialect", propConfig.getString(Property.HIBERNATE_DIALECT));
    }
    // initialize the entityManagerFactory.
    PersistenceManager.initializeEntityManagerFactory(propConfig.getString(Property.JUDDI_PERSISTENCEUNIT_NAME),
            properties);
    // Properties from the persistence layer 
    MapConfiguration persistentConfig = new MapConfiguration(getPersistentConfiguration(compositeConfig));

    compositeConfig.addConfiguration(persistentConfig);
    //Making the new configuration globally accessible.
    config = compositeConfig;
}

From source file:org.apache.juddi.v3.client.config.ClientConfig.java

/**
 * Attempts to save any changes made to the configuration back to disk
 * @throws ConfigurationException /*from   w  w w .  j ava 2 s .c o  m*/
 */
public void saveConfig() throws ConfigurationException {

    XMLConfiguration saveConfiguration = new XMLConfiguration(configurationFile);
    Configuration cc = new CompositeConfiguration(saveConfiguration);
    Iterator<String> keys = this.config.getKeys();
    while (keys.hasNext()) {
        String key = keys.next();
        if (key.startsWith("client") || key.startsWith("config")) {
            cc.setProperty(key, config.getProperty(key));
        }
    }
    saveConfiguration.save();
}

From source file:org.apache.juddi.v3.client.config.ClientConfig.java

/**
 * Does the actual work of reading the configuration from System
 * Properties and/or uddi.xml file. When the uddi.xml
 * file is updated the file will be reloaded. By default the reloadDelay is
 * set to 1 second to prevent excessive date stamp checking.
 *///w w w .j  a  va2  s  .  c o m
private void loadConfiguration(String configurationFile, Properties properties) throws ConfigurationException {
    //Properties from system properties
    CompositeConfiguration compositeConfig = new CompositeConfiguration();
    compositeConfig.addConfiguration(new SystemConfiguration());
    //Properties from XML file
    if (System.getProperty(UDDI_CONFIG_FILENAME_PROPERTY) != null) {
        log.info("Using system property config override");
        configurationFile = System.getProperty(UDDI_CONFIG_FILENAME_PROPERTY);
    }
    XMLConfiguration xmlConfig = null;
    if (configurationFile != null) {
        xmlConfig = new XMLConfiguration(configurationFile);
    } else {
        final String filename = System.getProperty(UDDI_CONFIG_FILENAME_PROPERTY);
        if (filename != null) {
            xmlConfig = new XMLConfiguration(filename);
        } else {
            xmlConfig = new XMLConfiguration(DEFAULT_UDDI_CONFIG);
        }
    }
    log.info("Reading UDDI Client properties file " + xmlConfig.getBasePath() + " use -D"
            + UDDI_CONFIG_FILENAME_PROPERTY + " to override");
    this.configurationFile = xmlConfig.getBasePath();
    long refreshDelay = xmlConfig.getLong(Property.UDDI_RELOAD_DELAY, 1000l);
    log.debug("Setting refreshDelay to " + refreshDelay);
    FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
    fileChangedReloadingStrategy.setRefreshDelay(refreshDelay);
    xmlConfig.setReloadingStrategy(fileChangedReloadingStrategy);
    compositeConfig.addConfiguration(xmlConfig);
    //Making the new configuration globally accessible.
    config = compositeConfig;
    readConfig(properties);
}

From source file:org.apache.qpid.server.configuration.XmlConfigurationUtilities.java

public final static Configuration flatConfig(File file) throws ConfigurationException {
    // We have to override the interpolate methods so that
    // interpolation takes place across the entirety of the
    // composite configuration. Without doing this each
    // configuration object only interpolates variables defined
    // inside itself.
    final MyConfiguration conf = new MyConfiguration();
    conf.addConfiguration(new SystemConfiguration() {
        protected String interpolate(String o) {
            return conf.interpolate(o);
        }//from  w w w  .  ja v a  2 s  . co m
    });
    conf.addConfiguration(new XMLConfiguration(file) {
        protected String interpolate(String o) {
            return conf.interpolate(o);
        }
    });
    return conf;
}

From source file:org.apache.rya.accumulo.mr.merge.util.ToolConfigUtils.java

/**
 * Gets the set of user arguments from the user's config and/or their extra supplied
 * command line arguments.  This weeds out all the automatically generated parameters created
 * from initializing a {@link Configuration} object and should only give back a set of arguments
 * provided directly by the user./*from  w  ww  .jav  a  2  s . c  o m*/
 * @param conf the {@link Configuration} provided.
 * @param args the extra arguments from the command line.
 * @return a {@link Set} of argument strings.
 * @throws IOException
 */
public static Set<String> getUserArguments(final Configuration conf, final String[] args) throws IOException {
    String[] filteredArgs = new String[] {};
    if (Arrays.asList(args).contains("-conf")) {
        // parse args
        new GenericOptionsParser(conf, args);

        final List<String> commandLineArgs = new ArrayList<>();
        for (final String arg : args) {
            if (arg.startsWith("-D")) {
                commandLineArgs.add(arg);
            }
        }
        filteredArgs = commandLineArgs.toArray(new String[0]);
    } else {
        filteredArgs = args;
    }

    // Get the supplied config name from the resource string.
    // No real easy way of getting the name.
    // So, pulling it off the list of resource names in the Configuration's toString() method
    // where it should be the last one.
    final String confString = conf.toString();
    final String resourceString = StringUtils.removeStart(confString, "Configuration: ");
    final List<String> resourceNames = Arrays.asList(StringUtils.split(resourceString, ", "));
    final String configFilename = resourceNames.get(resourceNames.size() - 1);

    final Set<String> toolArgsSet = new HashSet<>();
    final File file = new File(configFilename);
    // Check that the last resource name is the actual user's config by seeing if it's a file
    // on the system, the other resources seem to be contained in jars and so should fail here which
    // should happen if no config is supplied.
    if (file.exists()) {
        XMLConfiguration configuration = null;
        try {
            configuration = new XMLConfiguration(configFilename);
            toolArgsSet.addAll(getConfigArguments(configuration));
        } catch (final ConfigurationException e) {
            log.error("Unable to load configuration file.", e);
        }
    }

    toolArgsSet.addAll(Arrays.asList(filteredArgs));
    return Collections.unmodifiableSet(toolArgsSet);
}

From source file:org.apache.rya.indexing.smarturi.duplication.conf.DuplicateDataConfig.java

/**
 * Creates a new instance of {@link DuplicateDataConfig}.
 * @throws ConfigurationException/*  w  w w.j  a  v  a  2 s.com*/
 */
public DuplicateDataConfig() throws ConfigurationException {
    this(new XMLConfiguration(DEFAULT_CONFIG_FILE_PATH));
}