Example usage for org.springframework.util PropertyPlaceholderHelper PropertyPlaceholderHelper

List of usage examples for org.springframework.util PropertyPlaceholderHelper PropertyPlaceholderHelper

Introduction

In this page you can find the example usage for org.springframework.util PropertyPlaceholderHelper PropertyPlaceholderHelper.

Prototype

public PropertyPlaceholderHelper(String placeholderPrefix, String placeholderSuffix) 

Source Link

Document

Creates a new PropertyPlaceholderHelper that uses the supplied prefix and suffix.

Usage

From source file:com.brienwheeler.lib.spring.beans.PropertyPlaceholderConfigurer.java

/**
  * Resolves any placeholders in the supplied properties map, preferring the use of
  * previously set System properties over the current property map contents for
  * placeholder substitution./*from www  . ja  va2  s  .  c o m*/
  * <p>
  * Also sets any resolved properties as System properties, if no System
  * property by that name already exists.
  * 
  * @param properties the merged context properties
  */
public static void processProperties(final Properties properties, String placeholderPrefix,
        String placeholderSuffix) {
    PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(placeholderPrefix, placeholderSuffix);
    PropertyPlaceholderHelper.PlaceholderResolver resolver = new PropertyPlaceholderHelper.PlaceholderResolver() {
        @Override
        public String resolvePlaceholder(String placeholderName) {
            // SYSTEM_PROPERTIES_MODE_OVERRIDE means we look at previously set
            // system properties in preference to properties defined in our file
            String value = System.getProperty(placeholderName);
            if (value == null)
                value = properties.getProperty(placeholderName);
            return value;
        }
    };

    for (Object key : properties.keySet()) {
        String propertyName = (String) key;
        // get the value from the map
        String propertyValue = properties.getProperty(propertyName);
        // resolve it against system properties then other properties in the
        // passed-in Properties
        String resolvedValue = helper.replacePlaceholders(propertyValue, resolver);

        // set back into passed-in Properties if changed
        if (!ObjectUtils.nullSafeEquals(propertyValue, resolvedValue)) {
            properties.setProperty(propertyName, resolvedValue);
        }

        // set into System properties if not already set
        setProperty(propertyName, resolvedValue);
    }
}

From source file:com.alvexcore.repo.SimpleKeySelectorResult.java

private File findLicenseFile() {
    PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}");
    String sharedLoader = System.getProperty("shared.loader");
    if (sharedLoader == null)
        return null;
    String sharedLoadPaths = helper.replacePlaceholders(sharedLoader, System.getProperties());
    for (String path : sharedLoadPaths.split(",")) { // FIXME is this separator the same for all envs?
        File file = new File(path, "alfresco" + File.separator + "extension" + File.separator + "license");
        if (file.isDirectory())
            for (File elem : file.listFiles())
                if (elem.getAbsolutePath().endsWith("alvex.lic"))
                    return elem;
    }/* ww w .j  av a  2  s. c  o m*/
    return null;
}

From source file:org.artifactory.webapp.wicket.page.home.settings.ivy.gradle.GradleBuildScriptPanel.java

private String replaceValuesAndGetTemplateValue(String templateResourcePath, Properties templateProperties) {
    InputStream gradleInitTemplateStream = null;
    try {//from ww  w.j  a  v a2  s.  c om
        gradleInitTemplateStream = getClass().getResourceAsStream(templateResourcePath);
        String gradleTemplate = IOUtils.toString(gradleInitTemplateStream);
        PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper("${", "}");
        return propertyPlaceholderHelper.replacePlaceholders(gradleTemplate, templateProperties);
    } catch (IOException e) {
        String errorMessage = "An error occurred while preparing the Gradle Init Script template: ";
        error(errorMessage + e.getMessage());
        log.error(errorMessage, e);
    } finally {
        IOUtils.closeQuietly(gradleInitTemplateStream);
    }
    return "";
}

From source file:org.jahia.osgi.FrameworkService.java

private void setupSystemProperties() {

    @SuppressWarnings("unchecked")
    Map<String, String> unreplaced = (Map<String, String>) SpringContextSingleton.getBean("osgiProperties");
    Map<String, String> newSystemProperties = new TreeMap<>();

    PropertyPlaceholderHelper placeholderHelper = new PropertyPlaceholderHelper("${", "}");
    Properties systemProps = System.getProperties();

    for (Map.Entry<String, String> entry : unreplaced.entrySet()) {
        newSystemProperties.put(entry.getKey(),
                placeholderHelper.replacePlaceholders(entry.getValue(), systemProps));
    }//from  w w  w .  jav a  2  s  . c o m

    for (Map.Entry<String, String> property : newSystemProperties.entrySet()) {
        String propertyName = property.getKey();
        String oldPropertyValue = System.getProperty(propertyName);
        if (oldPropertyValue != null) {
            logger.warn("Overriding system property " + propertyName + "=" + oldPropertyValue
                    + " with new value=" + property.getValue());
        }
        JahiaContextLoaderListener.setSystemProperty(propertyName, property.getValue());
    }

    File file = new File(System.getProperty("karaf.etc"), "config.properties");
    org.apache.felix.utils.properties.Properties karafConfigProperties = null;
    try {
        karafConfigProperties = PropertiesLoader.loadConfigProperties(file);
    } catch (Exception e) {
        logger.error("Unable to load properties from file " + file + ". Cause: " + e.getMessage(), e);
        karafConfigProperties = new org.apache.felix.utils.properties.Properties();
    }

    StringBuilder extraSystemPackages = new StringBuilder(
            karafConfigProperties.getProperty("org.osgi.framework.system.packages.extra"));
    boolean modifiedExtraSystemPackages = false;
    for (Map.Entry<String, String> entry : newSystemProperties.entrySet()) {
        if (entry.getKey().startsWith("org.osgi.framework.system.packages.extra.")) {
            extraSystemPackages.append(',').append(entry.getValue());
            modifiedExtraSystemPackages = true;
        }
    }
    if (modifiedExtraSystemPackages) {
        JahiaContextLoaderListener.setSystemProperty("org.osgi.framework.system.packages.extra",
                extraSystemPackages.toString());
    }

}

From source file:org.slc.sli.api.init.ApplicationInitializer.java

/**
 * Load the JSON template and perform string substitution on the ${...} tokens
 * @param is/*from   w ww  .  j  ava  2  s  .  com*/
 * @return
 * @throws IOException
 */
@SuppressWarnings("unchecked")
Map<String, Object> loadJsonFile(InputStream is, Properties sliProps) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer);
    String template = writer.toString();
    PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}");
    template = helper.replacePlaceholders(template, sliProps);
    return (Map<String, Object>) JSON.parse(template);
}