1 16 17 package org.apache.log4j.helpers; 18 19 import java.util.Properties ; 20 import java.net.URL ; 21 import org.apache.log4j.Level; 22 import org.apache.log4j.spi.Configurator; 23 import org.apache.log4j.spi.LoggerRepository; 24 import org.apache.log4j.PropertyConfigurator; 25 26 30 37 public class OptionConverter { 38 39 static String DELIM_START = "${"; 40 static char DELIM_STOP = '}'; 41 static int DELIM_START_LEN = 2; 42 static int DELIM_STOP_LEN = 1; 43 44 45 private OptionConverter() {} 46 47 public 48 static 49 String [] concatanateArrays(String [] l, String [] r) { 50 int len = l.length + r.length; 51 String [] a = new String [len]; 52 53 System.arraycopy(l, 0, a, 0, l.length); 54 System.arraycopy(r, 0, a, l.length, r.length); 55 56 return a; 57 } 58 59 public 60 static 61 String convertSpecialChars(String s) { 62 char c; 63 int len = s.length(); 64 StringBuffer sbuf = new StringBuffer (len); 65 66 int i = 0; 67 while(i < len) { 68 c = s.charAt(i++); 69 if (c == '\\') { 70 c = s.charAt(i++); 71 if(c == 'n') c = '\n'; 72 else if(c == 'r') c = '\r'; 73 else if(c == 't') c = '\t'; 74 else if(c == 'f') c = '\f'; 75 else if(c == '\b') c = '\b'; 76 else if(c == '\"') c = '\"'; 77 else if(c == '\'') c = '\''; 78 else if(c == '\\') c = '\\'; 79 } 80 sbuf.append(c); 81 } 82 return sbuf.toString(); 83 } 84 85 86 96 public 97 static 98 String getSystemProperty(String key, String def) { 99 try { 100 return System.getProperty(key, def); 101 } catch(Throwable e) { LogLog.debug("Was not allowed to read system property \""+key+"\"."); 103 return def; 104 } 105 } 106 107 108 public 109 static 110 Object instantiateByKey(Properties props, String key, Class superClass, 111 Object defaultValue) { 112 113 String className = findAndSubst(key, props); 115 if(className == null) { 116 LogLog.error("Could not find value for key " + key); 117 return defaultValue; 118 } 119 return OptionConverter.instantiateByClassName(className.trim(), superClass, 121 defaultValue); 122 } 123 124 131 public 132 static 133 boolean toBoolean(String value, boolean dEfault) { 134 if(value == null) 135 return dEfault; 136 String trimmedVal = value.trim(); 137 if("true".equalsIgnoreCase(trimmedVal)) 138 return true; 139 if("false".equalsIgnoreCase(trimmedVal)) 140 return false; 141 return dEfault; 142 } 143 144 public 145 static 146 int toInt(String value, int dEfault) { 147 if(value != null) { 148 String s = value.trim(); 149 try { 150 return Integer.valueOf(s).intValue(); 151 } 152 catch (NumberFormatException e) { 153 LogLog.error("[" + s + "] is not in proper int form."); 154 e.printStackTrace(); 155 } 156 } 157 return dEfault; 158 } 159 160 180 public 181 static 182 Level toLevel(String value, Level defaultValue) { 183 if(value == null) 184 return defaultValue; 185 186 int hashIndex = value.indexOf('#'); 187 if (hashIndex == -1) { 188 if("NULL".equalsIgnoreCase(value)) { 189 return null; 190 } else { 191 return(Level) Level.toLevel(value, defaultValue); 193 } 194 } 195 196 Level result = defaultValue; 197 198 String clazz = value.substring(hashIndex+1); 199 String levelName = value.substring(0, hashIndex); 200 201 if("NULL".equalsIgnoreCase(levelName)) { 203 return null; 204 } 205 206 LogLog.debug("toLevel" + ":class=[" + clazz + "]" 207 + ":pri=[" + levelName + "]"); 208 209 try { 210 Class customLevel = Loader.loadClass(clazz); 211 212 Class [] paramTypes = new Class [] { String .class, 215 org.apache.log4j.Level.class 216 }; 217 java.lang.reflect.Method toLevelMethod = 218 customLevel.getMethod("toLevel", paramTypes); 219 220 Object [] params = new Object [] {levelName, defaultValue}; 222 Object o = toLevelMethod.invoke(null, params); 223 224 result = (Level) o; 225 } catch(ClassNotFoundException e) { 226 LogLog.warn("custom level class [" + clazz + "] not found."); 227 } catch(NoSuchMethodException e) { 228 LogLog.warn("custom level class [" + clazz + "]" 229 + " does not have a constructor which takes one string parameter", e); 230 } catch(java.lang.reflect.InvocationTargetException e) { 231 LogLog.warn("custom level class [" + clazz + "]" 232 + " could not be instantiated", e); 233 } catch(ClassCastException e) { 234 LogLog.warn("class [" + clazz 235 + "] is not a subclass of org.apache.log4j.Level", e); 236 } catch(IllegalAccessException e) { 237 LogLog.warn("class ["+clazz+ 238 "] cannot be instantiated due to access restrictions", e); 239 } catch(Exception e) { 240 LogLog.warn("class ["+clazz+"], level ["+levelName+ 241 "] conversion failed.", e); 242 } 243 return result; 244 } 245 246 public 247 static 248 long toFileSize(String value, long dEfault) { 249 if(value == null) 250 return dEfault; 251 252 String s = value.trim().toUpperCase(); 253 long multiplier = 1; 254 int index; 255 256 if((index = s.indexOf("KB")) != -1) { 257 multiplier = 1024; 258 s = s.substring(0, index); 259 } 260 else if((index = s.indexOf("MB")) != -1) { 261 multiplier = 1024*1024; 262 s = s.substring(0, index); 263 } 264 else if((index = s.indexOf("GB")) != -1) { 265 multiplier = 1024*1024*1024; 266 s = s.substring(0, index); 267 } 268 if(s != null) { 269 try { 270 return Long.valueOf(s).longValue() * multiplier; 271 } 272 catch (NumberFormatException e) { 273 LogLog.error("[" + s + "] is not in proper int form."); 274 LogLog.error("[" + value + "] not in expected format.", e); 275 } 276 } 277 return dEfault; 278 } 279 280 286 public 287 static 288 String findAndSubst(String key, Properties props) { 289 String value = props.getProperty(key); 290 if(value == null) 291 return null; 292 293 try { 294 return substVars(value, props); 295 } catch(IllegalArgumentException e) { 296 LogLog.error("Bad option value ["+value+"].", e); 297 return value; 298 } 299 } 300 301 311 public 312 static 313 Object instantiateByClassName(String className, Class superClass, 314 Object defaultValue) { 315 if(className != null) { 316 try { 317 Class classObj = Loader.loadClass(className); 318 if(!superClass.isAssignableFrom(classObj)) { 319 LogLog.error("A \""+className+"\" object is not assignable to a \""+ 320 superClass.getName() + "\" variable."); 321 LogLog.error("The class \""+ superClass.getName()+"\" was loaded by "); 322 LogLog.error("["+superClass.getClassLoader()+"] whereas object of type "); 323 LogLog.error("\"" +classObj.getName()+"\" was loaded by [" 324 +classObj.getClassLoader()+"]."); 325 return defaultValue; 326 } 327 return classObj.newInstance(); 328 } catch (Exception e) { 329 LogLog.error("Could not instantiate class [" + className + "].", e); 330 } 331 } 332 return defaultValue; 333 } 334 335 336 372 public static 373 String substVars(String val, Properties props) throws 374 IllegalArgumentException { 375 376 StringBuffer sbuf = new StringBuffer (); 377 378 int i = 0; 379 int j, k; 380 381 while(true) { 382 j=val.indexOf(DELIM_START, i); 383 if(j == -1) { 384 if(i==0) { return val; 387 } else { sbuf.append(val.substring(i, val.length())); 389 return sbuf.toString(); 390 } 391 } else { 392 sbuf.append(val.substring(i, j)); 393 k = val.indexOf(DELIM_STOP, j); 394 if(k == -1) { 395 throw new IllegalArgumentException ('"'+val+ 396 "\" has no closing brace. Opening brace at position " + j 397 + '.'); 398 } else { 399 j += DELIM_START_LEN; 400 String key = val.substring(j, k); 401 String replacement = getSystemProperty(key, null); 403 if(replacement == null && props != null) { 405 replacement = props.getProperty(key); 406 } 407 408 if(replacement != null) { 409 String recursiveReplacement = substVars(replacement, props); 415 sbuf.append(recursiveReplacement); 416 } 417 i = k + DELIM_STOP_LEN; 418 } 419 } 420 } 421 } 422 423 424 444 445 static 446 public 447 void selectAndConfigure(URL url, String clazz, LoggerRepository hierarchy) { 448 Configurator configurator = null; 449 String filename = url.getFile(); 450 451 if(clazz == null && filename != null && filename.endsWith(".xml")) { 452 clazz = "org.apache.log4j.xml.DOMConfigurator"; 453 } 454 455 if(clazz != null) { 456 LogLog.debug("Preferred configurator class: " + clazz); 457 configurator = (Configurator) instantiateByClassName(clazz, 458 Configurator.class, 459 null); 460 if(configurator == null) { 461 LogLog.error("Could not instantiate configurator ["+clazz+"]."); 462 return; 463 } 464 } else { 465 configurator = new PropertyConfigurator(); 466 } 467 468 configurator.doConfigure(url, hierarchy); 469 } 470 } 471 | Popular Tags |