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:org.ejbca.config.ConfigurationHolder.java

public static Configuration instance() {
    if (config == null) {
        // read ejbca.properties, from config file built into jar, and see if we allow configuration by external files
        boolean allowexternal = false;
        try {/*from  ww  w  .j  av  a 2  s .com*/
            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/ejbca/conf/, this is prio 3
            for (int i = 0; i < CONFIG_FILES.length; i++) {
                File f = null;
                try {
                    f = new File("/etc/ejbca/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(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.ejbca.config.EjbcaConfigurationHolder.java

public static Configuration instance() {
    if (config == null) {
        // read ejbca.properties, from config file built into jar, and see if we allow configuration by external files
        boolean allowexternal = false;
        try {/*  w w w. j  a  va2  s .  com*/
            final URL url = EjbcaConfigurationHolder.class.getResource("/conf/" + CONFIG_FILES[0]);
            if (url != null) {
                final PropertiesConfiguration pc = new PropertiesConfiguration(url);
                allowexternal = "true".equalsIgnoreCase(pc.getString(CONFIGALLOWEXTERNAL, "false"));
                if (allowexternal) {
                    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/ejbca/conf/, this is prio 3
            for (int i = 0; i < CONFIG_FILES.length; i++) {
                File f = null;
                try {
                    f = new File("/etc/ejbca/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(CONFIG_FILES[i]);
        }
        // Load internal.properties only from built in configuration file
        try {
            final URL url = EjbcaConfigurationHolder.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.ejbca.externalra.gui.ExternalRaGuiConfiguration.java

private static Configuration instance() {
    if (config == null) {
        try {//from  ww w.j ava2s.com
            // Default values build into war file, this is last prio used if no of the other sources override this
            boolean allowexternal = Boolean.getBoolean(new PropertiesConfiguration(
                    ExternalRaGuiConfiguration.class.getResource("/" + PROPERTIES_FILENAME))
                            .getString(PROPERTY_CONFIGALLOWEXTERNAL, "false"));
            config = new CompositeConfiguration();
            PropertiesConfiguration pc;
            // 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
                File f1 = new File("conf/" + PROPERTIES_FILENAME);
                pc = new PropertiesConfiguration(f1);
                pc.setReloadingStrategy(new FileChangedReloadingStrategy());
                config.addConfiguration(pc);
                log.info("Added file to configuration source: " + f1.getAbsolutePath());
                // Override with file in "/etc/ejbca/conf/extra, this is prio 3
                File f2 = new File("/etc/ejbca/conf/extra/" + PROPERTIES_FILENAME);
                pc = new PropertiesConfiguration(f2);
                pc.setReloadingStrategy(new FileChangedReloadingStrategy());
                config.addConfiguration(pc);
                log.info("Added file to configuration source: " + f2.getAbsolutePath());
            }
            // Default values build into war file, this is last prio used if no of the other sources override this
            URL url = ExternalRaGuiConfiguration.class.getResource("/" + PROPERTIES_FILENAME);
            pc = new PropertiesConfiguration(url);
            config.addConfiguration(pc);
            log.info("Added url to configuration source: " + url);
            log.info("Allow external re-configuration: " + allowexternal);
        } catch (ConfigurationException e) {
            log.error("Error intializing ExtRA Configuration: ", e);
        }
    }
    return config;
}

From source file:org.ejbca.extra.util.ExtraConfiguration.java

public static Configuration instance() {
    if (config == null) {
        try {/*from  w w w.ja  v  a  2 s . c  om*/
            // Default values build into war file, this is last prio used if no of the other sources override this
            boolean allowexternal = Boolean.getBoolean(
                    new PropertiesConfiguration(ExtraConfiguration.class.getResource("/" + PROPERTY_FILENAME))
                            .getString(CONFIGALLOWEXTERNAL, "false"));

            config = new CompositeConfiguration();

            PropertiesConfiguration pc;
            // 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
                File f1 = new File("conf/" + PROPERTY_FILENAME);
                pc = new PropertiesConfiguration(f1);
                pc.setReloadingStrategy(new FileChangedReloadingStrategy());
                config.addConfiguration(pc);
                log.info("Added file to configuration source: " + f1.getAbsolutePath());

                // Override with file in "/etc/ejbca/conf/extra, this is prio 3
                File f2 = new File("/etc/ejbca/conf/extra/" + PROPERTY_FILENAME);
                pc = new PropertiesConfiguration(f2);
                pc.setReloadingStrategy(new FileChangedReloadingStrategy());
                config.addConfiguration(pc);
                log.info("Added file to configuration source: " + f2.getAbsolutePath());
            }

            // Default values build into war file, this is last prio used if no of the other sources override this
            URL url = ExtraConfiguration.class.getResource("/" + PROPERTY_FILENAME);
            pc = new PropertiesConfiguration(url);
            config.addConfiguration(pc);
            log.info("Added url to configuration source: " + url);

            log.info("Allow external re-configuration: " + allowexternal);
            // Test
            log.debug("Using keystore path (1): " + config.getString(SCEPKEYSTOREPATH + ".1"));
            //log.debug("Using keystore pwd (1): "+config.getString(SCEPKEYSTOREPWD+".1"));
            //log.debug("Using authPwd: "+config.getString(SCEPAUTHPWD));
            log.debug("Using certificate profile: " + config.getString(SCEPCERTPROFILEKEY));
            log.debug("Using entity profile: " + config.getString(SCEPENTITYPROFILEKEY));
            log.debug("Using default CA: " + config.getString(SCEPDEFAULTCA));
            log.debug("Create or edit user: " + config.getBoolean(SCEPEDITUSER));
            log.debug("Mapping for CN=Scep CA,O=EJBCA Sample,C=SE: "
                    + config.getString("CN=Scep CA,O=EJBCA Sample,C=SE"));
        } catch (ConfigurationException e) {
            log.error("Error intializing ExtRA Configuration: ", e);
        }
    }
    return config;
}

From source file:org.ejbca.ui.cli.config.cmp.UploadFileCommand.java

@Override
public CommandResult execute(ParameterContainer parameters) {
    String filename = parameters.get(FILE_KEY);
    String alias = parameters.get(ALIAS_KEY);

    CompositeConfiguration config = null;
    File f = null;/*from   w w w . j  a v  a2  s . c o m*/
    try {
        f = new File(filename);
        final PropertiesConfiguration pc = new PropertiesConfiguration(f);
        pc.setReloadingStrategy(new FileChangedReloadingStrategy());
        config = new CompositeConfiguration();
        config.addConfiguration(pc);
        log.info("Reading CMP configuration from file: " + f.getAbsolutePath());
    } catch (ConfigurationException e) {
        log.error("Failed to load configuration from file " + f.getAbsolutePath());
        return CommandResult.FUNCTIONAL_FAILURE;
    }
    readConfigurations(config, alias);
    return CommandResult.SUCCESS;

}

From source file:org.ejbca.ui.cli.config.scep.UploadFileCommand.java

@Override
public CommandResult execute(ParameterContainer parameters) {
    String filename = parameters.get(FILE_KEY);
    String alias = parameters.get(ALIAS_KEY);

    CompositeConfiguration config = null;
    File f = null;//from  w  w w . jav a 2  s  .c  om
    try {
        f = new File(filename);
        final PropertiesConfiguration pc = new PropertiesConfiguration(f);
        pc.setReloadingStrategy(new FileChangedReloadingStrategy());
        config = new CompositeConfiguration();
        config.addConfiguration(pc);
        log.info("Reading SCEP configuration from file: " + f.getAbsolutePath());
    } catch (ConfigurationException e) {
        log.error("Failed to load configuration from file " + f.getAbsolutePath());
        return CommandResult.FUNCTIONAL_FAILURE;
    }
    readConfigurations(config, alias);
    return CommandResult.SUCCESS;

}

From source file:org.esco.grouperui.tools.property.PropertyManagerFactory.java

/**
 * iterate on each property file and add all property in manager.
 *///from w  ww  . j  a va2s . c om
private void addPropertiesToManager() {

    String[] repertoireDeConfiguration = PropertyManager.baseDirs();
    String folderWork = null;
    final Map<String, PropertiesConfiguration> map = new HashMap<String, PropertiesConfiguration>();

    for (final Entry<String, String> property : this.properties.entrySet()) {
        PropertiesConfiguration config = null;

        for (String folderConf : repertoireDeConfiguration) {
            try {

                if (!folderConf.endsWith("/") && !folderConf.endsWith("\\")) {
                    folderWork = folderConf + File.separator + "/";
                } else {
                    folderWork = folderConf;
                }

                final File fichierACharger = new File(folderWork + property.getValue());
                if (fichierACharger.exists()) {
                    // Chargement du fichier de properties dans un
                    // PropertiesConfiguration
                    config = new PropertiesConfiguration(fichierACharger);
                    PropertyManagerFactory.LOGGER
                            .debug("Chargement de " + folderWork + property.getValue() + " effectu");

                    // Rechargement  chaud du fichier en cas de
                    // modification de celui-ci
                    config.setReloadingStrategy(new FileChangedReloadingStrategy());
                    map.put(property.getKey(), config);
                } else {
                    PropertyManagerFactory.LOGGER
                            .debug("Impossible de charger le fichier " + folderWork + property.getValue());
                }
            } catch (final ConfigurationException e) {
                PropertyManagerFactory.LOGGER.error(e, "Le fichier de properties " + folderWork
                        + property.getValue() + " n'a pas pu se charger correctement");
            }
        }
    }

    final PropertyManager manager = PropertyManager.getInstance();
    manager.setPropertiesConfiguration(map);
}

From source file:org.finra.dm.dao.ReloadablePropertySourceTest.java

/**
 * Gets a new properties configuration that will re-load the properties from a file every time it is called.
 *
 * @return the properties configuration.
 * @throws ConfigurationException if the properties configuration couldn't be created.
 *///w  ww . ja  v a2  s  .c om
private PropertiesConfiguration getNewPropertiesConfiguration() throws ConfigurationException {
    // Create a new properties configuration.
    // We are using this instead of a database configuration for easier testing.
    PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration(propertiesFile);

    // Create a reloading strategy that will always reload when asked.
    // There were some problems using the FileChangedReloadingStrategy where it wasn't detecting changed files and causing some methods in this
    // JUnit to fail.
    propertiesConfiguration.setReloadingStrategy(new ReloadingStrategy() {
        @Override
        public void setConfiguration(FileConfiguration configuration) {
        }

        @Override
        public void init() {
        }

        @Override
        public boolean reloadingRequired() {
            // Tell the caller that the properties should always be reloaded.
            return true;
        }

        @Override
        public void reloadingPerformed() {
        }
    });

    return propertiesConfiguration;
}

From source file:org.freemedsoftware.shim.Configuration.java

/**
 * Load configuration from both template and override properties files.
 *///  w w w.jav  a  2 s.c  o m
public static void loadConfiguration() {
    log.trace("Entered loadConfiguration");
    if (servletContext == null) {
        log.error("servletContext not set!");
    }
    if (compositeConfiguration == null) {
        log.info("Configuration object not present, instantiating");
        compositeConfiguration = new CompositeConfiguration();

        PropertiesConfiguration defaults = null;
        try {
            defaults = new PropertiesConfiguration(
                    servletContext.getServletContext().getRealPath(DEFAULT_CONFIG));
            log.info("Loading default configuration from "
                    + servletContext.getServletContext().getRealPath(DEFAULT_CONFIG));
        } catch (ConfigurationException e) {
            log.error("Could not load default configuration from "
                    + servletContext.getServletContext().getRealPath(DEFAULT_CONFIG));
            // e.printStackTrace();
        }
        if (OVERRIDE_CONFIG != null) {
            PropertiesConfiguration overrides = null;
            try {
                overrides = new PropertiesConfiguration();
                overrides.setFile(new File(OVERRIDE_CONFIG));
                overrides.setReloadingStrategy(new FileChangedReloadingStrategy());
                overrides.load();
            } catch (ConfigurationException e) {
                log.info("Could not load overrides", e);
            }
            compositeConfiguration.addConfiguration(overrides);
        }
        // Afterwards, add defaults so they're read second.
        compositeConfiguration.addConfiguration(defaults);
    }
}

From source file:org.gss_project.gss.server.configuration.GSSConfigurationFactory.java

/**
 * @return the configuration object//w  w w.ja v a2 s . c om
 */
public synchronized static DataConfiguration getConfiguration() {
    try {
        if (configuration == null) {
            PropertiesConfiguration gssConfig = (PropertiesConfiguration) getClass(
                    PropertiesConfiguration.class.getCanonicalName()).newInstance();
            gssConfig.setBasePath("");
            gssConfig.setFileName(configFilename);
            gssConfig.setEncoding("ISO-8859-7");
            // Set automatic reloading.
            gssConfig.setReloadingStrategy(new VfsFileChangedReloadingStrategy());
            gssConfig.load();
            // Decorator.
            configuration = new DataConfiguration(gssConfig);
        }
        return configuration;
    } catch (ClassNotFoundException e) {
        // Use empty configuration, so we get no NPE but default values
        configuration = new DataConfiguration(new BaseConfiguration());
        logger.error("Error in ExternalAPI initialization! GSS is running with default values!", e);
        return configuration;
    } catch (InstantiationException e) {
        // Use empty configuration, so we get no NPE but default values
        configuration = new DataConfiguration(new BaseConfiguration());
        logger.error("Error in ExternalAPI initialization! GSS is running with default values!", e);
        return configuration;
    } catch (IllegalAccessException e) {
        // Use empty configuration, so we get no NPE but default values
        configuration = new DataConfiguration(new BaseConfiguration());
        logger.error("Error in ExternalAPI initialization! GSS is running with default values!", e);
        return configuration;
    } catch (ConfigurationException e) {
        // Use empty configuration, so we get no NPE but default values
        configuration = new DataConfiguration(new BaseConfiguration());
        logger.error("Error in ExternalAPI initialization! GSS is running with default values!", e);
        return configuration;
    }
}