Skip to content

Commit c1dcd85

Browse files
committed
Simplify MustacheEnvironmentCollector
Simplify the MustacheEnvironmentCollector so that it no longer binds the Spring Environment to a Map. See gh-2242
1 parent d5a82aa commit c1dcd85

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheEnvironmentCollector.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121

2222
import org.springframework.boot.bind.PropertySourcesPropertyValues;
2323
import org.springframework.boot.bind.RelaxedDataBinder;
24+
import org.springframework.boot.bind.RelaxedPropertyResolver;
2425
import org.springframework.context.EnvironmentAware;
2526
import org.springframework.core.env.ConfigurableEnvironment;
2627
import org.springframework.core.env.Environment;
2728

2829
import com.samskivert.mustache.DefaultCollector;
29-
import com.samskivert.mustache.Mustache;
3030
import com.samskivert.mustache.Mustache.Collector;
3131
import com.samskivert.mustache.Mustache.VariableFetcher;
3232

@@ -43,42 +43,38 @@ public class MustacheEnvironmentCollector extends DefaultCollector implements
4343

4444
private Map<String, Object> target;
4545

46+
private RelaxedPropertyResolver propertyResolver;
47+
48+
private final VariableFetcher propertyFetcher = new PropertyVariableFetcher();
49+
4650
@Override
4751
public void setEnvironment(Environment environment) {
4852
this.environment = (ConfigurableEnvironment) environment;
4953
this.target = new HashMap<String, Object>();
5054
new RelaxedDataBinder(this.target).bind(new PropertySourcesPropertyValues(
5155
this.environment.getPropertySources()));
56+
this.propertyResolver = new RelaxedPropertyResolver(environment);
5257
}
5358

5459
@Override
55-
public Mustache.VariableFetcher createFetcher(Object ctx, String name) {
60+
public VariableFetcher createFetcher(Object ctx, String name) {
5661
VariableFetcher fetcher = super.createFetcher(ctx, name);
5762
if (fetcher != null) {
5863
return fetcher;
5964
}
60-
if (this.environment.containsProperty(name)) {
61-
return new VariableFetcher() {
62-
63-
@Override
64-
public Object get(Object ctx, String name) throws Exception {
65-
return MustacheEnvironmentCollector.this.environment
66-
.getProperty(name);
67-
}
68-
69-
};
65+
if (this.propertyResolver.containsProperty(name)) {
66+
return this.propertyFetcher;
7067
}
71-
if (this.target.containsKey(name)) {
72-
return new VariableFetcher() {
68+
return null;
69+
}
7370

74-
@Override
75-
public Object get(Object ctx, String name) throws Exception {
76-
return MustacheEnvironmentCollector.this.target.get(name);
77-
}
71+
private class PropertyVariableFetcher implements VariableFetcher {
7872

79-
};
73+
@Override
74+
public Object get(Object ctx, String name) throws Exception {
75+
return MustacheEnvironmentCollector.this.propertyResolver.getProperty(name);
8076
}
81-
return null;
77+
8278
}
8379

8480
}

0 commit comments

Comments
 (0)