1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 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.
28
28
import org .springframework .core .SpringProperties ;
29
29
import org .springframework .core .convert .support .ConfigurableConversionService ;
30
30
import org .springframework .util .Assert ;
31
+ import org .springframework .util .ObjectUtils ;
31
32
import org .springframework .util .StringUtils ;
32
33
33
34
import static java .lang .String .*;
@@ -103,9 +104,9 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
103
104
104
105
protected final Log logger = LogFactory .getLog (getClass ());
105
106
106
- private Set <String > activeProfiles = new LinkedHashSet <String >();
107
+ private final Set <String > activeProfiles = new LinkedHashSet <String >();
107
108
108
- private Set <String > defaultProfiles = new LinkedHashSet <String >(getReservedDefaultProfiles ());
109
+ private final Set <String > defaultProfiles = new LinkedHashSet <String >(getReservedDefaultProfiles ());
109
110
110
111
private final MutablePropertySources propertySources = new MutablePropertySources (this .logger );
111
112
@@ -236,21 +237,25 @@ public String[] getActiveProfiles() {
236
237
* @see #ACTIVE_PROFILES_PROPERTY_NAME
237
238
*/
238
239
protected Set <String > doGetActiveProfiles () {
239
- if (this .activeProfiles .isEmpty ()) {
240
- String profiles = getProperty (ACTIVE_PROFILES_PROPERTY_NAME );
241
- if (StringUtils .hasText (profiles )) {
242
- setActiveProfiles (commaDelimitedListToStringArray (trimAllWhitespace (profiles )));
240
+ synchronized (this .activeProfiles ) {
241
+ if (this .activeProfiles .isEmpty ()) {
242
+ String profiles = getProperty (ACTIVE_PROFILES_PROPERTY_NAME );
243
+ if (StringUtils .hasText (profiles )) {
244
+ setActiveProfiles (commaDelimitedListToStringArray (trimAllWhitespace (profiles )));
245
+ }
243
246
}
247
+ return this .activeProfiles ;
244
248
}
245
- return this .activeProfiles ;
246
249
}
247
250
248
251
public void setActiveProfiles (String ... profiles ) {
249
252
Assert .notNull (profiles , "Profile array must not be null" );
250
- this .activeProfiles .clear ();
251
- for (String profile : profiles ) {
252
- validateProfile (profile );
253
- this .activeProfiles .add (profile );
253
+ synchronized (this .activeProfiles ) {
254
+ this .activeProfiles .clear ();
255
+ for (String profile : profiles ) {
256
+ validateProfile (profile );
257
+ this .activeProfiles .add (profile );
258
+ }
254
259
}
255
260
}
256
261
@@ -260,7 +265,9 @@ public void addActiveProfile(String profile) {
260
265
}
261
266
validateProfile (profile );
262
267
doGetActiveProfiles ();
263
- this .activeProfiles .add (profile );
268
+ synchronized (this .activeProfiles ) {
269
+ this .activeProfiles .add (profile );
270
+ }
264
271
}
265
272
266
273
@@ -281,13 +288,15 @@ public String[] getDefaultProfiles() {
281
288
* @see #getReservedDefaultProfiles()
282
289
*/
283
290
protected Set <String > doGetDefaultProfiles () {
284
- if (this .defaultProfiles .equals (getReservedDefaultProfiles ())) {
285
- String profiles = getProperty (DEFAULT_PROFILES_PROPERTY_NAME );
286
- if (StringUtils .hasText (profiles )) {
287
- setDefaultProfiles (commaDelimitedListToStringArray (trimAllWhitespace (profiles )));
291
+ synchronized (this .defaultProfiles ) {
292
+ if (this .defaultProfiles .equals (getReservedDefaultProfiles ())) {
293
+ String profiles = getProperty (DEFAULT_PROFILES_PROPERTY_NAME );
294
+ if (StringUtils .hasText (profiles )) {
295
+ setDefaultProfiles (commaDelimitedListToStringArray (trimAllWhitespace (profiles )));
296
+ }
288
297
}
298
+ return this .defaultProfiles ;
289
299
}
290
- return this .defaultProfiles ;
291
300
}
292
301
293
302
/**
@@ -300,17 +309,19 @@ protected Set<String> doGetDefaultProfiles() {
300
309
*/
301
310
public void setDefaultProfiles (String ... profiles ) {
302
311
Assert .notNull (profiles , "Profile array must not be null" );
303
- this .defaultProfiles .clear ();
304
- for (String profile : profiles ) {
305
- validateProfile (profile );
306
- this .defaultProfiles .add (profile );
312
+ synchronized (this .defaultProfiles ) {
313
+ this .defaultProfiles .clear ();
314
+ for (String profile : profiles ) {
315
+ validateProfile (profile );
316
+ this .defaultProfiles .add (profile );
317
+ }
307
318
}
308
319
}
309
320
310
321
public boolean acceptsProfiles (String ... profiles ) {
311
322
Assert .notEmpty (profiles , "Must specify at least one profile" );
312
323
for (String profile : profiles ) {
313
- if (profile != null && profile . length () > 0 && profile .charAt (0 ) == '!' ) {
324
+ if (StringUtils . hasLength ( profile ) && profile .charAt (0 ) == '!' ) {
314
325
if (!isProfileActive (profile .substring (1 ))) {
315
326
return true ;
316
327
}
@@ -329,8 +340,9 @@ else if (isProfileActive(profile)) {
329
340
*/
330
341
protected boolean isProfileActive (String profile ) {
331
342
validateProfile (profile );
332
- return doGetActiveProfiles ().contains (profile ) ||
333
- (doGetActiveProfiles ().isEmpty () && doGetDefaultProfiles ().contains (profile ));
343
+ Set <String > currentActiveProfiles = doGetActiveProfiles ();
344
+ return (currentActiveProfiles .contains (profile ) ||
345
+ (currentActiveProfiles .isEmpty () && doGetDefaultProfiles ().contains (profile )));
334
346
}
335
347
336
348
/**
@@ -430,13 +442,21 @@ public void merge(ConfigurableEnvironment parent) {
430
442
this .propertySources .addLast (ps );
431
443
}
432
444
}
433
- for (String profile : parent .getActiveProfiles ()) {
434
- this .activeProfiles .add (profile );
445
+ String [] parentActiveProfiles = parent .getActiveProfiles ();
446
+ if (!ObjectUtils .isEmpty (parentActiveProfiles )) {
447
+ synchronized (this .activeProfiles ) {
448
+ for (String profile : parentActiveProfiles ) {
449
+ this .activeProfiles .add (profile );
450
+ }
451
+ }
435
452
}
436
- if (parent .getDefaultProfiles ().length > 0 ) {
437
- this .defaultProfiles .remove (RESERVED_DEFAULT_PROFILE_NAME );
438
- for (String profile : parent .getDefaultProfiles ()) {
439
- this .defaultProfiles .add (profile );
453
+ String [] parentDefaultProfiles = parent .getDefaultProfiles ();
454
+ if (!ObjectUtils .isEmpty (parentDefaultProfiles )) {
455
+ synchronized (this .defaultProfiles ) {
456
+ this .defaultProfiles .remove (RESERVED_DEFAULT_PROFILE_NAME );
457
+ for (String profile : parentDefaultProfiles ) {
458
+ this .defaultProfiles .add (profile );
459
+ }
440
460
}
441
461
}
442
462
}
0 commit comments