Author: markt Date: Mon May 26 15:16:45 2014 New Revision: 1597602 URL: http://svn.apache.org/r1597602 Log: Implement second part of pull request from Greg Wilkins to make Jasper more independent from Tomcat. Add ServiceLoader based discovery support to the JULI LogFactory
Modified: tomcat/trunk/java/org/apache/juli/logging/LogFactory.java tomcat/trunk/webapps/docs/changelog.xml Modified: tomcat/trunk/java/org/apache/juli/logging/LogFactory.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/juli/logging/LogFactory.java?rev=1597602&r1=1597601&r2=1597602&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/juli/logging/LogFactory.java (original) +++ tomcat/trunk/java/org/apache/juli/logging/LogFactory.java Mon May 26 15:16:45 2014 @@ -16,33 +16,34 @@ */ package org.apache.juli.logging; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.ServiceLoader; import java.util.logging.LogManager; /** - * Modified LogFactory: removed all discovery, hardcode a specific implementation - * If you like a different logging implementation - use either the discovery-based - * commons-logging, or better - another implementation hardcoded to your favourite - * logging impl. - * - * Why ? Each application and deployment can choose a logging implementation - - * that involves configuration, installing the logger jar and optional plugins, etc. - * As part of this process - they can as well install the commons-logging implementation - * that corresponds to their logger of choice. This completely avoids any discovery - * problem, while still allowing the user to switch. - * - * Note that this implementation is not just a wrapper around JDK logging ( like - * the original commons-logging impl ). It adds 2 features - a simpler configuration - * ( which is in fact a subset of log4j.properties ) and a formatter that is - * less ugly. - * - * The removal of 'abstract' preserves binary backward compatibility. It is possible - * to preserve the abstract - and introduce another ( hardcoded ) factory - but I - * see no benefit. - * - * Since this class is not intended to be extended - and provides - * no plugin for other LogFactory implementation - all protected methods are removed. - * This can be changed - but again, there is little value in keeping dead code. - * Just take a quick look at the removed code ( and it's complexity) + * This is a modified LogFactory that uses a simple {@link ServiceLoader} based + * discovery mechanism with a default of using JDK based logging. An + * implementation that uses the full Commons Logging discovery mechanism is + * available as part of the Tomcat extras download. + * + * Why? It is an attempt to strike a balance between simpler code (no discovery) + * and providing flexibility - particularly for those projects that embed Tomcat + * or some of Tomcat's components - is an alternative logging + * implementationnis desired. + * + * Note that this implementation is not just a wrapper around JDK logging (like + * the original commons-logging impl). It adds 2 features - a simpler + * configuration (which is in fact a subset of log4j.properties) and a + * formatter that is less ugly. + * + * The removal of 'abstract' preserves binary backward compatibility. It is + * possible to preserve the abstract - and introduce another (hardcoded) factory + * - but I see no benefit. + * + * Since this class is not intended to be extended - all protected methods are + * removed. This can be changed - but again, there is little value in keeping + * dead code. Just take a quick look at the removed code ( and it's complexity). * * -------------- * @@ -64,11 +65,26 @@ public class LogFactory { private static final LogFactory singleton = new LogFactory(); + private final Constructor<? extends Log> discoveredLogConstructor; /** - * Protected constructor that is not available for public use. + * Private constructor that is not available for public use. */ private LogFactory() { + // Look via a ServiceLoader for a Log implementation that has a + // constructor taking the String name. + ServiceLoader<Log> logLoader = ServiceLoader.load(Log.class); + Constructor<? extends Log> m=null; + for (Log log: logLoader) { + Class<? extends Log> c=log.getClass(); + try { + m=c.getConstructor(String.class); + } + catch (NoSuchMethodException | SecurityException e) { + throw new Error(e); + } + } + discoveredLogConstructor=m; } @@ -93,9 +109,17 @@ public class LogFactory { * @exception LogConfigurationException if a suitable <code>Log</code> * instance cannot be returned */ - public Log getInstance(String name) - throws LogConfigurationException { - return DirectJDKLog.getInstance(name); + public Log getInstance(String name) throws LogConfigurationException { + if (discoveredLogConstructor == null) { + return DirectJDKLog.getInstance(name); + } + + try { + return discoveredLogConstructor.newInstance(name); + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | + InvocationTargetException e) { + throw new RuntimeException(e); + } } @@ -108,8 +132,7 @@ public class LogFactory { * @exception LogConfigurationException if a suitable <code>Log</code> * instance cannot be returned */ - public Log getInstance(Class<?> clazz) - throws LogConfigurationException { + public Log getInstance(Class<?> clazz) throws LogConfigurationException { return getInstance( clazz.getName()); } Modified: tomcat/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1597602&r1=1597601&r2=1597602&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/changelog.xml (original) +++ tomcat/trunk/webapps/docs/changelog.xml Mon May 26 15:16:45 2014 @@ -105,6 +105,12 @@ <code>=</code> character leading to a mis-match between the cookie the server set and the cookie returned by the browser. (jboynes/markt) </fix> + <add> + Add a simple <code>ServiceLoader</code> based discovery mechanism to the + JULI <code>LogFactory</code> to make it easier to use JULI and Tomcat + components that depend on JULI (such as Jasper) independently from + Tomcat. Patch provided by Greg Wilkins. (markt) + </add> </changelog> </subsection> <subsection name="Coyote"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org