1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.util.internal;
17
18 import org.jboss.netty.logging.InternalLogger;
19 import org.jboss.netty.logging.InternalLoggerFactory;
20
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import java.util.regex.Pattern;
24
25
26
27
28 public final class SystemPropertyUtil {
29
30 @SuppressWarnings("all")
31 private static boolean initializedLogger;
32 private static final InternalLogger logger;
33 private static boolean loggedException;
34
35 static {
36 logger = InternalLoggerFactory.getInstance(SystemPropertyUtil.class);
37 initializedLogger = true;
38 }
39
40
41
42
43
44 public static boolean contains(String key) {
45 return get(key) != null;
46 }
47
48
49
50
51
52
53
54 public static String get(String key) {
55 return get(key, null);
56 }
57
58
59
60
61
62
63
64
65
66
67 public static String get(String key, String def) {
68 if (key == null) {
69 throw new NullPointerException("key");
70 }
71 if (key.length() == 0) {
72 throw new IllegalArgumentException("key must not be empty.");
73 }
74
75 String value = null;
76 try {
77 value = System.getProperty(key);
78 } catch (Exception e) {
79 if (!loggedException) {
80 log("Unable to retrieve a system property '" + key + "'; default values will be used.", e);
81 loggedException = true;
82 }
83 }
84
85 if (value == null) {
86 return def;
87 }
88
89 return value;
90 }
91
92
93
94
95
96
97
98
99
100
101 public static boolean getBoolean(String key, boolean def) {
102 String value = get(key);
103 if (value == null) {
104 return def;
105 }
106
107 value = value.trim().toLowerCase();
108 if (value.length() == 0) {
109 return true;
110 }
111
112 if ("true".equals(value) || "yes".equals(value) || "1".equals(value)) {
113 return true;
114 }
115
116 if ("false".equals(value) || "no".equals(value) || "0".equals(value)) {
117 return false;
118 }
119
120 log(
121 "Unable to parse the boolean system property '" + key + "':" + value + " - " +
122 "using the default value: " + def);
123
124 return def;
125 }
126
127 private static final Pattern INTEGER_PATTERN = Pattern.compile("-?[0-9]+");
128
129
130
131
132
133
134
135
136
137
138 public static int getInt(String key, int def) {
139 String value = get(key);
140 if (value == null) {
141 return def;
142 }
143
144 value = value.trim().toLowerCase();
145 if (INTEGER_PATTERN.matcher(value).matches()) {
146 try {
147 return Integer.parseInt(value);
148 } catch (Exception e) {
149
150 }
151 }
152
153 log(
154 "Unable to parse the integer system property '" + key + "':" + value + " - " +
155 "using the default value: " + def);
156
157 return def;
158 }
159
160
161
162
163
164
165
166
167
168
169 public static long getLong(String key, long def) {
170 String value = get(key);
171 if (value == null) {
172 return def;
173 }
174
175 value = value.trim().toLowerCase();
176 if (INTEGER_PATTERN.matcher(value).matches()) {
177 try {
178 return Long.parseLong(value);
179 } catch (Exception e) {
180
181 }
182 }
183
184 log(
185 "Unable to parse the long integer system property '" + key + "':" + value + " - " +
186 "using the default value: " + def);
187
188 return def;
189 }
190
191 private static void log(String msg) {
192 if (initializedLogger) {
193 logger.warn(msg);
194 } else {
195
196 Logger.getLogger(SystemPropertyUtil.class.getName()).log(Level.WARNING, msg);
197 }
198 }
199
200 private static void log(String msg, Exception e) {
201 if (initializedLogger) {
202 logger.warn(msg, e);
203 } else {
204
205 Logger.getLogger(SystemPropertyUtil.class.getName()).log(Level.WARNING, msg, e);
206 }
207 }
208
209 private SystemPropertyUtil() {
210
211 }
212 }