-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEnvironmentVariableUtil.java
More file actions
53 lines (44 loc) · 1.52 KB
/
EnvironmentVariableUtil.java
File metadata and controls
53 lines (44 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package org.utplsql.api;
import javax.annotation.Nullable;
/**
* This class provides an easy way to get environmental variables.
* This is mainly to improve testability but also to standardize the way how utPLSQL API and CLI read from
* environment.
* <p>
* Variables are obtained from the following scopes in that order (chain breaks as soon as a value is obtained):
* <ul>
* <li>Properties (System.getProperty())</li>
* <li>Environment (System.getEnv())</li>
* <li>Default value</li>
* </ul>
* <p>
* An empty string is treated the same as null.
*
* @author pesse
*/
public class EnvironmentVariableUtil {
private EnvironmentVariableUtil() {
}
/**
* Returns the value for a given key from environment (see class description)
*
* @param key Key of environment or property value
* @return Environment value or null
*/
public static String getEnvValue(String key) {
return getEnvValue(key, null);
}
/**
* Returns the value for a given key from environment or a default value (see class description)
*
* @param key Key of environment or property value
* @param defaultValue Default value if nothing found
* @return Environment value or defaultValue
*/
public static String getEnvValue(String key, @Nullable String defaultValue) {
String val = System.getProperty(key);
if (val == null || val.isEmpty()) val = System.getenv(key);
if (val == null || val.isEmpty()) val = defaultValue;
return val;
}
}