From ff66278812eaf60a4ff2e204678b2e3cd13cd560 Mon Sep 17 00:00:00 2001 From: Zach Kimberg Date: Wed, 9 Aug 2023 16:50:03 -0700 Subject: [PATCH] Adds Utils.getEnvOrSystemProperty with default (#2742) Adds the Utils.getEnvOrSystemProperty that also supports a default value. --- api/src/main/java/ai/djl/util/Utils.java | 27 +++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/ai/djl/util/Utils.java b/api/src/main/java/ai/djl/util/Utils.java index 437f0727ec4..c8e1bd514ac 100644 --- a/api/src/main/java/ai/djl/util/Utils.java +++ b/api/src/main/java/ai/djl/util/Utils.java @@ -388,7 +388,32 @@ public static Path getNestedModelDir(Path modelDir) { * @return the string value of the variable or system property */ public static String getEnvOrSystemProperty(String name) { - return getenv(name, System.getProperty(name)); + return getEnvOrSystemProperty(name, null); + } + + /** + * Gets the value of the specified environment variable or system property. + * + * @param name the name of the environment variable + * @param def a default value + * @return the string value of the variable or system property + */ + public static String getEnvOrSystemProperty(String name, String def) { + try { + String env = System.getenv(name); + if (env != null) { + return env; + } + } catch (SecurityException e) { + logger.warn("Security manager doesn't allow access to the environment variable"); + } + + String prop = System.getProperty(name); + if (prop != null) { + return prop; + } + + return def; } /**