//======================================================================== //Copyright 1997-2006 Mort Bay Consulting Pty. Ltd. //------------------------------------------------------------------------ //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.mortbay.jetty; import java.io.File; import java.net.URL; import java.net.URLClassLoader; import org.mortbay.jetty.Log4JConfigLoader; import org.mortbay.jetty.Log4JRequestLog; /* ------------------------------------------------------------ */ /** * This abstract class provides static methods for configuring Log4J within a * separate class loader and for instantiating and providing access to Log4J * Loggers via interface to prevent any Log4J classes from being loaded within * the server's application class loader. * * Note that setLog4JJarPath(), setLog4JImplJarPath(), and setLog4JConfigPath() * must be set before configure() is called. Also, these methods only need to be * called once during server startup. Calls to these methods are included in * etc/jetty-logging-log4j.xml. * * The log4j-1.2.14.jar and jetty6-server-log4j-impl-6.1.7.jar files must be * specified and must not be included in the server's class path. * * @author Chance Yeoman */ public abstract class JettyServerLog4JConfig { private static final String DEFAULT_LOG4J_JAR_PATH = "../lib/log4j/log4j-1.2.14.jar"; private static final String DEFAULT_LOG4J_BRIDGE_JAR_PATH = "../lib/log4j/jetty6-server-log4j-impl-6.1.7.jar"; private static final String DEFAULT_LOG4J_CONFIG_PATH = "../etc/log4j-jetty-config.xml"; private static final String CONFIG_LOADER_IMPLEMENTATION_CLASS_NAME = "org.mortbay.jetty.Log4JConfigLoaderImpl"; private static final String LOGGER_IMPLEMENTATION_CLASS_NAME = "org.mortbay.jetty.Log4JRequestLogImpl"; private static String log4jJarPath = DEFAULT_LOG4J_JAR_PATH; private static String log4jBridgeJarPath = DEFAULT_LOG4J_BRIDGE_JAR_PATH; private static String log4jConfigPath = DEFAULT_LOG4J_CONFIG_PATH; private static URLClassLoader log4jLoader = null; /* ------------------------------------------------------------ */ /** * Sets the path to the standard log4j-1.2.14.jar file. */ public static void setLog4JJarPath(String pathToFile) { log4jJarPath = pathToFile; } /* ------------------------------------------------------------ */ /** * Sets the path to the new jetty6-server-log4j-impl-6.1.7.jar file. */ public static void setLog4JImplJarPath(String pathToFile) { log4jBridgeJarPath = pathToFile; } /* ------------------------------------------------------------ */ /** * Sets the path to the log4j configuration file. */ public static void setLog4JConfigPath(String pathToFile) { log4jConfigPath = pathToFile; } /* ------------------------------------------------------------ */ /** * If not yet configured, this method first creates a new class loader with * access to the standard log4j 1.2.14 package as well as the new package * that contains the logger implementations and a simple configuration * implementation. After the new class loader is created, it is used to * create a configuration implementation instance, which is then used to * load and configure Log4J classes. * * An Exception is thrown if this method is executed a second time. */ public static void configure() throws Exception { if (log4jLoader == null) { File log4jJarFile = new File(log4jJarPath); File log4jBridgeJarFile = new File(log4jBridgeJarPath); URL[] log4jJarURL = new URL[2]; log4jJarURL[0] = log4jJarFile.toURL(); log4jJarURL[1] = log4jBridgeJarFile.toURL(); log4jLoader = new URLClassLoader(log4jJarURL); } else { throw new IllegalStateException( "Log4JServerLog configuration has already been set."); } ((Log4JConfigLoader) log4jLoader.loadClass( CONFIG_LOADER_IMPLEMENTATION_CLASS_NAME).newInstance()) .loadLog4JConfig(log4jConfigPath); } /* ------------------------------------------------------------ */ /** * If not yet configured, this method throws an Exception. Otherwise, the * server's Log4J class loader is used to instantiate a Log4JRequestLog * implementation that is then returned. */ public static Log4JRequestLog getLog4JRequestLogInstance() throws Exception { if (log4jLoader == null) { throw new IllegalStateException( "Log4JServerLog configuration has not been set."); } return ((Log4JRequestLog) log4jLoader.loadClass( LOGGER_IMPLEMENTATION_CLASS_NAME).newInstance()); } }