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

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

Introduction

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

Prototype

public synchronized PropertiesConfigurationLayout getLayout() 

Source Link

Document

Returns the associated layout object.

Usage

From source file:com.mirth.connect.server.controllers.DefaultConfigurationController.java

private void saveConfigurationProperties(Map<String, ConfigurationProperty> map) throws ControllerException {
    try {//from w w  w  .  ja  v  a2s .  com
        PropertiesConfiguration configurationMapProperties = new PropertiesConfiguration();
        configurationMapProperties.setDelimiterParsingDisabled(true);
        configurationMapProperties.setListDelimiter((char) 0);
        configurationMapProperties.clear();

        PropertiesConfigurationLayout layout = configurationMapProperties.getLayout();

        Map<String, ConfigurationProperty> sortedMap = new TreeMap<String, ConfigurationProperty>(
                String.CASE_INSENSITIVE_ORDER);
        sortedMap.putAll(map);

        for (Entry<String, ConfigurationProperty> entry : sortedMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue().getValue();
            String comment = entry.getValue().getComment();

            if (StringUtils.isNotBlank(key)) {
                configurationMapProperties.addProperty(key, value);
                layout.setComment(key, StringUtils.isBlank(comment) ? null : comment);
            }
        }

        configurationMapProperties.save(new File(configurationFile));
    } catch (Exception e) {
        throw new ControllerException(e);
    }
}

From source file:com.mirth.connect.cli.CommandLineInterface.java

private void commandExportMap(Token[] arguments) throws ClientException {
    if (hasInvalidNumberOfArguments(arguments, 1)) {
        return;/*from w w w  .  j  av a2 s .  c  o  m*/
    }

    // file path
    String path = arguments[1].getText();
    File file = new File(path);

    if (file != null) {
        try {
            PropertiesConfiguration properties = new PropertiesConfiguration(file);
            properties.clear();
            PropertiesConfigurationLayout layout = properties.getLayout();

            Map<String, ConfigurationProperty> configurationMap = client.getConfigurationMap();
            Map<String, ConfigurationProperty> sortedMap = new TreeMap<String, ConfigurationProperty>(
                    String.CASE_INSENSITIVE_ORDER);
            sortedMap.putAll(configurationMap);

            for (Entry<String, ConfigurationProperty> entry : sortedMap.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue().getValue();
                String comment = entry.getValue().getComment();

                if (StringUtils.isNotBlank(key)) {
                    properties.setProperty(key, value);
                    layout.setComment(key, StringUtils.isBlank(comment) ? null : comment);
                }
            }

            properties.save();

            out.println("Configuration map export complete.");
        } catch (ConfigurationException e) {
            error("Unable to export configuration map.", e);
        }
    }
}

From source file:com.mirth.connect.server.controllers.DefaultConfigurationController.java

private void initialize() {
    try {/*from  w  w  w  . j a  v  a 2 s.  c o m*/
        // Disable delimiter parsing so getString() returns the whole
        // property, even if there are commas
        mirthConfig.setDelimiterParsingDisabled(true);
        mirthConfig.setFile(new File(ClassPathResource.getResourceURI("mirth.properties")));
        mirthConfig.load();

        MigrationController.getInstance().migrateConfiguration(mirthConfig);

        // load the server version
        versionConfig.setDelimiterParsingDisabled(true);
        InputStream versionPropertiesStream = ResourceUtil.getResourceStream(this.getClass(),
                "version.properties");
        versionConfig.load(versionPropertiesStream);
        IOUtils.closeQuietly(versionPropertiesStream);

        if (mirthConfig.getString(PROPERTY_TEMP_DIR) != null) {
            File tempDataDirFile = new File(mirthConfig.getString(PROPERTY_TEMP_DIR));

            if (!tempDataDirFile.exists()) {
                if (tempDataDirFile.mkdirs()) {
                    logger.debug("created tempdir: " + tempDataDirFile.getAbsolutePath());
                } else {
                    logger.error("error creating tempdir: " + tempDataDirFile.getAbsolutePath());
                }
            }

            System.setProperty("java.io.tmpdir", tempDataDirFile.getAbsolutePath());
            logger.debug("set temp data dir: " + tempDataDirFile.getAbsolutePath());
        }

        File appDataDirFile = null;

        if (mirthConfig.getString(PROPERTY_APP_DATA_DIR) != null) {
            appDataDirFile = new File(mirthConfig.getString(PROPERTY_APP_DATA_DIR));

            if (!appDataDirFile.exists()) {
                if (appDataDirFile.mkdir()) {
                    logger.debug("created app data dir: " + appDataDirFile.getAbsolutePath());
                } else {
                    logger.error("error creating app data dir: " + appDataDirFile.getAbsolutePath());
                }
            }
        } else {
            appDataDirFile = new File(".");
        }

        appDataDir = appDataDirFile.getAbsolutePath();
        logger.debug("set app data dir: " + appDataDir);

        baseDir = new File(ClassPathResource.getResourceURI("mirth.properties")).getParentFile().getParent();
        logger.debug("set base dir: " + baseDir);

        if (mirthConfig.getString(CHARSET) != null) {
            System.setProperty(CHARSET, mirthConfig.getString(CHARSET));
        }

        String[] httpsClientProtocolsArray = mirthConfig.getStringArray(HTTPS_CLIENT_PROTOCOLS);
        if (ArrayUtils.isNotEmpty(httpsClientProtocolsArray)) {
            // Support both comma separated and multiline values
            List<String> httpsClientProtocolsList = new ArrayList<String>();
            for (String protocol : httpsClientProtocolsArray) {
                httpsClientProtocolsList.addAll(Arrays.asList(StringUtils.split(protocol, ',')));
            }
            httpsClientProtocols = httpsClientProtocolsList
                    .toArray(new String[httpsClientProtocolsList.size()]);
        } else {
            httpsClientProtocols = MirthSSLUtil.DEFAULT_HTTPS_CLIENT_PROTOCOLS;
        }

        String[] httpsServerProtocolsArray = mirthConfig.getStringArray(HTTPS_SERVER_PROTOCOLS);
        if (ArrayUtils.isNotEmpty(httpsServerProtocolsArray)) {
            // Support both comma separated and multiline values
            List<String> httpsServerProtocolsList = new ArrayList<String>();
            for (String protocol : httpsServerProtocolsArray) {
                httpsServerProtocolsList.addAll(Arrays.asList(StringUtils.split(protocol, ',')));
            }
            httpsServerProtocols = httpsServerProtocolsList
                    .toArray(new String[httpsServerProtocolsList.size()]);
        } else {
            httpsServerProtocols = MirthSSLUtil.DEFAULT_HTTPS_SERVER_PROTOCOLS;
        }

        String[] httpsCipherSuitesArray = mirthConfig.getStringArray(HTTPS_CIPHER_SUITES);
        if (ArrayUtils.isNotEmpty(httpsCipherSuitesArray)) {
            // Support both comma separated and multiline values
            List<String> httpsCipherSuitesList = new ArrayList<String>();
            for (String cipherSuite : httpsCipherSuitesArray) {
                httpsCipherSuitesList.addAll(Arrays.asList(StringUtils.split(cipherSuite, ',')));
            }
            httpsCipherSuites = httpsCipherSuitesList.toArray(new String[httpsCipherSuitesList.size()]);
        } else {
            httpsCipherSuites = MirthSSLUtil.DEFAULT_HTTPS_CIPHER_SUITES;
        }

        String deploy = String.valueOf(mirthConfig.getProperty(STARTUP_DEPLOY));
        if (StringUtils.isNotBlank(deploy)) {
            startupDeploy = Boolean.parseBoolean(deploy);
        }

        // Check for server GUID and generate a new one if it doesn't exist
        PropertiesConfiguration serverIdConfig = new PropertiesConfiguration(
                new File(getApplicationDataDir(), "server.id"));

        if ((serverIdConfig.getString("server.id") != null)
                && (serverIdConfig.getString("server.id").length() > 0)) {
            serverId = serverIdConfig.getString("server.id");
        } else {
            serverId = generateGuid();
            logger.debug("generated new server id: " + serverId);
            serverIdConfig.setProperty("server.id", serverId);
            serverIdConfig.save();
        }

        passwordRequirements = PasswordRequirementsChecker.getInstance().loadPasswordRequirements(mirthConfig);

        apiBypassword = mirthConfig.getString(API_BYPASSWORD);
        if (StringUtils.isNotBlank(apiBypassword)) {
            apiBypassword = new String(Base64.decodeBase64(apiBypassword), "US-ASCII");
        }

        statsUpdateInterval = NumberUtils.toInt(mirthConfig.getString(STATS_UPDATE_INTERVAL),
                DonkeyStatisticsUpdater.DEFAULT_UPDATE_INTERVAL);

        // Check for configuration map properties
        if (mirthConfig.getString(CONFIGURATION_MAP_PATH) != null) {
            configurationFile = mirthConfig.getString(CONFIGURATION_MAP_PATH);
        } else {
            configurationFile = getApplicationDataDir() + File.separator + "configuration.properties";
        }

        PropertiesConfiguration configurationMapProperties = new PropertiesConfiguration();
        configurationMapProperties.setDelimiterParsingDisabled(true);
        configurationMapProperties.setListDelimiter((char) 0);
        try {
            configurationMapProperties.load(new File(configurationFile));
        } catch (ConfigurationException e) {
            logger.warn("Failed to find configuration map file");
        }

        Map<String, ConfigurationProperty> configurationMap = new HashMap<String, ConfigurationProperty>();
        Iterator<String> iterator = configurationMapProperties.getKeys();

        while (iterator.hasNext()) {
            String key = iterator.next();
            String value = configurationMapProperties.getString(key);
            String comment = configurationMapProperties.getLayout().getCanonicalComment(key, false);

            configurationMap.put(key, new ConfigurationProperty(value, comment));
        }

        setConfigurationProperties(configurationMap, false);
    } catch (Exception e) {
        logger.error("Failed to initialize configuration controller", e);
    }
}

From source file:com.mirth.connect.cli.CommandLineInterface.java

private void commandImportMap(Token[] arguments) throws ClientException {
    if (hasInvalidNumberOfArguments(arguments, 1)) {
        return;/*from   w  w w  .  j av a  2  s . c  o  m*/
    }

    // file path
    String path = arguments[1].getText();
    File file = new File(path);

    if (file != null && file.exists()) {
        try {
            PropertiesConfiguration properties = new PropertiesConfiguration(file);
            Map<String, ConfigurationProperty> configurationMap = new HashMap<String, ConfigurationProperty>();
            Iterator<String> iterator = properties.getKeys();

            while (iterator.hasNext()) {
                String key = iterator.next();
                String value = properties.getString(key);
                String comment = properties.getLayout().getCanonicalComment(key, false);

                configurationMap.put(key, new ConfigurationProperty(value, comment));
            }

            client.setConfigurationMap(configurationMap);

            out.println("Configuration map import complete");
        } catch (ConfigurationException e) {
            error("Unable to import configuration map", e);
        }
    } else {
        error("Unable to read file " + path, null);
    }
}

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/*from w w  w .  j a  v  a 2  s.  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.waveprotocol.wave.util.flags.ClientFlagsGenerator.java

private static List<Parameter> extractParametersFromDefaultConfigFile(String fileName) {
    try {//ww w .ja  va2s  .c  o  m
        List<Parameter> parameters = new ArrayList<>();
        PropertiesConfiguration configuration = new PropertiesConfiguration(fileName);
        Iterator<String> keys = configuration.getKeys();
        while (keys.hasNext()) {
            String key = keys.next();
            parameters.add(Parameter.create(key, configuration.getString(key).trim(),
                    configuration.getLayout().getComment(key)));
        }
        return parameters;
    } catch (ConfigurationException e) {
        throw new RuntimeException(e);
    }
}

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

/**
 * This is important: We load stuff as UTF-8.
 * <p>//  w w  w.j  a v  a  2 s  . com
 * 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;
}

From source file:org.wisdom.maven.utils.Properties2HoconConverterTest.java

public final PropertiesConfiguration loadPropertiesWithApacheConfiguration(File file) {

    PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
    propertiesConfiguration.setEncoding("utf-8");
    propertiesConfiguration.setDelimiterParsingDisabled(true);
    propertiesConfiguration.setFile(file);
    propertiesConfiguration.setListDelimiter(',');
    propertiesConfiguration.getLayout().setSingleLine("application.secret", true);

    try {/*from w  w w  . jav  a 2 s  . c o  m*/
        propertiesConfiguration.load(file);
    } catch (ConfigurationException e) {
        return null;
    }

    return propertiesConfiguration;
}