1 18 19 package org.objectweb.util.monolog.file.monolog; 20 21 import org.objectweb.util.monolog.api.Handler; 22 import org.objectweb.util.monolog.api.HandlerFactory; 23 import org.objectweb.util.monolog.api.Level; 24 import org.objectweb.util.monolog.api.LevelFactory; 25 import org.objectweb.util.monolog.api.Logger; 26 import org.objectweb.util.monolog.api.LoggerFactory; 27 import org.objectweb.util.monolog.api.TopicalLogger; 28 import org.objectweb.util.monolog.api.BasicLevel; 29 import org.objectweb.util.monolog.api.MonologFactory; 30 import org.objectweb.util.monolog.file.DottedStringTools; 31 import org.objectweb.util.monolog.wrapper.common.LevelImpl; 32 33 import java.io.PrintStream ; 34 import java.io.PrintWriter ; 35 import java.io.Serializable ; 36 import java.util.Enumeration ; 37 import java.util.Properties ; 38 import java.util.StringTokenizer ; 39 import java.util.Vector ; 40 41 56 public class PropertiesConfAccess implements Serializable { 57 58 61 private static final long serialVersionUID = -321110630195680214L; 62 63 public final static String LOGGER_FIELD = "logger"; 64 public final static String ACTIVATION = "activation"; 65 public final static String ADDITIVITY_FIELD = "additivity"; 66 public final static String USE_PARENT_FIELD = "useParent"; 67 public final static String HANDLER_FIELD = "handler"; 68 public final static String LEVEL_FIELD = "level"; 69 public final static String TOPIC_FIELD = "topic"; 70 public final static String CLEAN_HANDLERS_FIELD = "cleanHandlers"; 71 public final static char DOT = '.'; 72 73 public final static String HANDLER_TYPE_ATTRIBUTE = "type"; 74 75 public final static String HANDLER_TYPE_ATTRIBUTE_FILE_VALUE = "file"; 76 public final static String HANDLER_TYPE_ATTRIBUTE_CONSOLE_VALUE = "console"; 77 public final static String HANDLER_TYPE_ATTRIBUTE_ROLLING_FILE_VALUE = "rollingfile"; 78 public final static String HANDLER_TYPE_ATTRIBUTE_NTEVENT_VALUE = "ntevent"; 79 public final static String HANDLER_TYPE_ATTRIBUTE_JMX_VALUE = "jmx"; 80 81 public static boolean debug = new Boolean ( 82 System.getProperty("monolog.debug")).booleanValue(); 83 84 public static void load(Properties prop, LoggerFactory lof, HandlerFactory hf, 85 LevelFactory lef) throws Exception { 86 new PropertiesConfAccess().read(prop, lof, hf, lef); 87 } 88 89 public static void load(Properties prop, MonologFactory mf) throws Exception { 90 new PropertiesConfAccess().read(prop, mf, mf, mf); 91 } 92 93 public static void store(Properties prop, LoggerFactory lof, HandlerFactory hf, 94 LevelFactory lef) throws Exception { 95 new PropertiesConfAccess().write(prop, lof, hf, lef); 96 } 97 98 public static void store(Properties prop, MonologFactory mf) throws Exception { 99 new PropertiesConfAccess().write(prop, mf, mf, mf); 100 } 101 102 public void read(Properties prop, LoggerFactory lof, HandlerFactory hf, 103 LevelFactory lef) throws Exception { 104 read(prop, (MonologFactory) lof); 105 } 106 public void read(Properties prop, MonologFactory mf) throws Exception { 107 for (Enumeration e = prop.keys(); e.hasMoreElements();) { 110 String key = ((String ) e.nextElement()); 111 if (key.startsWith(LOGGER_FIELD + DOT) 112 && key.endsWith(DOT + CLEAN_HANDLERS_FIELD) 113 && Boolean.getBoolean(prop.getProperty(key).trim())) { 114 String loggerName = key.substring( 115 LOGGER_FIELD.length() + 1, 116 key.length() - 1 - CLEAN_HANDLERS_FIELD.length()); 117 ((TopicalLogger) mf.getLogger(loggerName)).removeAllHandlers(); 118 } 119 } 120 for (Enumeration en = prop.keys(); en.hasMoreElements();) { 122 String key = (String ) en.nextElement(); 123 if (key.startsWith(LOGGER_FIELD + DOT)) { 124 parseLoggerProp(prop, key, mf); 125 } else if (key.startsWith(LEVEL_FIELD + DOT)) { 126 parseLevelProp(prop, key, mf); 127 } 128 } 129 Vector hs = new Vector (); for (Enumeration en = prop.keys(); en.hasMoreElements();) { 132 String key = (String ) en.nextElement(); 133 if (key.startsWith(HANDLER_FIELD + DOT)) { 134 String handlerName = DottedStringTools.getBegin( 135 DottedStringTools.getEnd(key)); 136 Handler h = mf.getHandler(handlerName); 137 if (h != null && !hs.contains(h)) { 138 hs.addElement(h); 140 } parseHandlerProp(prop, key, mf); 142 } 143 } 144 for (int i = 0; i < hs.size(); i++) { 146 ((Handler) hs.elementAt(i)).setAttribute("activation", mf); 147 } 148 } 149 150 public void write(Properties prop, LoggerFactory lof, HandlerFactory hf, 151 LevelFactory lef) throws Exception { 152 153 String key = null; 155 String val = null; 156 Level[] levels = lef.getLevels(); 157 for (int i = 0; i < levels.length; i++) { 158 if (!isDefaultLevel(levels[i])) { 159 key = LEVEL_FIELD + DOT + levels[i].getName(); 160 val = ((LevelImpl) levels[i]).getStringValue(); 161 debug(key + " " + val); 162 prop.put(key, val.trim()); 163 } 164 } 165 166 Handler[] handlers = hf.getHandlers(); 168 for (int i = 0; i < handlers.length; i++) { 169 key = HANDLER_FIELD + DOT + handlers[i].getName() + DOT + HANDLER_TYPE_ATTRIBUTE; 170 val = handlers[i].getType(); 171 debug(key + " " + val); 172 prop.put(key, val); 173 String [] ats = handlers[i].getAttributeNames(); 174 for (int j = 0; j < ats.length; j++) { 175 key = HANDLER_FIELD + DOT + handlers[i].getName() + DOT + ats[j]; 176 Object o = handlers[i].getAttribute(ats[j]); 177 if (o instanceof String ) { 178 val = (String ) o; 179 debug(key + " " + val); 180 prop.put(key, val.trim()); 181 } 182 } 183 } 184 185 TopicalLogger[] loggers = (TopicalLogger[]) lof.getLoggers(); 187 final String topicPrefix = lof.getTopicPrefix(); 188 for (int i = 0; i < loggers.length; i++) { 189 String [] topics = loggers[i].getTopic(); 190 if (topics.length == 0) { 191 throw new Exception ( 192 "Impossible to set the definition of a logger without name"); 193 } 194 String topic = loggers[i].getTopic()[0]; 195 if (topicPrefix != null && topic.startsWith(topicPrefix)) { 196 topic = topic.substring(topicPrefix.length()); 197 } 198 String begin = LOGGER_FIELD + DOT + topic; 199 200 if (!loggers[i].getAdditivity()) { 201 key = begin + DOT + ADDITIVITY_FIELD; 202 val = "false"; 203 debug(key + " " + val); 204 prop.put(key, val.trim()); 205 } 206 207 Level level = loggers[i].getCurrentLevel(); 208 if (level != null && level.getIntValue() != BasicLevel.INHERIT) { 209 key = begin + DOT + LEVEL_FIELD; 210 val = " " + level.getName(); 211 debug(key + " " + val); 212 prop.put(key, val.trim()); 213 } 214 215 handlers = loggers[i].getHandler(); 216 if (handlers != null && handlers.length > 0) { 217 StringBuffer sb = new StringBuffer (); 218 String sep = ""; 219 for (int j = 0; j < handlers.length; j++) { 220 if (handlers[j].getName() != null) { 221 sb.append(sep); 222 sep = ", "; 223 sb.append(handlers[j].getName()); 224 } 225 } 226 debug(begin + DOT + HANDLER_FIELD + " " + sb.toString()); 227 prop.put(begin + DOT + HANDLER_FIELD, sb.toString()); 228 } 229 230 if (topics.length > 1) { 231 StringBuffer sb = new StringBuffer (); 232 String sep = ""; 233 for (int j = 1; j < topics.length; j++) { 234 sb.append(sep); 235 sep = ", "; 236 String ntopic = topics[j]; 237 if (topicPrefix != null && ntopic.startsWith(topicPrefix)) { 238 ntopic = ntopic.substring(topicPrefix.length()); 239 } 240 sb.append(ntopic); 241 } 242 debug(begin + DOT + TOPIC_FIELD + " " + sb.toString()); 243 prop.put(begin + DOT + TOPIC_FIELD, sb.toString()); 244 } 245 } 246 } 247 248 259 protected boolean isDefaultLevel(Level l) { 260 return (l.getName().equalsIgnoreCase("DEBUG") 261 || l.getName().equalsIgnoreCase("INFO") 262 || l.getName().equalsIgnoreCase("WARN") 263 || l.getName().equalsIgnoreCase("ERROR") 264 || l.getName().equalsIgnoreCase("FATAL") 265 || l.getName().equalsIgnoreCase("INHERIT") 266 ); 267 } 268 269 272 285 protected Logger parseLoggerProp(Properties prop, 286 String key, 287 LoggerFactory lof, 288 HandlerFactory hf, 289 LevelFactory lef) throws Exception { 290 return parseLoggerProp(prop, key, (MonologFactory) lof); 291 } 292 301 protected Logger parseLoggerProp(Properties prop, 302 String key, 303 MonologFactory mf) throws Exception { 304 if (prop == null 305 || key == null 306 || mf == null) { 307 throw new Exception ("The null parameters are not allowed"); 308 } 309 310 String temp = DottedStringTools.getFirst(key); 311 if (temp == null || !temp.equalsIgnoreCase(LOGGER_FIELD)) { 312 throw new Exception ( 313 "This key is not a Logger property:" + key); 314 } 315 316 TopicalLogger logger = null; 317 temp = DottedStringTools.getEnd(key); 318 String last = DottedStringTools.getLast(temp); 319 if (last.equalsIgnoreCase(LEVEL_FIELD)) { 320 logger = (TopicalLogger) 322 mf.getLogger(DottedStringTools.getBegin(temp)); 323 String levelName = prop.getProperty(key).trim(); 324 Level l = mf.getLevel(levelName); 325 if (l == null) { 326 l = parseLevelProp(prop, LEVEL_FIELD + DOT + levelName, mf); 327 } 328 if (debug) { 329 debug("set level to " + l.getName() + " to the logger " + logger.getName()); 330 } 331 logger.setLevel(l); 332 } else if (ADDITIVITY_FIELD.equalsIgnoreCase(last) 333 || USE_PARENT_FIELD.equalsIgnoreCase(last)) { 334 logger = (TopicalLogger) 335 mf.getLogger(DottedStringTools.getBegin(temp)); 336 boolean a = Boolean.getBoolean(prop.getProperty(key).trim()); 337 if (debug) { 338 debug("set additivity to " + a + " to the logger " + logger.getName()); 339 } 340 logger.setAdditivity(a); 341 } else if (CLEAN_HANDLERS_FIELD.equalsIgnoreCase(last)) { 342 } else if (HANDLER_FIELD.equalsIgnoreCase(last)) { 344 String value = prop.getProperty(key).trim(); 345 StringTokenizer st = new StringTokenizer (value, ", ;:", false); 346 logger = (TopicalLogger) 347 mf.getLogger(DottedStringTools.getBegin(temp)); 348 cleanOldHandler(logger); 349 while(st.hasMoreTokens()) { 350 String hn = st.nextToken(); 351 Handler h = mf.getHandler(hn); 352 if (h == null) { 353 h = parseHandlerProp(prop, HANDLER_FIELD + DOT + value 354 + DOT + HANDLER_TYPE_ATTRIBUTE, mf); 355 } 356 if (debug) { 357 debug("add handler " + h.getName() + " to the logger " + logger.getName()); 358 } 359 logger.addHandler(h); 360 } 361 } else if (TOPIC_FIELD.equalsIgnoreCase(last)) { 362 logger = (TopicalLogger) 363 mf.getLogger(DottedStringTools.getBegin(temp)); 364 String value = prop.getProperty(key).trim(); 365 StringTokenizer st = new StringTokenizer (value, ", ;:", false); 366 cleanOldHandler(logger); 367 while(st.hasMoreTokens()) { 368 String topic = st.nextToken(); 369 if (debug) { 370 debug("add topic " + topic + " to the logger " + logger.getName()); 371 } 372 logger.addTopic(topic); 373 } 374 } else { temp = DottedStringTools.getBegin(temp); last = DottedStringTools.getLast(temp); temp = DottedStringTools.getBegin(temp); logger = (TopicalLogger) mf.getLogger(temp); 379 if (last.equalsIgnoreCase(HANDLER_FIELD)) { 380 String value = prop.getProperty(key).trim(); 382 Handler h = mf.getHandler(value); 383 if (h == null) { 384 h = parseHandlerProp(prop, HANDLER_FIELD + DOT + value 385 + DOT + HANDLER_TYPE_ATTRIBUTE, mf); 386 } 387 cleanOldHandler(logger); 388 if (debug) { 389 debug("add handler " + h.getName() + " to the logger " + logger.getName()); 390 } 391 logger.addHandler(h); 392 } else if (last.equalsIgnoreCase(TOPIC_FIELD)) { 393 String topic = prop.getProperty(key).trim(); 395 if (debug) { 396 debug("add topic " + topic + " to the logger " + logger.getName()); 397 } 398 logger.addTopic(topic); 399 } else { 400 throw new Exception ( 401 "Unknown definition" + key + " " + prop.getProperty(key)); 402 } 403 } 404 return logger; 405 } 406 407 416 protected Handler parseHandlerProp(Properties prop, 417 String key, 418 HandlerFactory hf) throws Exception { 419 420 if (prop == null 421 || key == null 422 || hf == null) { 423 throw new Exception ("The null parameters are not allowed"); 424 } 425 String temp = DottedStringTools.getFirst(key); 426 if (temp == null || !temp.equalsIgnoreCase(HANDLER_FIELD)) { 427 throw new Exception ( 428 "This key is not a Handler field:" + key); 429 } 430 temp = DottedStringTools.getEnd(key); 431 String attribute = DottedStringTools.getLast(temp); 432 String handlerName = DottedStringTools.getBegin(temp); 433 Handler hc = hf.getHandler(handlerName); 434 if (hc == null) { 435 String stringType = prop.getProperty(HANDLER_FIELD + DOT + handlerName 436 + DOT + HANDLER_TYPE_ATTRIBUTE, null); 437 if (stringType == null) { 438 throw new Exception ("Impossible to define the handler " 439 + temp + ": the type is not defined"); 440 } 441 hc = hf.createHandler(handlerName, stringType); 442 if (hc == null) { 443 throw new Exception ( 444 "The HandlerFactory does not create the Handler: name=" 445 + handlerName + " / type=" + stringType); 446 } 447 } 448 if (hc != null && !HANDLER_TYPE_ATTRIBUTE.equalsIgnoreCase(attribute)) { 449 String v = prop.getProperty(key).trim(); 450 if (debug) { 451 debug("assign property (" + attribute + ", " + v 452 + ") to the handler '" + handlerName); 453 } 454 hc.setAttribute(attribute, v); 455 } 456 return hc; 457 } 458 459 469 protected Level parseLevelProp(Properties prop, 470 String key, 471 LevelFactory lef) throws Exception { 472 return parseLevelProp(prop, key, lef, new Vector ()); 473 } 474 475 486 protected Level parseLevelProp(Properties prop, 487 String key, 488 LevelFactory lef, 489 Vector currentLevelParse) throws Exception { 490 if (prop == null 491 || key == null 492 || lef == null) { 493 throw new Exception ("The null parameters are not allowed"); 494 } 495 String temp = DottedStringTools.getFirst(key); 496 if (temp == null || !temp.equalsIgnoreCase(LEVEL_FIELD)) 497 throw new Exception ("This key is not a level field:" + key); 498 499 temp = DottedStringTools.getEnd(key); 500 Level l = lef.getLevel(temp); 501 if (l == null) { 502 if (!prop.containsKey(key)) { 503 throw new Exception ("Level definition not found: " + key); 504 } 505 if (currentLevelParse.contains(temp)) { 506 throw new Exception ("Circular level definition: " + temp); 507 } 508 String levelValue = prop.getProperty(key).trim(); 509 String [] depends = getDependsLevel(levelValue); 510 if (depends.length > 0) { 511 currentLevelParse.addElement(temp); 512 for (int i = 0; i < depends.length; i++) { 513 parseLevelProp(prop, LEVEL_FIELD + DOT + depends[i], 514 lef, currentLevelParse); 515 } 516 currentLevelParse.removeElement(temp); 517 } 518 l = lef.defineLevel(temp, levelValue); 519 } 520 return l; 521 } 522 523 530 protected String [] getDependsLevel(String expr) { 531 Vector res = new Vector (); 532 for (StringTokenizer st = new StringTokenizer (expr, " +-", true); 533 st.hasMoreTokens();) { 534 String elem = st.nextToken(); 535 if (Character.isLetter(elem.charAt(0))) { 536 res.addElement(elem); 537 } 538 } 539 String [] r = new String [res.size()]; 540 res.copyInto(r); 541 return r; 542 } 543 544 protected Vector loggerCleaned = null; 545 546 550 protected void cleanOldHandler(TopicalLogger l) throws Exception { 551 if (loggerCleaned == null) { 552 loggerCleaned = new Vector (); 553 } 554 if (!loggerCleaned.contains(l)) { 555 loggerCleaned.addElement(l); 556 l.removeAllHandlers(); 557 } 558 } 559 560 563 class NestedException extends Exception { 564 565 568 private static final long serialVersionUID = -1218233947556357710L; 569 570 Exception nestedException = null; 571 572 public NestedException() { 573 super(); 574 } 575 576 public NestedException(Exception e) { 577 super(); 578 nestedException = e; 579 } 580 581 public NestedException(Exception e, String msg) { 582 super(msg); 583 nestedException = e; 584 } 585 586 public Exception getNestedException() { 587 return nestedException; 588 } 589 590 public void printStackTrace(PrintStream ps) { 591 if (nestedException != null) { 592 ps.println("Nested: " + getMessage()); 593 nestedException.printStackTrace(ps); 594 } else { 595 super.printStackTrace(ps); 596 } 597 } 598 599 public void printStackTrace(PrintWriter w) { 600 if (nestedException != null) { 601 w.println("Nested: " + getMessage()); 602 nestedException.printStackTrace(w); 603 } else { 604 super.printStackTrace(w); 605 } 606 } 607 608 public void printStackTrace() { 609 printStackTrace(new PrintStream (System.out)); 610 } 611 612 } 613 614 public void debug(String m) { 615 if (debug) { 616 System.out.println(m); 617 } 618 } 619 } 620 | Popular Tags |