Example usage for org.apache.commons.configuration EnvironmentConfiguration getKeys

List of usage examples for org.apache.commons.configuration EnvironmentConfiguration getKeys

Introduction

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

Prototype

public Iterator getKeys() 

Source Link

Usage

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. ja  va 2  s .  c  om*/
    }
    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);
    }
}