You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sk...@apache.org on 2006/03/02 10:06:18 UTC

svn commit: r382327 - in /jakarta/commons/proper/logging/contrib/simon/jcl2: build.xml src/core-dynamic/java/org/apache/commons/logging/LogFactoryDynamic.java src/core/java/org/apache/commons/logging/Utils.java

Author: skitching
Date: Thu Mar  2 01:06:17 2006
New Revision: 382327

URL: http://svn.apache.org/viewcvs?rev=382327&view=rev
Log:
Implement LogFactoryDynamic, ie TCCL-based log config mgmt.

Added:
    jakarta/commons/proper/logging/contrib/simon/jcl2/src/core-dynamic/java/org/apache/commons/logging/LogFactoryDynamic.java   (with props)
Modified:
    jakarta/commons/proper/logging/contrib/simon/jcl2/build.xml
    jakarta/commons/proper/logging/contrib/simon/jcl2/src/core/java/org/apache/commons/logging/Utils.java

Modified: jakarta/commons/proper/logging/contrib/simon/jcl2/build.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/contrib/simon/jcl2/build.xml?rev=382327&r1=382326&r2=382327&view=diff
==============================================================================
--- jakarta/commons/proper/logging/contrib/simon/jcl2/build.xml (original)
+++ jakarta/commons/proper/logging/contrib/simon/jcl2/build.xml Thu Mar  2 01:06:17 2006
@@ -244,7 +244,7 @@
           description="Compile shareable components"/>
 
   <target name="compile-only" 
-    depends="prepare,compile-core,compile-core-static,compile-noop,compile-simple,compile-log4j"/>
+    depends="prepare,compile-core,compile-core-static,compile-core-dynamic,compile-noop,compile-simple,compile-log4j"/>
 
   <target name="compile-core" depends="prepare">
   	<mkdir dir="src/core/classes"/>
@@ -295,6 +295,42 @@
       </fileset>
     </jar>
   	
+  </target>
+
+
+  <target name="compile-core-dynamic" depends="prepare,compile-core">
+  	<mkdir dir="src/core-dynamic/classes"/>
+  	<mkdir dir="src/core-dynamic/classes/META-INF"/>
+  	<mkdir dir="src/core-dynamic/classes/META-INF/services"/>
+  	<copy file="src/core-dynamic/conf/org.apache.commons.logging.LogFactory"
+  		todir="src/core-dynamic/classes/META-INF/services"/>
+    <javac srcdir="src/core-dynamic/java"
+           destdir="src/core-dynamic/classes"
+           debug="${compile.debug}"
+           deprecation="${compile.deprecation}"
+           optimize="${compile.optimize}"
+           source="${source.version}"
+           target="${target.version}">
+
+      <classpath refid="compile.classpath"/>
+      <classpath>
+        <pathelement location="src/core/classes"/>
+      </classpath>
+    </javac>
+    <jar jarfile="src/core-dynamic/commons-logging-core-dynamic.jar"
+         manifest="${build.home}/conf/MANIFEST.MF">
+      <fileset dir="src/core-dynamic/classes">
+        <include name="org/apache/commons/logging/**" />
+        <include name="META-INF/**" />
+        <exclude name="**/package.html"/>
+      </fileset>
+      <fileset dir="src/core/classes">
+        <include name="org/apache/commons/logging/**" />
+        <include name="META-INF/LICENSE.txt"/>
+        <include name="META-INF/NOTICE.txt"/>
+        <exclude name="**/package.html"/>
+      </fileset>
+    </jar>
   </target>
 
   <target name="compile-noop" depends="prepare, compile-core">

Added: jakarta/commons/proper/logging/contrib/simon/jcl2/src/core-dynamic/java/org/apache/commons/logging/LogFactoryDynamic.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/contrib/simon/jcl2/src/core-dynamic/java/org/apache/commons/logging/LogFactoryDynamic.java?rev=382327&view=auto
==============================================================================
--- jakarta/commons/proper/logging/contrib/simon/jcl2/src/core-dynamic/java/org/apache/commons/logging/LogFactoryDynamic.java (added)
+++ jakarta/commons/proper/logging/contrib/simon/jcl2/src/core-dynamic/java/org/apache/commons/logging/LogFactoryDynamic.java Thu Mar  2 01:06:17 2006
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2001-2006 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.commons.logging;
+
+import java.util.WeakHashMap;
+
+
+/**
+ * Factory for creating {@link Log} instances.
+ * <p>
+ * This class is intended for deployment into the shared classpath of some
+ * kind of container, eg a J2EE server. It tries to load the LogHandler
+ * class used via the TCCL, so that:
+ * <ol>
+ * <li>the concrete logging library used by a webapp can be deployed in the
+ * webapp rather than having to be in the shared classpath, and
+ * <li>logging configuration can be specified at the webapp level even when
+ * the logging library isn't TCCL-aware.
+ * </ol>
+ * <p>
+ * Stand-alone applications don't need to deploy this class as there is no
+ * child ClassLoader to worry about. Webapps themselves don't need to deploy
+ * this class either; again there is no child ClassLoader of the webapp's
+ * classloader. Only when commons-logging is deployed in a container's shared
+ * classpath is this useful. 
+ */
+
+public final class LogFactoryDynamic extends LogFactory {
+
+    private WeakHashMap handlers = new WeakHashMap();
+
+    public LogFactoryDynamic() {
+    }
+
+    /**
+     * Return a named logger.
+     *
+     * @param clazz Class from which a log name will be derived
+     */
+    public Log getInstance(Class clazz) {
+        return getHandler().getLog(clazz);
+    }
+
+    /**
+     * @param name Logical name of the <code>Log</code> instance to be
+     *  returned (the meaning of this name is only known to the underlying
+     *  logging implementation that is being wrapped)
+     */
+    public Log getInstance(String name) {
+        return getHandler().getLog(name);
+    }
+    
+    public void releaseInstance() {
+        getHandler().release();
+    }
+    
+    private synchronized LogHandler getHandler() {
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+        LogHandler handler = (LogHandler) handlers.get(tccl);
+        if (handler == null) {
+            handler = (LogHandler) Utils.createServiceInstance(LogHandler.class, tccl);
+            handlers.put(tccl, handler);
+        }
+        return handler;
+    }
+}

Propchange: jakarta/commons/proper/logging/contrib/simon/jcl2/src/core-dynamic/java/org/apache/commons/logging/LogFactoryDynamic.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/logging/contrib/simon/jcl2/src/core-dynamic/java/org/apache/commons/logging/LogFactoryDynamic.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: jakarta/commons/proper/logging/contrib/simon/jcl2/src/core/java/org/apache/commons/logging/Utils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/logging/contrib/simon/jcl2/src/core/java/org/apache/commons/logging/Utils.java?rev=382327&r1=382326&r2=382327&view=diff
==============================================================================
--- jakarta/commons/proper/logging/contrib/simon/jcl2/src/core/java/org/apache/commons/logging/Utils.java (original)
+++ jakarta/commons/proper/logging/contrib/simon/jcl2/src/core/java/org/apache/commons/logging/Utils.java Thu Mar  2 01:06:17 2006
@@ -35,22 +35,60 @@
     }
 
     /**
-     * Each jarfile that contains this class is also expected to contain:
-     * <ul>
-     * <li>A concrete subclass of this class, and
-     * <li>A META-INF/services/org.apache.commons.logging.LogFactory file. 
-     * </ul>
+     * Implement the standard Java Service Provider pattern which allows
+     * a jarfile's META-INF to specify that certain classes within it 
+     * implement specific standard "service provider interfaces".
+     * <p>
+     * The baseClass parameter is the interface or abstract class that
+     * one or more jars in the classpath are expected to implement. The
+     * implementing jar(s) are expected to contain a file of name
+     * <code>META-INF/services/service.name</code> where service.name
+     * matches the fully qualified name of the provided base class.
+     * <p>
      * The file contains a single non-comment line containing the name of
      * the concrete class. An instance of that type will be created, and
-     * all methods on this class then simply delegate to that instance.
+     * returned.
+     * <p>
+     * The classloader that is used as the base of the search for the
+     * service file is the one that loaded the specified base class. The
+     * first service file found will be used.
      */
     public static Object createServiceInstance(Class baseClass) 
     throws LogConfigurationException {
+        
+        return createServiceInstance(baseClass, null);
+    }
+
+    /**
+     * Implement the standard Java Service Provider pattern which allows
+     * a jarfile's META-INF to specify that certain classes within it 
+     * implement specific standard "service provider interfaces".
+     * <p>
+     * This is the same as the createServiceInstance(Class) method except
+     * that the classloader used as the base of the search is the specified
+     * one. This is expected to be some child classloader of the one that
+     * loaded the base class (otherwise the service class found won't be
+     * castable to the desired base type).
+     * <p>
+     * Passing null as the baseLoader indicates that the loader which loaded
+     * baseClass should be used. 
+     */
+    public static Object createServiceInstance(Class baseClass, ClassLoader baseLoader) 
+    throws LogConfigurationException {
 
         String serviceName = baseClass.getName();
         String serviceFileName = "/META-INF/services/" + serviceName;
-        InputStream is = baseClass.getResourceAsStream(serviceFileName);
+        InputStream is;
         
+        if (baseLoader == null) {
+            // Don't call baseClass.getClassLoader here, as that may cause
+            // a SecurityException in some environments. Just use the method
+            // on the Class object which should have the same effect.
+            is = baseClass.getResourceAsStream(serviceFileName);
+        } else {
+            is = baseLoader.getResourceAsStream(serviceFileName);
+        }
+
         if (is == null) {
             throw new LogConfigurationException(
                 "Service '" + serviceName + "'"



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