@@ -139,15 +139,27 @@ public static PropertiesUtil getProperties() {
139
139
}
140
140
141
141
/**
142
- * Allows a PropertySource to be added after PropertiesUtil has been created.
143
- * @param propertySource the PropertySource to add.
142
+ * Allows a {@link PropertySource} to be added after PropertiesUtil has been created.
143
+ * @param propertySource the {@code PropertySource} to add.
144
+ * @since 2.19.0
144
145
*/
145
146
public void addPropertySource (final PropertySource propertySource ) {
146
147
if (environment != null ) {
147
148
environment .addPropertySource (propertySource );
148
149
}
149
150
}
150
151
152
+ /**
153
+ * Removes a {@link PropertySource}.
154
+ * @param propertySource the {@code PropertySource} to remove.
155
+ * @since 2.24.0
156
+ */
157
+ public void removePropertySource (final PropertySource propertySource ) {
158
+ if (environment != null ) {
159
+ environment .removePropertySource (propertySource );
160
+ }
161
+ }
162
+
151
163
/**
152
164
* Returns {@code true} if the specified property is defined, regardless of its value (it may not have a value).
153
165
*
@@ -529,6 +541,10 @@ public void addPropertySource(final PropertySource propertySource) {
529
541
sources .add (propertySource );
530
542
}
531
543
544
+ private void removePropertySource (final PropertySource propertySource ) {
545
+ sources .remove (propertySource );
546
+ }
547
+
532
548
private synchronized void reload () {
533
549
literal .clear ();
534
550
tokenized .clear ();
@@ -540,18 +556,18 @@ private synchronized void reload() {
540
556
final List <CharSequence > tokens = PropertySource .Util .tokenize (key );
541
557
final boolean hasTokens = !tokens .isEmpty ();
542
558
sources .forEach (source -> {
543
- if (source . containsProperty ( key )) {
544
- final String value = source . getProperty ( key );
559
+ if (sourceContainsProperty ( source , key )) {
560
+ final String value = sourceGetProperty ( source , key );
545
561
if (hasTokens ) {
546
562
tokenized .putIfAbsent (tokens , value );
547
563
}
548
564
}
549
565
if (hasTokens ) {
550
566
final String normalKey = Objects .toString (source .getNormalForm (tokens ), null );
551
- if (normalKey != null && source . containsProperty ( normalKey )) {
552
- literal .putIfAbsent (key , source . getProperty ( normalKey ));
553
- } else if (source . containsProperty ( key )) {
554
- literal .putIfAbsent (key , source . getProperty ( key ));
567
+ if (normalKey != null && sourceContainsProperty ( source , normalKey )) {
568
+ literal .putIfAbsent (key , sourceGetProperty ( source , normalKey ));
569
+ } else if (sourceContainsProperty ( source , key )) {
570
+ literal .putIfAbsent (key , sourceGetProperty ( source , key ));
555
571
}
556
572
}
557
573
});
@@ -567,25 +583,41 @@ private String get(final String key) {
567
583
for (final PropertySource source : sources ) {
568
584
if (hasTokens ) {
569
585
final String normalKey = Objects .toString (source .getNormalForm (tokens ), null );
570
- if (normalKey != null && source . containsProperty ( normalKey )) {
571
- return source . getProperty ( normalKey );
586
+ if (normalKey != null && sourceContainsProperty ( source , normalKey )) {
587
+ return sourceGetProperty ( source , normalKey );
572
588
}
573
589
}
574
- if (source . containsProperty ( key )) {
575
- return source . getProperty ( key );
590
+ if (sourceContainsProperty ( source , key )) {
591
+ return sourceGetProperty ( source , key );
576
592
}
577
593
}
578
594
return tokenized .get (tokens );
579
595
}
580
596
597
+ private boolean sourceContainsProperty (final PropertySource source , final String key ) {
598
+ try {
599
+ return source .containsProperty (key );
600
+ } catch (final Throwable ex ) {
601
+ return false ;
602
+ }
603
+ }
604
+
605
+ private String sourceGetProperty (final PropertySource source , final String key ) {
606
+ try {
607
+ return source .getProperty (key );
608
+ } catch (final Throwable ex ) {
609
+ return null ;
610
+ }
611
+ }
612
+
581
613
private boolean containsKey (final String key ) {
582
614
final List <CharSequence > tokens = PropertySource .Util .tokenize (key );
583
615
return literal .containsKey (key )
584
616
|| tokenized .containsKey (tokens )
585
617
|| sources .stream ().anyMatch (s -> {
586
618
final CharSequence normalizedKey = s .getNormalForm (tokens );
587
- return s . containsProperty ( key )
588
- || (normalizedKey != null && s . containsProperty ( normalizedKey .toString ()));
619
+ return sourceContainsProperty ( s , key )
620
+ || (normalizedKey != null && sourceContainsProperty ( s , normalizedKey .toString ()));
589
621
});
590
622
}
591
623
}
0 commit comments