Commit 0e46fc06 authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #5521 from eddumelendez/gh-5447

* pr/5521:
  Polish contribution
  Add `defaultValue` property in springProperty tag
parents 54bea725 a66045fa
...@@ -1351,11 +1351,14 @@ for use within Logback. This can be useful if you want to access values from you ...@@ -1351,11 +1351,14 @@ for use within Logback. This can be useful if you want to access values from you
`application.properties` file in your logback configuration. The tag works in a similar `application.properties` file in your logback configuration. The tag works in a similar
way to Logback's standard `<property>` tag, but rather than specifying a direct `value` way to Logback's standard `<property>` tag, but rather than specifying a direct `value`
you specify the `source` of the property (from the `Environment`). You can use the `scope` you specify the `source` of the property (from the `Environment`). You can use the `scope`
attribute if you need to store the property somewhere other than in `local` scope. attribute if you need to store the property somewhere other than in `local` scope. If
you need a fallback value in case the property is not set in the `Environment`, you can
use the `defaultValue` attribute.
[source,xml,indent=0] [source,xml,indent=0]
---- ----
<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"/> <springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"
defaultValue="localhost"/>
<appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender"> <appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">
<remoteHost>${fluentHost}</remoteHost> <remoteHost>${fluentHost}</remoteHost>
... ...
......
...@@ -32,10 +32,12 @@ import org.springframework.core.env.Environment; ...@@ -32,10 +32,12 @@ import org.springframework.core.env.Environment;
* properties to be sourced from the Spring environment. * properties to be sourced from the Spring environment.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Eddú Meléndez
*/ */
class SpringPropertyAction extends Action { class SpringPropertyAction extends Action {
private static final String SOURCE_ATTRIBUTE = "source"; private static final String SOURCE_ATTRIBUTE = "source";
private static final String DEFAULT_VALUE_ATTRIBUTE = "defaultValue";
private final Environment environment; private final Environment environment;
...@@ -49,17 +51,18 @@ class SpringPropertyAction extends Action { ...@@ -49,17 +51,18 @@ class SpringPropertyAction extends Action {
String name = attributes.getValue(NAME_ATTRIBUTE); String name = attributes.getValue(NAME_ATTRIBUTE);
String source = attributes.getValue(SOURCE_ATTRIBUTE); String source = attributes.getValue(SOURCE_ATTRIBUTE);
Scope scope = ActionUtil.stringToScope(attributes.getValue(SCOPE_ATTRIBUTE)); Scope scope = ActionUtil.stringToScope(attributes.getValue(SCOPE_ATTRIBUTE));
String defaultValue = attributes.getValue(DEFAULT_VALUE_ATTRIBUTE);
if (OptionHelper.isEmpty(name) || OptionHelper.isEmpty(source)) { if (OptionHelper.isEmpty(name) || OptionHelper.isEmpty(source)) {
addError( addError(
"The \"name\" and \"source\" attributes of <springProperty> must be set"); "The \"name\" and \"source\" attributes of <springProperty> must be set");
} }
ActionUtil.setProperty(ic, name, getValue(source), scope); ActionUtil.setProperty(ic, name, getValue(source, defaultValue), scope);
} }
private String getValue(String source) { private String getValue(String source, String defaultValue) {
if (this.environment == null) { if (this.environment == null) {
addWarn("No Spring Environment available to resolve " + source); addWarn("No Spring Environment available to resolve " + source);
return null; return defaultValue;
} }
String value = this.environment.getProperty(source); String value = this.environment.getProperty(source);
if (value != null) { if (value != null) {
...@@ -70,9 +73,9 @@ class SpringPropertyAction extends Action { ...@@ -70,9 +73,9 @@ class SpringPropertyAction extends Action {
String prefix = source.substring(0, lastDot + 1); String prefix = source.substring(0, lastDot + 1);
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver( RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
this.environment, prefix); this.environment, prefix);
return resolver.getProperty(source.substring(lastDot + 1)); return resolver.getProperty(source.substring(lastDot + 1), defaultValue);
} }
return null; return defaultValue;
} }
@Override @Override
......
...@@ -41,6 +41,7 @@ import static org.hamcrest.Matchers.not; ...@@ -41,6 +41,7 @@ import static org.hamcrest.Matchers.not;
* *
* @author Phillip Webb * @author Phillip Webb
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Stephane Nicoll
*/ */
public class SpringBootJoranConfiguratorTests { public class SpringBootJoranConfiguratorTests {
...@@ -140,6 +141,30 @@ public class SpringBootJoranConfiguratorTests { ...@@ -140,6 +141,30 @@ public class SpringBootJoranConfiguratorTests {
assertThat(this.context.getProperty("MINE")).isEqualTo("test"); assertThat(this.context.getProperty("MINE")).isEqualTo("test");
} }
@Test
public void springPropertyNoValue() throws Exception {
initialize("property.xml");
assertThat(this.context.getProperty("SIMPLE")).isNull();
}
@Test
public void relaxedSpringPropertyNoValue() throws Exception {
initialize("property.xml");
assertThat(this.context.getProperty("MINE")).isNull();
}
@Test
public void springPropertyWithDefaultValue() throws Exception {
initialize("property-default-value.xml");
assertThat(this.context.getProperty("SIMPLE")).isEqualTo("foo");
}
@Test
public void relaxedSpringPropertyWithDefaultValue() throws Exception {
initialize("property-default-value.xml");
assertThat(this.context.getProperty("MINE")).isEqualTo("bar");
}
private void doTestNestedProfile(boolean expected, String... profiles) private void doTestNestedProfile(boolean expected, String... profiles)
throws JoranException { throws JoranException {
this.environment.setActiveProfiles(profiles); this.environment.setActiveProfiles(profiles);
......
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<springProperty scope="context" name="SIMPLE" source="testpropertyfoobar" defaultValue="foo"/>
<springProperty scope="context" name="MINE" source="my.example-property" defaultValue="bar"/>
</configuration>
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<configuration> <configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" /> <include resource="org/springframework/boot/logging/logback/base.xml" />
<springProperty scope="context" name="SIMPLE" source="testpropertyfoobar"/>
<springProperty scope="context" name="MINE" source="my.example-property"/> <springProperty scope="context" name="MINE" source="my.example-property"/>
</configuration> </configuration>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment