1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.helpers;
18
19 import org.apache.logging.log4j.Logger;
20 import org.apache.logging.log4j.status.StatusLogger;
21 import org.apache.logging.log4j.util.PropertiesUtil;
22
23 import java.util.Locale;
24 import java.util.Properties;
25
26
27
28
29 public final class OptionConverter {
30
31 private static final Logger LOGGER = StatusLogger.getLogger();
32
33 private static final String DELIM_START = "${";
34 private static final char DELIM_STOP = '}';
35 private static final int DELIM_START_LEN = 2;
36 private static final int DELIM_STOP_LEN = 1;
37 private static final int ONE_K = 1024;
38
39
40
41
42 private OptionConverter() {
43 }
44
45 public static String[] concatanateArrays(final String[] l, final String[] r) {
46 final int len = l.length + r.length;
47 final String[] a = new String[len];
48
49 System.arraycopy(l, 0, a, 0, l.length);
50 System.arraycopy(r, 0, a, l.length, r.length);
51
52 return a;
53 }
54
55 public static String convertSpecialChars(final String s) {
56 char c;
57 final int len = s.length();
58 final StringBuffer sbuf = new StringBuffer(len);
59
60 int i = 0;
61 while (i < len) {
62 c = s.charAt(i++);
63 if (c == '\\') {
64 c = s.charAt(i++);
65 if (c == 'n') {
66 c = '\n';
67 } else if (c == 'r') {
68 c = '\r';
69 } else if (c == 't') {
70 c = '\t';
71 } else if (c == 'f') {
72 c = '\f';
73 } else if (c == '\b') {
74 c = '\b';
75 } else if (c == '\"') {
76 c = '\"';
77 } else if (c == '\'') {
78 c = '\'';
79 } else if (c == '\\') {
80 c = '\\';
81 }
82 }
83 sbuf.append(c);
84 }
85 return sbuf.toString();
86 }
87
88 public static Object instantiateByKey(final Properties props, final String key, final Class<?> superClass,
89 final Object defaultValue) {
90
91
92 final String className = findAndSubst(key, props);
93 if (className == null) {
94 LOGGER.error("Could not find value for key " + key);
95 return defaultValue;
96 }
97
98 return OptionConverter.instantiateByClassName(className.trim(), superClass,
99 defaultValue);
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113 public static boolean toBoolean(final String value, final boolean dEfault) {
114 if (value == null) {
115 return dEfault;
116 }
117 final String trimmedVal = value.trim();
118 if ("true".equalsIgnoreCase(trimmedVal)) {
119 return true;
120 }
121 if ("false".equalsIgnoreCase(trimmedVal)) {
122 return false;
123 }
124 return dEfault;
125 }
126
127
128
129
130
131
132
133 public static int toInt(final String value, final int dEfault) {
134 if (value != null) {
135 final String s = value.trim();
136 try {
137 return Integer.valueOf(s);
138 } catch (final NumberFormatException e) {
139 LOGGER.error("[" + s + "] is not in proper int form.");
140 e.printStackTrace();
141 }
142 }
143 return dEfault;
144 }
145
146
147
148
149
150
151
152 public static long toFileSize(final String value, final long defaultValue) {
153 if (value == null) {
154 return defaultValue;
155 }
156
157 String s = value.trim().toUpperCase(Locale.ENGLISH);
158 long multiplier = 1;
159 int index;
160
161 if ((index = s.indexOf("KB")) != -1) {
162 multiplier = ONE_K;
163 s = s.substring(0, index);
164 } else if ((index = s.indexOf("MB")) != -1) {
165 multiplier = ONE_K * ONE_K;
166 s = s.substring(0, index);
167 } else if ((index = s.indexOf("GB")) != -1) {
168 multiplier = ONE_K * ONE_K * ONE_K;
169 s = s.substring(0, index);
170 }
171 if (s != null) {
172 try {
173 return Long.valueOf(s) * multiplier;
174 } catch (final NumberFormatException e) {
175 LOGGER.error("[" + s + "] is not in proper int form.");
176 LOGGER.error("[" + value + "] not in expected format.", e);
177 }
178 }
179 return defaultValue;
180 }
181
182
183
184
185
186
187
188
189
190 public static String findAndSubst(final String key, final Properties props) {
191 final String value = props.getProperty(key);
192 if (value == null) {
193 return null;
194 }
195
196 try {
197 return substVars(value, props);
198 } catch (final IllegalArgumentException e) {
199 LOGGER.error("Bad option value [" + value + "].", e);
200 return value;
201 }
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215 public static Object instantiateByClassName(final String className, final Class<?> superClass,
216 final Object defaultValue) {
217 if (className != null) {
218 try {
219 final Class<?> classObj = Loader.loadClass(className);
220 if (!superClass.isAssignableFrom(classObj)) {
221 LOGGER.error("A \"" + className + "\" object is not assignable to a \"" +
222 superClass.getName() + "\" variable.");
223 LOGGER.error("The class \"" + superClass.getName() + "\" was loaded by ");
224 LOGGER.error("[" + superClass.getClassLoader() + "] whereas object of type ");
225 LOGGER.error("\"" + classObj.getName() + "\" was loaded by ["
226 + classObj.getClassLoader() + "].");
227 return defaultValue;
228 }
229 return classObj.newInstance();
230 } catch (final ClassNotFoundException e) {
231 LOGGER.error("Could not instantiate class [" + className + "].", e);
232 } catch (final IllegalAccessException e) {
233 LOGGER.error("Could not instantiate class [" + className + "].", e);
234 } catch (final InstantiationException e) {
235 LOGGER.error("Could not instantiate class [" + className + "].", e);
236 } catch (final RuntimeException e) {
237 LOGGER.error("Could not instantiate class [" + className + "].", e);
238 }
239 }
240 return defaultValue;
241 }
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281 public static String substVars(final String val, final Properties props) throws
282 IllegalArgumentException {
283
284 final StringBuilder sbuf = new StringBuilder();
285
286 int i = 0;
287 int j;
288 int k;
289
290 while (true) {
291 j = val.indexOf(DELIM_START, i);
292 if (j == -1) {
293
294 if (i == 0) {
295 return val;
296 } else {
297 sbuf.append(val.substring(i, val.length()));
298 return sbuf.toString();
299 }
300 } else {
301 sbuf.append(val.substring(i, j));
302 k = val.indexOf(DELIM_STOP, j);
303 if (k == -1) {
304 throw new IllegalArgumentException('"' + val +
305 "\" has no closing brace. Opening brace at position " + j
306 + '.');
307 } else {
308 j += DELIM_START_LEN;
309 final String key = val.substring(j, k);
310
311 String replacement = PropertiesUtil.getProperties().getStringProperty(key, null);
312
313 if (replacement == null && props != null) {
314 replacement = props.getProperty(key);
315 }
316
317 if (replacement != null) {
318
319
320
321
322
323 final String recursiveReplacement = substVars(replacement, props);
324 sbuf.append(recursiveReplacement);
325 }
326 i = k + DELIM_STOP_LEN;
327 }
328 }
329 }
330 }
331 }