1 package org.apache.commons.jcs3.utils.config;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Properties;
23
24 import org.apache.commons.jcs3.log.Log;
25 import org.apache.commons.jcs3.log.LogManager;
26
27
28
29
30
31
32 public class OptionConverter
33 {
34
35 private static final Log log = LogManager.getLog( OptionConverter.class );
36
37
38 private static final String DELIM_START = "${";
39
40
41 private static final char DELIM_STOP = '}';
42
43
44 private static final int DELIM_START_LEN = 2;
45
46
47 private static final int DELIM_STOP_LEN = 1;
48
49
50 private OptionConverter()
51 {
52 }
53
54
55
56
57
58
59
60 public static String[] concatanateArrays( final String[] l, final String[] r )
61 {
62 final int len = l.length + r.length;
63 final String[] a = new String[len];
64
65 System.arraycopy( l, 0, a, 0, l.length );
66 System.arraycopy( r, 0, a, l.length, r.length );
67
68 return a;
69 }
70
71
72
73
74
75
76
77 public static String convertSpecialChars( final String s )
78 {
79 char c;
80 final int len = s.length();
81 final StringBuilder sb = new StringBuilder( len );
82
83 int i = 0;
84 while ( i < len )
85 {
86 c = s.charAt( i++ );
87 if ( c == '\\' )
88 {
89 c = s.charAt( i++ );
90 if ( c == 'n' )
91 {
92 c = '\n';
93 }
94 else if ( c == 'r' )
95 {
96 c = '\r';
97 }
98 else if ( c == 't' )
99 {
100 c = '\t';
101 }
102 else if ( c == 'f' )
103 {
104 c = '\f';
105 }
106 else if ( c == '\b' )
107 {
108 c = '\b';
109 }
110 else if ( c == '\"' )
111 {
112 c = '\"';
113 }
114 else if ( c == '\'' )
115 {
116 c = '\'';
117 }
118 else if ( c == '\\' )
119 {
120 c = '\\';
121 }
122 }
123 sb.append( c );
124 }
125 return sb.toString();
126 }
127
128
129
130
131
132
133
134
135
136
137
138 public static String getSystemProperty( final String key, final String def )
139 {
140 try
141 {
142 return System.getProperty( key, def );
143 }
144 catch ( final Throwable e )
145 {
146
147 log.debug( "Was not allowed to read system property \"{0}\".", key );
148 return def;
149 }
150 }
151
152
153
154
155
156
157
158
159
160 public static <T> T instantiateByKey( final Properties props, final String key, final T defaultValue )
161 {
162
163
164 final String className = findAndSubst( key, props );
165 if ( className == null )
166 {
167 log.trace( "Could not find value for key {0}", key );
168 return defaultValue;
169 }
170
171 return OptionConverter.instantiateByClassName( className.trim(), defaultValue );
172 }
173
174
175
176
177
178
179
180
181
182
183 public static boolean toBoolean( final String value, final boolean defaultValue )
184 {
185 if ( value == null )
186 {
187 return defaultValue;
188 }
189 final String trimmedVal = value.trim();
190 if ( "true".equalsIgnoreCase( trimmedVal ) )
191 {
192 return true;
193 }
194 if ( "false".equalsIgnoreCase( trimmedVal ) )
195 {
196 return false;
197 }
198 return defaultValue;
199 }
200
201
202
203
204
205
206
207
208 public static int toInt( final String value, final int defaultValue )
209 {
210 if ( value != null )
211 {
212 final String s = value.trim();
213 try
214 {
215 return Integer.parseInt(s);
216 }
217 catch ( final NumberFormatException e )
218 {
219 log.error( "[{0}] is not in proper int form.", s, e );
220 }
221 }
222 return defaultValue;
223 }
224
225
226
227
228
229
230 public static long toFileSize( final String value, final long defaultValue )
231 {
232 if ( value == null )
233 {
234 return defaultValue;
235 }
236
237 String s = value.trim().toUpperCase();
238 long multiplier = 1;
239 int index;
240
241 if ( ( index = s.indexOf( "KB" ) ) != -1 )
242 {
243 multiplier = 1024;
244 s = s.substring( 0, index );
245 }
246 else if ( ( index = s.indexOf( "MB" ) ) != -1 )
247 {
248 multiplier = 1024 * 1024;
249 s = s.substring( 0, index );
250 }
251 else if ( ( index = s.indexOf( "GB" ) ) != -1 )
252 {
253 multiplier = 1024 * 1024 * 1024;
254 s = s.substring( 0, index );
255 }
256 try
257 {
258 return Long.parseLong(s) * multiplier;
259 }
260 catch ( final NumberFormatException e )
261 {
262 log.error( "[{0}] is not in proper int form.", s);
263 log.error( "[{0}] not in expected format", value, e );
264 }
265 return defaultValue;
266 }
267
268
269
270
271
272
273
274
275
276
277 public static String findAndSubst( final String key, final Properties props )
278 {
279 final String value = props.getProperty( key );
280 if ( value == null )
281 {
282 return null;
283 }
284
285 try
286 {
287 return substVars( value, props );
288 }
289 catch ( final IllegalArgumentException e )
290 {
291 log.error( "Bad option value [{0}]", value, e );
292 return value;
293 }
294 }
295
296
297
298
299
300
301
302
303
304
305
306 public static <T> T instantiateByClassName( final String className, final T defaultValue )
307 {
308 if ( className != null )
309 {
310 try
311 {
312 final Class<?> classObj = Class.forName( className );
313 final Object o = classObj.getDeclaredConstructor().newInstance();
314
315 try
316 {
317 @SuppressWarnings("unchecked")
318 final
319 T t = (T) o;
320 return t;
321 }
322 catch (final ClassCastException e)
323 {
324 log.error( "A \"{0}\" object is not assignable to the "
325 + "generic variable.", className );
326 return defaultValue;
327 }
328 }
329 catch (final Exception e )
330 {
331 log.error( "Could not instantiate class [{0}]", className, e );
332 }
333 }
334 return defaultValue;
335 }
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375 public static String substVars( final String val, final Properties props )
376 throws IllegalArgumentException
377 {
378 final StringBuilder sbuf = new StringBuilder();
379
380 int i = 0;
381 int j;
382 int k;
383
384 while ( true )
385 {
386 j = val.indexOf( DELIM_START, i );
387 if ( j == -1 )
388 {
389 if ( i == 0 )
390 {
391 return val;
392 }
393 sbuf.append( val.substring( i ) );
394 return sbuf.toString();
395 }
396 sbuf.append(val, i, j);
397 k = val.indexOf( DELIM_STOP, j );
398 if ( k == -1 )
399 {
400 throw new IllegalArgumentException( '"' + val + "\" has no closing brace. Opening brace at position "
401 + j + '.' );
402 }
403 j += DELIM_START_LEN;
404 final String key = val.substring( j, k );
405
406 String replacement = getSystemProperty( key, null );
407
408 if ( replacement == null && props != null )
409 {
410 replacement = props.getProperty( key );
411 }
412
413 if ( replacement != null )
414 {
415 sbuf.append( replacement );
416 }
417 i = k + DELIM_STOP_LEN;
418 }
419 }
420 }