1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.util;
15
16 import java.util.Properties;
17
18 import ch.qos.logback.core.Context;
19 import ch.qos.logback.core.CoreConstants;
20 import ch.qos.logback.core.spi.ContextAware;
21 import ch.qos.logback.core.spi.PropertyContainer;
22
23
24
25
26 public class OptionHelper {
27
28 public static Object instantiateByClassName(String className,
29 Class superClass, Context context) throws IncompatibleClassException,
30 DynamicClassLoadingException {
31 ClassLoader classLoader = Loader.getClassLoaderOfObject(context);
32 return instantiateByClassName(className, superClass, classLoader);
33 }
34
35 @SuppressWarnings("unchecked")
36 public static Object instantiateByClassName(String className,
37 Class superClass, ClassLoader classLoader)
38 throws IncompatibleClassException, DynamicClassLoadingException {
39
40 if (className == null) {
41 throw new NullPointerException();
42 }
43
44 try {
45 Class classObj = null;
46 classObj = classLoader.loadClass(className);
47 if (!superClass.isAssignableFrom(classObj)) {
48 throw new IncompatibleClassException(superClass, classObj);
49 }
50 return classObj.newInstance();
51 } catch (IncompatibleClassException ice) {
52 throw ice;
53 } catch (Throwable t) {
54 throw new DynamicClassLoadingException("Failed to instantiate type "
55 + className, t);
56 }
57 }
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 final static String DELIM_START = "${";
78 final static char DELIM_STOP = '}';
79 final static int DELIM_START_LEN = 2;
80 final static int DELIM_STOP_LEN = 1;
81 final static String _IS_UNDEFINED = "_IS_UNDEFINED";
82
83
84
85
86 public static String substVars(String val, PropertyContainer pc1) {
87 return substVars(val, pc1, null);
88 }
89
90
91
92
93 public static String substVars(String val, PropertyContainer pc1, PropertyContainer pc2) {
94
95 StringBuffer sbuf = new StringBuffer();
96
97 int i = 0;
98 int j;
99 int k;
100
101 while (true) {
102 j = val.indexOf(DELIM_START, i);
103
104 if (j == -1) {
105
106 if (i == 0) {
107
108 return val;
109 } else {
110
111 sbuf.append(val.substring(i, val.length()));
112
113 return sbuf.toString();
114 }
115 } else {
116 sbuf.append(val.substring(i, j));
117 k = val.indexOf(DELIM_STOP, j);
118
119 if (k == -1) {
120 throw new IllegalArgumentException('"' + val
121 + "\" has no closing brace. Opening brace at position " + j + '.');
122 } else {
123 j += DELIM_START_LEN;
124
125 String rawKey = val.substring(j, k);
126
127
128 String[] extracted = extractDefaultReplacement(rawKey);
129 String key = extracted[0];
130 String defaultReplacement = extracted[1];
131
132 String replacement = propertyLookup(key, pc1, pc2);
133
134
135
136 if (replacement == null) {
137 replacement = defaultReplacement;
138 }
139
140 if (replacement != null) {
141
142
143
144
145
146 String recursiveReplacement = substVars(replacement, pc1, pc2);
147 sbuf.append(recursiveReplacement);
148 } else {
149
150 sbuf.append(key + "_IS_UNDEFINED");
151 }
152
153 i = k + DELIM_STOP_LEN;
154 }
155 }
156 }
157 }
158
159 public static String propertyLookup(String key, PropertyContainer pc1,
160 PropertyContainer pc2) {
161 String value = null;
162
163 value = pc1.getProperty(key);
164
165
166 if (value == null && pc2 != null) {
167 value = pc2.getProperty(key);
168 }
169
170 if (value == null) {
171 value = getSystemProperty(key, null);
172 }
173 if (value == null) {
174 value = getEnv(key);
175 }
176 return value;
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190 public static String getSystemProperty(String key, String def) {
191 try {
192 return System.getProperty(key, def);
193 } catch (SecurityException e) {
194 return def;
195 }
196 }
197
198
199
200
201
202
203 public static String getEnv(String key) {
204 try {
205 return System.getenv(key);
206 } catch (SecurityException e) {
207 return null;
208 }
209 }
210
211
212
213
214
215
216
217
218
219
220
221 public static String getSystemProperty(String key) {
222 try {
223 return System.getProperty(key);
224 } catch (SecurityException e) {
225 return null;
226 }
227 }
228
229 public static void setSystemProperties(ContextAware contextAware, Properties props) {
230 for(Object o: props.keySet()) {
231 String key = (String) o;
232 String value = props.getProperty(key);
233 setSystemProperty(contextAware, key, value);
234 }
235 }
236
237 public static void setSystemProperty(ContextAware contextAware, String key, String value) {
238 try {
239 System.setProperty(key, value);
240 } catch (SecurityException e) {
241 contextAware.addError("Failed to set system property ["+key+"]", e);
242 }
243 }
244
245
246
247
248
249
250
251 public static Properties getSystemProperties() {
252 try {
253 return System.getProperties();
254 } catch (SecurityException e) {
255 return new Properties();
256 }
257 }
258
259 static public String[] extractDefaultReplacement(String key) {
260 String[] result = new String[2];
261 result[0] = key;
262 int d = key.indexOf(":-");
263 if (d != -1) {
264 result[0] = key.substring(0, d);
265 result[1] = key.substring(d + 2);
266 }
267 return result;
268 }
269
270
271
272
273
274
275
276
277 public static boolean toBoolean(String value, boolean dEfault) {
278 if (value == null) {
279 return dEfault;
280 }
281
282 String trimmedVal = value.trim();
283
284 if ("true".equalsIgnoreCase(trimmedVal)) {
285 return true;
286 }
287
288 if ("false".equalsIgnoreCase(trimmedVal)) {
289 return false;
290 }
291
292 return dEfault;
293 }
294
295 public static boolean isEmpty(String str) {
296 return ((str == null) || CoreConstants.EMPTY_STRING.equals(str));
297 }
298
299
300 }