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