logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From h...@apache.org
Subject cvs commit: jakarta-log4j-sandbox/src/java/org/apache/log4j/servlet InitContextListener.java InitServlet.java
Date Wed, 26 Feb 2003 23:22:57 GMT
hoju        2003/02/26 15:22:56

  Modified:    src/java/org/apache/log4j/selector
                        ContextClassLoaderSelector.java
               src/java/org/apache/log4j/servlet InitContextListener.java
                        InitServlet.java
  Added:       src/java/org/apache/log4j/selector ContextJNDISelector.java
  Log:
  Updating the servlet initializers classes to work be generic and work with any custom repository
selector.  The preferred selector class is provided as a context init parameter in the web.xml.
 Also adding a ContextJNDISelector to implement the custom repository selector described by
Ceki in his article.  Also added the full Apache license to the top of each of the files being
committed.  Updated javadoc as well.
  
  Jake
  
  Revision  Changes    Path
  1.4       +62 -10    jakarta-log4j-sandbox/src/java/org/apache/log4j/selector/ContextClassLoaderSelector.java
  
  Index: ContextClassLoaderSelector.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j-sandbox/src/java/org/apache/log4j/selector/ContextClassLoaderSelector.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ContextClassLoaderSelector.java	18 Feb 2003 15:50:20 -0000	1.3
  +++ ContextClassLoaderSelector.java	26 Feb 2003 23:22:55 -0000	1.4
  @@ -1,9 +1,60 @@
  -/*
  - * Copyright (C) The Apache Software Foundation. All rights reserved.
  +/* ====================================================================
  + * The Apache Software License, Version 1.1
    *
  - * This software is published under the terms of the Apache Software
  - * License version 1.1, a copy of which has been included with this
  - * distribution in the LICENSE.txt file.  */
  + * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
  + * reserved.
  + *
  + * Redistribution and use in source and binary forms, with or without
  + * modification, are permitted provided that the following conditions
  + * are met:
  + *
  + * 1. Redistributions of source code must retain the above copyright
  + *    notice, this list of conditions and the following disclaimer.
  + *
  + * 2. Redistributions in binary form must reproduce the above copyright
  + *    notice, this list of conditions and the following disclaimer in
  + *    the documentation and/or other materials provided with the
  + *    distribution.
  + *
  + * 3. The end-user documentation included with the redistribution,
  + *    if any, must include the following acknowledgment:
  + *       "This product includes software developed by the
  + *        Apache Software Foundation (http://www.apache.org/)."
  + *    Alternately, this acknowledgment may appear in the software itself,
  + *    if and wherever such third-party acknowledgments normally appear.
  + *
  + * 4. The names "Apache" and "Apache Software Foundation" must
  + *    not be used to endorse or promote products derived from this
  + *    software without prior written permission. For written
  + *    permission, please contact apache@apache.org.
  + *
  + * 5. Products derived from this software may not be called "Apache",
  + *    nor may "Apache" appear in their name, without prior written
  + *    permission of the Apache Software Foundation.
  + *
  + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  + * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  + * SUCH DAMAGE.
  + * ====================================================================
  + *
  + * This software consists of voluntary contributions made by many
  + * individuals on behalf of the Apache Software Foundation.  For more
  + * information on the Apache Software Foundation, please see
  + * <http://www.apache.org/>.
  + *
  + * Portions of this software are based upon public domain software
  + * originally written at the National Center for Supercomputing Applications,
  + * University of Illinois, Urbana-Champaign.
  + */
   package org.apache.log4j.selector;
   
   import org.apache.log4j.Hierarchy;
  @@ -54,27 +105,28 @@
      * key: current thread's ContextClassLoader,
      * value: Hierarchy instance
      */
  -  private static final Map HIER_MAP =
  -    Collections.synchronizedMap(new WeakHashMap());
  +  private final Map hierMap;
   
     /**
      * public no-args constructor
      */
     public ContextClassLoaderSelector() {
  +    hierMap = Collections.synchronizedMap(new WeakHashMap());
     }
   
     /**
  -   * implemented RepositorySelector interface method.
  +   * implemented RepositorySelector interface method. The returned
  +   * value is guaranteed to be non-null.
      *
      * @return the appropriate classloader-keyed Hierarchy/LoggerRepository
      */
     public LoggerRepository getLoggerRepository() {
       ClassLoader cl = Thread.currentThread().getContextClassLoader();
  -    Hierarchy hierarchy = (Hierarchy) HIER_MAP.get(cl);
  +    Hierarchy hierarchy = (Hierarchy) hierMap.get(cl);
   
       if (hierarchy == null) {
         hierarchy = new Hierarchy(new RootCategory((Level) Level.DEBUG));
  -      HIER_MAP.put(cl, hierarchy);
  +      hierMap.put(cl, hierarchy);
       }
   
       return hierarchy;
  
  
  
  1.1                  jakarta-log4j-sandbox/src/java/org/apache/log4j/selector/ContextJNDISelector.java
  
  Index: ContextJNDISelector.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * Portions of this software are based upon public domain software
   * originally written at the National Center for Supercomputing Applications,
   * University of Illinois, Urbana-Champaign.
   */
  package org.apache.log4j.selector;
  
  import org.apache.log4j.Hierarchy;
  import org.apache.log4j.Level;
  import org.apache.log4j.spi.LoggerRepository;
  import org.apache.log4j.spi.RepositorySelector;
  import org.apache.log4j.spi.RootCategory;
  
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.Map;
  
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  
  
  /**
   * Log4j JNDI based Repository selector
   *
   * <p>based primarily on Ceki Gülcü's article <h3>Supporting the Log4j
   * <code>RepositorySelector</code> in Servlet Containers</h3> at:
   * http://qos.ch/logging/sc.html</p>
   *
   * <p>By default, the class static <code>RepositorySelector</code> variable
   * of the <code>LogManager</code> class is set to a trivial
   * <code>RepositorySelector</code> implementation which always
   * returns the same logger repository, which also happens to be a
   * <code>Hierarchy</code> instance. In other words, by default log4j will use
   * one hierarchy, the default hierarchy. This behavior can be overridden via
   * the <code>LogManager</code>'s
   * <code>setRepositorySelector(RepositorySelector, Object)</code> method.</p>
   *
   * <p>That is where this class enters the picture.  It can be used to define a
   * custom logger repository.  It makes use of the fact that in J2EE
   * environments, each web-application is guaranteed to have its own JNDI
   * context relative to the java:comp/env context. In EJBs, each enterprise
   * bean (albeit not each application) has its own context relative to the
   * java:comp/env context.  An env-entry in a deployment descriptor provides
   * the information to the JNDI context.  Once the env-entry is set, a
   * repository selector can query the JNDI application context to look up the
   * value of the entry. The logging context of the web-application will depend
   * on the value the env-entry.  The JNDI context which is looked up by this
   * class is java:comp/env/log4j/logging-context.  Here is an example of an
   * env-entry:
   * <blockquote>
   * <pre>
   * &lt;env-entry&gt;
   *   &lt;description&gt;The Tiger webapp logging context&lt;/description&gt;
   *   &lt;env-entry-name&gt;log4j/logging-context&lt;/env-entry-name&gt;
   *   &lt;env-entry-type&gt;java.lang.String&lt;/env-entry-type&gt;
   *   &lt;env-entry-value&gt;TigerLoggingContext&lt;/env-entry-value&gt;
   * &lt;/env-entry&gt;
   * </pre>
   * </blockquote>
   * </p>
   *
   * <p>Unlike the {@link ContextClassLoaderSelector} which will only work in
   * containers that provide for separate classloaders, JNDI is available in all
   * servers claiming to be servlet or J2EE compliant.  So, the JNDI selector
   * may be the better choice.  However it is possible, at least in non-servlet
   * parts of J2EE apps, to spoof the value of the env-entry.  There are ways
   * to avoid this, but this class makes no attempt to do so.  It would require
   * a container specific implementation to, maybe, append a non-random unique
   * name to the user-defined value of the env-entry.  Keep that in mind as you
   * choose which custom repository selector you would like to use in your own
   * application.</p>
   *
   * @author  Jacob Kjome
   * @since 1.3
   */
  public class ContextJNDISelector implements RepositorySelector {
    /**
     * key: name of logging context,
     * value: Hierarchy instance
     */
    private final Map hierMap;
  
    /**
     * default hierarchy used in case the JNDI lookup
     * fails to return a non-null value
     */
    private final Hierarchy defaultHierarchy;
  
    /**
     * public no-args constructor
     */
    public ContextJNDISelector() {
      hierMap = Collections.synchronizedMap(new HashMap());
      defaultHierarchy = new Hierarchy(new RootCategory(Level.DEBUG));
    }
  
    /**
     * implemented RepositorySelector interface method. The returned
     * value is guaranteed to be non-null.
     *
     * @return the appropriate JNDI-keyed Hierarchy/LoggerRepository
     */
    public LoggerRepository getLoggerRepository() {
      String loggingContextName = null;
  
      try {
        Context ctx = new InitialContext();
        loggingContextName =
          (String) ctx.lookup("java:comp/env/log4j/logging-context");
      } catch (NamingException ne) {
        // we can't log here
      }
  
      if (loggingContextName == null) {
        return defaultHierarchy;
      } else {
        Hierarchy hierarchy = (Hierarchy) hierMap.get(loggingContextName);
  
        if (hierarchy == null) {
          hierarchy = new Hierarchy(new RootCategory(Level.DEBUG));
          hierMap.put(loggingContextName, hierarchy);
        }
  
        return hierarchy;
      }
    }
  }
  
  
  
  1.3       +108 -15   jakarta-log4j-sandbox/src/java/org/apache/log4j/servlet/InitContextListener.java
  
  Index: InitContextListener.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j-sandbox/src/java/org/apache/log4j/servlet/InitContextListener.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- InitContextListener.java	18 Feb 2003 15:50:20 -0000	1.2
  +++ InitContextListener.java	26 Feb 2003 23:22:55 -0000	1.3
  @@ -1,15 +1,66 @@
  -/*
  - * Copyright (C) The Apache Software Foundation. All rights reserved.
  +/* ====================================================================
  + * The Apache Software License, Version 1.1
    *
  - * This software is published under the terms of the Apache Software
  - * License version 1.1, a copy of which has been included with this
  - * distribution in the LICENSE.txt file.  */
  + * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
  + * reserved.
  + *
  + * Redistribution and use in source and binary forms, with or without
  + * modification, are permitted provided that the following conditions
  + * are met:
  + *
  + * 1. Redistributions of source code must retain the above copyright
  + *    notice, this list of conditions and the following disclaimer.
  + *
  + * 2. Redistributions in binary form must reproduce the above copyright
  + *    notice, this list of conditions and the following disclaimer in
  + *    the documentation and/or other materials provided with the
  + *    distribution.
  + *
  + * 3. The end-user documentation included with the redistribution,
  + *    if any, must include the following acknowledgment:
  + *       "This product includes software developed by the
  + *        Apache Software Foundation (http://www.apache.org/)."
  + *    Alternately, this acknowledgment may appear in the software itself,
  + *    if and wherever such third-party acknowledgments normally appear.
  + *
  + * 4. The names "Apache" and "Apache Software Foundation" must
  + *    not be used to endorse or promote products derived from this
  + *    software without prior written permission. For written
  + *    permission, please contact apache@apache.org.
  + *
  + * 5. Products derived from this software may not be called "Apache",
  + *    nor may "Apache" appear in their name, without prior written
  + *    permission of the Apache Software Foundation.
  + *
  + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  + * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  + * SUCH DAMAGE.
  + * ====================================================================
  + *
  + * This software consists of voluntary contributions made by many
  + * individuals on behalf of the Apache Software Foundation.  For more
  + * information on the Apache Software Foundation, please see
  + * <http://www.apache.org/>.
  + *
  + * Portions of this software are based upon public domain software
  + * originally written at the National Center for Supercomputing Applications,
  + * University of Illinois, Urbana-Champaign.
  + */
   package org.apache.log4j.servlet;
   
   import org.apache.log4j.LogManager;
   import org.apache.log4j.PropertyConfigurator;
   import org.apache.log4j.helpers.LogLog;
  -import org.apache.log4j.selector.ContextClassLoaderSelector;
  +import org.apache.log4j.spi.RepositorySelector;
   import org.apache.log4j.xml.DOMConfigurator;
   
   import java.io.File;
  @@ -54,6 +105,14 @@
    * <blockquote>
    * <pre>
    * &lt;context-param&gt;
  + *     &lt;!-- preferred repository selector. "preferred" because if one
  + *           is already installed, this choice is ignored. --&gt;
  + *     &lt;param-name&gt;log4j-selector&lt;/param-name&gt;
  + *     &lt;param-value&gt;
  + *     org.apache.log4j.selector.ContextClassLoaderSelector
  + *     &lt;/param-value&gt;
  + * &lt;/context-param&gt;
  + * &lt;context-param&gt;
    *     &lt;!-- relative path to config file within current webapp --&gt;
    *     &lt;param-name&gt;log4j-config&lt;/param-name&gt;
    *     &lt;param-value&gt;WEB-INF/log4j.xml&lt;/param-value&gt;
  @@ -100,6 +159,8 @@
    *     &lt;/listener-class&gt;
    * &lt;/listener&gt;
    * </pre>
  + * NOTE: Make sure to reference the documentation for each individual selector
  + * to see if any configuration is required above an beyond the above.
    * </blockquote>
    * </p>
    * <h4>Below is some more information on each of the configuration properties
  @@ -164,17 +225,23 @@
    */
   public class InitContextListener implements ServletContextListener {
     /**
  -   * relative path to config file within current webapp
  +   * preferred repository selector config param
  +   */
  +  private static final String PARAM_LOG4J_PREF_SELECTOR = "log4j-selector";
  +
  +  /**
  +   * relative path to config file within current webapp config param
      */
     private static final String PARAM_LOG4J_CONFIG_PATH = "log4j-config";
   
     /**
  -   * config file re-reading specified in milliseconds
  +   * config file re-reading specified in milliseconds config param
      */
     private static final String PARAM_LOG4J_WATCH_INTERVAL = "log4j-cron";
   
     /**
      * path to be read from a log4j xml config file as a system property
  +   * config param
      */
     private static final String PARAM_LOG4J_LOG_HOME = "log4j-log-home";
   
  @@ -185,6 +252,11 @@
       "WEB-INF" + File.separator + "logs";
   
     /**
  +   * preferred selector specified via config param
  +   */
  +  private String selector = null;
  +
  +  /**
      * Application Startup Event
      *
      * @param sce the context event provided by the container
  @@ -192,6 +264,7 @@
     public void contextInitialized(ServletContextEvent sce) {
       ServletContext context = sce.getServletContext();
   
  +    selector = context.getInitParameter(PARAM_LOG4J_PREF_SELECTOR);
       initializeLog4j(context);
     }
   
  @@ -424,16 +497,36 @@
      * class loader.
      */
     private void setSelector() {
  +    if (selector == null) {
  +      LogLog.warn(
  +        "No preferred selector supplied. Using default repository selector...");
  +
  +      return;
  +    }
  +
       try {
         Object guard = new Object();
  -      ContextClassLoaderSelector crs = new ContextClassLoaderSelector();
  -      LogManager.setRepositorySelector(crs, guard);
  +
  +      Class clazz =
  +        Class.forName(
  +          selector, true, Thread.currentThread().getContextClassLoader());
  +      LogManager.setRepositorySelector(
  +        (RepositorySelector) clazz.newInstance(), guard);
  +    } catch (ClassNotFoundException cnfe) {
  +      LogLog.warn(
  +        "Preferred selector not found. Using default repository selector...");
  +    } catch (InstantiationException ie) {
  +      LogLog.warn(
  +        "Error in instantiation of preferred selector. Using default "
  +        + "repository selector...");
  +    } catch (IllegalAccessException iae) {
  +      LogLog.warn(
  +        "Unable to access preferred selector. Using default repository "
  +        + "selector...");
       } catch (IllegalArgumentException iae) {
  -      //either ignore the exception or log the fact that the
  -      //setting of this custom repository selector failed because
  -      //another had been set previously and maybe we should set
  -      //"initialized" to "true" in here so this exception doesn't
  -      //occur again in this class
  +      LogLog.warn(
  +        "Unable to install preferred selector because another selector has "
  +        + "already been installed.  Using existing selector...");
       }
     }
   }
  
  
  
  1.3       +103 -15   jakarta-log4j-sandbox/src/java/org/apache/log4j/servlet/InitServlet.java
  
  Index: InitServlet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j-sandbox/src/java/org/apache/log4j/servlet/InitServlet.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- InitServlet.java	18 Feb 2003 15:50:20 -0000	1.2
  +++ InitServlet.java	26 Feb 2003 23:22:56 -0000	1.3
  @@ -1,15 +1,66 @@
  -/*
  - * Copyright (C) The Apache Software Foundation. All rights reserved.
  +/* ====================================================================
  + * The Apache Software License, Version 1.1
    *
  - * This software is published under the terms of the Apache Software
  - * License version 1.1, a copy of which has been included with this
  - * distribution in the LICENSE.txt file.  */
  + * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
  + * reserved.
  + *
  + * Redistribution and use in source and binary forms, with or without
  + * modification, are permitted provided that the following conditions
  + * are met:
  + *
  + * 1. Redistributions of source code must retain the above copyright
  + *    notice, this list of conditions and the following disclaimer.
  + *
  + * 2. Redistributions in binary form must reproduce the above copyright
  + *    notice, this list of conditions and the following disclaimer in
  + *    the documentation and/or other materials provided with the
  + *    distribution.
  + *
  + * 3. The end-user documentation included with the redistribution,
  + *    if any, must include the following acknowledgment:
  + *       "This product includes software developed by the
  + *        Apache Software Foundation (http://www.apache.org/)."
  + *    Alternately, this acknowledgment may appear in the software itself,
  + *    if and wherever such third-party acknowledgments normally appear.
  + *
  + * 4. The names "Apache" and "Apache Software Foundation" must
  + *    not be used to endorse or promote products derived from this
  + *    software without prior written permission. For written
  + *    permission, please contact apache@apache.org.
  + *
  + * 5. Products derived from this software may not be called "Apache",
  + *    nor may "Apache" appear in their name, without prior written
  + *    permission of the Apache Software Foundation.
  + *
  + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  + * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  + * SUCH DAMAGE.
  + * ====================================================================
  + *
  + * This software consists of voluntary contributions made by many
  + * individuals on behalf of the Apache Software Foundation.  For more
  + * information on the Apache Software Foundation, please see
  + * <http://www.apache.org/>.
  + *
  + * Portions of this software are based upon public domain software
  + * originally written at the National Center for Supercomputing Applications,
  + * University of Illinois, Urbana-Champaign.
  + */
   package org.apache.log4j.servlet;
   
   import org.apache.log4j.LogManager;
   import org.apache.log4j.PropertyConfigurator;
   import org.apache.log4j.helpers.LogLog;
  -import org.apache.log4j.selector.ContextClassLoaderSelector;
  +import org.apache.log4j.spi.RepositorySelector;
   import org.apache.log4j.xml.DOMConfigurator;
   
   import java.io.File;
  @@ -36,7 +87,7 @@
    * <strong>Note:</strong> This log4j initialization servlet should be used
    * *only* when running under a container which doesn't support servlet-2.3.
    * A far better choice for servlet-2.3 configuration exists in
  - * {@link Log4jApplicationWatch}. Use it instead of this class for
  + * {@link InitContextListener}. Use it instead of this class for
    * initialization.  If you really need to use this class, read on...</p>
    * <p>
    * This servlet is never called by a client, but should be called during
  @@ -51,6 +102,12 @@
    *        org.apache.log4j.servlet.InitServlet
    *      &lt;/servlet-class&gt;
    *      &lt;init-param&gt;
  + *           &lt;param-name&gt;log4j-selector&lt;/param-name&gt;
  + *           &lt;param-value&gt;
  + *           org.apache.log4j.selector.ContextClassLoaderSelector
  + *           &lt;/param-value&gt;
  + *      &lt;/init-param&gt;
  + *      &lt;init-param&gt;
    *           &lt;param-name&gt;log4j-config&lt;/param-name&gt;
    *           &lt;param-value&gt;WEB-INF/log4j.xml&lt;/param-value&gt;
    *      &lt;/init-param&gt;
  @@ -67,7 +124,7 @@
    * </pre>
    * </p>
    * <p>
  - * See {@link Log4jApplicationWatch} for detailed information about these
  + * See {@link InitContextListener} for detailed information about these
    * parameters.
    * </p>
    *
  @@ -76,6 +133,11 @@
    */
   public class InitServlet extends HttpServlet {
     /**
  +   * preferred repository selector config param
  +   */
  +  private static final String PARAM_LOG4J_PREF_SELECTOR = "log4j-selector";
  +
  +  /**
      * relative path to config file within current webapp
      */
     private static final String PARAM_LOG4J_CONFIG_PATH = "log4j-config";
  @@ -102,6 +164,11 @@
     private static Boolean configured = Boolean.FALSE;
   
     /**
  +   * preferred selector specified via config param
  +   */
  +  private String selector = null;
  +
  +  /**
      * Log4j specific initialization.  Sets up log4j for the current
      * servlet context. Installs a custom repository selector if one hasn't
      * already been installed.
  @@ -111,6 +178,7 @@
     public void init() throws ServletException {
       if (configured.equals(Boolean.FALSE)) {
         String configPath = getInitParameter(PARAM_LOG4J_CONFIG_PATH);
  +      selector = getInitParameter(PARAM_LOG4J_PREF_SELECTOR);
   
         // if the log4j-config parameter is not set, then no point in trying
         if (configPath != null) {
  @@ -337,16 +405,36 @@
      * class loader.
      */
     private void setSelector() {
  +    if (selector == null) {
  +      LogLog.warn(
  +        "No preferred selector supplied. Using default repository selector...");
  +
  +      return;
  +    }
  +
       try {
         Object guard = new Object();
  -      ContextClassLoaderSelector crs = new ContextClassLoaderSelector();
  -      LogManager.setRepositorySelector(crs, guard);
  +
  +      Class clazz =
  +        Class.forName(
  +          selector, true, Thread.currentThread().getContextClassLoader());
  +      LogManager.setRepositorySelector(
  +        (RepositorySelector) clazz.newInstance(), guard);
  +    } catch (ClassNotFoundException cnfe) {
  +      LogLog.warn(
  +        "Preferred selector not found. Using default repository selector...");
  +    } catch (InstantiationException ie) {
  +      LogLog.warn(
  +        "Error in instantiation of preferred selector. Using default "
  +        + "repository selector...");
  +    } catch (IllegalAccessException iae) {
  +      LogLog.warn(
  +        "Unable to access preferred selector. Using default repository "
  +        + "selector...");
       } catch (IllegalArgumentException iae) {
  -      //either ignore the exception or log the fact that the
  -      //setting of this custom repository selector failed because
  -      //another had been set previously and maybe we should set
  -      //"initialized" to "true" in here so this exception doesn't
  -      //occur again in this class
  +      LogLog.warn(
  +        "Unable to install preferred selector because another selector has "
  +        + "already been installed.  Using existing selector...");
       }
     }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org


Mime
View raw message