KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > logging > jdk > xml > DOMConfigurator


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.jboss.logging.jdk.xml;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.Reader JavaDoc;
23 import java.lang.reflect.Constructor JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.logging.ErrorManager JavaDoc;
28 import java.util.logging.Filter JavaDoc;
29 import java.util.logging.Handler JavaDoc;
30 import java.util.logging.Level JavaDoc;
31 import java.util.logging.LogManager JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33 import java.util.logging.Formatter JavaDoc;
34 import javax.xml.parsers.DocumentBuilder JavaDoc;
35 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
36 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
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 JavaDoc;
43 import org.w3c.dom.Element JavaDoc;
44 import org.w3c.dom.Node JavaDoc;
45 import org.w3c.dom.NodeList JavaDoc;
46 import org.xml.sax.InputSource JavaDoc;
47 import org.xml.sax.SAXException JavaDoc;
48
49 // Contributors: Mark Womack
50
// Arun Katkere
51

52 /**
53  * Use this class to initialize the log4j environment using a DOM tree.
54  * <p/>
55  * <p>The DTD is specified in <a
56  * HREF="log4j.dtd"><b>log4j.dtd</b></a>.
57  * <p/>
58  * <p>Sometimes it is useful to see how log4j is reading configuration
59  * files. You can enable log4j internal logging by defining the
60  * <b>log4j.debug</b> variable on the java command
61  * line. Alternatively, set the <code>debug</code> attribute in the
62  * <code>jdk:configuration</code> element. As in
63  * <pre>
64  * &lt;log4j:configuration <b>debug="true"</b> xmlns:log4j="http://jakarta.apache.org/log4j/">
65  * ...
66  * &lt;/log4j:configuration>
67  * </pre>
68  * <p/>
69  * <p>There are sample XML files included in the package.
70  *
71  * @author Christopher Taylor
72  * @author Ceki G&uuml;lc&uuml;
73  * @author Anders Kristensen
74  * @author Scott.Stark@jboss.org
75  * @version $Revision: 1958 $
76  */

77 public class DOMConfigurator
78 {
79
80    static final String JavaDoc CONFIGURATION_TAG = "jdk:configuration";
81    static final String JavaDoc OLD_CONFIGURATION_TAG = "configuration";
82    static final String JavaDoc RENDERER_TAG = "renderer";
83    static final String JavaDoc APPENDER_TAG = "appender";
84    static final String JavaDoc APPENDER_REF_TAG = "appender-ref";
85    static final String JavaDoc PARAM_TAG = "param";
86    static final String JavaDoc LAYOUT_TAG = "layout";
87    static final String JavaDoc CATEGORY = "category";
88    static final String JavaDoc LOGGER = "logger";
89    static final String JavaDoc LOGGER_REF = "logger-ref";
90    static final String JavaDoc CATEGORY_FACTORY_TAG = "categoryFactory";
91    static final String JavaDoc NAME_ATTR = "name";
92    static final String JavaDoc CLASS_ATTR = "class";
93    static final String JavaDoc VALUE_ATTR = "value";
94    static final String JavaDoc ROOT_TAG = "root";
95    static final String JavaDoc ROOT_REF = "root-ref";
96    static final String JavaDoc LEVEL_TAG = "level";
97    static final String JavaDoc PRIORITY_TAG = "priority";
98    static final String JavaDoc FILTER_TAG = "filter";
99    static final String JavaDoc ERROR_HANDLER_TAG = "errorHandler";
100    static final String JavaDoc REF_ATTR = "ref";
101    static final String JavaDoc ADDITIVITY_ATTR = "additivity";
102    static final String JavaDoc THRESHOLD_ATTR = "threshold";
103    static final String JavaDoc CONFIG_DEBUG_ATTR = "configDebug";
104    static final String JavaDoc INTERNAL_DEBUG_ATTR = "debug";
105    static final String JavaDoc RENDERING_CLASS_ATTR = "renderingClass";
106    static final String JavaDoc RENDERED_CLASS_ATTR = "renderedClass";
107
108    static final String JavaDoc EMPTY_STR = "";
109    static final Class JavaDoc[] ONE_STRING_PARAM = new Class JavaDoc[]{String JavaDoc.class};
110
111    final static String JavaDoc dbfKey = "javax.xml.parsers.DocumentBuilderFactory";
112
113
114    // key: appenderName, value: appender
115
private Hashtable JavaDoc appenderBag;
116    private ErrorManager JavaDoc errorLog;
117    private Properties JavaDoc props;
118    private LogManager JavaDoc repository;
119    private boolean debug;
120
121    /**
122     * Configure jdk using a <code>configuration</code> element as
123     * defined in the jdk.dtd.
124     */

125    static public void configure(Element JavaDoc element)
126    {
127       DOMConfigurator configurator = new DOMConfigurator();
128       configurator.doConfigure(element, LogManager.getLogManager());
129    }
130
131    /**
132     * A static version of {@link #doConfigure(String, LogManager)}.
133     */

134    static public void configure(String JavaDoc filename)
135       throws FactoryConfigurationError JavaDoc
136    {
137       new DOMConfigurator().doConfigure(filename,
138          LogManager.getLogManager());
139    }
140
141    /**
142     * A static version of {@link #doConfigure(java.net.URL, LogManager)}.
143     */

144    static public void configure(URL JavaDoc url)
145       throws FactoryConfigurationError JavaDoc
146    {
147       new DOMConfigurator().doConfigure(url, LogManager.getLogManager());
148    }
149
150    /**
151     * A static version of {@link #doConfigure(java.net.URL, LogManager)}.
152     */

153    static public void configure(InputStream JavaDoc is)
154       throws FactoryConfigurationError JavaDoc
155    {
156       new DOMConfigurator().doConfigure(is, LogManager.getLogManager());
157    }
158
159    /**
160     * No argument constructor.
161     */

162    public DOMConfigurator()
163    {
164       this(new ErrorManager JavaDoc());
165    }
166
167    public DOMConfigurator(ErrorManager JavaDoc errorLog)
168    {
169       appenderBag = new Hashtable JavaDoc();
170       this.errorLog = errorLog;
171    }
172
173    /**
174     * Used internally to parse appenders by IDREF name.
175     */

176    protected Handler JavaDoc findHandlerByName(Document JavaDoc doc, String JavaDoc appenderName)
177    {
178       Handler JavaDoc appender = (Handler JavaDoc) appenderBag.get(appenderName);
179
180       if (appender != null)
181       {
182          return appender;
183       }
184       else
185       {
186          // Doesn't work on DOM Level 1 :
187
Element JavaDoc 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    /**
204     * Used internally to parse appenders by IDREF element.
205     */

206    protected Handler JavaDoc findHandlerByReference(Element JavaDoc appenderRef)
207    {
208       String JavaDoc appenderName = subst(appenderRef.getAttribute(REF_ATTR));
209       Document JavaDoc doc = appenderRef.getOwnerDocument();
210       return findHandlerByName(doc, appenderName);
211    }
212
213    /**
214     * Used internally to parse an appender element.
215     */

216    protected Handler JavaDoc parseHandler(Element JavaDoc appenderElement)
217    {
218       String JavaDoc className = subst(appenderElement.getAttribute(CLASS_ATTR));
219       debug("Class name: [" + className + ']');
220       try
221       {
222          Object JavaDoc instance = instantiateByClassName(className, Handler JavaDoc.class, null);
223          Handler JavaDoc appender = (Handler JavaDoc) instance;
224          Properties JavaDoc beanProps = new Properties JavaDoc();
225          String JavaDoc 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 JavaDoc children = appenderElement.getChildNodes();
234          final int length = children.getLength();
235
236          for (int loop = 0; loop < length; loop++)
237          {
238             Node JavaDoc currentNode = children.item(loop);
239
240             /* We're only interested in Elements */
241             if (currentNode.getNodeType() == Node.ELEMENT_NODE)
242             {
243                Element JavaDoc currentElement = (Element JavaDoc) currentNode;
244
245                // Parse appender parameters
246
if (currentElement.getTagName().equals(PARAM_TAG))
247                {
248                   setParameter(currentElement, beanProps);
249                }
250                // Set appender layout
251
else if (currentElement.getTagName().equals(LAYOUT_TAG))
252                {
253                   Formatter JavaDoc format = parseLayout(currentElement);
254                   appender.setFormatter(format);
255                }
256                // Add filters
257
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 JavaDoc 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       /* Yes, it's ugly, but all of these exceptions point to the same
281       problem: we can't create an Handler
282       */

283       catch (Exception JavaDoc 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    /**
292     * Used internally to parse an {@link ErrorManager} element.
293     */

294    protected void parseErrorManager(Element JavaDoc element, Handler JavaDoc appender)
295       throws Exception JavaDoc
296    {
297       String JavaDoc className = subst(element.getAttribute(CLASS_ATTR));
298       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
299       Class JavaDoc ehClazz = loader.loadClass(className);
300       ErrorManager JavaDoc eh = (ErrorManager JavaDoc) ehClazz.newInstance();
301       appender.setErrorManager(eh);
302    }
303
304    /**
305     * Used internally to parse a filter element.
306     */

307    protected void parseFilters(Element JavaDoc element, Handler JavaDoc appender)
308       throws Exception JavaDoc
309    {
310       String JavaDoc clazz = subst(element.getAttribute(CLASS_ATTR));
311       Filter JavaDoc filter = (Filter JavaDoc) instantiateByClassName(clazz,
312          Filter JavaDoc.class, null);
313
314       if (filter != null)
315       {
316          Properties JavaDoc beanProps = new Properties JavaDoc();
317          NodeList JavaDoc children = element.getChildNodes();
318          final int length = children.getLength();
319
320          for (int loop = 0; loop < length; loop++)
321          {
322             Node JavaDoc currentNode = children.item(loop);
323             if (currentNode.getNodeType() == Node.ELEMENT_NODE)
324             {
325                Element JavaDoc currentElement = (Element JavaDoc) currentNode;
326                String JavaDoc 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    /**
341     * Used internally to parse an category element.
342     */

343    protected void parseCategory(Element JavaDoc loggerElement)
344       throws Exception JavaDoc
345    {
346       // Create a new org.apache.jdk.Category object from the <category> element.
347
String JavaDoc catName = subst(loggerElement.getAttribute(NAME_ATTR));
348
349       Logger JavaDoc logger;
350
351       String JavaDoc 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 JavaDoc loader = Thread.currentThread().getContextClassLoader();
370             Class JavaDoc c = loader.loadClass(className);
371             Class JavaDoc[] sig = {String JavaDoc.class, String JavaDoc.class};
372             Constructor JavaDoc ctor = c.getConstructor(sig);
373             Object JavaDoc[] args = {catName, null};
374             logger = (Logger JavaDoc) ctor.newInstance(args);
375          }
376          catch (Exception JavaDoc oops)
377          {
378             errorLog.error("Could not retrieve category [" + catName +
379                "]. Reported error follows.", oops, ErrorManager.GENERIC_FAILURE);
380             return;
381          }
382       }
383
384       // Setting up a category needs to be an atomic operation, in order
385
// to protect potential log operations while category
386
// configuration is in progress.
387
synchronized (logger)
388       {
389          String JavaDoc 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    /**
399     * Used internally to parse the category factory element.
400     */

401    protected void parseCategoryFactory(Element JavaDoc factoryElement)
402       throws Exception JavaDoc
403    {
404       String JavaDoc 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 JavaDoc catFactory = instantiateByClassName(className,
415             Object JavaDoc.class,
416             null);
417          Properties JavaDoc beanProps = new Properties JavaDoc();
418
419          Element JavaDoc currentElement = null;
420          Node JavaDoc currentNode = null;
421          NodeList JavaDoc 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 JavaDoc) 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    /**
442     * Used internally to parse the root category element.
443     */

444    protected void parseRoot(Element JavaDoc rootElement)
445       throws Exception JavaDoc
446    {
447       Logger JavaDoc root = repository.getLogger("");
448       if( root == null )
449       {
450          root = Logger.getLogger("");
451          repository.addLogger(root);
452       }
453       // category configuration needs to be atomic
454
synchronized (root)
455       {
456          parseChildrenOfLoggerElement(rootElement, root, true);
457       }
458    }
459
460
461    /**
462     * Used internally to parse the children of a category element.
463     */

464    protected void parseChildrenOfLoggerElement(Element JavaDoc catElement,
465       Logger JavaDoc logger, boolean isRoot)
466       throws Exception JavaDoc
467    {
468       Properties JavaDoc beanProps = new Properties JavaDoc();
469
470       // Remove all existing appenders from logger. They will be
471
// reconstructed if need be.
472
Handler JavaDoc[] handlers = logger.getHandlers();
473       for(int n = 0; n < handlers.length; n ++)
474       {
475          Handler JavaDoc h = handlers[n];
476          logger.removeHandler(h);
477       }
478
479       NodeList JavaDoc children = catElement.getChildNodes();
480       final int length = children.getLength();
481
482       for (int loop = 0; loop < length; loop++)
483       {
484          Node JavaDoc currentNode = children.item(loop);
485
486          if (currentNode.getNodeType() == Node.ELEMENT_NODE)
487          {
488             Element JavaDoc currentElement = (Element JavaDoc) currentNode;
489             String JavaDoc tagName = currentElement.getTagName();
490
491             if (tagName.equals(APPENDER_REF_TAG))
492             {
493                Element JavaDoc appenderRef = (Element JavaDoc) currentNode;
494                Handler JavaDoc appender = findHandlerByReference(appenderRef);
495                String JavaDoc 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    /**
523     * Used internally to parse a layout element.
524     */

525    protected Formatter JavaDoc parseLayout(Element JavaDoc layout_element)
526    {
527       String JavaDoc className = subst(layout_element.getAttribute(CLASS_ATTR));
528       debug("Parsing layout of class: \"" + className + "\"");
529       try
530       {
531          Object JavaDoc instance = instantiateByClassName(className, Formatter JavaDoc.class, null);
532          Formatter JavaDoc layout = (Formatter JavaDoc) instance;
533          Properties JavaDoc beanProps = new Properties JavaDoc();
534
535          NodeList JavaDoc params = layout_element.getChildNodes();
536          final int length = params.getLength();
537
538          for (int loop = 0; loop < length; loop++)
539          {
540             Node JavaDoc currentNode = params.item(loop);
541             if (currentNode.getNodeType() == Node.ELEMENT_NODE)
542             {
543                Element JavaDoc currentElement = (Element JavaDoc) currentNode;
544                String JavaDoc 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 JavaDoc 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 JavaDoc element)
563    {
564       String JavaDoc renderingClass = subst(element.getAttribute(RENDERING_CLASS_ATTR));
565       String JavaDoc renderedClass = subst(element.getAttribute(RENDERED_CLASS_ATTR));
566    }
567
568    /**
569     * Used internally to parse a level element.
570     */

571    protected void parseLevel(Element JavaDoc element, Logger JavaDoc logger, boolean isRoot)
572    {
573       String JavaDoc catName = logger.getName();
574       if (isRoot)
575       {
576          catName = "root";
577       }
578
579       String JavaDoc levelName = subst(element.getAttribute(VALUE_ATTR));
580       // Check for a jdk level name
581
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 JavaDoc className = subst(element.getAttribute(CLASS_ATTR));
599          if (EMPTY_STR.equals(className))
600          {
601             Level JavaDoc level = Level.parse(levelName);
602             logger.setLevel(level);
603          }
604          else if( className.equals("org.jboss.logging.XLevel") )
605          {
606             // Special handling of the jboss XLevel
607
logger.setLevel(Level.FINER);
608          }
609          else
610          {
611             debug("Desired Level sub-class: [" + className + ']');
612             try
613             {
614                ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
615                Class JavaDoc clazz = loader.loadClass(className);
616                Class JavaDoc[] sig = {String JavaDoc.class, int.class};
617                Object JavaDoc[] args = {levelName, new Integer JavaDoc(Level.FINEST.intValue())};
618                Constructor JavaDoc ctor = clazz.getConstructor(sig);
619                Level JavaDoc pri = (Level JavaDoc) ctor.newInstance(args);
620                logger.setLevel(pri);
621             }
622             catch (Exception JavaDoc 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 JavaDoc elem, Properties JavaDoc beanProps)
634    {
635       String JavaDoc name = subst(elem.getAttribute(NAME_ATTR));
636       String JavaDoc 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 JavaDoc parse(final DocumentBuilder JavaDoc parser) throws SAXException JavaDoc, IOException JavaDoc;
645    }
646
647
648    public void doConfigure(final String JavaDoc filename, LogManager JavaDoc repository)
649    {
650       ParseAction action = new ParseAction()
651       {
652          public Document JavaDoc parse(final DocumentBuilder JavaDoc parser) throws SAXException JavaDoc, IOException JavaDoc
653          {
654             return parser.parse(new File JavaDoc(filename));
655          }
656
657          public String JavaDoc toString()
658          {
659             return "file [" + filename + "]";
660          }
661       };
662       doConfigure(action, repository);
663    }
664
665
666    public void doConfigure(final URL JavaDoc url, LogManager JavaDoc repository)
667    {
668       ParseAction action = new ParseAction()
669       {
670          public Document JavaDoc parse(final DocumentBuilder JavaDoc parser) throws SAXException JavaDoc, IOException JavaDoc
671          {
672             return parser.parse(url.toString());
673          }
674
675          public String JavaDoc toString()
676          {
677             return "url [" + url.toString() + "]";
678          }
679       };
680       doConfigure(action, repository);
681    }
682
683    /**
684     * Configure jdk by reading in a jdk.dtd compliant XML
685     * configuration file.
686     */

687    public void doConfigure(final InputStream JavaDoc inputStream, LogManager JavaDoc repository)
688       throws FactoryConfigurationError JavaDoc
689    {
690       ParseAction action = new ParseAction()
691       {
692          public Document JavaDoc parse(final DocumentBuilder JavaDoc parser) throws SAXException JavaDoc, IOException JavaDoc
693          {
694             InputSource JavaDoc inputSource = new InputSource JavaDoc(inputStream);
695             inputSource.setSystemId("dummy://jdk.dtd");
696             return parser.parse(inputSource);
697          }
698
699          public String JavaDoc toString()
700          {
701             return "input stream [" + inputStream.toString() + "]";
702          }
703       };
704       doConfigure(action, repository);
705    }
706
707    /**
708     * Configure jdk by reading in a jdk.dtd compliant XML
709     * configuration file.
710     */

711    public void doConfigure(final Reader JavaDoc reader, LogManager JavaDoc repository)
712       throws FactoryConfigurationError JavaDoc
713    {
714       ParseAction action = new ParseAction()
715       {
716          public Document JavaDoc parse(final DocumentBuilder JavaDoc parser) throws SAXException JavaDoc, IOException JavaDoc
717          {
718             InputSource JavaDoc inputSource = new InputSource JavaDoc(reader);
719             inputSource.setSystemId("dummy://jdk.dtd");
720             return parser.parse(inputSource);
721          }
722
723          public String JavaDoc toString()
724          {
725             return "reader [" + reader.toString() + "]";
726          }
727       };
728       doConfigure(action, repository);
729    }
730
731    /**
732     * Configure jdk by reading in a jdk.dtd compliant XML
733     * configuration file.
734     */

735    protected void doConfigure(final InputSource JavaDoc inputSource, LogManager JavaDoc repository)
736       throws FactoryConfigurationError JavaDoc
737    {
738       if (inputSource.getSystemId() == null)
739       {
740          inputSource.setSystemId("dummy://jdk.dtd");
741       }
742       ParseAction action = new ParseAction()
743       {
744          public Document JavaDoc parse(final DocumentBuilder JavaDoc parser) throws SAXException JavaDoc, IOException JavaDoc
745          {
746             return parser.parse(inputSource);
747          }
748
749          public String JavaDoc 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 JavaDoc repository)
759       throws FactoryConfigurationError JavaDoc
760    {
761       DocumentBuilderFactory JavaDoc 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 JavaDoc fce)
770       {
771          Exception JavaDoc 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 JavaDoc docBuilder = dbf.newDocumentBuilder();
781          JBossEntityResolver resolver = new JBossEntityResolver();
782          resolver.registerLocalEntity("urn:jboss:jdklogger.dtd", "jdklogger.dtd");
783          docBuilder.setEntityResolver(resolver);
784
785          Document JavaDoc doc = action.parse(docBuilder);
786          parse(doc.getDocumentElement());
787       }
788       catch (Exception JavaDoc e)
789       {
790          // I know this is miserable...
791
errorLog.error("Could not parse " + action.toString() + ".", e, ErrorManager.GENERIC_FAILURE);
792       }
793    }
794
795    /**
796     * Configure by taking in an DOM element.
797     */

798    public void doConfigure(Element JavaDoc element, LogManager JavaDoc repository)
799    {
800       this.repository = repository;
801       parse(element);
802    }
803
804    /**
805     * Used internally to configure the jdk framework by parsing a DOM
806     * tree of XML elements based on <a
807     * HREF="doc-files/jdk.dtd">jdk.dtd</a>.
808     */

809    protected void parse(Element JavaDoc element)
810    {
811
812       String JavaDoc 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 JavaDoc debugAttrib = subst(element.getAttribute(INTERNAL_DEBUG_ATTR));
832
833       debug("debug attribute= '" + debugAttrib + "'.");
834       // if the jdk.dtd is not specified in the XML file, then the
835
// "debug" attribute is returned as the empty string.
836
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 JavaDoc confDebug = subst(element.getAttribute(CONFIG_DEBUG_ATTR));
847       if (!confDebug.equals("") && !confDebug.equals("null"))
848       {
849          debug = true;
850       }
851
852       String JavaDoc thresholdStr = subst(element.getAttribute(THRESHOLD_ATTR));
853       debug("Threshold ='" + thresholdStr + "'.");
854       if (!"".equals(thresholdStr) && !"null".equals(thresholdStr))
855       {
856          Level JavaDoc threshold = Level.parse(thresholdStr);
857          Logger JavaDoc root = repository.getLogger("");
858          root.setLevel(threshold);
859       }
860
861       // First configure each category factory under the root element.
862
// Category factories need to be configured before any of
863
// categories they support.
864
//
865
String JavaDoc tagName = null;
866       Element JavaDoc currentElement = null;
867       Node JavaDoc currentNode = null;
868       NodeList JavaDoc 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 JavaDoc) currentNode;
877             tagName = currentElement.getTagName();
878
879             if (tagName.equals(CATEGORY_FACTORY_TAG))
880             {
881                try
882                {
883                   parseCategoryFactory(currentElement);
884                }
885                catch(Exception JavaDoc 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 JavaDoc) 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 JavaDoc e)
917             {
918                errorLog.error("Failed to parse element: "+tagName, e, ErrorManager.GENERIC_FAILURE);
919             }
920          }
921       }
922    }
923
924
925    protected String JavaDoc subst(String JavaDoc value)
926    {
927       if( value == null )
928          return null;
929
930       try
931       {
932          return StringPropertyReplacer.replaceProperties(value);
933       }
934       catch (Exception JavaDoc e)
935       {
936          errorLog.error("Could not perform variable substitution.", e, ErrorManager.GENERIC_FAILURE);
937          return value;
938       }
939    }
940
941    static String JavaDoc mapLog4jLevel(String JavaDoc name)
942    {
943       String JavaDoc 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 JavaDoc convertSpecialChars(String JavaDoc s)
964    {
965      char c;
966      int len = s.length();
967      StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(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 JavaDoc msg)
989    {
990       if( debug )
991          System.out.println(msg);
992    }
993    Object JavaDoc instantiateByClassName(String JavaDoc className, Class JavaDoc superClass,
994       Object JavaDoc defaultValue)
995    {
996       if (className != null)
997       {
998          try
999          {
1000            ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
1001            Class JavaDoc 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 JavaDoc 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