📄 logfactoryimpl.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.impl;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogConfigurationException;
import org.apache.commons.logging.LogFactory;
/**
* <p>Concrete subclass of {@link LogFactory} that implements the
* following algorithm to dynamically select a logging implementation
* class to instantiate a wrapper for.</p>
* <ul>
* <li>Use a factory configuration attribute named
* <code>org.apache.commons.logging.Log</code> to identify the
* requested implementation class.</li>
* <li>Use the <code>org.apache.commons.logging.Log</code> system property
* to identify the requested implementation class.</li>
* <li>If <em>Log4J</em> is available, return an instance of
* <code>org.apache.commons.logging.impl.Log4JLogger</code>.</li>
* <li>If <em>JDK 1.4 or later</em> is available, return an instance of
* <code>org.apache.commons.logging.impl.Jdk14Logger</code>.</li>
* <li>Otherwise, return an instance of
* <code>org.apache.commons.logging.impl.SimpleLog</code>.</li>
* </ul>
*
* <p>If the selected {@link Log} implementation class has a
* <code>setLogFactory()</code> method that accepts a {@link LogFactory}
* parameter, this method will be called on each newly created instance
* to identify the associated factory. This makes factory configuration
* attributes available to the Log instance, if it so desires.</p>
*
* <p>This factory will remember previously created <code>Log</code> instances
* for the same name, and will return them on repeated requests to the
* <code>getInstance()</code> method.</p>
*
* @author Rod Waldhoff
* @author Craig R. McClanahan
* @author Richard A. Sitze
* @author Brian Stansberry
* @version $Revision: 581090 $ $Date: 2007-10-02 00:01:06 +0200 (ti, 02 okt 2007) $
*/
public class LogFactoryImpl extends LogFactory {
/** Log4JLogger class name */
private static final String LOGGING_IMPL_LOG4J_LOGGER = "org.apache.commons.logging.impl.Log4JLogger";
/** Jdk14Logger class name */
private static final String LOGGING_IMPL_JDK14_LOGGER = "org.apache.commons.logging.impl.Jdk14Logger";
/** Jdk13LumberjackLogger class name */
private static final String LOGGING_IMPL_LUMBERJACK_LOGGER = "org.apache.commons.logging.impl.Jdk13LumberjackLogger";
/** SimpleLog class name */
private static final String LOGGING_IMPL_SIMPLE_LOGGER = "org.apache.commons.logging.impl.SimpleLog";
private static final String PKG_IMPL="org.apache.commons.logging.impl.";
private static final int PKG_LEN = PKG_IMPL.length();
// ----------------------------------------------------------- Constructors
/**
* Public no-arguments constructor required by the lookup mechanism.
*/
public LogFactoryImpl() {
super();
initDiagnostics(); // method on this object
if (isDiagnosticsEnabled()) {
logDiagnostic("Instance created.");
}
}
// ----------------------------------------------------- Manifest Constants
/**
* The name (<code>org.apache.commons.logging.Log</code>) of the system
* property identifying our {@link Log} implementation class.
*/
public static final String LOG_PROPERTY =
"org.apache.commons.logging.Log";
/**
* The deprecated system property used for backwards compatibility with
* old versions of JCL.
*/
protected static final String LOG_PROPERTY_OLD =
"org.apache.commons.logging.log";
/**
* The name (<code>org.apache.commons.logging.Log.allowFlawedContext</code>)
* of the system property which can be set true/false to
* determine system behaviour when a bad context-classloader is encountered.
* When set to false, a LogConfigurationException is thrown if
* LogFactoryImpl is loaded via a child classloader of the TCCL (this
* should never happen in sane systems).
*
* Default behaviour: true (tolerates bad context classloaders)
*
* See also method setAttribute.
*/
public static final String ALLOW_FLAWED_CONTEXT_PROPERTY =
"org.apache.commons.logging.Log.allowFlawedContext";
/**
* The name (<code>org.apache.commons.logging.Log.allowFlawedDiscovery</code>)
* of the system property which can be set true/false to
* determine system behaviour when a bad logging adapter class is
* encountered during logging discovery. When set to false, an
* exception will be thrown and the app will fail to start. When set
* to true, discovery will continue (though the user might end up
* with a different logging implementation than they expected).
*
* Default behaviour: true (tolerates bad logging adapters)
*
* See also method setAttribute.
*/
public static final String ALLOW_FLAWED_DISCOVERY_PROPERTY =
"org.apache.commons.logging.Log.allowFlawedDiscovery";
/**
* The name (<code>org.apache.commons.logging.Log.allowFlawedHierarchy</code>)
* of the system property which can be set true/false to
* determine system behaviour when a logging adapter class is
* encountered which has bound to the wrong Log class implementation.
* When set to false, an exception will be thrown and the app will fail
* to start. When set to true, discovery will continue (though the user
* might end up with a different logging implementation than they expected).
*
* Default behaviour: true (tolerates bad Log class hierarchy)
*
* See also method setAttribute.
*/
public static final String ALLOW_FLAWED_HIERARCHY_PROPERTY =
"org.apache.commons.logging.Log.allowFlawedHierarchy";
/**
* The names of classes that will be tried (in order) as logging
* adapters. Each class is expected to implement the Log interface,
* and to throw NoClassDefFound or ExceptionInInitializerError when
* loaded if the underlying logging library is not available. Any
* other error indicates that the underlying logging library is available
* but broken/unusable for some reason.
*/
private static final String[] classesToDiscover = {
LOGGING_IMPL_LOG4J_LOGGER,
"org.apache.commons.logging.impl.Jdk14Logger",
"org.apache.commons.logging.impl.Jdk13LumberjackLogger",
"org.apache.commons.logging.impl.SimpleLog"
};
// ----------------------------------------------------- Instance Variables
/**
* Determines whether logging classes should be loaded using the thread-context
* classloader, or via the classloader that loaded this LogFactoryImpl class.
*/
private boolean useTCCL = true;
/**
* The string prefixed to every message output by the logDiagnostic method.
*/
private String diagnosticPrefix;
/**
* Configuration attributes.
*/
protected Hashtable attributes = new Hashtable();
/**
* The {@link org.apache.commons.logging.Log} instances that have
* already been created, keyed by logger name.
*/
protected Hashtable instances = new Hashtable();
/**
* Name of the class implementing the Log interface.
*/
private String logClassName;
/**
* The one-argument constructor of the
* {@link org.apache.commons.logging.Log}
* implementation class that will be used to create new instances.
* This value is initialized by <code>getLogConstructor()</code>,
* and then returned repeatedly.
*/
protected Constructor logConstructor = null;
/**
* The signature of the Constructor to be used.
*/
protected Class logConstructorSignature[] =
{ java.lang.String.class };
/**
* The one-argument <code>setLogFactory</code> method of the selected
* {@link org.apache.commons.logging.Log} method, if it exists.
*/
protected Method logMethod = null;
/**
* The signature of the <code>setLogFactory</code> method to be used.
*/
protected Class logMethodSignature[] =
{ LogFactory.class };
/**
* See getBaseClassLoader and initConfiguration.
*/
private boolean allowFlawedContext;
/**
* See handleFlawedDiscovery and initConfiguration.
*/
private boolean allowFlawedDiscovery;
/**
* See handleFlawedHierarchy and initConfiguration.
*/
private boolean allowFlawedHierarchy;
// --------------------------------------------------------- Public Methods
/**
* Return the configuration attribute with the specified name (if any),
* or <code>null</code> if there is no such attribute.
*
* @param name Name of the attribute to return
*/
public Object getAttribute(String name) {
return (attributes.get(name));
}
/**
* Return an array containing the names of all currently defined
* configuration attributes. If there are no such attributes, a zero
* length array is returned.
*/
public String[] getAttributeNames() {
Vector names = new Vector();
Enumeration keys = attributes.keys();
while (keys.hasMoreElements()) {
names.addElement((String) keys.nextElement());
}
String results[] = new String[names.size()];
for (int i = 0; i < results.length; i++) {
results[i] = (String) names.elementAt(i);
}
return (results);
}
/**
* Convenience method to derive a name from the specified class and
* call <code>getInstance(String)</code> with it.
*
* @param clazz Class for which a suitable Log name will be derived
*
* @exception LogConfigurationException if a suitable <code>Log</code>
* instance cannot be returned
*/
public Log getInstance(Class clazz) throws LogConfigurationException {
return (getInstance(clazz.getName()));
}
/**
* <p>Construct (if necessary) and return a <code>Log</code> instance,
* using the factory's current set of configuration attributes.</p>
*
* <p><strong>NOTE</strong> - Depending upon the implementation of
* the <code>LogFactory</code> you are using, the <code>Log</code>
* instance you are returned may or may not be local to the current
* application, and may or may not be returned again on a subsequent
* call with the same name argument.</p>
*
* @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)
*
* @exception LogConfigurationException if a suitable <code>Log</code>
* instance cannot be returned
*/
public Log getInstance(String name) throws LogConfigurationException {
Log instance = (Log) instances.get(name);
if (instance == null) {
instance = newInstance(name);
instances.put(name, instance);
}
return (instance);
}
/**
* Release any internal references to previously created
* {@link org.apache.commons.logging.Log}
* instances returned by this factory. This is useful in environments
* like servlet containers, which implement application reloading by
* throwing away a ClassLoader. Dangling references to objects in that
* class loader would prevent garbage collection.
*/
public void release() {
logDiagnostic("Releasing all known loggers");
instances.clear();
}
/**
* Remove any configuration attribute associated with the specified name.
* If there is no such attribute, no action is taken.
*
* @param name Name of the attribute to remove
*/
public void removeAttribute(String name) {
attributes.remove(name);
}
/**
* Set the configuration attribute with the specified name. Calling
* this with a <code>null</code> value is equivalent to calling
* <code>removeAttribute(name)</code>.
* <p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -