Clover coverage report - brownies library - 1.0-beta-1
Coverage timestamp: 月 8 16 2004 17:14:42 GMT+09:00
file stats: LOC: 184   Methods: 7
NCLOC: 126   Classes: 2
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ServletDOMConfigurator.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  * Joey and its relative products are published under the terms
 3   
  * of the Apache Software License.
 4   
  */
 5   
 /*
 6   
  * Created on 2003/12/27
 7   
  */
 8   
 package org.asyrinx.brownie.log.log4j.servlet;
 9   
 
 10   
 import javax.servlet.ServletContext;
 11   
 
 12   
 import org.apache.log4j.Appender;
 13   
 import org.apache.log4j.LogManager;
 14   
 import org.apache.log4j.config.PropertySetter;
 15   
 import org.apache.log4j.helpers.FileWatchdog;
 16   
 import org.apache.log4j.helpers.Loader;
 17   
 import org.apache.log4j.helpers.LogLog;
 18   
 import org.apache.log4j.spi.AppenderAttachable;
 19   
 import org.apache.log4j.xml.DOMConfigurator;
 20   
 import org.asyrinx.brownie.servlet.FileNameResolver;
 21   
 import org.w3c.dom.Element;
 22   
 import org.w3c.dom.Node;
 23   
 import org.w3c.dom.NodeList;
 24   
 
 25   
 /**
 26   
  * @author akima
 27   
  */
 28   
 public class ServletDOMConfigurator extends DOMConfigurator {
 29   
 
 30   
     /**
 31   
      *  
 32   
      */
 33  0
     public ServletDOMConfigurator(ServletContext servletContext) {
 34  0
         super();
 35  0
         if (servletContext == null)
 36  0
             throw new RuntimeException("servletContext == null");
 37  0
         this.resolver = new FileNameResolver(servletContext);
 38   
     }
 39   
 
 40   
     protected final FileNameResolver resolver;
 41   
 
 42  0
     protected Appender parseAppender(Element appenderElement) {
 43  0
         String className = subst(appenderElement.getAttribute("class"));
 44  0
         LogLog.debug("Class name: [" + className + ']');
 45  0
         try {
 46  0
             Object instance = Loader.loadClass(className).newInstance();
 47  0
             Appender appender = (Appender) instance;
 48  0
             PropertySetter propSetter = new PropertySetter(appender);
 49  0
             appender.setName(subst(appenderElement.getAttribute("name")));
 50  0
             NodeList children = appenderElement.getChildNodes();
 51  0
             int length = children.getLength();
 52  0
             for (int loop = 0; loop < length; loop++) {
 53  0
                 Node currentNode = children.item(loop);
 54  0
                 if (currentNode.getNodeType() == 1) {
 55  0
                     Element currentElement = (Element) currentNode;
 56  0
                     if (currentElement.getTagName().equals("param")) {
 57   
 
 58   
                         //convert from relative file path to real path.
 59  0
                         if ("file".equalsIgnoreCase(currentElement
 60   
                                 .getAttribute("name"))) {
 61  0
                             currentElement.setAttribute("value", this.resolver
 62   
                                     .toRealPath(currentElement
 63   
                                             .getAttribute("value")));
 64   
                         }
 65   
 
 66  0
                         setParameter(currentElement, propSetter);
 67  0
                     } else if (currentElement.getTagName().equals("layout")) {
 68  0
                         appender.setLayout(parseLayout(currentElement));
 69  0
                     } else if (currentElement.getTagName().equals("filter")) {
 70  0
                         parseFilters(currentElement, appender);
 71  0
                     } else if (currentElement.getTagName().equals(
 72   
                             "errorHandler")) {
 73  0
                         parseErrorHandler(currentElement, appender);
 74  0
                     } else if (currentElement.getTagName().equals(
 75   
                             "appender-ref")) {
 76  0
                         String refName = subst(currentElement
 77   
                                 .getAttribute("ref"));
 78  0
                         if (appender instanceof AppenderAttachable) {
 79  0
                             AppenderAttachable aa = (AppenderAttachable) appender;
 80  0
                             LogLog.debug("Attaching appender named [" + refName
 81   
                                     + "] to appender named ["
 82   
                                     + appender.getName() + "].");
 83  0
                             aa
 84   
                                     .addAppender(findAppenderByReference(currentElement));
 85   
                         } else {
 86  0
                             LogLog
 87   
                                     .error("Requesting attachment of appender named ["
 88   
                                             + refName
 89   
                                             + "] to appender named ["
 90   
                                             + appender.getName()
 91   
                                             + "] which does not implement org.apache.log4j.spi.AppenderAttachable.");
 92   
                         }
 93   
                     }
 94   
                 }
 95   
             }
 96   
 
 97  0
             propSetter.activate();
 98  0
             return appender;
 99   
         } catch (Exception oops) {
 100  0
             LogLog.error(
 101   
                     "Could not create an Appender. Reported error follows.",
 102   
                     oops);
 103   
         }
 104  0
         return null;
 105   
     }
 106   
 
 107  0
     static public void configure(String configFilename,
 108   
             ServletContext servletContext) {
 109  0
         if (servletContext == null)
 110  0
             throw new RuntimeException("servletContext == null");
 111  0
         servletContext.log(ServletDOMConfigurator.class.getName()
 112   
                 + "#configure(\"" + configFilename + "\")");
 113  0
         ServletDOMConfigurator configurator = new ServletDOMConfigurator(
 114   
                 servletContext);
 115  0
         configurator.doConfigure(configFilename, LogManager
 116   
                 .getLoggerRepository());
 117   
     }
 118   
 
 119   
     /**
 120   
      * Like {@link #configureAndWatch(String, long)}except that the default
 121   
      * delay as defined by {@link FileWatchdog#DEFAULT_DELAY}is used.
 122   
      * 
 123   
      * @param configFilename
 124   
      *            A log4j configuration file in XML format.
 125   
      *  
 126   
      */
 127  0
     static public void configureAndWatch(String configFilename,
 128   
             ServletContext servletContext) {
 129  0
         if (servletContext == null)
 130  0
             throw new RuntimeException("servletContext == null");
 131  0
         configureAndWatch(configFilename, FileWatchdog.DEFAULT_DELAY,
 132   
                 servletContext);
 133   
     }
 134   
 
 135   
     /**
 136   
      * Read the configuration file <code>configFilename</code> if it exists.
 137   
      * Moreover, a thread will be created that will periodically check if
 138   
      * <code>configFilename</code> has been created or modified. The period is
 139   
      * determined by the <code>delay</code> argument. If a change or file
 140   
      * creation is detected, then <code>configFilename</code> is read to
 141   
      * configure log4j.
 142   
      * 
 143   
      * @param configFilename
 144   
      *            A log4j configuration file in XML format.
 145   
      * @param delay
 146   
      *            The delay in milliseconds to wait between each check.
 147   
      */
 148  0
     static public void configureAndWatch(String configFilename, long delay,
 149   
             ServletContext servletContext) {
 150  0
         if (servletContext == null)
 151  0
             throw new RuntimeException("servletContext == null");
 152  0
         servletContext.log(ServletDOMConfigurator.class.getName()
 153   
                 + "#configureAndWatch(\"" + configFilename + "\")");
 154  0
         ServletXmlWatchdog xdog = new ServletXmlWatchdog(configFilename,
 155   
                 servletContext);
 156  0
         xdog.setDelay(delay);
 157  0
         xdog.start();
 158   
     }
 159   
 
 160   
 }
 161   
 
 162   
 class ServletXmlWatchdog extends FileWatchdog {
 163   
 
 164  0
     ServletXmlWatchdog(String filename, ServletContext servletContext) {
 165  0
         super(filename);
 166  0
         if (servletContext == null)
 167  0
             throw new RuntimeException("servletContext == null");
 168  0
         this.servletContext = servletContext;
 169   
     }
 170   
 
 171   
     final ServletContext servletContext;
 172   
 
 173   
     /**
 174   
      * Call {@link PropertyConfigurator#configure(String)}with the
 175   
      * <code>filename</code> to reconfigure log4j.
 176   
      */
 177  0
     public void doOnChange() {
 178  0
         if (this.servletContext == null)
 179  0
             throw new RuntimeException("servletContext == null");
 180   
 
 181  0
         new ServletDOMConfigurator(this.servletContext).doConfigure(filename,
 182   
                 LogManager.getLoggerRepository());
 183   
     }
 184   
 }