/** * Get a {@link LogFile} from the given Spring {@link Environment}. * @param propertyResolver the {@link PropertyResolver} used to obtain the logging * properties * @return a {@link LogFile} or {@code null} if the environment didn't contain any * suitable properties */ public static LogFile get(PropertyResolver propertyResolver) { String file = propertyResolver.getProperty(FILE_PROPERTY); String path = propertyResolver.getProperty(PATH_PROPERTY); if (StringUtils.hasLength(file) || StringUtils.hasLength(path)) { return new LogFile(file, path); } return null; }
private static String resolvePlaceholders(String attributeValue, PropertyResolver propertyResolver) { String resolvedValue = attributeValue; if (propertyResolver != null) { resolvedValue = propertyResolver.resolvePlaceholders(resolvedValue); resolvedValue = trimWhitespace(resolvedValue); } return resolvedValue; }
/** * Resolve the given path, replacing placeholders with corresponding * property values from the {@code environment} if necessary. * @param path the original file path * @return the resolved file path * @see PropertyResolver#resolvePlaceholders * @see PropertyResolver#resolveRequiredPlaceholders */ protected String resolvePath(String path) { if (this.propertyResolver == null) { this.propertyResolver = new StandardEnvironment(); } return (this.ignoreUnresolvablePlaceholders ? this.propertyResolver.resolvePlaceholders(path) : this.propertyResolver.resolveRequiredPlaceholders(path)); }
@Override public <T> T getProperty(String key, Class<T> targetType, T defaultValue) { RelaxedNames prefixes = new RelaxedNames(this.prefix); RelaxedNames keys = new RelaxedNames(key); for (String prefix : prefixes) { for (String relaxedKey : keys) { if (this.resolver.containsProperty(prefix + relaxedKey)) { return this.resolver.getProperty(prefix + relaxedKey, targetType); } } } return defaultValue; }
@Test public void resolveRequiredPlaceholders_withDefaultValue() { MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(new MockPropertySource().withProperty("key", "value")); PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources); assertThat(resolver.resolveRequiredPlaceholders("Replace this ${key} plus ${unknown:defaultValue}"), equalTo("Replace this value plus defaultValue")); }
@Override public boolean containsProperty(String key) { return delegate.containsProperty(key); }
@SuppressWarnings("unchecked") @Deprecated public <T> Class<T> getPropertyAsClass(String key, Class<T> targetType) { RelaxedNames prefixes = new RelaxedNames(this.prefix); RelaxedNames keys = new RelaxedNames(key); for (String prefix : prefixes) { for (String relaxedKey : keys) { if (this.resolver.containsProperty(prefix + relaxedKey)) { return (Class<T>) this.resolver.getProperty(prefix + relaxedKey, targetType); } } } return null; }
@Test public void resolveRequiredPlaceholders() { MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(new MockPropertySource().withProperty("key", "value")); PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources); assertThat(resolver.resolveRequiredPlaceholders("Replace this ${key}"), equalTo("Replace this value")); }
@Override public boolean containsKey(String key) { return propertyResolver.containsProperty(key); }
private void setSystemProperty(PropertyResolver resolver, String systemPropertyName, String propertyName) { setSystemProperty(systemPropertyName, resolver.getProperty("logging." + propertyName)); }
private static String resolvePlaceholders(String attributeValue, PropertyResolver propertyResolver) { String resolvedValue = attributeValue; if (propertyResolver != null) { resolvedValue = propertyResolver.resolvePlaceholders(resolvedValue); resolvedValue = trimWhitespace(resolvedValue); } return resolvedValue; }
/** * Resolve the given path, replacing placeholders with * corresponding system property values if necessary. * @param path the original file path * @return the resolved file path * @see PropertyResolver#resolvePlaceholders * @see PropertyResolver#resolveRequiredPlaceholders(String) */ protected String resolvePath(String path) { if (this.propertyResolver == null) { this.propertyResolver = new StandardEnvironment(); } return (this.ignoreUnresolvablePlaceholders ? this.propertyResolver.resolvePlaceholders(path) : this.propertyResolver.resolveRequiredPlaceholders(path)); }
@Test(expected = IllegalArgumentException.class) public void resolveRequiredPlaceholders_withUnresolvable() { MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(new MockPropertySource().withProperty("key", "value")); PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources); resolver.resolveRequiredPlaceholders("Replace this ${key} plus ${unknown}"); }
@Override public boolean containsProperty(String key) { return delegate.containsProperty(key); }
@Test public void getProperty_doesNotCache_replaceExistingKeyPostConstruction() { String key = "foo"; String value1 = "bar"; String value2 = "biz"; HashMap<String, Object> map = new HashMap<>(); map.put(key, value1); // before construction MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(new MapPropertySource("testProperties", map)); PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources); assertThat(propertyResolver.getProperty(key), equalTo(value1)); map.put(key, value2); // after construction and first resolution assertThat(propertyResolver.getProperty(key), equalTo(value2)); }
@Override public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) { try { String banner = StreamUtils.copyToString(this.resource.getInputStream(), environment.getProperty("spring.banner.charset", Charset.class, StandardCharsets.UTF_8)); for (PropertyResolver resolver : getPropertyResolvers(environment, sourceClass)) { banner = resolver.resolvePlaceholders(banner); } out.println(banner); } catch (Exception ex) { logger.warn("Banner not printable: " + this.resource + " (" + ex.getClass() + ": '" + ex.getMessage() + "')", ex); } }
/** * Resolve the given path, replacing placeholders with * corresponding system property values if necessary. * @param path the original file path * @return the resolved file path * @see PropertyResolver#resolvePlaceholders * @see PropertyResolver#resolveRequiredPlaceholders(String) */ protected String resolvePath(String path) { if (this.propertyResolver == null) { this.propertyResolver = new StandardEnvironment(); } return (this.ignoreUnresolvablePlaceholders ? this.propertyResolver.resolvePlaceholders(path) : this.propertyResolver.resolveRequiredPlaceholders(path)); }
@Override public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException { return delegate.resolveRequiredPlaceholders(text); } }
@Override public boolean containsKey(String key) { return propertyResolver.containsProperty(key); }
@Test public void getProperty_doesNotCache_addNewKeyPostConstruction() { HashMap<String, Object> map = new HashMap<>(); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(new MapPropertySource("testProperties", map)); PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources); assertThat(propertyResolver.getProperty("foo"), equalTo(null)); map.put("foo", "42"); assertThat(propertyResolver.getProperty("foo"), equalTo("42")); }