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.xemantic.tadedon.configuration.Configurations.java

/**
 * Copies property identified by {@code key} from the {@code srcConf} to {@code dstConf}.
 * This method will also copy property comments.
 *
 * @param defaultConf//from   w  w w  .j ava  2s .c  om
 * @param conf
 * @param key
 */
public static void copyProperty(String key, PropertiesConfiguration srcConf, PropertiesConfiguration dstConf) {
    LOG.debug("Copying property: {}", key);
    Object value = srcConf.getProperty(key);
    dstConf.setProperty(key, value);
    String comment = srcConf.getLayout().getComment(key);
    if (comment != null) {
        dstConf.getLayout().setComment(key, comment);
    }
}

From source file:com.mirth.connect.server.migration.Migrate3_4_0.java

@Override
public void updateConfiguration(PropertiesConfiguration configuration) {
    if (StringUtils.containsIgnoreCase(configuration.getLayout().getComment("database"), "sqlserver2000")) {
        configuration.getLayout().setComment("database", "options: derby, mysql, postgres, oracle, sqlserver");

        try {/*from  w w  w.j av  a2 s  .  c  om*/
            configuration.save();
        } catch (ConfigurationException e) {
            logger.warn("An error occurred updating the database property comment.");
        }
    }
}

From source file:com.impetus.ankush.agent.action.impl.PropertyFileManipulator.java

/**
 * Delete conf value.//from  w  w w. j  a  v a  2  s  .com
 * 
 * @param file
 *            the file
 * @param propertyName
 *            the property name
 * @return true, if successful
 */
@Override
public boolean deleteConfValue(String file, String propertyName) {
    boolean status = false;
    try {
        // read conf file
        File confFile = new File(file);

        if (!confFile.exists()) {
            System.err.println("File " + file + " does not exists.");
            status = false;
        }
        PropertiesConfiguration props = new PropertiesConfiguration(file);
        props.getLayout().setSeparator(propertyName, "=");
        if (props.getProperty(propertyName) != null) {
            props.clearProperty(propertyName);
            props.save();
            status = true;
        }
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    return status;

}

From source file:com.impetus.ankush.agent.action.impl.PropertyFileManipulator.java

/**
 * Edits the conf value./*from w w w . j ava  2 s.com*/
 * 
 * @param file
 *            the file
 * @param propertyName
 *            the property name
 * @param newPropertyValue
 *            the new property value
 * @return true, if successful
 */
@Override
public boolean editConfValue(String file, String propertyName, String newPropertyValue) {
    boolean status = false;
    try {
        // read conf file
        File confFile = new File(file);

        if (!confFile.exists()) {
            System.err.println("File " + file + " does not exists.");
            status = false;
        }
        PropertiesConfiguration props = new PropertiesConfiguration(file);
        props.setProperty(propertyName, newPropertyValue);
        props.getLayout().setSeparator(propertyName, "=");
        props.save();
        status = true;
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    return status;
}

From source file:com.jaspersoft.buildomatic.crypto.MasterPropertiesObfuscator.java

@Override
public void execute() throws BuildException {
    try {//  w  ww .  j  av a  2 s .  com
        //load master props
        PropertiesConfiguration config = new PropertiesConfiguration(new File(propsFile));
        PropertiesConfigurationLayout configLayout = config.getLayout();
        configLayout.setGlobalSeparator("=");

        Boolean encFlag = Boolean.parseBoolean(config.getString(ENCRYPT_FLAG));
        Boolean encDoneFlag = Boolean.parseBoolean(config.getString(ENCRYPT_DONE_FLAG));
        if (encFlag && !encDoneFlag) {
            String blockSzStr = config.getString(CRYPTO_BLOCK_SIZE_PARAM);
            EncryptionProperties encProps = new EncryptionProperties(
                    blockSzStr != null ? Integer.parseInt(blockSzStr) : null,
                    config.getString(CRYPTO_TRANSFORMATION_PARAM));
            List<Object> propsToEncryptList = config.getList(PROPS_TO_ENCRYPT_PARAM,
                    Arrays.<Object>asList(PROPS_TO_ENCRYPT_DEF));
            log("Encrypt " + StringUtils.join(propsToEncryptList.toArray(), ','), Project.MSG_INFO);
            log("Encryption block size: " + encProps.getBlockSize(), Project.MSG_DEBUG);
            log("Encryption mode: " + encProps.getCipherTransformation(), Project.MSG_DEBUG);

            //obtain Keystore Manager
            KeystoreManager.init(this.ksp);
            KeystoreManager ksManager = KeystoreManager.getInstance();

            //obtain key
            Key secret = ksManager.getBuildKey();

            Set<String> paramSet = new HashSet<String>(propsToEncryptList.size());
            for (Object prop : propsToEncryptList) {
                String propNameToEnc = prop.toString().trim();
                if (paramSet.contains(propNameToEnc))
                    continue; //was already encrypted once
                paramSet.add(propNameToEnc);

                String pVal = config.getString(propNameToEnc);
                if (pVal != null) {
                    if (EncryptionEngine.isEncrypted(pVal))
                        log("encrypt=true was set, but param " + propNameToEnc
                                + " was found already encrypted. " + " Skipping its encryption.",
                                Project.MSG_WARN);
                    else {
                        String ct = EncryptionEngine.encrypt(secret, pVal, encProps);
                        config.setProperty(propNameToEnc, ct);
                    }
                }
            }

            //set encryption to done
            config.clearProperty(ENCRYPT_FLAG);
            config.setProperty(ENCRYPT_DONE_FLAG, "true");

            //write master props back
            config.save();
        } else if (encDoneFlag) {
            log("The master properties have already been encrypted. To re-enable the encryption, "
                    + "make sure the passwords are in plain text, set master property "
                    + "encrypt to true and remove encrypt.done.", Project.MSG_INFO);
        }
    } catch (Exception e) {
        throw new BuildException(e, getLocation());
    }
}

From source file:com.francetelecom.clara.cloud.logicalmodel.LogicalConfigServiceUtils.java

public void dumpConfigContent(StructuredLogicalConfigServiceContent content, Writer writer)
        throws InvalidConfigServiceException {
    try {/*from  w ww . j a v  a2  s  .com*/
        ValidatorUtil.validate(content);
    } catch (TechnicalException e) {
        throw new InvalidConfigServiceException("Invalid structured content:" + e, e);
    }
    PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
    PropertiesConfigurationLayout layout = propertiesConfiguration.getLayout();
    layout.setLineSeparator("\n");
    String headerComment = content.getHeaderComment();
    if (headerComment != null) {
        layout.setHeaderComment(headerComment);
    }
    for (ConfigEntry configEntry : content.configEntries) {
        String key = configEntry.getKey();
        propertiesConfiguration.addProperty(key, configEntry.getValue());
        String comment = configEntry.getComment();
        layout.setSeparator(key, "=");
        if (comment != null) {
            layout.setComment(key, comment);
        }
    }
    try {
        propertiesConfiguration.save(writer);
    } catch (ConfigurationException e) {
        throw new InvalidConfigServiceException("Invalid structured content or output:" + e, e);
    }
}

From source file:com.mirth.connect.client.ui.SettingsPanelMap.java

public void doExportMap() {
    if (isSaveEnabled()) {
        int option = JOptionPane.showConfirmDialog(this, "Would you like to save the settings first?");

        if (option == JOptionPane.YES_OPTION) {
            if (!doSave()) {
                return;
            }/*  w w w .ja va 2 s.  com*/
        } else if (option == JOptionPane.CANCEL_OPTION || option == JOptionPane.CLOSED_OPTION) {
            return;
        }
    }

    final String workingId = getFrame().startWorking("Exporting " + getTabName() + " settings...");

    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {

        private Map<String, ConfigurationProperty> configurationMap;

        public Void doInBackground() {
            try {
                File file = getFrame().createFileForExport(null, "PROPERTIES");
                if (file != null) {
                    PropertiesConfiguration properties = new PropertiesConfiguration();
                    properties.setDelimiterParsingDisabled(true);
                    properties.setListDelimiter((char) 0);
                    properties.clear();
                    PropertiesConfigurationLayout layout = properties.getLayout();

                    configurationMap = getFrame().mirthClient.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(file);
                }
            } catch (Exception e) {
                getFrame().alertThrowable(getFrame(), e);
            }

            return null;
        }

        @Override
        public void done() {
            getFrame().stopWorking(workingId);
        }
    };

    worker.execute();
}

From source file:com.francetelecom.clara.cloud.logicalmodel.LogicalConfigServiceUtils.java

/**
 *
 *
 * @param reader// w  w  w. j a v a2  s .  c  o m
 * @return
 */
public StructuredLogicalConfigServiceContent parseConfigContent(Reader reader)
        throws InvalidConfigServiceException {
    List<ConfigEntry> parsedEntries = new ArrayList<ConfigEntry>();
    PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();

    try {
        propertiesConfiguration.load(reader);
    } catch (ConfigurationException e) {
        InvalidConfigServiceException invalidConfigServiceException = new InvalidConfigServiceException(
                "Invalid config content. Caught:" + e, e);
        throw invalidConfigServiceException;
    }
    PropertiesConfigurationLayout layout = propertiesConfiguration.getLayout();
    String headerComment = layout.getHeaderComment();

    Set<String> keys = layout.getKeys();
    Set<String> duplicates = new HashSet<String>();
    for (String key : keys) {
        String comment = layout.getComment(key);
        if (comment != null) {
            comment = escapesPoundsInComments(comment);
        }
        if (!layout.isSingleLine(key)) {
            //reject the duplicate key
            duplicates.add(key);
        } else {
            String value = propertiesConfiguration.getString(key);
            parsedEntries.add(new ConfigEntry(key, value, comment));
        }
    }

    if (duplicates.size() > 0) {
        InvalidConfigServiceException invalidConfigServiceException = new InvalidConfigServiceException(
                "Collisions! " + duplicates);
        invalidConfigServiceException.setType(ErrorType.DUPLICATE_KEYS);
        invalidConfigServiceException.getDuplicateKeys().addAll(duplicates);
        throw invalidConfigServiceException;
    }

    List<ConfigEntry> configEntries = parsedEntries;
    StructuredLogicalConfigServiceContent structuredLogicalConfigServiceContent = new StructuredLogicalConfigServiceContent(
            headerComment, configEntries);
    return structuredLogicalConfigServiceContent;
}

From source file:com.mirth.connect.client.ui.SettingsPanelMap.java

public void doImportMap() {
    File file = getFrame().browseForFile("PROPERTIES");

    if (file != null) {
        try {// w w w .j ava2  s.  c o m
            PropertiesConfiguration properties = new PropertiesConfiguration();
            properties.setDelimiterParsingDisabled(true);
            properties.setListDelimiter((char) 0);
            properties.load(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));
            }

            updateConfigurationTable(configurationMap);
            setSaveEnabled(true);
        } catch (Exception e) {
            getFrame().alertThrowable(getFrame(), e, "Error importing configuration map");
        }
    }
}

From source file:com.mirth.connect.server.migration.ServerMigrator.java

private void runConfigurationMigrator(ConfigurationMigrator configurationMigrator,
        PropertiesConfiguration mirthConfig, Version version) {
    configurationMigrator.updateConfiguration(mirthConfig);

    HashMap<String, Object> addedProperties = new LinkedHashMap<String, Object>();
    Map<String, Object> propertiesToAdd = configurationMigrator.getConfigurationPropertiesToAdd();

    if (propertiesToAdd != null) {
        for (Entry<String, Object> propertyToAdd : propertiesToAdd.entrySet()) {
            if (!mirthConfig.containsKey(propertyToAdd.getKey())) {
                PropertiesConfigurationLayout layout = mirthConfig.getLayout();
                String key = propertyToAdd.getKey();
                Object value;//from w  ww  . ja  va 2  s.  c  o m
                String comment = "";

                if (propertyToAdd.getValue() instanceof Pair) {
                    // If a pair is used, get both the value and comment
                    Pair<Object, String> pair = (Pair<Object, String>) propertyToAdd.getValue();
                    value = pair.getLeft();
                    comment = pair.getRight();
                } else {
                    // Only the value was specified
                    value = propertyToAdd.getValue();
                }

                mirthConfig.setProperty(key, value);

                // If this is the first added property, add a general comment about the added properties before it
                if (addedProperties.isEmpty()) {
                    if (StringUtils.isNotEmpty(comment)) {
                        comment = "\n\n" + comment;
                    }
                    comment = "The following properties were automatically added on startup for version "
                            + version + comment;
                }

                if (StringUtils.isNotEmpty(comment)) {
                    // When a comment is specified, always put a blank line before it
                    layout.setBlancLinesBefore(key, 1);
                    layout.setComment(key, comment);
                }

                addedProperties.put(key, value);
            }
        }
    }

    List<String> removedProperties = new ArrayList<String>();
    String[] propertiesToRemove = configurationMigrator.getConfigurationPropertiesToRemove();

    if (propertiesToRemove != null) {
        for (String propertyToRemove : propertiesToRemove) {
            if (mirthConfig.containsKey(propertyToRemove)) {
                mirthConfig.clearProperty(propertyToRemove);
                removedProperties.add(propertyToRemove);
            }
        }
    }

    if (!addedProperties.isEmpty() || !removedProperties.isEmpty()) {
        if (!addedProperties.isEmpty()) {
            logger.info("Adding properties in mirth.properties: " + addedProperties);
        }

        if (!removedProperties.isEmpty()) {
            logger.info("Removing properties in mirth.properties: " + removedProperties);
        }

        try {
            mirthConfig.save();
        } catch (ConfigurationException e) {
            logger.error("There was an error updating mirth.properties.", e);

            if (!addedProperties.isEmpty()) {
                logger.error("The following properties should be added to mirth.properties manually: "
                        + addedProperties.toString());
            }

            if (!removedProperties.isEmpty()) {
                logger.error("The following properties should be removed from mirth.properties manually: "
                        + removedProperties.toString());
            }
        }
    }
}