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