@@ -60,6 +60,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
60
60
* resolvable otherwise. Consider switching this flag to "true" if you experience
61
61
* log warnings from {@code getenv} calls coming from Spring, e.g. on WebSphere
62
62
* with strict SecurityManager settings and AccessControlExceptions warnings.
63
+ * @see #suppressGetenvAccess()
63
64
*/
64
65
public static final String IGNORE_GETENV_PROPERTY_NAME = "spring.getenv.ignore" ;
65
66
@@ -359,17 +360,9 @@ public MutablePropertySources getPropertySources() {
359
360
360
361
@ SuppressWarnings ("unchecked" )
361
362
public Map <String , Object > getSystemEnvironment () {
362
- try {
363
- if ("true" .equalsIgnoreCase (System .getProperty (IGNORE_GETENV_PROPERTY_NAME ))) {
364
- return Collections .emptyMap ();
365
- }
366
- }
367
- catch (Throwable ex ) {
368
- if (logger .isDebugEnabled ()) {
369
- logger .debug ("Could not obtain system property '" + IGNORE_GETENV_PROPERTY_NAME + "': " + ex );
370
- }
363
+ if (suppressGetenvAccess ()) {
364
+ return Collections .emptyMap ();
371
365
}
372
-
373
366
try {
374
367
return (Map ) System .getenv ();
375
368
}
@@ -393,6 +386,27 @@ protected String getSystemAttribute(String variableName) {
393
386
}
394
387
}
395
388
389
+ /**
390
+ * Determine whether to suppress {@link System#getenv()}/{@link System#getenv(String)}
391
+ * access for the purposes of {@link #getSystemEnvironment()}.
392
+ * <p>If this method returns {@code true}, an empty dummy Map will be used instead
393
+ * of the regular system environment Map, never even trying to call {@code getenv}
394
+ * and therefore avoiding security manager warnings (if any).
395
+ * <p>The default implementation checks for the "spring.getenv.ignore" system property,
396
+ * returning {@code true} if its value equals "true" in any case.
397
+ */
398
+ protected boolean suppressGetenvAccess () {
399
+ try {
400
+ return "true" .equalsIgnoreCase (System .getProperty (IGNORE_GETENV_PROPERTY_NAME ));
401
+ }
402
+ catch (Throwable ex ) {
403
+ if (logger .isDebugEnabled ()) {
404
+ logger .debug ("Could not obtain system property '" + IGNORE_GETENV_PROPERTY_NAME + "': " + ex );
405
+ }
406
+ return false ;
407
+ }
408
+ }
409
+
396
410
@ SuppressWarnings ("unchecked" )
397
411
public Map <String , Object > getSystemProperties () {
398
412
try {
0 commit comments