1
1
/*
2
- * Copyright 2002-2011 the original author or authors.
2
+ * Copyright 2002-2012 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .core .env ;
18
18
19
- import static java .lang .String .format ;
20
- import static org .springframework .util .StringUtils .commaDelimitedListToStringArray ;
21
- import static org .springframework .util .StringUtils .trimAllWhitespace ;
22
-
23
19
import java .security .AccessControlException ;
20
+
24
21
import java .util .Collections ;
25
22
import java .util .LinkedHashSet ;
26
23
import java .util .Map ;
27
24
import java .util .Set ;
28
25
29
26
import org .apache .commons .logging .Log ;
30
27
import org .apache .commons .logging .LogFactory ;
28
+
31
29
import org .springframework .core .convert .support .ConfigurableConversionService ;
32
30
import org .springframework .util .Assert ;
33
31
import org .springframework .util .StringUtils ;
34
32
33
+ import static java .lang .String .*;
34
+ import static org .springframework .util .StringUtils .*;
35
+
35
36
/**
36
37
* Abstract base class for {@link Environment} implementations. Supports the notion of
37
38
* reserved default profile names and enables specifying active and default profiles
@@ -89,19 +90,39 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
89
90
protected final Log logger = LogFactory .getLog (getClass ());
90
91
91
92
private Set <String > activeProfiles = new LinkedHashSet <String >();
92
- private Set <String > defaultProfiles = new LinkedHashSet <String >(this .getReservedDefaultProfiles ());
93
93
94
- private final MutablePropertySources propertySources = new MutablePropertySources ( logger );
95
- private final ConfigurablePropertyResolver propertyResolver = new PropertySourcesPropertyResolver ( propertySources );
94
+ private Set < String > defaultProfiles =
95
+ new LinkedHashSet < String >( this . getReservedDefaultProfiles () );
96
96
97
+ private final MutablePropertySources propertySources =
98
+ new MutablePropertySources (this .logger );
97
99
100
+ private final ConfigurablePropertyResolver propertyResolver =
101
+ new PropertySourcesPropertyResolver (this .propertySources );
102
+
103
+
104
+ /**
105
+ * Create a new {@code Environment} instance, calling back to
106
+ * {@link #customizePropertySources(MutablePropertySources)} during construction to
107
+ * allow subclasses to contribute or manipulate {@link PropertySource} instances as
108
+ * appropriate.
109
+ * @see #customizePropertySources(MutablePropertySources)
110
+ */
98
111
public AbstractEnvironment () {
99
112
String name = this .getClass ().getSimpleName ();
100
- logger .debug (String .format ("Initializing new %s" , name ));
101
- this .customizePropertySources (propertySources );
102
- logger .debug (String .format ("Initialized %s with PropertySources %s" , name , propertySources ));
113
+ if (this .logger .isDebugEnabled ()) {
114
+ this .logger .debug (format ("Initializing new %s" , name ));
115
+ }
116
+
117
+ this .customizePropertySources (this .propertySources );
118
+
119
+ if (this .logger .isDebugEnabled ()) {
120
+ this .logger .debug (format (
121
+ "Initialized %s with PropertySources %s" , name , this .propertySources ));
122
+ }
103
123
}
104
124
125
+
105
126
/**
106
127
* Customize the set of {@link PropertySource} objects to be searched by this
107
128
* {@code Environment} during calls to {@link #getProperty(String)} and related
@@ -163,6 +184,17 @@ public AbstractEnvironment() {
163
184
* env.getPropertySources().addLast(new PropertySourceX(...));
164
185
* </pre>
165
186
*
187
+ * <h2>A warning about instance variable access</h2>
188
+ * Instance variables declared in subclasses and having default initial values should
189
+ * <em>not</em> be accessed from within this method. Due to Java object creation
190
+ * lifecycle constraints, any initial value will not yet be assigned when this
191
+ * callback is invoked by the {@link #AbstractEnvironment()} constructor, which may
192
+ * lead to a {@code NullPointerException} or other problems. If you need to access
193
+ * default values of instance variables, leave this method as a no-op and perform
194
+ * property source manipulation and instance variable access directly within the
195
+ * subclass constructor. Note that <em>assigning</em> values to instance variables is
196
+ * not problematic; it is only attempting to read default values that must be avoided.
197
+ *
166
198
* @see MutablePropertySources
167
199
* @see PropertySourcesPropertyResolver
168
200
* @see org.springframework.context.ApplicationContextInitializer
@@ -217,7 +249,9 @@ public void setActiveProfiles(String... profiles) {
217
249
}
218
250
219
251
public void addActiveProfile (String profile ) {
220
- logger .debug (String .format ("Activating profile '%s'" , profile ));
252
+ if (this .logger .isDebugEnabled ()) {
253
+ this .logger .debug (format ("Activating profile '%s'" , profile ));
254
+ }
221
255
this .validateProfile (profile );
222
256
this .activeProfiles .add (profile );
223
257
}
@@ -312,8 +346,10 @@ protected String getSystemAttribute(String variableName) {
312
346
}
313
347
catch (AccessControlException ex ) {
314
348
if (logger .isInfoEnabled ()) {
315
- logger .info (format ("Caught AccessControlException when accessing system environment variable " +
316
- "[%s]; its value will be returned [null]. Reason: %s" , variableName , ex .getMessage ()));
349
+ logger .info (format ("Caught AccessControlException when " +
350
+ "accessing system environment variable [%s]; its " +
351
+ "value will be returned [null]. Reason: %s" ,
352
+ variableName , ex .getMessage ()));
317
353
}
318
354
return null ;
319
355
}
@@ -338,8 +374,10 @@ protected String getSystemAttribute(String propertyName) {
338
374
}
339
375
catch (AccessControlException ex ) {
340
376
if (logger .isInfoEnabled ()) {
341
- logger .info (format ("Caught AccessControlException when accessing system property " +
342
- "[%s]; its value will be returned [null]. Reason: %s" , propertyName , ex .getMessage ()));
377
+ logger .info (format ("Caught AccessControlException when " +
378
+ "accessing system property [%s]; its value will be " +
379
+ "returned [null]. Reason: %s" ,
380
+ propertyName , ex .getMessage ()));
343
381
}
344
382
return null ;
345
383
}
@@ -428,7 +466,8 @@ public void setValueSeparator(String valueSeparator) {
428
466
@ Override
429
467
public String toString () {
430
468
return format ("%s {activeProfiles=%s, defaultProfiles=%s, propertySources=%s}" ,
431
- getClass ().getSimpleName (), this .activeProfiles , this .defaultProfiles , this .propertySources );
469
+ getClass ().getSimpleName (), this .activeProfiles , this .defaultProfiles ,
470
+ this .propertySources );
432
471
}
433
472
434
473
}
0 commit comments