Example usage for org.apache.commons.configuration PropertiesConfiguration setReloadingStrategy

List of usage examples for org.apache.commons.configuration PropertiesConfiguration setReloadingStrategy

Introduction

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

Prototype

public void setReloadingStrategy(ReloadingStrategy strategy) 

Source Link

Usage

From source file:cz.cas.lib.proarc.common.config.AppConfiguration.java

private void buildConfiguration(CompositeConfiguration cc, File cfgFile) {
    try {//from w  ww.  j  av a 2s .  co m
        // envConfig contains interpolated properties
        PropertiesConfiguration envConfig = new PropertiesConfiguration();
        envConfig.addProperty(PROPERTY_APP_HOME, configHome.getPath());
        cc.addConfiguration(envConfig);
        // external configuration editable by users; UTF-8 expected
        PropertiesConfiguration external = new PropertiesConfiguration();
        external.setEncoding("UTF-8");
        FileChangedReloadingStrategy reloading = new FileChangedReloadingStrategy();
        external.setReloadingStrategy(reloading);
        external.setFile(cfgFile);
        cc.addConfiguration(external);
        try {
            // bundled default configurations
            Enumeration<URL> resources = AppConfiguration.class.getClassLoader()
                    .getResources(DEFAULT_PROPERTIES_RESOURCE);
            for (URL resource; resources.hasMoreElements();) {
                resource = resources.nextElement();
                LOG.log(Level.FINE, "classpath config: {0}", resource);
                cc.addConfiguration(new PropertiesConfiguration(resource));
            }
        } catch (IOException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
    } catch (ConfigurationException ex) {
        LOG.log(Level.SEVERE, null, ex);
    }
}

From source file:it.grid.storm.config.ConfigReader.java

public ConfigReader(String configurationPathname, int refresh) {

    if (configurationPathname != null) {
        if (refresh < 0)
            refresh = 0;/*  w w w . j  av a 2 s  .  c o m*/
        this.refresh = refresh;
        this.configurationPathname = configurationPathname;
        log.info("Configuration file {}. Refresh rate: {} seconds", configurationPathname, refresh);

        try {
            FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
            strategy.setRefreshDelay(refresh);
            PropertiesConfiguration properties = new PropertiesConfiguration(configurationPathname);
            log.debug("Configuration properties:");
            String key;
            for (Iterator<?> i = properties.getKeys(); i.hasNext();) {
                key = (String) i.next();
                log.debug("{} = {}", key, properties.getProperty(key).toString());
            }
            properties.setReloadingStrategy(strategy);
            this.c = new CompositeConfiguration();
            ((CompositeConfiguration) this.c).addConfiguration(properties);
            log.info("Configuration read successfully.");
        } catch (ConfigurationException e) {
            this.c = makeEmptyConfiguration();
            log.error("Configuration parsing error: {}", e.getMessage(), e);
        }
    } else {
        throw new NullPointerException("Null configuration pathname.");
    }
}

From source file:com.mirth.connect.manager.ManagerController.java

private PropertiesConfiguration initializeProperties(String path, boolean alert) {
    PropertiesConfiguration properties = new PropertiesConfiguration();

    // Auto reload changes
    FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
    fileChangedReloadingStrategy.setRefreshDelay(1000);
    properties.setReloadingStrategy(fileChangedReloadingStrategy);
    properties.setFile(new File(path));

    if (properties.isEmpty() && alert) {
        alertErrorDialog("Could not load properties from file: " + path);
    }//  w  w w.jav a2  s  .c  om

    return properties;
}

From source file:com.manydesigns.portofino.i18n.ResourceBundleManager.java

public ResourceBundle getBundle(Locale locale) {
    ConfigurationResourceBundle bundle = resourceBundles.get(locale);
    if (bundle == null) {
        CompositeConfiguration configuration = new CompositeConfiguration();
        Iterator<String> iterator = searchPaths.descendingIterator();
        while (iterator.hasNext()) {
            String path = iterator.next();
            int index = path.lastIndexOf('/') + 1;
            String basePath = path.substring(0, index);
            int suffixIndex = path.length() - ".properties".length();
            String resourceBundleBaseName = path.substring(index, suffixIndex);
            String bundleName = getBundleFileName(resourceBundleBaseName, locale);
            PropertiesConfiguration conf;
            try {
                conf = new PropertiesConfiguration();
                conf.setFileName(basePath + bundleName);
                conf.setDelimiterParsingDisabled(true);
                conf.load();//from   w w w  .  ja  v a  2  s  .com
            } catch (ConfigurationException e) {
                logger.debug("Couldn't load resource bundle for locale " + locale + " from " + basePath, e);
                //Fall back to default .properties without _locale
                try {
                    String defaultBundleName = basePath + resourceBundleBaseName + ".properties";
                    conf = new PropertiesConfiguration();
                    conf.setFileName(defaultBundleName);
                    conf.setDelimiterParsingDisabled(true);
                    conf.load();
                } catch (ConfigurationException e1) {
                    logger.debug("Couldn't load default resource bundle from " + basePath, e1);
                    conf = null;
                }
            }
            if (conf != null) {
                FileChangedReloadingStrategy reloadingStrategy = new FileChangedReloadingStrategy();
                conf.setReloadingStrategy(reloadingStrategy);
                configuration.addConfiguration(conf);
            }
        }
        bundle = new ConfigurationResourceBundle(configuration, locale);
        //TODO setParent?
        resourceBundles.put(locale, bundle);
    }
    return bundle;
}

From source file:com.nridge.core.app.mgr.AppMgr.java

/**
 * Loads the default property files ("application.properties" and
 * "logback.xml") from the file system and assigns default application
 * properties.//from   ww  w  .ja v a 2 s  . co m
 * <p>
 * <b>Note:</b>&nbsp;This method will establish a default 5 minute reloading
 * policy for the "application.properties" file.  Therefore, any
 * changes to this property file while the application is running
 * will be recognized within a 5 minute period.
 * </p>
 *
 * @throws NSException Typically thrown for I/O related issues.
 */
public void loadPropertyFiles() throws NSException {
    String logFileName;

    try {

        // First, we will read our application properties.

        mConfiguration = new CompositeConfiguration();
        mConfiguration.setDelimiterParsingDisabled(true);
        mConfiguration.addConfiguration(new SystemConfiguration());
        mConfiguration.addConfiguration(new EnvironmentConfiguration());
        PropertiesConfiguration propertyCfg = new PropertiesConfiguration(deriveCfgPathFileName());
        FileChangedReloadingStrategy reloadingStrategy = new FileChangedReloadingStrategy();
        reloadingStrategy.setRefreshDelay(DateUtils.MILLIS_PER_MINUTE * 2L);
        propertyCfg.setReloadingStrategy(reloadingStrategy);
        mConfiguration.addConfiguration(propertyCfg);

        // Next, we will load our Logback properties file and configure our application logger.

        if (mCmdLine == null)
            logFileName = LOG_PROPERTY_FILE_NAME;
        else
            logFileName = mCmdLine.getOptionValue("logfile", LOG_PROPERTY_FILE_NAME);
        File logFile = new File(logFileName);
        if (!logFile.exists()) {
            String logPathFileName = String.format("%s%c%s", deriveCfgPathName(), File.separatorChar,
                    logFileName);
            logFile = new File(logPathFileName);
            if (logFile.exists())
                logFileName = logPathFileName;
            else
                throw new NSException("Unable to locate logging properties file: " + logFileName);
        }

        if (StringUtils.isEmpty(getString(APP_PROPERTY_INS_PATH)))
            mConfiguration.addProperty(APP_PROPERTY_INS_PATH, getInsPathName());
        if (StringUtils.isEmpty(getString(APP_PROPERTY_CFG_PATH)))
            mConfiguration.addProperty(APP_PROPERTY_CFG_PATH, deriveCfgPathName());
        if (StringUtils.isEmpty(getString(APP_PROPERTY_LOG_PATH)))
            mConfiguration.addProperty(APP_PROPERTY_LOG_PATH, deriveLogPathName());
        if (StringUtils.isEmpty(getString(APP_PROPERTY_DS_PATH)))
            mConfiguration.addProperty(APP_PROPERTY_DS_PATH, deriveDSPathName());
        if (StringUtils.isEmpty(getString(APP_PROPERTY_RDB_PATH)))
            mConfiguration.addProperty(APP_PROPERTY_RDB_PATH, deriveRDBMSPathName());
        if (StringUtils.isEmpty(getString(APP_PROPERTY_GDB_PATH)))
            mConfiguration.addProperty(APP_PROPERTY_GDB_PATH, deriveGraphPathName());

        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        JoranConfigurator joranConfigurator = new JoranConfigurator();
        joranConfigurator.setContext(loggerContext);
        loggerContext.reset();
        joranConfigurator.doConfigure(logFileName);
    } catch (ConfigurationException e) {
        throw new NSException("Configuration parsing error: " + e.getMessage());
    } catch (JoranException e) {
        throw new NSException("Logback parsing error: " + e.getMessage());
    } catch (ClassCastException ignored) {

        /* Note that a WARNING related to java.lang.ClassCastException will trigger due to
        Smart GWT and NSD being dependent on different releases of the same logging
        framework.  http://www.slf4j.org/codes.html */

    }
}

From source file:ninja.i18n.MessagesImpl.java

/**
 * Attempts to load a message file and sets the file changed reloading
 * strategy on the configuration if the runtime mode is Dev.
 *//* www.  ja v  a 2s  .  co m*/
private PropertiesConfiguration loadLanguageConfiguration(String fileOrUrl) {
    PropertiesConfiguration configuration = SwissKnife.loadConfigurationInUtf8(fileOrUrl);

    if (configuration != null && ninjaProperties.isDev()) {
        // enable runtime reloading of translations in dev mode
        FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
        configuration.setReloadingStrategy(strategy);
    }

    return configuration;
}

From source file:ninja.utils.NinjaPropertiesImpl.java

public NinjaPropertiesImpl(NinjaMode ninjaMode, String externalConfigurationPath) {

    this.ninjaMode = ninjaMode;

    // This is our main configuration.
    // In the following we'll read the individual configurations and merge
    // them into the composite configuration at the end.
    compositeConfiguration = new CompositeConfiguration();

    // That is the default config.
    PropertiesConfiguration defaultConfiguration = null;

    // Config of prefixed mode corresponding to current mode (eg.
    // %test.myproperty=...)
    Configuration prefixedDefaultConfiguration = null;

    // (Optional) Config set via a system property
    PropertiesConfiguration externalConfiguration = null;

    // (Optional) Config of prefixed mode corresponding to current mode (eg.
    // %test.myproperty=...)
    Configuration prefixedExternalConfiguration = null;

    // First step => load application.conf and also merge properties that
    // correspond to a mode into the configuration.

    defaultConfiguration = SwissKnife.loadConfigurationInUtf8(NinjaProperties.CONF_FILE_LOCATION_BY_CONVENTION);

    if (defaultConfiguration != null) {
        // Second step:
        // Copy special prefix of mode to parent configuration
        // By convention it will be something like %test.myproperty
        prefixedDefaultConfiguration = defaultConfiguration.subset("%" + ninjaMode.name());

        // allow application.conf to be reloaded on changes in dev mode
        if (NinjaMode.dev == ninjaMode) {
            defaultConfiguration.setReloadingStrategy(new FileChangedReloadingStrategy());
        }/*from  www. ja v  a2  s . c om*/

    } else {

        // If the property was set, but the file not found we emit
        // a RuntimeException
        String errorMessage = String.format(
                "Error reading configuration file. Make sure you got a default config file %s",
                NinjaProperties.CONF_FILE_LOCATION_BY_CONVENTION);

        logger.error(errorMessage);

        throw new RuntimeException(errorMessage);
    }

    // third step => load external configuration when a system property is defined.
    String ninjaExternalConf = externalConfigurationPath;

    if (ninjaExternalConf == null) {
        // if not set fallback to system property
        ninjaExternalConf = System.getProperty(NINJA_EXTERNAL_CONF);
    }

    if (ninjaExternalConf != null) {

        // only load it when the property is defined.

        externalConfiguration = SwissKnife.loadConfigurationInUtf8(ninjaExternalConf);

        // this should not happen:
        if (externalConfiguration == null) {

            String errorMessage = String.format(
                    "Ninja was told to use an external configuration%n" + " %s = %s %n."
                            + "But the corresponding file cannot be found.%n"
                            + " Make sure it is visible to this application and on the classpath.",
                    NINJA_EXTERNAL_CONF, ninjaExternalConf);

            logger.error(errorMessage);

            throw new RuntimeException(errorMessage);

        } else {

            // allow the external configuration to be reloaded at
            // runtime based on detected file changes
            final boolean shouldReload = Boolean.getBoolean(NINJA_EXTERNAL_RELOAD);
            if (shouldReload) {
                externalConfiguration.setReloadingStrategy(new FileChangedReloadingStrategy());
            }

            // Copy special prefix of mode to parent configuration
            // By convention it will be something like %test.myproperty
            prefixedExternalConfiguration = externalConfiguration.subset("%" + ninjaMode.name());
        }

    }

    // /////////////////////////////////////////////////////////////////////
    // Finally add the stuff to the composite configuration
    // Note: Configurations added earlier will overwrite configurations
    // added later.
    // /////////////////////////////////////////////////////////////////////

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

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

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

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

    // /////////////////////////////////////////////////////////////////////
    // Check that the secret is set or generate a new one if the property
    // does not exist
    // /////////////////////////////////////////////////////////////////////
    NinjaPropertiesImplTool.checkThatApplicationSecretIsSet(isProd(), new File("").getAbsolutePath(),
            defaultConfiguration, compositeConfiguration);

}

From source file:org.apache.falcon.regression.core.util.Config.java

private void initConfig(String propFileName) throws ConfigurationException {
    CompositeConfiguration compositeConfiguration = new CompositeConfiguration();
    LOGGER.info("Going to add properties from system properties.");
    compositeConfiguration.addConfiguration(new SystemConfiguration());

    LOGGER.info("Going to read properties from: " + propFileName);
    final PropertiesConfiguration merlinConfig = new PropertiesConfiguration(
            Config.class.getResource("/" + propFileName));
    //if changed configuration will be reloaded within 2 min
    final FileChangedReloadingStrategy reloadingStrategy = new FileChangedReloadingStrategy();
    reloadingStrategy.setRefreshDelay(2 * 60 * 1000);
    merlinConfig.setReloadingStrategy(reloadingStrategy);
    compositeConfiguration.addConfiguration(merlinConfig);
    this.confObj = compositeConfiguration;
}

From source file:org.cesecore.config.ConfigurationHolder.java

public static synchronized Configuration instance() {
    if (config == null) {
        // Read in default values
        defaultValues = new CompositeConfiguration();
        final URL defaultConfigUrl = ConfigurationHolder.class.getResource(DEFAULT_CONFIG_FILE);
        try {//from w w w  .j  a  v  a 2  s .com
            defaultValues.addConfiguration(new PropertiesConfiguration(defaultConfigUrl));
        } catch (ConfigurationException e) {
            log.error("Error encountered when loading default properties. Could not load configuration from "
                    + defaultConfigUrl, e);
        }

        // read cesecore.properties, from config file built into jar, and see if we allow configuration by external files
        boolean allowexternal = false;
        try {
            final URL url = ConfigurationHolder.class.getResource("/conf/" + CONFIG_FILES[0]);
            if (url != null) {
                final PropertiesConfiguration pc = new PropertiesConfiguration(url);
                allowexternal = "true".equalsIgnoreCase(pc.getString(CONFIGALLOWEXTERNAL, "false"));
                log.info("Allow external re-configuration: " + allowexternal);
            }
        } catch (ConfigurationException e) {
            log.error("Error intializing configuration: ", e);
        }
        config = new CompositeConfiguration();

        // Only add these config sources if we allow external configuration
        if (allowexternal) {
            // Override with system properties, this is prio 1 if it exists (java -Dscep.test=foo)
            config.addConfiguration(new SystemConfiguration());
            log.info("Added system properties to configuration source (java -Dfoo.prop=bar).");

            // Override with file in "application server home directory"/conf, this is prio 2
            for (int i = 0; i < CONFIG_FILES.length; i++) {
                File f = null;
                try {
                    f = new File("conf" + File.separator + CONFIG_FILES[i]);
                    final PropertiesConfiguration pc = new PropertiesConfiguration(f);
                    pc.setReloadingStrategy(new FileChangedReloadingStrategy());
                    config.addConfiguration(pc);
                    log.info("Added file to configuration source: " + f.getAbsolutePath());
                } catch (ConfigurationException e) {
                    log.error("Failed to load configuration from file " + f.getAbsolutePath());
                }
            }
            // Override with file in "/etc/cesecore/conf/, this is prio 3
            for (int i = 0; i < CONFIG_FILES.length; i++) {
                File f = null;
                try {
                    f = new File("/etc/cesecore/conf/" + CONFIG_FILES[i]);
                    final PropertiesConfiguration pc = new PropertiesConfiguration(f);
                    pc.setReloadingStrategy(new FileChangedReloadingStrategy());
                    config.addConfiguration(pc);
                    log.info("Added file to configuration source: " + f.getAbsolutePath());
                } catch (ConfigurationException e) {
                    log.error("Failed to load configuration from file " + f.getAbsolutePath());
                }
            }
        } // if (allowexternal)

        // Default values build into jar file, this is last prio used if no of the other sources override this
        for (int i = 0; i < CONFIG_FILES.length; i++) {
            addConfigurationResource("/conf/" + CONFIG_FILES[i]);
        }
        // Load internal.properties only from built in configuration file
        try {
            final URL url = ConfigurationHolder.class.getResource("/internal.properties");
            if (url != null) {
                final PropertiesConfiguration pc = new PropertiesConfiguration(url);
                config.addConfiguration(pc);
                log.debug("Added url to configuration source: " + url);
            }
        } catch (ConfigurationException e) {
            log.error("Failed to load configuration from resource internal.properties", e);
        }
    }
    return config;
}

From source file:org.cesecore.config.ConfigurationHolder.java

/**
 * Method used primarily for JUnit testing, where we can add a new properties file (in tmp directory) to the configuration.
 * //w  w  w.j ava 2 s. com
 * @param filename the full path to the properties file used for configuration.
 */
public static void addConfigurationFile(final String filename) {
    // Make sure the basic initialization has been done
    instance();
    File f = null;
    try {
        f = new File(filename);
        final PropertiesConfiguration pc = new PropertiesConfiguration(f);
        pc.setReloadingStrategy(new FileChangedReloadingStrategy());
        config.addConfiguration(pc);
        log.info("Added file to configuration source: " + f.getAbsolutePath());
    } catch (ConfigurationException e) {
        log.error("Failed to load configuration from file " + f.getAbsolutePath());
    }
}