1 16 17 package org.jboss.logging.jdk.xml; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.Reader ; 23 import java.lang.reflect.Constructor ; 24 import java.net.URL ; 25 import java.util.Hashtable ; 26 import java.util.Properties ; 27 import java.util.logging.ErrorManager ; 28 import java.util.logging.Filter ; 29 import java.util.logging.Handler ; 30 import java.util.logging.Level ; 31 import java.util.logging.LogManager ; 32 import java.util.logging.Logger ; 33 import java.util.logging.Formatter ; 34 import javax.xml.parsers.DocumentBuilder ; 35 import javax.xml.parsers.DocumentBuilderFactory ; 36 import javax.xml.parsers.FactoryConfigurationError ; 37 38 import org.jboss.util.propertyeditor.PropertyEditors; 39 import org.jboss.util.StringPropertyReplacer; 40 import org.jboss.util.xml.JBossEntityResolver; 41 import org.jboss.logging.jdk.handlers.HandlerSkeleton; 42 import org.w3c.dom.Document ; 43 import org.w3c.dom.Element ; 44 import org.w3c.dom.Node ; 45 import org.w3c.dom.NodeList ; 46 import org.xml.sax.InputSource ; 47 import org.xml.sax.SAXException ; 48 49 52 77 public class DOMConfigurator 78 { 79 80 static final String CONFIGURATION_TAG = "jdk:configuration"; 81 static final String OLD_CONFIGURATION_TAG = "configuration"; 82 static final String RENDERER_TAG = "renderer"; 83 static final String APPENDER_TAG = "appender"; 84 static final String APPENDER_REF_TAG = "appender-ref"; 85 static final String PARAM_TAG = "param"; 86 static final String LAYOUT_TAG = "layout"; 87 static final String CATEGORY = "category"; 88 static final String LOGGER = "logger"; 89 static final String LOGGER_REF = "logger-ref"; 90 static final String CATEGORY_FACTORY_TAG = "categoryFactory"; 91 static final String NAME_ATTR = "name"; 92 static final String CLASS_ATTR = "class"; 93 static final String VALUE_ATTR = "value"; 94 static final String ROOT_TAG = "root"; 95 static final String ROOT_REF = "root-ref"; 96 static final String LEVEL_TAG = "level"; 97 static final String PRIORITY_TAG = "priority"; 98 static final String FILTER_TAG = "filter"; 99 static final String ERROR_HANDLER_TAG = "errorHandler"; 100 static final String REF_ATTR = "ref"; 101 static final String ADDITIVITY_ATTR = "additivity"; 102 static final String THRESHOLD_ATTR = "threshold"; 103 static final String CONFIG_DEBUG_ATTR = "configDebug"; 104 static final String INTERNAL_DEBUG_ATTR = "debug"; 105 static final String RENDERING_CLASS_ATTR = "renderingClass"; 106 static final String RENDERED_CLASS_ATTR = "renderedClass"; 107 108 static final String EMPTY_STR = ""; 109 static final Class [] ONE_STRING_PARAM = new Class []{String .class}; 110 111 final static String dbfKey = "javax.xml.parsers.DocumentBuilderFactory"; 112 113 114 private Hashtable appenderBag; 116 private ErrorManager errorLog; 117 private Properties props; 118 private LogManager repository; 119 private boolean debug; 120 121 125 static public void configure(Element element) 126 { 127 DOMConfigurator configurator = new DOMConfigurator(); 128 configurator.doConfigure(element, LogManager.getLogManager()); 129 } 130 131 134 static public void configure(String filename) 135 throws FactoryConfigurationError 136 { 137 new DOMConfigurator().doConfigure(filename, 138 LogManager.getLogManager()); 139 } 140 141 144 static public void configure(URL url) 145 throws FactoryConfigurationError 146 { 147 new DOMConfigurator().doConfigure(url, LogManager.getLogManager()); 148 } 149 150 153 static public void configure(InputStream is) 154 throws FactoryConfigurationError 155 { 156 new DOMConfigurator().doConfigure(is, LogManager.getLogManager()); 157 } 158 159 162 public DOMConfigurator() 163 { 164 this(new ErrorManager ()); 165 } 166 167 public DOMConfigurator(ErrorManager errorLog) 168 { 169 appenderBag = new Hashtable (); 170 this.errorLog = errorLog; 171 } 172 173 176 protected Handler findHandlerByName(Document doc, String appenderName) 177 { 178 Handler appender = (Handler ) appenderBag.get(appenderName); 179 180 if (appender != null) 181 { 182 return appender; 183 } 184 else 185 { 186 Element element = doc.getElementById(appenderName); 188 if (element == null) 189 { 190 errorLog.error("No appender named [" + appenderName + "] could be found.", 191 null, ErrorManager.GENERIC_FAILURE); 192 return null; 193 } 194 else 195 { 196 appender = parseHandler(element); 197 appenderBag.put(appenderName, appender); 198 return appender; 199 } 200 } 201 } 202 203 206 protected Handler findHandlerByReference(Element appenderRef) 207 { 208 String appenderName = subst(appenderRef.getAttribute(REF_ATTR)); 209 Document doc = appenderRef.getOwnerDocument(); 210 return findHandlerByName(doc, appenderName); 211 } 212 213 216 protected Handler parseHandler(Element appenderElement) 217 { 218 String className = subst(appenderElement.getAttribute(CLASS_ATTR)); 219 debug("Class name: [" + className + ']'); 220 try 221 { 222 Object instance = instantiateByClassName(className, Handler .class, null); 223 Handler appender = (Handler ) instance; 224 Properties beanProps = new Properties (); 225 String name = subst(appenderElement.getAttribute(NAME_ATTR)); 226 HandlerSkeleton handlerSkeleton = null; 227 if( appender instanceof HandlerSkeleton ) 228 { 229 handlerSkeleton = (HandlerSkeleton) appender; 230 handlerSkeleton.setName(name); 231 } 232 233 NodeList children = appenderElement.getChildNodes(); 234 final int length = children.getLength(); 235 236 for (int loop = 0; loop < length; loop++) 237 { 238 Node currentNode = children.item(loop); 239 240 241 if (currentNode.getNodeType() == Node.ELEMENT_NODE) 242 { 243 Element currentElement = (Element ) currentNode; 244 245 if (currentElement.getTagName().equals(PARAM_TAG)) 247 { 248 setParameter(currentElement, beanProps); 249 } 250 else if (currentElement.getTagName().equals(LAYOUT_TAG)) 252 { 253 Formatter format = parseLayout(currentElement); 254 appender.setFormatter(format); 255 } 256 else if (currentElement.getTagName().equals(FILTER_TAG)) 258 { 259 parseFilters(currentElement, appender); 260 } 261 else if (currentElement.getTagName().equals(ERROR_HANDLER_TAG)) 262 { 263 parseErrorManager(currentElement, appender); 264 } 265 else if (currentElement.getTagName().equals(APPENDER_REF_TAG)) 266 { 267 String refName = subst(currentElement.getAttribute(REF_ATTR)); 268 errorLog.error("Requesting attachment of handler named [" + 269 refName + "] to handler named [" + appender + 270 "] which does not implement org.apache.jdk.spi.HandlerAttachable.", 271 null, ErrorManager.GENERIC_FAILURE); 272 } 273 } 274 } 275 PropertyEditors.mapJavaBeanProperties(appender, beanProps); 276 if( handlerSkeleton != null ) 277 handlerSkeleton.activateOptions(); 278 return appender; 279 } 280 283 catch (Exception oops) 284 { 285 errorLog.error("Could not create an Handler. Reported error follows.", 286 oops, ErrorManager.GENERIC_FAILURE); 287 return null; 288 } 289 } 290 291 294 protected void parseErrorManager(Element element, Handler appender) 295 throws Exception 296 { 297 String className = subst(element.getAttribute(CLASS_ATTR)); 298 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 299 Class ehClazz = loader.loadClass(className); 300 ErrorManager eh = (ErrorManager ) ehClazz.newInstance(); 301 appender.setErrorManager(eh); 302 } 303 304 307 protected void parseFilters(Element element, Handler appender) 308 throws Exception 309 { 310 String clazz = subst(element.getAttribute(CLASS_ATTR)); 311 Filter filter = (Filter ) instantiateByClassName(clazz, 312 Filter .class, null); 313 314 if (filter != null) 315 { 316 Properties beanProps = new Properties (); 317 NodeList children = element.getChildNodes(); 318 final int length = children.getLength(); 319 320 for (int loop = 0; loop < length; loop++) 321 { 322 Node currentNode = children.item(loop); 323 if (currentNode.getNodeType() == Node.ELEMENT_NODE) 324 { 325 Element currentElement = (Element ) currentNode; 326 String tagName = currentElement.getTagName(); 327 if (tagName.equals(PARAM_TAG)) 328 { 329 setParameter(currentElement, beanProps); 330 } 331 } 332 } 333 PropertyEditors.mapJavaBeanProperties(filter, beanProps); 334 debug("Setting filter of type [" + filter.getClass() 335 + "] to appender named [" + appender + "]."); 336 appender.setFilter(filter); 337 } 338 } 339 340 343 protected void parseCategory(Element loggerElement) 344 throws Exception 345 { 346 String catName = subst(loggerElement.getAttribute(NAME_ATTR)); 348 349 Logger logger; 350 351 String className = subst(loggerElement.getAttribute(CLASS_ATTR)); 352 353 354 if (EMPTY_STR.equals(className)) 355 { 356 debug("Retreiving an instance of java.util.logging.Logger."); 357 logger = repository.getLogger(catName); 358 if( logger == null ) 359 { 360 logger = Logger.getLogger(catName); 361 repository.addLogger(logger); 362 } 363 } 364 else 365 { 366 debug("Desired logger sub-class: [" + className + ']'); 367 try 368 { 369 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 370 Class c = loader.loadClass(className); 371 Class [] sig = {String .class, String .class}; 372 Constructor ctor = c.getConstructor(sig); 373 Object [] args = {catName, null}; 374 logger = (Logger ) ctor.newInstance(args); 375 } 376 catch (Exception oops) 377 { 378 errorLog.error("Could not retrieve category [" + catName + 379 "]. Reported error follows.", oops, ErrorManager.GENERIC_FAILURE); 380 return; 381 } 382 } 383 384 synchronized (logger) 388 { 389 String flag = subst(loggerElement.getAttribute(ADDITIVITY_ATTR)); 390 boolean additivity = Boolean.valueOf(flag).booleanValue(); 391 debug("Setting [" + logger.getName() + "] additivity to [" + additivity + "]."); 392 logger.setUseParentHandlers(additivity); 393 parseChildrenOfLoggerElement(loggerElement, logger, false); 394 } 395 } 396 397 398 401 protected void parseCategoryFactory(Element factoryElement) 402 throws Exception 403 { 404 String className = subst(factoryElement.getAttribute(CLASS_ATTR)); 405 406 if (EMPTY_STR.equals(className)) 407 { 408 errorLog.error("Category Factory tag " + CLASS_ATTR + " attribute not found.", 409 null, ErrorManager.GENERIC_FAILURE); 410 } 411 else 412 { 413 debug("Desired category factory: [" + className + ']'); 414 Object catFactory = instantiateByClassName(className, 415 Object .class, 416 null); 417 Properties beanProps = new Properties (); 418 419 Element currentElement = null; 420 Node currentNode = null; 421 NodeList children = factoryElement.getChildNodes(); 422 final int length = children.getLength(); 423 424 for (int loop = 0; loop < length; loop++) 425 { 426 currentNode = children.item(loop); 427 if (currentNode.getNodeType() == Node.ELEMENT_NODE) 428 { 429 currentElement = (Element ) currentNode; 430 if (currentElement.getTagName().equals(PARAM_TAG)) 431 { 432 setParameter(currentElement, beanProps); 433 } 434 } 435 } 436 PropertyEditors.mapJavaBeanProperties(catFactory, beanProps); 437 } 438 } 439 440 441 444 protected void parseRoot(Element rootElement) 445 throws Exception 446 { 447 Logger root = repository.getLogger(""); 448 if( root == null ) 449 { 450 root = Logger.getLogger(""); 451 repository.addLogger(root); 452 } 453 synchronized (root) 455 { 456 parseChildrenOfLoggerElement(rootElement, root, true); 457 } 458 } 459 460 461 464 protected void parseChildrenOfLoggerElement(Element catElement, 465 Logger logger, boolean isRoot) 466 throws Exception 467 { 468 Properties beanProps = new Properties (); 469 470 Handler [] handlers = logger.getHandlers(); 473 for(int n = 0; n < handlers.length; n ++) 474 { 475 Handler h = handlers[n]; 476 logger.removeHandler(h); 477 } 478 479 NodeList children = catElement.getChildNodes(); 480 final int length = children.getLength(); 481 482 for (int loop = 0; loop < length; loop++) 483 { 484 Node currentNode = children.item(loop); 485 486 if (currentNode.getNodeType() == Node.ELEMENT_NODE) 487 { 488 Element currentElement = (Element ) currentNode; 489 String tagName = currentElement.getTagName(); 490 491 if (tagName.equals(APPENDER_REF_TAG)) 492 { 493 Element appenderRef = (Element ) currentNode; 494 Handler appender = findHandlerByReference(appenderRef); 495 String refName = subst(appenderRef.getAttribute(REF_ATTR)); 496 if (appender != null) 497 debug("Adding appender named [" + refName + 498 "] to category [" + logger.getName() + "]."); 499 else 500 debug("Handler named [" + refName + "] not found."); 501 502 logger.addHandler(appender); 503 504 } 505 else if (tagName.equals(LEVEL_TAG)) 506 { 507 parseLevel(currentElement, logger, isRoot); 508 } 509 else if (tagName.equals(PRIORITY_TAG)) 510 { 511 parseLevel(currentElement, logger, isRoot); 512 } 513 else if (tagName.equals(PARAM_TAG)) 514 { 515 setParameter(currentElement, beanProps); 516 } 517 } 518 } 519 PropertyEditors.mapJavaBeanProperties(logger, beanProps); 520 } 521 522 525 protected Formatter parseLayout(Element layout_element) 526 { 527 String className = subst(layout_element.getAttribute(CLASS_ATTR)); 528 debug("Parsing layout of class: \"" + className + "\""); 529 try 530 { 531 Object instance = instantiateByClassName(className, Formatter .class, null); 532 Formatter layout = (Formatter ) instance; 533 Properties beanProps = new Properties (); 534 535 NodeList params = layout_element.getChildNodes(); 536 final int length = params.getLength(); 537 538 for (int loop = 0; loop < length; loop++) 539 { 540 Node currentNode = params.item(loop); 541 if (currentNode.getNodeType() == Node.ELEMENT_NODE) 542 { 543 Element currentElement = (Element ) currentNode; 544 String tagName = currentElement.getTagName(); 545 if (tagName.equals(PARAM_TAG)) 546 { 547 setParameter(currentElement, beanProps); 548 } 549 } 550 } 551 PropertyEditors.mapJavaBeanProperties(layout, beanProps); 552 return layout; 553 } 554 catch (Exception oops) 555 { 556 errorLog.error("Could not create the Layout. Reported error follows.", 557 oops, ErrorManager.GENERIC_FAILURE); 558 return null; 559 } 560 } 561 562 protected void parseRenderer(Element element) 563 { 564 String renderingClass = subst(element.getAttribute(RENDERING_CLASS_ATTR)); 565 String renderedClass = subst(element.getAttribute(RENDERED_CLASS_ATTR)); 566 } 567 568 571 protected void parseLevel(Element element, Logger logger, boolean isRoot) 572 { 573 String catName = logger.getName(); 574 if (isRoot) 575 { 576 catName = "root"; 577 } 578 579 String levelName = subst(element.getAttribute(VALUE_ATTR)); 580 levelName = mapLog4jLevel(levelName); 582 debug("Level value for " + catName + " is [" + levelName + "]."); 583 584 if ("INHERITED".equalsIgnoreCase(levelName) || "NULL".equalsIgnoreCase(levelName)) 585 { 586 if (isRoot) 587 { 588 errorLog.error("Root level cannot be inherited. Ignoring directive.", 589 null, ErrorManager.GENERIC_FAILURE); 590 } 591 else 592 { 593 logger.setLevel(null); 594 } 595 } 596 else 597 { 598 String className = subst(element.getAttribute(CLASS_ATTR)); 599 if (EMPTY_STR.equals(className)) 600 { 601 Level level = Level.parse(levelName); 602 logger.setLevel(level); 603 } 604 else if( className.equals("org.jboss.logging.XLevel") ) 605 { 606 logger.setLevel(Level.FINER); 608 } 609 else 610 { 611 debug("Desired Level sub-class: [" + className + ']'); 612 try 613 { 614 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 615 Class clazz = loader.loadClass(className); 616 Class [] sig = {String .class, int.class}; 617 Object [] args = {levelName, new Integer (Level.FINEST.intValue())}; 618 Constructor ctor = clazz.getConstructor(sig); 619 Level pri = (Level ) ctor.newInstance(args); 620 logger.setLevel(pri); 621 } 622 catch (Exception oops) 623 { 624 errorLog.error("Could not create level [" + levelName + 625 "]. Reported error follows.", oops, ErrorManager.GENERIC_FAILURE); 626 return; 627 } 628 } 629 } 630 debug(catName + " level set to " + logger.getLevel()); 631 } 632 633 protected void setParameter(Element elem, Properties beanProps) 634 { 635 String name = subst(elem.getAttribute(NAME_ATTR)); 636 String value = (elem.getAttribute(VALUE_ATTR)); 637 value = subst(convertSpecialChars(value)); 638 beanProps.setProperty(name, value); 639 } 640 641 642 private interface ParseAction 643 { 644 Document parse(final DocumentBuilder parser) throws SAXException , IOException ; 645 } 646 647 648 public void doConfigure(final String filename, LogManager repository) 649 { 650 ParseAction action = new ParseAction() 651 { 652 public Document parse(final DocumentBuilder parser) throws SAXException , IOException 653 { 654 return parser.parse(new File (filename)); 655 } 656 657 public String toString() 658 { 659 return "file [" + filename + "]"; 660 } 661 }; 662 doConfigure(action, repository); 663 } 664 665 666 public void doConfigure(final URL url, LogManager repository) 667 { 668 ParseAction action = new ParseAction() 669 { 670 public Document parse(final DocumentBuilder parser) throws SAXException , IOException 671 { 672 return parser.parse(url.toString()); 673 } 674 675 public String toString() 676 { 677 return "url [" + url.toString() + "]"; 678 } 679 }; 680 doConfigure(action, repository); 681 } 682 683 687 public void doConfigure(final InputStream inputStream, LogManager repository) 688 throws FactoryConfigurationError 689 { 690 ParseAction action = new ParseAction() 691 { 692 public Document parse(final DocumentBuilder parser) throws SAXException , IOException 693 { 694 InputSource inputSource = new InputSource (inputStream); 695 inputSource.setSystemId("dummy://jdk.dtd"); 696 return parser.parse(inputSource); 697 } 698 699 public String toString() 700 { 701 return "input stream [" + inputStream.toString() + "]"; 702 } 703 }; 704 doConfigure(action, repository); 705 } 706 707 711 public void doConfigure(final Reader reader, LogManager repository) 712 throws FactoryConfigurationError 713 { 714 ParseAction action = new ParseAction() 715 { 716 public Document parse(final DocumentBuilder parser) throws SAXException , IOException 717 { 718 InputSource inputSource = new InputSource (reader); 719 inputSource.setSystemId("dummy://jdk.dtd"); 720 return parser.parse(inputSource); 721 } 722 723 public String toString() 724 { 725 return "reader [" + reader.toString() + "]"; 726 } 727 }; 728 doConfigure(action, repository); 729 } 730 731 735 protected void doConfigure(final InputSource inputSource, LogManager repository) 736 throws FactoryConfigurationError 737 { 738 if (inputSource.getSystemId() == null) 739 { 740 inputSource.setSystemId("dummy://jdk.dtd"); 741 } 742 ParseAction action = new ParseAction() 743 { 744 public Document parse(final DocumentBuilder parser) throws SAXException , IOException 745 { 746 return parser.parse(inputSource); 747 } 748 749 public String toString() 750 { 751 return "input source [" + inputSource.toString() + "]"; 752 } 753 }; 754 doConfigure(action, repository); 755 } 756 757 758 private final void doConfigure(final ParseAction action, final LogManager repository) 759 throws FactoryConfigurationError 760 { 761 DocumentBuilderFactory dbf = null; 762 this.repository = repository; 763 try 764 { 765 dbf = DocumentBuilderFactory.newInstance(); 766 debug("Standard DocumentBuilderFactory search succeded."); 767 debug("DocumentBuilderFactory is: " + dbf.getClass().getName()); 768 } 769 catch (FactoryConfigurationError fce) 770 { 771 Exception e = fce.getException(); 772 errorLog.error("Could not instantiate a DocumentBuilderFactory.", e, ErrorManager.GENERIC_FAILURE); 773 throw fce; 774 } 775 776 try 777 { 778 dbf.setValidating(true); 779 780 DocumentBuilder docBuilder = dbf.newDocumentBuilder(); 781 JBossEntityResolver resolver = new JBossEntityResolver(); 782 resolver.registerLocalEntity("urn:jboss:jdklogger.dtd", "jdklogger.dtd"); 783 docBuilder.setEntityResolver(resolver); 784 785 Document doc = action.parse(docBuilder); 786 parse(doc.getDocumentElement()); 787 } 788 catch (Exception e) 789 { 790 errorLog.error("Could not parse " + action.toString() + ".", e, ErrorManager.GENERIC_FAILURE); 792 } 793 } 794 795 798 public void doConfigure(Element element, LogManager repository) 799 { 800 this.repository = repository; 801 parse(element); 802 } 803 804 809 protected void parse(Element element) 810 { 811 812 String rootElementName = element.getTagName(); 813 814 if (!rootElementName.equals(CONFIGURATION_TAG)) 815 { 816 if (rootElementName.equals(OLD_CONFIGURATION_TAG)) 817 { 818 errorLog.error("The <" + OLD_CONFIGURATION_TAG + 819 "> element has been deprecated." 820 + ", use the <" + CONFIGURATION_TAG + "> element instead.", null, 821 ErrorManager.GENERIC_FAILURE); 822 } 823 else 824 { 825 errorLog.error("DOM element is - not a <" + CONFIGURATION_TAG + "> element.", null, 826 ErrorManager.GENERIC_FAILURE); 827 return; 828 } 829 } 830 831 String debugAttrib = subst(element.getAttribute(INTERNAL_DEBUG_ATTR)); 832 833 debug("debug attribute= '" + debugAttrib + "'."); 834 if (!debugAttrib.equals("") && !debugAttrib.equals("null")) 837 { 838 debug = Boolean.valueOf(debugAttrib).booleanValue(); 839 } 840 else 841 { 842 debug("Ignoring " + INTERNAL_DEBUG_ATTR + " attribute."); 843 } 844 845 846 String confDebug = subst(element.getAttribute(CONFIG_DEBUG_ATTR)); 847 if (!confDebug.equals("") && !confDebug.equals("null")) 848 { 849 debug = true; 850 } 851 852 String thresholdStr = subst(element.getAttribute(THRESHOLD_ATTR)); 853 debug("Threshold ='" + thresholdStr + "'."); 854 if (!"".equals(thresholdStr) && !"null".equals(thresholdStr)) 855 { 856 Level threshold = Level.parse(thresholdStr); 857 Logger root = repository.getLogger(""); 858 root.setLevel(threshold); 859 } 860 861 String tagName = null; 866 Element currentElement = null; 867 Node currentNode = null; 868 NodeList children = element.getChildNodes(); 869 final int length = children.getLength(); 870 871 for (int loop = 0; loop < length; loop++) 872 { 873 currentNode = children.item(loop); 874 if (currentNode.getNodeType() == Node.ELEMENT_NODE) 875 { 876 currentElement = (Element ) currentNode; 877 tagName = currentElement.getTagName(); 878 879 if (tagName.equals(CATEGORY_FACTORY_TAG)) 880 { 881 try 882 { 883 parseCategoryFactory(currentElement); 884 } 885 catch(Exception e) 886 { 887 errorLog.error("Failed to parse: "+tagName, e, ErrorManager.GENERIC_FAILURE); 888 } 889 } 890 } 891 } 892 893 for (int loop = 0; loop < length; loop++) 894 { 895 currentNode = children.item(loop); 896 if (currentNode.getNodeType() == Node.ELEMENT_NODE) 897 { 898 currentElement = (Element ) currentNode; 899 tagName = currentElement.getTagName(); 900 901 try 902 { 903 if (tagName.equals(CATEGORY) || tagName.equals(LOGGER)) 904 { 905 parseCategory(currentElement); 906 } 907 else if (tagName.equals(ROOT_TAG)) 908 { 909 parseRoot(currentElement); 910 } 911 else if (tagName.equals(RENDERER_TAG)) 912 { 913 parseRenderer(currentElement); 914 } 915 } 916 catch(Exception e) 917 { 918 errorLog.error("Failed to parse element: "+tagName, e, ErrorManager.GENERIC_FAILURE); 919 } 920 } 921 } 922 } 923 924 925 protected String subst(String value) 926 { 927 if( value == null ) 928 return null; 929 930 try 931 { 932 return StringPropertyReplacer.replaceProperties(value); 933 } 934 catch (Exception e) 935 { 936 errorLog.error("Could not perform variable substitution.", e, ErrorManager.GENERIC_FAILURE); 937 return value; 938 } 939 } 940 941 static String mapLog4jLevel(String name) 942 { 943 String jdkName = null; 944 if( name.equals("OFF") ) 945 jdkName = Level.OFF.getName(); 946 else if( name.equals("FATAL") ) 947 jdkName = Level.SEVERE.getName(); 948 else if( name.equals("ERROR") ) 949 jdkName = Level.WARNING.getName(); 950 else if( name.equals("ERROR") ) 951 jdkName = Level.WARNING.getName(); 952 else if( name.equals("WARN") ) 953 jdkName = Level.WARNING.getName(); 954 else if( name.equals("INFO") ) 955 jdkName = Level.INFO.getName(); 956 else if( name.equals("DEBUG") ) 957 jdkName = Level.FINE.getName(); 958 else if( name.equals("TRACE") ) 959 jdkName = Level.FINER.getName(); 960 return jdkName; 961 } 962 963 static String convertSpecialChars(String s) 964 { 965 char c; 966 int len = s.length(); 967 StringBuffer sbuf = new StringBuffer (len); 968 969 int i = 0; 970 while(i < len) { 971 c = s.charAt(i++); 972 if (c == '\\') { 973 c = s.charAt(i++); 974 if(c == 'n') c = '\n'; 975 else if(c == 'r') c = '\r'; 976 else if(c == 't') c = '\t'; 977 else if(c == 'f') c = '\f'; 978 else if(c == '\b') c = '\b'; 979 else if(c == '\"') c = '\"'; 980 else if(c == '\'') c = '\''; 981 else if(c == '\\') c = '\\'; 982 } 983 sbuf.append(c); 984 } 985 return sbuf.toString(); 986 } 987 988 protected void debug(String msg) 989 { 990 if( debug ) 991 System.out.println(msg); 992 } 993 Object instantiateByClassName(String className, Class superClass, 994 Object defaultValue) 995 { 996 if (className != null) 997 { 998 try 999 { 1000 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 1001 Class classObj = loader.loadClass(className); 1002 if (!superClass.isAssignableFrom(classObj)) 1003 { 1004 errorLog.error("A \"" + className + "\" object is not assignable to a \"" + 1005 superClass.getName() + "\" variable." 1006 +"The class \"" + superClass.getName() + "\" was loaded by " 1007 +"[" + superClass.getClassLoader() + "] whereas object of type " 1008 +"'" + classObj.getName() + "\" was loaded by [" 1009 + classObj.getClassLoader() + "].", null, ErrorManager.GENERIC_FAILURE); 1010 return defaultValue; 1011 } 1012 return classObj.newInstance(); 1013 } 1014 catch (Exception e) 1015 { 1016 errorLog.error("Could not instantiate class [" + className + "].", e, ErrorManager.GENERIC_FAILURE); 1017 } 1018 } 1019 return defaultValue; 1020 } 1021 1022} 1023 | Popular Tags |