ceki 2004/03/30 02:45:22
Modified: src/java/org/apache/log4j/spi RepositorySelector.java
DefaultRepositorySelector.java
src/java/org/apache/log4j/helpers IntializationUtil.java
Constants.java
. build.xml build.properties.sample
src/java/org/apache/log4j concurrency.txt LogManager.java
examples/tiny-webapp INSTALL.txt
examples/tiny-webapp/Tata/src/WEB-INF web.xml
examples/tiny-webapp/Hello/src/WEB-INF web.xml
examples/tiny-webapp/Hello .cvsignore
examples/tiny-webapp/Hello/src/java/wombat HelloServlet.java
examples/tiny-webapp/Tata .cvsignore
src/java/org/apache/log4j/selector ContextJNDISelector.java
Added: src/java/org/apache/log4j/selector/servlet
ContextDetachingSCL.java
src/java/org/apache/log4j/helpers JNDIUtil.java
. HOWTOBUILD.txt
examples/tiny-webapp/Tata/src/java/wombat InitServlet.java
Removed: examples/sort sort2.properties sort4.properties
sort1.properties Sort.java sort3.properties
SortAlgo.java
Log:
Improved the ContextJNDISelector lifecycle.
- Recycled logging context can now be detached from the RepositorySelector
and shutdown.
- Added ContextDetachingSCL (SerrvletContextListerner) to serve as
a basic but still useful implementation.
- Improved the tiny-webapp examples with more documentation.
Revision Changes Path
1.1
logging-log4j/src/java/org/apache/log4j/selector/servlet/ContextDetachingSCL.java
Index: ContextDetachingSCL.java
===================================================================
/*
* Copyright 1999,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.log4j.selector.servlet;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.helpers.Constants;
import org.apache.log4j.helpers.JNDIUtil;
import org.apache.log4j.spi.LoggerRepository;
import org.apache.log4j.spi.RepositorySelector;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* This is a very simple ServletContextListener which detaches a
* [EMAIL PROTECTED] LoggerRepository} from [EMAIL PROTECTED] ContextJNDISlector}
when the
* web-application is destroyed.
*
* This class is highly coupled with JNDI but not necessarily
* ContextJNDISlector.
*
* @author Ceki Gülcü
*
* @since 1.3
*/
public class ContextDetachingSCL implements ServletContextListener {
static Logger logger = Logger.getLogger(ContextDetachingSCL.class);
/**
* When the context is destroy, detach the logging repository given by
* the value of "log4j/context-name" environment variable.
*
* If found, the logging repository is also shutdown.
*/
public void contextDestroyed(ServletContextEvent sce) {
String loggingContextName = null;
try {
Context ctx = JNDIUtil.getInitialContext();
loggingContextName =
(String) JNDIUtil.lookup(ctx, Constants.JNDI_CONTEXT_NAME);
} catch (NamingException ne) {
}
if (loggingContextName != null) {
logger.debug(
"About to detach logger context named [" + loggingContextName + "].");
RepositorySelector repositorySelector =
LogManager.getRepositorySelector();
LoggerRepository lr = repositorySelector.detachRepository(loggingContextName);
if(lr != null) {
logger.debug("About to shutdown logger repository named ["+lr.getName()+
"]");
lr.shutdown();
}
}
}
/**
* Does nothing.
*/
public void contextInitialized(ServletContextEvent sce) {
ServletContext sc = sce.getServletContext();
logger.debug("Context named ["+sc.getServletContextName()+"] initialized.");
}
}
1.9 +2 -1
logging-log4j/src/java/org/apache/log4j/spi/RepositorySelector.java
Index: RepositorySelector.java
===================================================================
RCS file:
/home/cvs/logging-log4j/src/java/org/apache/log4j/spi/RepositorySelector.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- RepositorySelector.java 26 Mar 2004 08:14:58 -0000 1.8
+++ RepositorySelector.java 30 Mar 2004 10:45:21 -0000 1.9
@@ -57,6 +57,7 @@
* If more than one application share the same logging context, then the
* applications need to coordinate their actions.
*
+ * @return The LoggerRepository instance that was detached.
*/
- public void remove(String contextName);
+ public LoggerRepository detachRepository(String contextName);
}
1.7 +7 -1
logging-log4j/src/java/org/apache/log4j/spi/DefaultRepositorySelector.java
Index: DefaultRepositorySelector.java
===================================================================
RCS file:
/home/cvs/logging-log4j/src/java/org/apache/log4j/spi/DefaultRepositorySelector.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- DefaultRepositorySelector.java 26 Mar 2004 08:14:58 -0000 1.6
+++ DefaultRepositorySelector.java 30 Mar 2004 10:45:21 -0000 1.7
@@ -34,7 +34,13 @@
}
}
- public void remove(String contextName) {
+ /**
+ * Does nothing.
+ *
+ * @return Always null
+ */
+ public LoggerRepository detachRepository(String contextName) {
// do nothing as the default reposiory cannot be removed
+ return null;
}
}
1.4 +1 -0
logging-log4j/src/java/org/apache/log4j/helpers/IntializationUtil.java
Index: IntializationUtil.java
===================================================================
RCS file:
/home/cvs/logging-log4j/src/java/org/apache/log4j/helpers/IntializationUtil.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- IntializationUtil.java 24 Mar 2004 09:41:08 -0000 1.3
+++ IntializationUtil.java 30 Mar 2004 10:45:21 -0000 1.4
@@ -60,6 +60,7 @@
public static void initialConfiguration(LoggerRepository repository,
String configuratonResourceStr,
String configuratorClassNameStr) {
+
if(configuratonResourceStr == null) {
return;
}
1.4 +2 -0 logging-log4j/src/java/org/apache/log4j/helpers/Constants.java
Index: Constants.java
===================================================================
RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/helpers/Constants.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- Constants.java 24 Mar 2004 19:28:06 -0000 1.3
+++ Constants.java 30 Mar 2004 10:45:21 -0000 1.4
@@ -34,4 +34,6 @@
static final String DEFAULT_XML_CONFIGURATION_FILE = "log4j.xml";
static final String DEFAULT_CONFIGURATION_KEY = "log4j.configuration";
static final String CONFIGURATOR_CLASS_KEY = "log4j.configuratorClass";
+
+ static String JNDI_CONTEXT_NAME = "java:comp/env/log4j/context-name";
}
1.1 logging-log4j/src/java/org/apache/log4j/helpers/JNDIUtil.java
Index: JNDIUtil.java
===================================================================
/*
* Created on Mar 28, 2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package org.apache.log4j.helpers;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* @author ceki
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class JNDIUtil {
static public Context getInitialContext() throws NamingException {
return new InitialContext();
}
static public String lookup(Context ctx, String name) {
if(ctx == null) {
return null;
}
try {
return (String) ctx.lookup(name);
} catch (NamingException e) {
//LogLog.warn("Failed to get "+name);
return null;
}
}
}
1.93 +2 -0 logging-log4j/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/logging-log4j/build.xml,v
retrieving revision 1.92
retrieving revision 1.93
diff -u -r1.92 -r1.93
--- build.xml 2 Feb 2004 06:12:43 -0000 1.92
+++ build.xml 30 Mar 2004 10:45:21 -0000 1.93
@@ -85,6 +85,7 @@
<pathelement location="${jmx.jar}"/>
<pathelement location="${jmx-extra.jar}"/>
<pathelement location="${avalon-framework.jar}"/>
+ <pathelement location="${servlet-api.jar}"/>
</path>
<!-- Construct classpath for building the html pages-->
@@ -467,6 +468,7 @@
${stem}/xml/*.class,
${stem}/jmx/*.class,
${stem}/selector/*.class,
+ ${stem}/selector/servlet/*.class,
${stem}/pattern/*.class,
${stem}/or/*.class,
${stem}/or/sax/*.class,
1.15 +2 -0 logging-log4j/build.properties.sample
Index: build.properties.sample
===================================================================
RCS file: /home/cvs/logging-log4j/build.properties.sample,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- build.properties.sample 23 Jan 2004 17:38:50 -0000 1.14
+++ build.properties.sample 30 Mar 2004 10:45:21 -0000 1.15
@@ -21,6 +21,8 @@
# Avalon Framework OPTIONAL; required to build the Avalon Logger facade.
avalon-framework.jar=/java/avalon-framework/avalon-framework.jar
+# Servlet api
+servlet-api.jar=/java/servlet-api.jar
# Checkstyle OPTIONAL; required to run the checkstyle target
# Available from http://checkstyle.sf.net
1.1 logging-log4j/HOWTOBUILD.txt
Index: HOWTOBUILD.txt
===================================================================
HOWTOBUILD.txt
==============
This document outlines the steps required to build and run the log4j sandbox classes.
Comments/Changes/Bugs for this document and build process:
* Log4j Developers list ([EMAIL PROTECTED])
Assumptions
===========
* Familiar with Ant, and have it installed (http://ant.apache.org/)
(requires the 1.5.x series of Ant, including Conditions support)
Step-by-Step to build Log4j-Sandbox
======================================
1. Download logging-log4j (you've probably already done this if you're reading
this...)
2. Copy the build.properties.sample file -> build.properties
3. Edit the build.properties file and modify to suit your needs. In particular
* Ensure the "Library Path Stuff" section contains correct relative/full paths to
the
relevant libraries (You might need to source these from the 'Net)
4. From the logging-log4j root directory, type:
ant jar
That's it ;)
1.2 +25 -12 logging-log4j/src/java/org/apache/log4j/concurrency.txt
Index: concurrency.txt
===================================================================
RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/concurrency.txt,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- concurrency.txt 10 Jul 2003 08:51:53 -0000 1.1
+++ concurrency.txt 30 Mar 2004 10:45:21 -0000 1.2
@@ -1,17 +1,30 @@
-This document describes the concurrency model used in some of the log4j core
classes such as Logger (i.e. Category) and Hierarchy.
+This document describes the concurrency model used in some of the
+log4j core classes such as Logger (i.e. Category) and Hierarchy.
-Concurrency primitives in these classes must be studied carefully because
concurrency problems are usually hard to reproduce.
-Moreover, since log4j is a tool to diagnose problems, the correctness of the
diagnosis tool must be guaranteed.
+Concurrency primitives in these classes must be studied carefully
+because concurrency problems are usually hard to reproduce. Moreover,
+since log4j is a tool to diagnose problems, the correctness of the
+diagnosis tool (i.e log4j) must be guaranteed.
-We must also recognize that the data in Logger and Hierarchy are read frequently
and written to rarely. Up to and including log4j 1.2,
-we did not differentiate between read and write operations. Instead, a coarse
object-wide locks were used which treated reads the same
-as writes. This serializes some logging operations. A better approach would be to
allow simultaneous reads but exclusive writes.
+We must also recognize that the data in Logger and Hierarchy are read
+frequently and written to rarely. Up to and including log4j 1.2, we
+did not differentiate between read and write operations. Instead, a
+coarse object-wide locks were used which treated reads the same as
+writes. This serializes some logging operations. A better approach
+would be to allow simultaneous reads but exclusive writes.
-For this purpose log4j 1.3 includes a ReaderWriterLock that allow simultaneous
reads but exclusive writes. When readers and writers
-compete for the lock, preference is given to writers. See
o.a.l.helpers.ReaderWriterLock.java for source code which is remarkably simple.
+For this purpose log4j 1.3 includes a ReaderWriterLock that allow
+simultaneous reads but exclusive writes. When readers and writers
+compete for the lock, preference is given to writers. See
+o.a.l.helpers.ReaderWriterLock.java for source code which is
+remarkably simple.
-The ReaderWriterLock is not reentrant. Thus, we must make sure that a writer owning
the lock does try to reacquire the lock because
-any such attempt would result in a deadlock. Similarly, it is not safe for a reader
owning the lock to try to reacquire it. This
-condition can be verified only if few simple instructions are allowed between the
line acquiring the lock and the line releasing it.
-If the code inside a protected block is complex, then either it is wrong or our
model needs to be changed.
+The ReaderWriterLock is not reentrant. Thus, we must make sure that a
+writer owning the lock does try to reacquire the lock because any such
+attempt would result in a deadlock. Similarly, it is not safe for a
+reader owning the lock to try to reacquire it. This condition can be
+verified only if few simple instructions are allowed between the line
+acquiring the lock and the line releasing it. If the code inside a
+protected block is complex, then either it is wrong or our model needs
+to be changed.
1.20 +22 -6 logging-log4j/src/java/org/apache/log4j/LogManager.java
Index: LogManager.java
===================================================================
RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/LogManager.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- LogManager.java 24 Mar 2004 19:32:54 -0000 1.19
+++ LogManager.java 30 Mar 2004 10:45:21 -0000 1.20
@@ -44,6 +44,7 @@
private static RepositorySelector repositorySelector;
static {
+ System.out.println("**Start of LogManager static initializer");
Hierarchy defaultHierarchy = new Hierarchy(new RootCategory(Level.DEBUG));
defaultHierarchy.setName("default");
@@ -79,9 +80,10 @@
// at this stage 'repositorySelector' should point to a valid selector
repositorySelector.setDefaultRepository(defaultHierarchy);
- // Use automatic configration to configure the default hierarchy
+ // configure log4j internal logging for the default hierarchy
IntializationUtil.log4jInternalConfiguration(defaultHierarchy);
+ // Attempt to perform automatic configuration of the default hierarchy
String configuratorClassName =
OptionConverter.getSystemProperty(
Constants.CONFIGURATOR_CLASS_KEY, null);
@@ -98,19 +100,22 @@
}
}
- System.out.println("configurationOptionStr=" + configurationOptionStr);
+ System.out.println("*** configurationOptionStr=" + configurationOptionStr);
IntializationUtil.initialConfiguration(
defaultHierarchy, configurationOptionStr, configuratorClassName);
+
+
+ System.out.println("** End of LogManager static initializer");
}
/**
- Sets <code>LoggerFactory</code> but only if the correct
+ Sets <code>RepositorySelector</code> but only if the correct
<em>guard</em> is passed as parameter.
<p>Initally the guard is null. If the guard is
<code>null</code>, then invoking this method sets the logger
- factory and the guard. Following invocations will throw a [EMAIL PROTECTED]
+ repository and the guard. Following invocations will throw a [EMAIL PROTECTED]
IllegalArgumentException}, unless the previously set
<code>guard</code> is passed as the second parameter.
@@ -121,14 +126,14 @@
own repository selector. However, if and when Tomcat is embedded
within JBoss, then JBoss will install its own repository selector
and Tomcat will use the repository selector set by its container,
- JBoss. */
+ JBoss.
+ */
public static void setRepositorySelector(
RepositorySelector selector, Object guard) throws IllegalArgumentException {
if ((LogManager.guard != null) && (LogManager.guard != guard)) {
throw new IllegalArgumentException(
"Attempted to reset the LoggerFactory without possessing the guard.");
}
-
if (selector == null) {
throw new IllegalArgumentException(
"RepositorySelector must be non-null.");
@@ -138,6 +143,17 @@
LogManager.repositorySelector = selector;
}
+
+ /**
+ * Return the repository selector currently in use.
+ *
+ * @since 1.3
+ * @return [EMAIL PROTECTED] RepositorySelector} currently in use.
+ */
+ public static RepositorySelector getRepositorySelector() {
+ return LogManager.repositorySelector;
+ }
+
public static LoggerRepository getLoggerRepository() {
return repositorySelector.getLoggerRepository();
}
1.2 +86 -5 logging-log4j/examples/tiny-webapp/INSTALL.txt
Index: INSTALL.txt
===================================================================
RCS file: /home/cvs/logging-log4j/examples/tiny-webapp/INSTALL.txt,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- INSTALL.txt 27 Mar 2004 17:31:56 -0000 1.1
+++ INSTALL.txt 30 Mar 2004 10:45:21 -0000 1.2
@@ -26,9 +26,32 @@
- You can now run the supplied web-applications hello.war and
tata.war.
+- Optionally, you can add a configuration file such as log4j.xml or
+ log4j.properties in the class directory of your web-server. For
+ Tomcat versions 4 or 5, that would be server/classes/ directory.
+
+ Thus, when log4j is first loaded into memory, it will configure the
+ default logging repository using the configuration file found in
+ server/classes/ directory.
+
+ For Tomcat version 5 (tested on Tomcat 5.0.19), you also need to
+ tell Tomcat to use log4j by placing commons-logging.jar in
+ common/lib directory. You should also remove the
+ commons-logging-api.jar from the bin/ directory.
+
+ This way the default (log4j) logger repository will be used by
+ Tomcat for its logging and the default logger repository will be
+ controlled by the configuration file log4j.xml or log4j.properties
+ found in server/classes/.
+
Steps performed per web-application
==================================
+- You will need log4j-VERSION.jar to compile the
+ web-applications. However, log4j-VERSION.jar file need not and
+ probably should not be included within the web-application's jar
+ file.
+
- In each web-application's web.ml file add a JNDI environment entry
for the log4j logging context name. For the "Hello" web-application
this takes the following form:
@@ -48,10 +71,68 @@
This file will be automatically taken to configure the repository
instance specific for your web-application.
- Note that you will need log4j-VERSION.jar to compile the
- web-applications but log4j-VERSION.jar file should not be included
- within the web-application's jar file.
-
- For more options see the javadoc for JNDIContextSelector.
+ Alternatively, you can specify the URL for this context's
+ configuration resource. The repository selector
+ (ContextJNDISelector) will use the specified resource to
+ automatically configure the log4j repository.
+
+ You can specify a resource other than log4j.xml or log4j.properties
+ with the "log4j/configuration-resource" environment entry.
+
+ <env-entry>
+ <description>URL for configuring log4j context</description>
+ <env-entry-name>log4j/configuration-resource</env-entry-name>
+ <env-entry-value>urlOfConfigrationResource</env-entry-value>
+ <env-entry-type>java.lang.String</env-entry-type>
+ </env-entry>
+
+ Note that only when "log4j/configuration-resource" environment entry
+ is missing that the default resources log4j.xml and log4j.properties are
+ looked up.
+
+ For more information on the available options see the javadoc for
+ ContextJNDISelector.
+
+- When the web-application is recycled or shutdown, it is often useful
+ to recycle the associated logging repository. This can be done by
+ installing a ServletContextListener which will detach the repository
+ from the repository selector and shut it down.
+
+ The ContextDetachingSCL which ships with log4j does exactly that. To
+ install it, add the following lines to your web-application's
+ web.xml file.
+
+ <listener>
+
<listener-class>org.apache.log4j.selector.servlet.ContextDetachingSCL</listener-class>
+ </listener>
+
+ See also the file examples/tiny-webapp/Hello/src/WEB-INF/web.xml
+
+- You are encouraged to name your web-application in the web
+ descriptor file. As in
+
+ <display-name>Hello sample web-application</display-name>
+
+
+KNOWN PROBLEMS WITH ContextJNDISelector
+=======================================
+- In case your J2EE container does not use log4j by default (e.g. T5,
+ Resin), and you are using ContextJNDISelector to configure multiple
+ logger repositories, then the default logger repository will be
+ configured at the same time the logger repository specific for your
+ first web-application is configured.
+
+ For various technical reasons, if that first web-application is
+ automatically configured using log4j.xml or log4j.properties files
+ contained in the web-application, then the default logger repository
+ will be configured using those same configuration files of the
+ web-application.
+
+ This is usually benign because even if configured with the wrong
+ file, the default logging respository will probably not be used in
+ this set up. Nevertheless, you can still correct this error by
+ forcing log4j to configure the default logging repository with the
+ configuration file of your choice by setting the
+ "log4j.configuration" system property.
1.3 +12 -1 logging-log4j/examples/tiny-webapp/Tata/src/WEB-INF/web.xml
Index: web.xml
===================================================================
RCS file: /home/cvs/logging-log4j/examples/tiny-webapp/Tata/src/WEB-INF/web.xml,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- web.xml 27 Mar 2004 17:31:56 -0000 1.2
+++ web.xml 30 Mar 2004 10:45:21 -0000 1.3
@@ -2,6 +2,13 @@
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
+ <servlet>
+ <servlet-name>some-init-servlet</servlet-name>
+ <servlet-class>wombat.InitServlet</servlet-class>
+ <load-on-startup>1</load-on-startup>
+ </servlet>
+
+
<servlet>
<servlet-name>TataServlet</servlet-name>
<servlet-class>wombat.TataServlet</servlet-class>
@@ -18,5 +25,9 @@
<env-entry-value>tata</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
-
+
+ <listener>
+
<listener-class>org.apache.log4j.selector.servlet.ContextDetachingSCL</listener-class>
+ </listener>
+
</web-app>
1.1
logging-log4j/examples/tiny-webapp/Tata/src/java/wombat/InitServlet.java
Index: InitServlet.java
===================================================================
package wombat;
import org.apache.log4j.*;
import org.apache.log4j.spi.RootCategory;
import javax.servlet.http.*;
import javax.servlet.*;
public class InitServlet extends HttpServlet {
static Logger logger = Logger.getLogger(InitServlet.class);
public void init() {
logger.info("Logging initialized for Tata.");
}
public void doGet(HttpServletRequest req, HttpServletResponse res) {
// nothing to do
}
}
1.3 +7 -0 logging-log4j/examples/tiny-webapp/Hello/src/WEB-INF/web.xml
Index: web.xml
===================================================================
RCS file: /home/cvs/logging-log4j/examples/tiny-webapp/Hello/src/WEB-INF/web.xml,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- web.xml 27 Mar 2004 17:31:56 -0000 1.2
+++ web.xml 30 Mar 2004 10:45:21 -0000 1.3
@@ -2,6 +2,9 @@
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
+
+ <display-name>Hello sample web-application</display-name>
+
<servlet>
<servlet-name>some-init-servlet</servlet-name>
<servlet-class>wombat.InitServlet</servlet-class>
@@ -24,5 +27,9 @@
<env-entry-value>hello</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
+
+ <listener>
+
<listener-class>org.apache.log4j.selector.servlet.ContextDetachingSCL</listener-class>
+ </listener>
</web-app>
1.3 +1 -0 logging-log4j/examples/tiny-webapp/Hello/.cvsignore
Index: .cvsignore
===================================================================
RCS file: /home/cvs/logging-log4j/examples/tiny-webapp/Hello/.cvsignore,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- .cvsignore 27 Mar 2004 17:34:02 -0000 1.2
+++ .cvsignore 30 Mar 2004 10:45:21 -0000 1.3
@@ -1,2 +1,3 @@
build.properties
hello.war
+classes
1.3 +1 -1
logging-log4j/examples/tiny-webapp/Hello/src/java/wombat/HelloServlet.java
Index: HelloServlet.java
===================================================================
RCS file:
/home/cvs/logging-log4j/examples/tiny-webapp/Hello/src/java/wombat/HelloServlet.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- HelloServlet.java 27 Mar 2004 17:31:56 -0000 1.2
+++ HelloServlet.java 30 Mar 2004 10:45:21 -0000 1.3
@@ -14,7 +14,7 @@
ServletContext context = getServletConfig().getServletContext();
logger.info("Servlet loaded");
}
-
+
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
1.3 +1 -1 logging-log4j/examples/tiny-webapp/Tata/.cvsignore
Index: .cvsignore
===================================================================
RCS file: /home/cvs/logging-log4j/examples/tiny-webapp/Tata/.cvsignore,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- .cvsignore 27 Mar 2004 17:36:33 -0000 1.2
+++ .cvsignore 30 Mar 2004 10:45:21 -0000 1.3
@@ -1,3 +1,3 @@
classes
build.properties
-tata.war
+tata.war
\ No newline at end of file
1.8 +27 -45
logging-log4j/src/java/org/apache/log4j/selector/ContextJNDISelector.java
Index: ContextJNDISelector.java
===================================================================
RCS file:
/home/cvs/logging-log4j/src/java/org/apache/log4j/selector/ContextJNDISelector.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- ContextJNDISelector.java 26 Mar 2004 08:14:59 -0000 1.7
+++ ContextJNDISelector.java 30 Mar 2004 10:45:21 -0000 1.8
@@ -20,8 +20,8 @@
import org.apache.log4j.Level;
import org.apache.log4j.helpers.Constants;
import org.apache.log4j.helpers.IntializationUtil;
+import org.apache.log4j.helpers.JNDIUtil;
import org.apache.log4j.helpers.Loader;
-import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggerRepository;
import org.apache.log4j.spi.RepositorySelector;
@@ -33,7 +33,6 @@
import java.util.Map;
import javax.naming.Context;
-import javax.naming.InitialContext;
import javax.naming.NamingException;
@@ -107,27 +106,16 @@
* and setting context name will be enough to ensure a separate logging
* environment for your applicaiton.
*
- * <p>Unlike the [EMAIL PROTECTED] 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
- * is the recommended context selector. However it is possible 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,
- * for instance, 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. Until this issue
- * is solved by container-controlled repository selectors, you will need to
- * be diligent in providing a distinctive env-entry-value for each application
- * running on the server. This is not an issue when using the
- * [EMAIL PROTECTED] ContextClassLoaderSelector} in containers in which it is
compatible
- * (such as Tomcat 4/5)</p>
- *
+ * <p>Given that JNDI is part of the J2EE specification, the JNDI selector
+ * is the recommended context selector.
+ * </p>
+ *
* @author <a href="mailto:[EMAIL PROTECTED]">Jacob Kjome</a>
* @author Ceki Gülcü
* @since 1.3
*/
public class ContextJNDISelector implements RepositorySelector {
- static String JNDI_CONTEXT_NAME = "java:comp/env/log4j/context-name";
+
static String JNDI_CONFIGURATION_RESOURCE =
"java:comp/env/log4j/configuration-resource";
static String JNDI_CONFIGURATOR_CLASS =
@@ -162,45 +150,43 @@
}
/**
- * implemented RepositorySelector interface method. The returned
- * value is guaranteed to be non-null.
+ * Return the repoistory selector based on the current JNDI environment.
+ *
+ * If the respository is retreived for the first time, then also configure
+ * the repository using a user specified resource or if no such resource
+ * is specified, using the resource names "log4j.xml" or "log4j.properties".
*
- * @return the appropriate JNDI-keyed Hierarchy/LoggerRepository
+ * @return the appropriate JNDI-keyed context name/LoggerRepository
*/
public LoggerRepository getLoggerRepository() {
String loggingContextName = null;
Context ctx = null;
try {
- ctx = new InitialContext();
- loggingContextName = (String) ctx.lookup(JNDI_CONTEXT_NAME);
+ ctx = JNDIUtil.getInitialContext();
+ loggingContextName = (String) JNDIUtil.lookup(ctx,
Constants.JNDI_CONTEXT_NAME);
} catch (NamingException ne) {
// we can't log here
- //debug minor issue in Tomcat5 where, after the first webapp install,
- //the second webapp first fails the JNDI lookup and Log4j reports that
- //"no appenders could be found". Subsequent webapp installs report the
- //same except with no "no appenders could be found" message. However,
- //the appender do indeed work so I'm not sure why it is reported that
- //they don't? No issues like this in Tomcat4.
- //System.out.println("failed to look up logging context!");
- ;
}
if (loggingContextName == null) {
return defaultRepository;
} else {
+ System.out.println("loggingContextName is ["+loggingContextName+"]");
Hierarchy hierarchy = (Hierarchy) hierMap.get(loggingContextName);
if (hierarchy == null) {
// create new hierarchy
hierarchy = new Hierarchy(new RootCategory(Level.DEBUG));
+ hierarchy.setName(loggingContextName);
hierMap.put(loggingContextName, hierarchy);
- // Use automatic configration to configure the default hierarchy
+ // configure log4j internal logging
IntializationUtil.log4jInternalConfiguration(hierarchy);
- String configResourceStr = lookup(ctx, JNDI_CONFIGURATION_RESOURCE);
- String configuratorClassName = lookup(ctx, JNDI_CONFIGURATOR_CLASS);
+ // Use automatic configration to configure the default hierarchy
+ String configResourceStr = JNDIUtil.lookup(ctx,
JNDI_CONFIGURATION_RESOURCE);
+ String configuratorClassName = JNDIUtil.lookup(ctx,
JNDI_CONFIGURATOR_CLASS);
if (configResourceStr == null) {
if (
@@ -220,19 +206,15 @@
}
}
- String lookup(Context ctx, String name) {
- try {
- return (String) ctx.lookup(name);
- } catch (NamingException e) {
- LogLog.warn("Failed to get "+name);
- return null;
- }
- }
+
- /** Remove the repository with the given context name from the list of
+ /**
+ * Remove the repository with the given context name from the list of
* known repositories.
+ *
+ * @return
*/
- public void remove(String contextName) {
- hierMap.remove(contextName);
+ public LoggerRepository detachRepository(String contextName) {
+ return (LoggerRepository) hierMap.remove(contextName);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]