1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.spring.env;
17
18 import java.util.Properties;
19
20 import org.kuali.common.util.Ascii;
21 import org.kuali.common.util.Assert;
22 import org.kuali.common.util.FormatUtils;
23 import org.kuali.common.util.PropertyUtils;
24 import org.springframework.core.env.Environment;
25
26 import com.google.common.base.Optional;
27
28 public class EnvUtils {
29
30 public static final Optional<EnvironmentService> ABSENT = Optional.absent();
31
32 private static final String ENV_PREFIX = "env";
33
34
35
36
37
38
39
40
41
42 public static long getBytes(EnvironmentService env, String key, long provided) {
43 if (env.containsProperty(key)) {
44 String size = env.getString(key);
45 long bytes = FormatUtils.getBytes(size);
46 return bytes;
47 } else {
48 return provided;
49 }
50 }
51
52 private static Environment instance;
53
54
55
56
57 public synchronized static Environment getDefaultEnvironment() {
58 if (instance == null) {
59 Properties global = PropertyUtils.getGlobalProperties();
60 instance = new PropertiesEnvironment(global);
61 }
62 return instance;
63 }
64
65
66
67
68
69
70
71 public static String getEnvironmentVariableKey(String key) {
72 Assert.noBlanks(key);
73
74 return ENV_PREFIX + "." + toUnderscore(key).toUpperCase();
75 }
76
77
78
79
80
81
82
83 public static String toUnderscore(String key) {
84 char[] chars = key.toCharArray();
85 StringBuilder sb = new StringBuilder();
86 char prevChar = 0;
87 for (char c : chars) {
88 if (c == '.') {
89
90 sb.append('_');
91 } else if (Ascii.isUpperCase(c) && Ascii.isLowerCase(prevChar)) {
92
93 sb.append('_');
94 sb.append(c);
95 } else {
96
97 sb.append(c);
98 }
99
100 prevChar = c;
101 }
102 return sb.toString();
103 }
104
105 }