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
@@ -237,22 +238,26 @@ public String[] getActiveProfiles() {
237
238
* @see #ACTIVE_PROFILES_PROPERTY_NAME
238
239
*/
239
240
protected Set <String > doGetActiveProfiles () {
240
- if (this .activeProfiles .isEmpty ()) {
241
- String profiles = getProperty (ACTIVE_PROFILES_PROPERTY_NAME );
242
- if (StringUtils .hasText (profiles )) {
243
- setActiveProfiles (commaDelimitedListToStringArray (trimAllWhitespace (profiles )));
241
+ synchronized (this .activeProfiles ) {
242
+ if (this .activeProfiles .isEmpty ()) {
243
+ String profiles = getProperty (ACTIVE_PROFILES_PROPERTY_NAME );
244
+ if (StringUtils .hasText (profiles )) {
245
+ setActiveProfiles (commaDelimitedListToStringArray (trimAllWhitespace (profiles )));
246
+ }
244
247
}
248
+ return this .activeProfiles ;
245
249
}
246
- return this .activeProfiles ;
247
250
}
248
251
249
252
@ Override
250
253
public void setActiveProfiles (String ... profiles ) {
251
254
Assert .notNull (profiles , "Profile array must not be null" );
252
- this .activeProfiles .clear ();
253
- for (String profile : profiles ) {
254
- validateProfile (profile );
255
- this .activeProfiles .add (profile );
255
+ synchronized (this .activeProfiles ) {
256
+ this .activeProfiles .clear ();
257
+ for (String profile : profiles ) {
258
+ validateProfile (profile );
259
+ this .activeProfiles .add (profile );
260
+ }
256
261
}
257
262
}
258
263
@@ -263,7 +268,9 @@ public void addActiveProfile(String profile) {
263
268
}
264
269
validateProfile (profile );
265
270
doGetActiveProfiles ();
266
- this .activeProfiles .add (profile );
271
+ synchronized (this .activeProfiles ) {
272
+ this .activeProfiles .add (profile );
273
+ }
267
274
}
268
275
269
276
@@ -285,13 +292,15 @@ public String[] getDefaultProfiles() {
285
292
* @see #getReservedDefaultProfiles()
286
293
*/
287
294
protected Set <String > doGetDefaultProfiles () {
288
- if (this .defaultProfiles .equals (getReservedDefaultProfiles ())) {
289
- String profiles = getProperty (DEFAULT_PROFILES_PROPERTY_NAME );
290
- if (StringUtils .hasText (profiles )) {
291
- setDefaultProfiles (commaDelimitedListToStringArray (trimAllWhitespace (profiles )));
295
+ synchronized (this .defaultProfiles ) {
296
+ if (this .defaultProfiles .equals (getReservedDefaultProfiles ())) {
297
+ String profiles = getProperty (DEFAULT_PROFILES_PROPERTY_NAME );
298
+ if (StringUtils .hasText (profiles )) {
299
+ setDefaultProfiles (commaDelimitedListToStringArray (trimAllWhitespace (profiles )));
300
+ }
292
301
}
302
+ return this .defaultProfiles ;
293
303
}
294
- return this .defaultProfiles ;
295
304
}
296
305
297
306
/**
@@ -305,18 +314,20 @@ protected Set<String> doGetDefaultProfiles() {
305
314
@ Override
306
315
public void setDefaultProfiles (String ... profiles ) {
307
316
Assert .notNull (profiles , "Profile array must not be null" );
308
- this .defaultProfiles .clear ();
309
- for (String profile : profiles ) {
310
- validateProfile (profile );
311
- this .defaultProfiles .add (profile );
317
+ synchronized (this .defaultProfiles ) {
318
+ this .defaultProfiles .clear ();
319
+ for (String profile : profiles ) {
320
+ validateProfile (profile );
321
+ this .defaultProfiles .add (profile );
322
+ }
312
323
}
313
324
}
314
325
315
326
@ Override
316
327
public boolean acceptsProfiles (String ... profiles ) {
317
328
Assert .notEmpty (profiles , "Must specify at least one profile" );
318
329
for (String profile : profiles ) {
319
- if (profile != null && profile . length () > 0 && profile .charAt (0 ) == '!' ) {
330
+ if (StringUtils . hasLength ( profile ) && profile .charAt (0 ) == '!' ) {
320
331
if (!isProfileActive (profile .substring (1 ))) {
321
332
return true ;
322
333
}
@@ -335,8 +346,9 @@ else if (isProfileActive(profile)) {
335
346
*/
336
347
protected boolean isProfileActive (String profile ) {
337
348
validateProfile (profile );
338
- return doGetActiveProfiles ().contains (profile ) ||
339
- (doGetActiveProfiles ().isEmpty () && doGetDefaultProfiles ().contains (profile ));
349
+ Set <String > currentActiveProfiles = doGetActiveProfiles ();
350
+ return (currentActiveProfiles .contains (profile ) ||
351
+ (currentActiveProfiles .isEmpty () && doGetDefaultProfiles ().contains (profile )));
340
352
}
341
353
342
354
/**
@@ -364,7 +376,7 @@ public MutablePropertySources getPropertySources() {
364
376
}
365
377
366
378
@ Override
367
- @ SuppressWarnings ({ "unchecked" , "rawtypes" })
379
+ @ SuppressWarnings ({"unchecked" , "rawtypes" })
368
380
public Map <String , Object > getSystemEnvironment () {
369
381
if (suppressGetenvAccess ()) {
370
382
return Collections .emptyMap ();
@@ -408,7 +420,7 @@ protected boolean suppressGetenvAccess() {
408
420
}
409
421
410
422
@ Override
411
- @ SuppressWarnings ({ "unchecked" , "rawtypes" })
423
+ @ SuppressWarnings ({"unchecked" , "rawtypes" })
412
424
public Map <String , Object > getSystemProperties () {
413
425
try {
414
426
return (Map ) System .getProperties ();
@@ -440,13 +452,21 @@ public void merge(ConfigurableEnvironment parent) {
440
452
this .propertySources .addLast (ps );
441
453
}
442
454
}
443
- for (String profile : parent .getActiveProfiles ()) {
444
- this .activeProfiles .add (profile );
455
+ String [] parentActiveProfiles = parent .getActiveProfiles ();
456
+ if (!ObjectUtils .isEmpty (parentActiveProfiles )) {
457
+ synchronized (this .activeProfiles ) {
458
+ for (String profile : parentActiveProfiles ) {
459
+ this .activeProfiles .add (profile );
460
+ }
461
+ }
445
462
}
446
- if (parent .getDefaultProfiles ().length > 0 ) {
447
- this .defaultProfiles .remove (RESERVED_DEFAULT_PROFILE_NAME );
448
- for (String profile : parent .getDefaultProfiles ()) {
449
- this .defaultProfiles .add (profile );
463
+ String [] parentDefaultProfiles = parent .getDefaultProfiles ();
464
+ if (!ObjectUtils .isEmpty (parentDefaultProfiles )) {
465
+ synchronized (this .defaultProfiles ) {
466
+ this .defaultProfiles .remove (RESERVED_DEFAULT_PROFILE_NAME );
467
+ for (String profile : parentDefaultProfiles ) {
468
+ this .defaultProfiles .add (profile );
469
+ }
450
470
}
451
471
}
452
472
}
0 commit comments