protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(DEFAULT_PLACEHOLDER_PREFIX, DEFAULT_PLACEHOLDER_SUFFIX, DEFAULT_VALUE_SEPARATOR, false); for (Entry<Object, Object> entry : props.entrySet()) { String stringKey = String.valueOf(entry.getKey()); String stringValue = String.valueOf(entry.getValue()); if ((stringKey.contains(DEFAULT_PLACEHOLDER_PREFIX) && stringKey.contains(DEFAULT_PLACEHOLDER_SUFFIX) && (stringKey.indexOf(DEFAULT_PLACEHOLDER_PREFIX) < stringKey.indexOf(DEFAULT_PLACEHOLDER_SUFFIX)))) { stringKey = getReplacedStringKey(stringKey, helper, props); } stringValue = helper.replacePlaceholders(stringValue, props); // if (stringKey.startsWith(this.appPropertiesPrefix)) { FileCloudPropertyConfigurer.appProperties.put(stringKey, stringValue); } properties.put(stringKey, stringValue); } super.processProperties(beanFactoryToProcess, props); }
public String getReplacedStringKey(String stringKey, PropertyPlaceholderHelper helper, Properties props) { if (stringKey.contains(DEFAULT_PLACEHOLDER_PREFIX) && stringKey.contains(DEFAULT_PLACEHOLDER_SUFFIX) && (stringKey.indexOf(DEFAULT_PLACEHOLDER_PREFIX) < stringKey.indexOf(DEFAULT_PLACEHOLDER_SUFFIX))) { // before of target String prefix = stringKey.substring(0, stringKey.indexOf(DEFAULT_PLACEHOLDER_PREFIX)); // replace String refString = stringKey.substring(stringKey.indexOf(DEFAULT_PLACEHOLDER_PREFIX), stringKey.indexOf(DEFAULT_PLACEHOLDER_SUFFIX) + 1); // after of target String suffix = stringKey.substring(stringKey.indexOf(DEFAULT_PLACEHOLDER_SUFFIX) + 1); // get target from props data String refStringValue = helper.replacePlaceholders(refString, props); // build a new local key String newKey = prefix + refStringValue + suffix; // while until the key not contains of ${} return getReplacedStringKey(newKey, helper, props); } return stringKey; }
/** * @param properties */ public DefaultConfigProvider(Properties properties) { if (properties == null) { throw new IllegalArgumentException("properties 不能为null"); } PropertyPlaceholderHelper placeholderHelper = new PropertyPlaceholderHelper("${", "}", ":", true); Properties replacedProperties = new Properties(); for (Map.Entry<?, ?> entry : properties.entrySet()) { String key = (String) entry.getKey(); String value = (String) entry.getValue(); replacedProperties.setProperty(key, placeholderHelper.replacePlaceholders(value, properties)); } this.properties = replacedProperties; }
/** * * {@inheritDoc} */ @Override public void afterPropertiesSet() { if (this.propertyPrefix == null || this.propertyPrefix.trim().isEmpty()) { throw new IllegalStateException("propertyPrefix has not been set"); } if (this.beanTypes == null) { throw new IllegalStateException("beanTypes has not been set"); } if (this.propertiesSource == null) { throw new IllegalStateException("propertiesSource has not been set"); } this.placeholderHelper = new PropertyPlaceholderHelper(this.placeholderPrefix, this.placeholderSuffix, this.valueSeparator, true); }
/** * Replaces each occurence of ${SOME_VALUE} with the varible SOME_VALUE in 'variables' * If any error occures during parsing, i.e: variable doesn't exist, bad syntax, etc... * a {@link PlaceholderResolutionException} is thrown, otherwise the new string after all replacements have been * made will be returned. * * @param variables The variables map to match placeholders against * @param value the string, possibly containing placeholders * @return the value after placeholder replacement have been made * @throws PlaceholderResolutionException if an empty placeholder is found * or a place holder with no suitable value in 'variables' */ public static String replacePlaceholders(final Map<String, String> variables, final String value) throws PlaceholderResolutionException { PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${","}"); return helper.replacePlaceholders(value, new PlaceholderResolver() { public String resolvePlaceholder(String key) { if (key.isEmpty()) { throw new PlaceholderResolutionException("Placeholder in '" + value + "' has to have a length of at least 1"); } String result = variables.get(key); if (result == null) { throw new PlaceholderResolutionException("Missing value for placeholder: '" + key + "' in '" + value + "'"); } return result; } }); }
private int injectPropertiesFromContent(PropertyPlaceholderHelper.PlaceholderResolver resolver) throws RunBuildException { String propertiesFileContent = getRunnerParameters().get("propertiesFileContent"); int propertiesCount = 0; if (StringUtils.hasText(propertiesFileContent)) { getLogger().message("Will use properties content string configured by user"); try (BufferedReader reader = new BufferedReader(new StringReader(propertiesFileContent))) { propertiesCount = injectEnvironmentVars(reader, resolver); } catch (IOException | RuntimeException e) { getLogger().exception(e); throw new RunBuildException("Exception occured reading/parsing given properties content. Message [" + e.getMessage() + "]"); } getLogger().message("Plugin injected [" + propertiesCount + "] parameters from properties content"); } return propertiesCount; }
private int injectPropertiesFromFile(PropertyPlaceholderHelper.PlaceholderResolver resolver) throws RunBuildException { String propertiesFilePath = getRunnerParameters().get("propertiesFilePath"); int propertiesCount = 0; if (StringUtils.hasText(propertiesFilePath)) { getLogger().message("Will use properties from file [" + propertiesFilePath + "]"); try (BufferedReader reader = newBufferedReader(Paths.get(propertiesFilePath), StandardCharsets.UTF_8)) { propertiesCount = injectEnvironmentVars(reader, resolver); } catch (IOException | RuntimeException e) { throw new RunBuildException("Exception occured reading/parsing [" + propertiesFilePath + "]. Message [" + e.getMessage() + "]"); } getLogger().message("Plugin injected [" + propertiesCount + "] parameters from [" + propertiesFilePath + "]"); } return propertiesCount; }
/** * * @param templateResourcePath * @param templateProperties * @return */ private String replaceValuesAndGetTemplateValue(String templateResourcePath, Properties templateProperties,RestResponse response) { InputStream gradleInitTemplateStream = null; try { 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: "; response.error(errorMessage + e.getMessage()); log.error(errorMessage, e); } finally { IOUtils.closeQuietly(gradleInitTemplateStream); } return ""; }
private String replaceValuesAndGetTemplateValue(String templateResourcePath, Properties templateProperties) { InputStream gradleInitTemplateStream = null; try { 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 ""; }
private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) { return helper.replacePlaceholders(text, new PropertyPlaceholderHelper.PlaceholderResolver() { @Override public String resolvePlaceholder(String placeholderName) { return getPropertyAsRawString(placeholderName); } }); }
public StaticStringValueResolver(final Map<String, String> values) { this.helper = new PropertyPlaceholderHelper("${", "}", ":", false); this.resolver = new PlaceholderResolver() { @Override public String resolvePlaceholder(String placeholderName) { return values.get(placeholderName); } }; }
public SpelView(String template) { this.template = template; this.prefix = new RandomValueStringGenerator().generate() + "{"; this.context.addPropertyAccessor(new MapAccessor()); this.resolver = new PropertyPlaceholderHelper.PlaceholderResolver() { public String resolvePlaceholder(String name) { Expression expression = parser.parseExpression(name); Object value = expression.getValue(context); return value == null ? null : value.toString(); } }; }
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception { Map<String, Object> map = new HashMap<String, Object>(model); String path = ServletUriComponentsBuilder.fromContextPath(request).build() .getPath(); map.put("path", (Object) path==null ? "" : path); context.setRootObject(map); String maskedTemplate = template.replace("${", prefix); PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(prefix, "}"); String result = helper.replacePlaceholders(maskedTemplate, resolver); result = result.replace(prefix, "${"); response.setContentType(getContentType()); response.getWriter().append(result); }
@Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { super.processProperties(beanFactoryToProcess, props); // store all the spring properties so we can refer to them later properties.putAll(props); // create helper helper = new PropertyPlaceholderHelper( configuredPlaceholderPrefix != null ? configuredPlaceholderPrefix : DEFAULT_PLACEHOLDER_PREFIX, configuredPlaceholderSuffix != null ? configuredPlaceholderSuffix : DEFAULT_PLACEHOLDER_SUFFIX, configuredValueSeparator != null ? configuredValueSeparator : DEFAULT_VALUE_SEPARATOR, configuredIgnoreUnresolvablePlaceholders != null ? configuredIgnoreUnresolvablePlaceholders : false); }
@NotNull @Override public ProgramCommandLine makeProgramCommandLine() throws RunBuildException { PropertyPlaceholderHelper.PlaceholderResolver resolver = new TeamcityPlaceholderResolver(); int envVarsFromFile = injectPropertiesFromFile(resolver); int envVarsFromContent = injectPropertiesFromContent(resolver); if (envVarsFromContent == 0 && envVarsFromFile == 0) { getLogger().error("You have used the EnvInject plugin but not provided a properties file or content"); getLogger().error("This runner will effectively be a no-op"); } return new SimpleProgramCommandLine(getRunnerContext(), "java", Collections.singletonList("-version")); }
protected int injectEnvironmentVars(BufferedReader reader, PropertyPlaceholderHelper.PlaceholderResolver resolver) throws IOException, RunBuildException { PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("{", "}"); String line; int propertiesCount = 0; while ((line = reader.readLine()) != null) { addDebugMessage("Read line [%s]", line); if (!line.contains("=")) { throw new RunBuildException("The line [" + line + "] does contain property of the form key=value"); } String[] split = line.split("=", 2); addDebugMessage("Will use key [%s] and value [%s]", split[0], split[1]); String finalValue = helper.replacePlaceholders(split[1], resolver); addDebugMessage("Resolved value of [%s] is [%s]", split[1], finalValue); if (split[0].startsWith(ENV_VAR_PREFIX)) { addDebugMessage("Will set environment variable"); getAgentConfiguration().addEnvironmentVariable(split[0].replace(ENV_VAR_PREFIX, ""), finalValue); } else if (split[0].startsWith(SYSTEM_VAR_PREFIX)) { addDebugMessage("Will set system property"); getAgentConfiguration().addSystemProperty(split[0].replace(SYSTEM_VAR_PREFIX, ""), finalValue); } else { addDebugMessage("Will set configuration property"); getAgentConfiguration().addConfigurationParameter(split[0], finalValue); } propertiesCount++; } return propertiesCount; }
private String replacePlaceholders(String value, Properties properties) { if (value.contains("$")) { LOGGER.debug("replacing placeholders in " + value); PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}"); String newValue = helper.replacePlaceholders(value, properties); LOGGER.debug("after replacement: " + newValue); return newValue; } else { return value; } }
private String replaceProperties(String uri) { if (uri == null) { return null; } PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", ":", true); return helper.replacePlaceholders(uri, new PropertyPlaceholderHelper.PlaceholderResolver() { @Override public String resolvePlaceholder(String placeholderName) { return propertyResolver.getProperty(placeholderName); } }); }
private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) { return helper.replacePlaceholders(text, new PropertyPlaceholderHelper.PlaceholderResolver() { public String resolvePlaceholder(String placeholderName) { return getPropertyAsRawString(placeholderName); } }); }
public SpelView(String template) { this.template = template; this.context.addPropertyAccessor(new MapAccessor()); this.helper = new PropertyPlaceholderHelper("${", "}"); this.resolver = new PlaceholderResolver() { public String resolvePlaceholder(String name) { Expression expression = parser.parseExpression(name); Object value = expression.getValue(context); return value==null ? null : value.toString(); } }; }
/** * replace placeholder in properties file with value; * @param placeholders placeholder to replace */ public final void replacePlaceholders(Map<String, String> placeholders) { PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders); PlaceholderResolver resolver = new PlaceholderConfigurerResolver(placeholders); for (Object key : this.properties.keySet()) { Object val = this.properties.get(key); this.properties.setProperty(key.toString(), helper.replacePlaceholders(val.toString(), resolver)); } }
/** * Load the JSON template and perform string substitution on the ${...} tokens * @param is * @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); }
/** * Default constructor */ public SpringPropertyReplacer() { phHelper = new PropertyPlaceholderHelper( PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX, PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX, PlaceholderConfigurerSupport.DEFAULT_VALUE_SEPARATOR, true); }
public PlaceholderResolvingStringValueResolver(Properties props) { this.helper = new PropertyPlaceholderHelper( placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders); this.resolver = new PropertyPlaceholderConfigurerResolver(props); }
private PropertyPlaceholderHelper createPlaceholderHelper(boolean ignoreUnresolvablePlaceholders) { return new PropertyPlaceholderHelper(this.placeholderPrefix, this.placeholderSuffix, this.valueSeparator, ignoreUnresolvablePlaceholders); }
public PlaceholderResolvingStringValueResolver(Properties props) { this.helper = new PropertyPlaceholderHelper("${", "}", ":", true); this.resolver = new PropertyPlaceholderConfigurerResolver(props); }
SpelView(String template) { this.template = template; this.context.addPropertyAccessor(new MapAccessor()); this.helper = new PropertyPlaceholderHelper("${", "}"); this.resolver = new SpelPlaceholderResolver(this.context); }
protected void initializeHelper() { helper = new PropertyPlaceholderHelper(PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX, PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX, PlaceholderConfigurerSupport.DEFAULT_VALUE_SEPARATOR, true); }
public void setPropertyPlaceholderHelper(PropertyPlaceholderHelper helper) { this.helper = helper; }
public String getFilteredContent(String s, Properties properties) { PropertyPlaceholderHelper pph = new PropertyPlaceholderHelper(PREFIX, SUFFIX); String filtered = pph.replacePlaceholders(s, properties); return filtered; }
public PlaceholderResolvingStringValueResolver() { this.helper = new PropertyPlaceholderHelper( placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders); this.resolver = new PropertyPlaceholderConfigurerResolver(); }