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

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

Introduction

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

Prototype

public void setFileName(String fileName) 

Source Link

Document

Set the name of the file.

Usage

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();//  w  w w. j  av a2  s .  co m
            } 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:ke.co.tawi.babblesms.server.servlet.util.PropertiesConfig.java

/**
 * Populate the internal HashMap which will hold configuration keys and values
 *///from w  ww.  j a va 2 s . c  o m
private void initConfigHash() {
    PropertiesConfiguration config;
    String key;

    try {
        config = new PropertiesConfiguration();
        config.setListDelimiter('|'); // our array delimiter
        config.setFileName(configFile);
        config.load();

        Iterator<String> keys = config.getKeys();

        while (keys.hasNext()) {
            key = keys.next();
            configHash.put(key, (String) config.getProperty(key));
        }

    } catch (ConfigurationException e) {
        logger.error("ConfigurationException when trying to initialize configuration HashMap");
        logger.error(ExceptionUtils.getStackTrace(e));
    }
}

From source file:net.nharyes.drivecopy.mod.MainModule.java

@Provides
@Singleton//from w w  w . ja v  a2 s .co  m
private PropertiesConfiguration providePropertiesConfiguration() {

    PropertiesConfiguration config;

    try {

        // load configuration from file
        config = new PropertiesConfiguration(configFile);

    } catch (ConfigurationException ex) {

        // create empty configuration
        config = new PropertiesConfiguration();
        config.setFileName(configFile);
    }

    return config;
}

From source file:ninja.utils.NinjaPropertiesImplToolTest.java

@Test
public void testMissingSecretCreatesNewOneInDevMode() throws Exception {

    String uuid = UUID.randomUUID().toString();
    String baseDirWithoutTrailingSlash = "/tmp/ninja-test-" + uuid;

    String devConf = baseDirWithoutTrailingSlash + File.separator + "src/main/java/conf/application.conf";

    PropertiesConfiguration defaultConfiguration = new PropertiesConfiguration();
    defaultConfiguration.setFileName(devConf);

    Configuration compositeConfiguration = new PropertiesConfiguration();

    boolean isProd = false;

    NinjaPropertiesImplTool.checkThatApplicationSecretIsSet(isProd, baseDirWithoutTrailingSlash,
            defaultConfiguration, compositeConfiguration);

    assertTrue(compositeConfiguration.getString(NinjaConstant.applicationSecret).length() == 64);
    assertTrue(defaultConfiguration.getString(NinjaConstant.applicationSecret).length() == 64);

    assertTrue(Files.toString(new File(devConf), Charsets.UTF_8).contains(NinjaConstant.applicationSecret));

    // tear down/* ww  w .j  av  a 2 s  .c o  m*/
    FileUtils.deleteDirectory(new File(baseDirWithoutTrailingSlash));

}

From source file:ninja.utils.SwissKnife.java

/**
 * This is important: We load stuff as UTF-8.
 *
 * We are using in the default Apache Commons loading mechanism.
 *
 * With two little tweaks: 1. We don't accept any delimimter by default 2.
 * We are reading in UTF-8/*  www .  jav  a  2s. co  m*/
 *
 * More about that:
 * http://commons.apache.org/configuration/userguide/howto_filebased
 * .html#Loading
 *
 * From the docs: - If the combination from base path and file name is a
 * full URL that points to an existing file, this URL will be used to load
 * the file. - If the combination from base path and file name is an
 * absolute file name and this file exists, it will be loaded. - If the
 * combination from base path and file name is a relative file path that
 * points to an existing file, this file will be loaded. - If a file with
 * the specified name exists in the user's home directory, this file will be
 * loaded. - Otherwise the file name is interpreted as a resource name, and
 * it is checked whether the data file can be loaded from the classpath.
 *
 * @param fileOrUrlOrClasspathUrl Location of the file. Can be on file
 * system, or on the classpath. Will both work.
 * @return A PropertiesConfiguration or null if there were problems getting
 * it.
 */
public static PropertiesConfiguration loadConfigurationInUtf8(String fileOrUrlOrClasspathUrl) {

    PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
    propertiesConfiguration.setEncoding(NinjaConstant.UTF_8);
    propertiesConfiguration.setDelimiterParsingDisabled(true);
    propertiesConfiguration.setFileName(fileOrUrlOrClasspathUrl);
    propertiesConfiguration.getLayout().setSingleLine(NinjaConstant.applicationSecret, true);

    try {

        propertiesConfiguration.load(fileOrUrlOrClasspathUrl);

    } catch (ConfigurationException e) {

        logger.info("Could not load file " + fileOrUrlOrClasspathUrl
                + " (not a bad thing necessarily, but I am returing null)");

        return null;
    }

    return propertiesConfiguration;
}

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

/**
 * @return the configuration object/*from www .  j  a va  2 s.  com*/
 */
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;
    }
}

From source file:org.wisdom.configuration.ApplicationConfigurationImpl.java

/**
 * This is important: We load stuff as UTF-8.
 * <p>/*from   ww w  .  j  a v a 2  s. c  om*/
 * We are using in the default Apache Commons loading mechanism.
 * <p>
 * With two little tweaks: 1. We don't accept any delimimter by default 2.
 * We are reading in UTF-8
 * <p>
 * More about that:
 * http://commons.apache.org/configuration/userguide/howto_filebased
 * .html#Loading
 * <p>
 * From the docs: - If the combination from base path and file name is a
 * full URL that points to an existing file, this URL will be used to load
 * the file. - If the combination from base path and file name is an
 * absolute file name and this file exists, it will be loaded. - If the
 * combination from base path and file name is a relative file path that
 * points to an existing file, this file will be loaded. - If a file with
 * the specified name exists in the user's home directory, this file will be
 * loaded. - Otherwise the file name is interpreted as a resource name, and
 * it is checked whether the data file can be loaded from the classpath.
 *
 * @param fileOrUrlOrClasspathUrl Location of the file. Can be on file system, or on the
 *                                classpath. Will both work.
 * @return A PropertiesConfiguration or null if there were problems getting it.
 */
public final PropertiesConfiguration loadConfigurationInUtf8(String fileOrUrlOrClasspathUrl) {

    PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
    propertiesConfiguration.setEncoding("utf-8");
    propertiesConfiguration.setDelimiterParsingDisabled(true);
    propertiesConfiguration.setFileName(fileOrUrlOrClasspathUrl);
    propertiesConfiguration.getLayout().setSingleLine(APPLICATION_SECRET, true);

    try {
        propertiesConfiguration.load(fileOrUrlOrClasspathUrl);
    } catch (ConfigurationException e) {
        LOGGER.info("Could not load file " + fileOrUrlOrClasspathUrl
                + " (not a bad thing necessarily, but you should have a look)", e);
        return null;
    }

    return propertiesConfiguration;
}