21
21
import java .util .Set ;
22
22
23
23
import org .springframework .core .env .ConfigurableEnvironment ;
24
+ import org .springframework .core .env .Environment ;
24
25
import org .springframework .core .env .MutablePropertySources ;
25
26
import org .springframework .core .env .PropertySource ;
26
27
import org .springframework .core .env .StandardEnvironment ;
28
+ import org .springframework .util .ClassUtils ;
29
+ import org .springframework .web .context .ConfigurableWebEnvironment ;
27
30
import org .springframework .web .context .support .StandardServletEnvironment ;
28
31
29
32
/**
30
- * Utility class for converting one environment type to another.
33
+ * Utility class for converting one type of {@link Environment} to another.
31
34
*
32
35
* @author Ethan Rubinson
33
- * @since 1.5.4
36
+ * @author Andy Wilkinson
34
37
*/
35
38
final class EnvironmentConverter {
36
39
40
+ private static final String CONFIGURABLE_WEB_ENVIRONMENT_CLASS = "org.springframework.web.context.ConfigurableWebEnvironment" ;
41
+
37
42
private static final Set <String > SERVLET_ENVIRONMENT_SOURCE_NAMES ;
38
43
39
44
static {
@@ -44,42 +49,68 @@ final class EnvironmentConverter {
44
49
SERVLET_ENVIRONMENT_SOURCE_NAMES = Collections .unmodifiableSet (names );
45
50
}
46
51
47
- private EnvironmentConverter () {
52
+ private final ClassLoader classLoader ;
48
53
54
+ /**
55
+ * Creates a new {@link EnvironmentConverter} that will use the given
56
+ * {@code classLoader} during conversion.
57
+ * @param classLoader the class loader to use
58
+ */
59
+ EnvironmentConverter (ClassLoader classLoader ) {
60
+ this .classLoader = classLoader ;
49
61
}
50
62
51
63
/**
52
- * Converts the specified environment to a {@link StandardEnvironment}.
64
+ * Converts the given {@code environment} to a {@link StandardEnvironment}. If the
65
+ * environment is already a {@code StandardEnvironment} and is not a
66
+ * {@link ConfigurableWebEnvironment} no conversion is performed and it is returned
67
+ * unchanged.
53
68
*
54
- * @param environment The environment to convert.
55
- * @return The converted environment.
69
+ * @param environment The Environment to convert
70
+ * @return The converted Environment
56
71
*/
57
- protected static ConfigurableEnvironment convertToStandardEnvironment (
72
+ StandardEnvironment convertToStandardEnvironmentIfNecessary (
58
73
ConfigurableEnvironment environment ) {
59
- final StandardEnvironment result = new StandardEnvironment ();
74
+ if (environment instanceof StandardEnvironment
75
+ && !isWebEnvironment (environment , this .classLoader )) {
76
+ return (StandardEnvironment ) environment ;
77
+ }
78
+ return convertToStandardEnvironment (environment );
79
+ }
60
80
61
- /* Copy the profiles */
62
- result .setActiveProfiles (environment .getActiveProfiles ());
81
+ private boolean isWebEnvironment (ConfigurableEnvironment environment ,
82
+ ClassLoader classLoader ) {
83
+ try {
84
+ Class <?> webEnvironmentClass = ClassUtils
85
+ .forName (CONFIGURABLE_WEB_ENVIRONMENT_CLASS , classLoader );
86
+ return (webEnvironmentClass .isInstance (environment ));
87
+ }
88
+ catch (Throwable ex ) {
89
+ return false ;
90
+ }
91
+ }
63
92
64
- /* Copy the conversion service */
93
+ private StandardEnvironment convertToStandardEnvironment (
94
+ ConfigurableEnvironment environment ) {
95
+ StandardEnvironment result = new StandardEnvironment ();
96
+ result .setActiveProfiles (environment .getActiveProfiles ());
65
97
result .setConversionService (environment .getConversionService ());
98
+ copyNonServletPropertySources (environment , result );
99
+ return result ;
100
+ }
66
101
67
- /*
68
- * Copy over all of the property sources except those unrelated to a standard
69
- * environment
70
- */
71
- removeAllPropertySources (result .getPropertySources ());
72
- for (PropertySource <?> propertySource : environment .getPropertySources ()) {
102
+ private void copyNonServletPropertySources (ConfigurableEnvironment source ,
103
+ StandardEnvironment target ) {
104
+ removeAllPropertySources (target .getPropertySources ());
105
+ for (PropertySource <?> propertySource : source .getPropertySources ()) {
73
106
if (!SERVLET_ENVIRONMENT_SOURCE_NAMES .contains (propertySource .getName ())) {
74
- result .getPropertySources ().addLast (propertySource );
107
+ target .getPropertySources ().addLast (propertySource );
75
108
}
76
109
}
77
-
78
- return result ;
79
110
}
80
111
81
- private static void removeAllPropertySources (MutablePropertySources propertySources ) {
82
- final Set <String > names = new HashSet <String >();
112
+ private void removeAllPropertySources (MutablePropertySources propertySources ) {
113
+ Set <String > names = new HashSet <String >();
83
114
for (PropertySource <?> propertySource : propertySources ) {
84
115
names .add (propertySource .getName ());
85
116
}
0 commit comments