Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
3378ede2
Commit
3378ede2
authored
Jun 16, 2014
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ensure CompositePropertySources are listed in EnvironmentEndpoint
parent
8b03834d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
5 deletions
+61
-5
EnvironmentEndpoint.java
...gframework/boot/actuate/endpoint/EnvironmentEndpoint.java
+44
-5
EnvironmentEndpointTests.java
...ework/boot/actuate/endpoint/EnvironmentEndpointTests.java
+17
-0
No files found.
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java
View file @
3378ede2
...
...
@@ -16,17 +16,23 @@
package
org
.
springframework
.
boot
.
actuate
.
endpoint
;
import
java.lang.reflect.Field
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
java.util.Map.Entry
;
import
java.util.Set
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.context.EnvironmentAware
;
import
org.springframework.core.env.CompositePropertySource
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.core.env.EnumerablePropertySource
;
import
org.springframework.core.env.Environment
;
import
org.springframework.core.env.MutablePropertySources
;
import
org.springframework.core.env.PropertySource
;
import
org.springframework.core.env.StandardEnvironment
;
import
org.springframework.util.Assert
;
import
org.springframework.util.ReflectionUtils
;
/**
* {@link Endpoint} to expose {@link ConfigurableEnvironment environment} information.
...
...
@@ -59,25 +65,58 @@ public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> i
public
Map
<
String
,
Object
>
invoke
()
{
Map
<
String
,
Object
>
result
=
new
LinkedHashMap
<
String
,
Object
>();
result
.
put
(
"profiles"
,
this
.
environment
.
getActiveProfiles
());
for
(
PropertySource
<?>
source
:
getPropertySources
())
{
for
(
Entry
<
String
,
PropertySource
<?>>
entry
:
getPropertySources
().
entrySet
())
{
PropertySource
<?>
source
=
entry
.
getValue
();
String
sourceName
=
entry
.
getKey
();
if
(
source
instanceof
EnumerablePropertySource
)
{
EnumerablePropertySource
<?>
enumerable
=
(
EnumerablePropertySource
<?>)
source
;
Map
<
String
,
Object
>
map
=
new
LinkedHashMap
<
String
,
Object
>();
for
(
String
name
:
enumerable
.
getPropertyNames
())
{
map
.
put
(
name
,
sanitize
(
name
,
enumerable
.
getProperty
(
name
)));
}
result
.
put
(
source
.
getName
()
,
map
);
result
.
put
(
source
Name
,
map
);
}
}
return
result
;
}
private
Iterable
<
PropertySource
<?>>
getPropertySources
()
{
private
Map
<
String
,
PropertySource
<?>>
getPropertySources
()
{
Map
<
String
,
PropertySource
<?>>
map
=
new
LinkedHashMap
<
String
,
PropertySource
<?>>();
MutablePropertySources
sources
=
null
;
if
(
this
.
environment
!=
null
&&
this
.
environment
instanceof
ConfigurableEnvironment
)
{
return
((
ConfigurableEnvironment
)
this
.
environment
).
getPropertySources
();
sources
=
((
ConfigurableEnvironment
)
this
.
environment
).
getPropertySources
();
}
else
{
sources
=
new
StandardEnvironment
().
getPropertySources
();
}
for
(
PropertySource
<?>
source
:
sources
)
{
extract
(
""
,
map
,
source
);
}
return
map
;
}
private
void
extract
(
String
root
,
Map
<
String
,
PropertySource
<?>>
map
,
PropertySource
<?>
source
)
{
if
(
source
instanceof
CompositePropertySource
)
{
try
{
Field
field
=
ReflectionUtils
.
findField
(
CompositePropertySource
.
class
,
"propertySources"
);
field
.
setAccessible
(
true
);
@SuppressWarnings
(
"unchecked"
)
Set
<
PropertySource
<?>>
nested
=
(
Set
<
PropertySource
<?>>)
field
.
get
(
source
);
for
(
PropertySource
<?>
nest
:
nested
)
{
extract
(
source
.
getName
()
+
":"
,
map
,
nest
);
}
}
catch
(
Exception
e
)
{
// ignore
}
}
else
{
map
.
put
(
root
+
source
.
getName
(),
source
);
}
return
new
StandardEnvironment
().
getPropertySources
();
}
public
Object
sanitize
(
String
name
,
Object
object
)
{
...
...
spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpointTests.java
View file @
3378ede2
...
...
@@ -16,12 +16,15 @@
package
org
.
springframework
.
boot
.
actuate
.
endpoint
;
import
java.util.Collections
;
import
java.util.Map
;
import
org.junit.Test
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.env.CompositePropertySource
;
import
org.springframework.core.env.MapPropertySource
;
import
static
org
.
hamcrest
.
Matchers
.
greaterThan
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
...
...
@@ -44,6 +47,20 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
assertThat
(
getEndpointBean
().
invoke
().
size
(),
greaterThan
(
0
));
}
@SuppressWarnings
(
"unchecked"
)
@Test
public
void
testCompositeSource
()
throws
Exception
{
EnvironmentEndpoint
report
=
getEndpointBean
();
CompositePropertySource
source
=
new
CompositePropertySource
(
"composite"
);
source
.
addPropertySource
(
new
MapPropertySource
(
"one"
,
Collections
.
singletonMap
(
"foo"
,
(
Object
)
"bar"
)));
source
.
addPropertySource
(
new
MapPropertySource
(
"two"
,
Collections
.
singletonMap
(
"foo"
,
(
Object
)
"spam"
)));
this
.
context
.
getEnvironment
().
getPropertySources
().
addFirst
(
source
);
Map
<
String
,
Object
>
env
=
report
.
invoke
();
assertEquals
(
"bar"
,
((
Map
<
String
,
Object
>)
env
.
get
(
"composite:one"
)).
get
(
"foo"
));
}
@SuppressWarnings
(
"unchecked"
)
@Test
public
void
testKeySanitization
()
throws
Exception
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment