19
19
import java .util .Iterator ;
20
20
import java .util .LinkedList ;
21
21
22
+ import org .apache .commons .logging .Log ;
23
+ import org .apache .commons .logging .LogFactory ;
22
24
import org .springframework .util .Assert ;
25
+ import org .springframework .util .StringUtils ;
23
26
24
27
/**
25
28
* Default implementation of the {@link PropertySources} interface.
@@ -39,24 +42,38 @@ public class MutablePropertySources implements PropertySources {
39
42
static final String NON_EXISTENT_PROPERTY_SOURCE_MESSAGE = "PropertySource named [%s] does not exist" ;
40
43
static final String ILLEGAL_RELATIVE_ADDITION_MESSAGE = "PropertySource named [%s] cannot be added relative to itself" ;
41
44
45
+ private final Log logger ;
46
+
42
47
private final LinkedList <PropertySource <?>> propertySourceList = new LinkedList <PropertySource <?>>();
43
48
49
+
44
50
/**
45
51
* Create a new {@link MutablePropertySources} object.
46
52
*/
47
53
public MutablePropertySources () {
54
+ this .logger = LogFactory .getLog (this .getClass ());
48
55
}
49
56
50
57
/**
51
58
* Create a new {@code MutablePropertySources} from the given propertySources
52
59
* object, preserving the original order of contained {@code PropertySource} objects.
53
60
*/
54
61
public MutablePropertySources (PropertySources propertySources ) {
62
+ this ();
55
63
for (PropertySource <?> propertySource : propertySources ) {
56
64
this .addLast (propertySource );
57
65
}
58
66
}
59
67
68
+ /**
69
+ * Create a new {@link MutablePropertySources} object and inheriting the given logger,
70
+ * usually from an enclosing {@link Environment}.
71
+ */
72
+ MutablePropertySources (Log logger ) {
73
+ this .logger = logger ;
74
+ }
75
+
76
+
60
77
public boolean contains (String name ) {
61
78
return this .propertySourceList .contains (PropertySource .named (name ));
62
79
}
@@ -73,6 +90,8 @@ public Iterator<PropertySource<?>> iterator() {
73
90
* Add the given property source object with highest precedence.
74
91
*/
75
92
public void addFirst (PropertySource <?> propertySource ) {
93
+ logger .debug (String .format ("Adding [%s] PropertySource with highest search precedence" ,
94
+ propertySource .getName ()));
76
95
removeIfPresent (propertySource );
77
96
this .propertySourceList .addFirst (propertySource );
78
97
}
@@ -81,6 +100,8 @@ public void addFirst(PropertySource<?> propertySource) {
81
100
* Add the given property source object with lowest precedence.
82
101
*/
83
102
public void addLast (PropertySource <?> propertySource ) {
103
+ logger .debug (String .format ("Adding [%s] PropertySource with lowest search precedence" ,
104
+ propertySource .getName ()));
84
105
removeIfPresent (propertySource );
85
106
this .propertySourceList .addLast (propertySource );
86
107
}
@@ -90,6 +111,8 @@ public void addLast(PropertySource<?> propertySource) {
90
111
* than the named relative property source.
91
112
*/
92
113
public void addBefore (String relativePropertySourceName , PropertySource <?> propertySource ) {
114
+ logger .debug (String .format ("Adding [%s] PropertySource with search precedence immediately higher than [%s]" ,
115
+ propertySource .getName (), relativePropertySourceName ));
93
116
assertLegalRelativeAddition (relativePropertySourceName , propertySource );
94
117
removeIfPresent (propertySource );
95
118
int index = assertPresentAndGetIndex (relativePropertySourceName );
@@ -101,6 +124,8 @@ public void addBefore(String relativePropertySourceName, PropertySource<?> prope
101
124
* than the named relative property source.
102
125
*/
103
126
public void addAfter (String relativePropertySourceName , PropertySource <?> propertySource ) {
127
+ logger .debug (String .format ("Adding [%s] PropertySource with search precedence immediately lower than [%s]" ,
128
+ propertySource .getName (), relativePropertySourceName ));
104
129
assertLegalRelativeAddition (relativePropertySourceName , propertySource );
105
130
removeIfPresent (propertySource );
106
131
int index = assertPresentAndGetIndex (relativePropertySourceName );
@@ -119,6 +144,7 @@ public int precedenceOf(PropertySource<?> propertySource) {
119
144
* @param name the name of the property source to find and remove
120
145
*/
121
146
public PropertySource <?> remove (String name ) {
147
+ logger .debug (String .format ("Removing [%s] PropertySource" , name ));
122
148
int index = this .propertySourceList .indexOf (PropertySource .named (name ));
123
149
if (index >= 0 ) {
124
150
return this .propertySourceList .remove (index );
@@ -134,6 +160,8 @@ public PropertySource<?> remove(String name) {
134
160
* @see #contains
135
161
*/
136
162
public void replace (String name , PropertySource <?> propertySource ) {
163
+ logger .debug (String .format ("Replacing [%s] PropertySource with [%s]" ,
164
+ name , propertySource .getName ()));
137
165
int index = assertPresentAndGetIndex (name );
138
166
this .propertySourceList .set (index , propertySource );
139
167
}
@@ -145,6 +173,15 @@ public int size() {
145
173
return this .propertySourceList .size ();
146
174
}
147
175
176
+ @ Override
177
+ public synchronized String toString () {
178
+ String [] names = new String [this .size ()];
179
+ for (int i =0 ; i < size (); i ++) {
180
+ names [i ] = this .propertySourceList .get (i ).getName ();
181
+ }
182
+ return String .format ("[%s]" , StringUtils .arrayToCommaDelimitedString (names ));
183
+ }
184
+
148
185
/**
149
186
* Ensure that the given property source is not being added relative to itself.
150
187
*/
0 commit comments