25
25
import java .util .regex .Pattern ;
26
26
import java .util .stream .Stream ;
27
27
28
+ import com .fasterxml .jackson .annotation .JsonInclude ;
29
+
28
30
import org .springframework .boot .actuate .endpoint .Sanitizer ;
29
31
import org .springframework .boot .actuate .endpoint .annotation .Endpoint ;
30
32
import org .springframework .boot .actuate .endpoint .annotation .ReadOperation ;
31
33
import org .springframework .boot .actuate .endpoint .annotation .Selector ;
32
- import org .springframework .boot .actuate .env .EnvironmentEndpoint .EnvironmentDescriptor .PropertySourceDescriptor ;
33
- import org .springframework .boot .actuate .env .EnvironmentEndpoint .EnvironmentDescriptor .PropertySourceDescriptor .PropertyValueDescriptor ;
34
34
import org .springframework .boot .context .properties .bind .PlaceholdersResolver ;
35
35
import org .springframework .boot .context .properties .bind .PropertySourcesPlaceholdersResolver ;
36
+ import org .springframework .boot .context .properties .source .ConfigurationPropertySources ;
36
37
import org .springframework .boot .origin .OriginLookup ;
37
38
import org .springframework .core .env .CompositePropertySource ;
38
39
import org .springframework .core .env .ConfigurableEnvironment ;
54
55
* @author Phillip Webb
55
56
* @author Christian Dupuis
56
57
* @author Madhura Bhave
58
+ * @author Stephane Nicoll
57
59
* @since 2.0.0
58
60
*/
59
61
@ Endpoint (id = "env" )
@@ -80,8 +82,8 @@ public EnvironmentDescriptor environment(String pattern) {
80
82
}
81
83
82
84
@ ReadOperation
83
- public EnvironmentDescriptor environmentEntry (@ Selector String toMatch ) {
84
- return getEnvironmentDescriptor (toMatch :: equals );
85
+ public EnvironmentEntryDescriptor environmentEntry (@ Selector String toMatch ) {
86
+ return getEnvironmentEntryDescriptor (toMatch );
85
87
}
86
88
87
89
private EnvironmentDescriptor getEnvironmentDescriptor (
@@ -99,6 +101,46 @@ private EnvironmentDescriptor getEnvironmentDescriptor(
99
101
Arrays .asList (this .environment .getActiveProfiles ()), propertySources );
100
102
}
101
103
104
+ private EnvironmentEntryDescriptor getEnvironmentEntryDescriptor (
105
+ String propertyName ) {
106
+ Map <String , PropertyValueDescriptor > descriptors = getPropertySourceDescriptors (
107
+ propertyName );
108
+ PropertySummaryDescriptor summary = getPropertySummaryDescriptor (descriptors );
109
+ return new EnvironmentEntryDescriptor (summary ,
110
+ Arrays .asList (this .environment .getActiveProfiles ()),
111
+ toPropertySourceDescriptors (descriptors ));
112
+ }
113
+
114
+ private List <PropertySourceEntryDescriptor > toPropertySourceDescriptors (
115
+ Map <String , PropertyValueDescriptor > descriptors ) {
116
+ List <PropertySourceEntryDescriptor > result = new ArrayList <>();
117
+ for (Map .Entry <String , PropertyValueDescriptor > entry : descriptors .entrySet ()) {
118
+ result .add (new PropertySourceEntryDescriptor (entry .getKey (), entry .getValue ()));
119
+ }
120
+ return result ;
121
+ }
122
+
123
+ private PropertySummaryDescriptor getPropertySummaryDescriptor (
124
+ Map <String , PropertyValueDescriptor > descriptors ) {
125
+ for (Map .Entry <String , PropertyValueDescriptor > entry : descriptors .entrySet ()) {
126
+ if (entry .getValue () != null ) {
127
+ return new PropertySummaryDescriptor (entry .getKey (),
128
+ entry .getValue ().getValue ());
129
+ }
130
+ }
131
+ return null ;
132
+ }
133
+
134
+ private Map <String , PropertyValueDescriptor > getPropertySourceDescriptors (
135
+ String propertyName ) {
136
+ Map <String , PropertyValueDescriptor > propertySources = new LinkedHashMap <>();
137
+ PlaceholdersResolver resolver = getResolver ();
138
+ getPropertySourcesAsMap ().forEach ((sourceName , source ) ->
139
+ propertySources .put (sourceName , source .containsProperty (propertyName ) ?
140
+ describeValueOf (propertyName , source , resolver ) : null ));
141
+ return propertySources ;
142
+ }
143
+
102
144
private PropertySourceDescriptor describeSource (String sourceName ,
103
145
EnumerablePropertySource <?> source , PlaceholdersResolver resolver ,
104
146
Predicate <String > namePredicate ) {
@@ -109,7 +151,7 @@ private PropertySourceDescriptor describeSource(String sourceName,
109
151
}
110
152
111
153
private PropertyValueDescriptor describeValueOf (String name ,
112
- EnumerablePropertySource <?> source , PlaceholdersResolver resolver ) {
154
+ PropertySource <?> source , PlaceholdersResolver resolver ) {
113
155
Object resolved = resolver .resolvePlaceholders (source .getProperty (name ));
114
156
@ SuppressWarnings ("unchecked" )
115
157
String origin = (source instanceof OriginLookup )
@@ -125,7 +167,9 @@ private PlaceholdersResolver getResolver() {
125
167
private Map <String , PropertySource <?>> getPropertySourcesAsMap () {
126
168
Map <String , PropertySource <?>> map = new LinkedHashMap <>();
127
169
for (PropertySource <?> source : getPropertySources ()) {
128
- extract ("" , map , source );
170
+ if (!ConfigurationPropertySources .isMainConfigurationPropertySource (source )) {
171
+ extract ("" , map , source );
172
+ }
129
173
}
130
174
return map ;
131
175
}
@@ -208,54 +252,141 @@ public List<PropertySourceDescriptor> getPropertySources() {
208
252
return this .propertySources ;
209
253
}
210
254
211
- /**
212
- * A description of a {@link PropertySource}.
213
- */
214
- public static final class PropertySourceDescriptor {
255
+ }
215
256
216
- private final String name ;
257
+ /**
258
+ * A description of an entry of the {@link Environment}.
259
+ */
260
+ @ JsonInclude (JsonInclude .Include .NON_NULL )
261
+ public static final class EnvironmentEntryDescriptor {
217
262
218
- private final Map < String , PropertyValueDescriptor > properties ;
263
+ private final PropertySummaryDescriptor property ;
219
264
220
- private PropertySourceDescriptor (String name ,
221
- Map <String , PropertyValueDescriptor > properties ) {
222
- this .name = name ;
223
- this .properties = properties ;
224
- }
265
+ private final List <String > activeProfiles ;
225
266
226
- public String getName () {
227
- return this .name ;
228
- }
267
+ private final List <PropertySourceEntryDescriptor > propertySources ;
229
268
230
- public Map <String , PropertyValueDescriptor > getProperties () {
231
- return this .properties ;
232
- }
269
+ private EnvironmentEntryDescriptor (PropertySummaryDescriptor property ,
270
+ List <String > activeProfiles ,
271
+ List <PropertySourceEntryDescriptor > propertySources ) {
272
+ this .property = property ;
273
+ this .activeProfiles = activeProfiles ;
274
+ this .propertySources = propertySources ;
275
+ }
233
276
234
- /**
235
- * A description of a property's value, including its origin if available.
236
- */
237
- public static final class PropertyValueDescriptor {
277
+ public PropertySummaryDescriptor getProperty () {
278
+ return this .property ;
279
+ }
238
280
239
- private final Object value ;
281
+ public List <String > getActiveProfiles () {
282
+ return this .activeProfiles ;
283
+ }
240
284
241
- private final String origin ;
285
+ public List <PropertySourceEntryDescriptor > getPropertySources () {
286
+ return this .propertySources ;
287
+ }
242
288
243
- private PropertyValueDescriptor (Object value , String origin ) {
244
- this .value = value ;
245
- this .origin = origin ;
246
- }
289
+ }
247
290
248
- public Object getValue () {
249
- return this .value ;
250
- }
291
+ /**
292
+ * A summary of a particular entry of the {@link Environment}.
293
+ */
294
+ @ JsonInclude (JsonInclude .Include .NON_NULL )
295
+ public static final class PropertySummaryDescriptor {
251
296
252
- public String getOrigin () {
253
- return this .origin ;
254
- }
297
+ private final String source ;
255
298
256
- }
299
+ private final Object value ;
300
+
301
+ public PropertySummaryDescriptor (String source , Object value ) {
302
+ this .source = source ;
303
+ this .value = value ;
304
+ }
305
+
306
+ public String getSource () {
307
+ return this .source ;
308
+ }
309
+
310
+ public Object getValue () {
311
+ return this .value ;
312
+ }
313
+
314
+ }
315
+
316
+ /**
317
+ * A description of a particular entry of {@link PropertySource}.
318
+ */
319
+ @ JsonInclude (JsonInclude .Include .NON_NULL )
320
+ public static final class PropertySourceEntryDescriptor {
321
+
322
+ private final String name ;
323
+
324
+ private final PropertyValueDescriptor property ;
325
+
326
+ private PropertySourceEntryDescriptor (String name ,
327
+ PropertyValueDescriptor property ) {
328
+ this .name = name ;
329
+ this .property = property ;
330
+ }
257
331
332
+ public String getName () {
333
+ return this .name ;
258
334
}
335
+
336
+ public PropertyValueDescriptor getProperty () {
337
+ return this .property ;
338
+ }
339
+
340
+ }
341
+
342
+ /**
343
+ * A description of a {@link PropertySource}.
344
+ */
345
+ public static final class PropertySourceDescriptor {
346
+
347
+ private final String name ;
348
+
349
+ private final Map <String , PropertyValueDescriptor > properties ;
350
+
351
+ private PropertySourceDescriptor (String name ,
352
+ Map <String , PropertyValueDescriptor > properties ) {
353
+ this .name = name ;
354
+ this .properties = properties ;
355
+ }
356
+
357
+ public String getName () {
358
+ return this .name ;
359
+ }
360
+
361
+ public Map <String , PropertyValueDescriptor > getProperties () {
362
+ return this .properties ;
363
+ }
364
+
365
+ }
366
+
367
+ /**
368
+ * A description of a property's value, including its origin if available.
369
+ */
370
+ @ JsonInclude (JsonInclude .Include .NON_NULL )
371
+ public static final class PropertyValueDescriptor {
372
+
373
+ private final Object value ;
374
+
375
+ private final String origin ;
376
+
377
+ private PropertyValueDescriptor (Object value , String origin ) {
378
+ this .value = value ;
379
+ this .origin = origin ;
380
+ }
381
+
382
+ public Object getValue () {
383
+ return this .value ;
384
+ }
385
+
386
+ public String getOrigin () {
387
+ return this .origin ;
388
+ }
389
+
259
390
}
260
391
261
392
/**
0 commit comments