@@ -72,65 +72,67 @@ public PropertySourcesPropertyValues(PropertySources propertySources,
72
72
this .propertySources = propertySources ;
73
73
PropertySourcesPropertyResolver resolver = new PropertySourcesPropertyResolver (
74
74
propertySources );
75
- String [] includes = patterns == null ? new String [0 ] : patterns
76
- .toArray (new String [0 ]);
77
- String [] exacts = names == null ? new String [0 ] : names .toArray (new String [0 ]);
75
+ String [] includes = toArray (patterns );
76
+ String [] exacts = toArray (names );
78
77
for (PropertySource <?> source : propertySources ) {
79
78
processPropertySource (source , resolver , includes , exacts );
80
79
}
81
80
}
82
81
82
+ private String [] toArray (Collection <String > strings ) {
83
+ if (strings == null ) {
84
+ return new String [0 ];
85
+ }
86
+ return strings .toArray (new String [strings .size ()]);
87
+ }
88
+
83
89
private void processPropertySource (PropertySource <?> source ,
84
90
PropertySourcesPropertyResolver resolver , String [] includes , String [] exacts ) {
85
91
if (source instanceof EnumerablePropertySource ) {
86
- EnumerablePropertySource <?> enumerable = (EnumerablePropertySource <?>) source ;
87
- if (enumerable .getPropertyNames ().length > 0 ) {
88
- for (String propertyName : enumerable .getPropertyNames ()) {
89
- if (this .NON_ENUMERABLE_ENUMERABLES .contains (source .getName ())
90
- && !PatternMatchUtils .simpleMatch (includes , propertyName )) {
91
- continue ;
92
- }
93
- Object value = source .getProperty (propertyName );
94
- try {
95
- value = resolver .getProperty (propertyName );
96
- }
97
- catch (RuntimeException ex ) {
98
- // Probably could not resolve placeholders, ignore it here
99
- }
100
- if (!this .propertyValues .containsKey (propertyName )) {
101
- this .propertyValues .put (propertyName , new PropertyValue (
102
- propertyName , value ));
103
- }
104
- }
105
- }
92
+ processEnumerablePropertySource ((EnumerablePropertySource <?>) source ,
93
+ resolver , includes , exacts );
106
94
}
107
95
else if (source instanceof CompositePropertySource ) {
108
- CompositePropertySource composite = (CompositePropertySource ) source ;
109
- for (PropertySource <?> nested : extractSources (composite )) {
110
- processPropertySource (nested , resolver , includes , exacts );
111
- }
96
+ processCompositePropertySource ((CompositePropertySource ) source , resolver ,
97
+ includes , exacts );
112
98
}
113
99
else {
114
100
// We can only do exact matches for non-enumerable property names, but
115
101
// that's better than nothing...
116
- for (String propertyName : exacts ) {
117
- Object value ;
118
- value = resolver .getProperty (propertyName );
119
- if (value != null && !this .propertyValues .containsKey (propertyName )) {
120
- this .propertyValues .put (propertyName , new PropertyValue (propertyName ,
121
- value ));
102
+ processDefaultPropertySource (source , resolver , includes , exacts );
103
+ }
104
+ }
105
+
106
+ private void processEnumerablePropertySource (EnumerablePropertySource <?> source ,
107
+ PropertySourcesPropertyResolver resolver , String [] includes , String [] exacts ) {
108
+ if (source .getPropertyNames ().length > 0 ) {
109
+ for (String propertyName : source .getPropertyNames ()) {
110
+ if (this .NON_ENUMERABLE_ENUMERABLES .contains (source .getName ())
111
+ && !PatternMatchUtils .simpleMatch (includes , propertyName )) {
122
112
continue ;
123
113
}
124
- value = source .getProperty (propertyName .toUpperCase ());
125
- if (value != null && !this .propertyValues .containsKey (propertyName )) {
114
+ Object value = source .getProperty (propertyName );
115
+ try {
116
+ value = resolver .getProperty (propertyName );
117
+ }
118
+ catch (RuntimeException ex ) {
119
+ // Probably could not resolve placeholders, ignore it here
120
+ }
121
+ if (!this .propertyValues .containsKey (propertyName )) {
126
122
this .propertyValues .put (propertyName , new PropertyValue (propertyName ,
127
123
value ));
128
- continue ;
129
124
}
130
125
}
131
126
}
132
127
}
133
128
129
+ private void processCompositePropertySource (CompositePropertySource source ,
130
+ PropertySourcesPropertyResolver resolver , String [] includes , String [] exacts ) {
131
+ for (PropertySource <?> nested : extractSources (source )) {
132
+ processPropertySource (nested , resolver , includes , exacts );
133
+ }
134
+ }
135
+
134
136
private Collection <PropertySource <?>> extractSources (CompositePropertySource composite ) {
135
137
Field field = ReflectionUtils .findField (CompositePropertySource .class ,
136
138
"propertySources" );
@@ -141,9 +143,24 @@ private Collection<PropertySource<?>> extractSources(CompositePropertySource com
141
143
.get (composite );
142
144
return collection ;
143
145
}
144
- catch (Exception e ) {
146
+ catch (Exception ex ) {
145
147
throw new IllegalStateException (
146
- "Cannot extract property sources from composite" , e );
148
+ "Cannot extract property sources from composite" , ex );
149
+ }
150
+ }
151
+
152
+ private void processDefaultPropertySource (PropertySource <?> source ,
153
+ PropertySourcesPropertyResolver resolver , String [] includes , String [] exacts ) {
154
+ for (String propertyName : exacts ) {
155
+ Object value = resolver .getProperty (propertyName );
156
+ if (value == null ) {
157
+ value = source .getProperty (propertyName .toUpperCase ());
158
+ }
159
+ if (value != null && !this .propertyValues .containsKey (propertyName )) {
160
+ this .propertyValues .put (propertyName , new PropertyValue (propertyName ,
161
+ value ));
162
+ continue ;
163
+ }
147
164
}
148
165
}
149
166
0 commit comments