Skip to content

Commit

Permalink
Adds Utils.getEnvOrSystemProperty with default (#2742)
Browse files Browse the repository at this point in the history
Adds the Utils.getEnvOrSystemProperty that also supports a default value.
  • Loading branch information
zachgk committed Aug 9, 2023
1 parent 41a395d commit ff66278
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion api/src/main/java/ai/djl/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit ff66278

Please sign in to comment.