|
What this is
Other links
The source code/* * Copyright 2001-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.commons.logging.impl; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; 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; /** *
If the selected {@link Log} implementation class has a
* This factory will remember previously created The name of the {@link Log} interface class. */ private static final String LOG_INTERFACE = "org.apache.commons.logging.Log"; // ----------------------------------------------------- Instance Variables /** * 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 bygetLogConstructor() ,
* 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 setLogFactory method of the selected
* {@link org.apache.commons.logging.Log} method, if it exists.
*/
protected Method logMethod = null;
/**
* The signature of the setLogFactory method to be used.
*/
protected Class logMethodSignature[] =
{ LogFactory.class };
// --------------------------------------------------------- Public Methods
/**
* Return the configuration attribute with the specified name (if any),
* or null 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 getInstance(String) with it.
*
* @param clazz Class for which a suitable Log name will be derived
*
* @exception LogConfigurationException if a suitable Log
* instance cannot be returned
*/
public Log getInstance(Class clazz) throws LogConfigurationException {
return (getInstance(clazz.getName()));
}
/**
* Construct (if necessary) and return a NOTE - Depending upon the implementation of
* the Log 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 Log
* 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() {
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 null value is equivalent to calling
* removeAttribute(name) .
*
* @param name Name of the attribute to set
* @param value Value of the attribute to set, or null
* to remove any setting for this attribute
*/
public void setAttribute(String name, Object value) {
if (value == null) {
attributes.remove(name);
} else {
attributes.put(name, value);
}
}
// ------------------------------------------------------ Protected Methods
/**
* Return the fully qualified Java classname of the {@link Log}
* implementation we will be using.
*/
protected String getLogClassName() {
// Return the previously identified class name (if any)
if (logClassName != null) {
return logClassName;
}
logClassName = (String) getAttribute(LOG_PROPERTY);
if (logClassName == null) { // @deprecated
logClassName = (String) getAttribute(LOG_PROPERTY_OLD);
}
if (logClassName == null) {
try {
logClassName = System.getProperty(LOG_PROPERTY);
} catch (SecurityException e) {
;
}
}
if (logClassName == null) { // @deprecated
try {
logClassName = System.getProperty(LOG_PROPERTY_OLD);
} catch (SecurityException e) {
;
}
}
if ((logClassName == null) && isLog4JAvailable()) {
logClassName = "org.apache.commons.logging.impl.Log4JLogger";
}
if ((logClassName == null) && isJdk14Available()) {
logClassName = "org.apache.commons.logging.impl.Jdk14Logger";
}
if ((logClassName == null) && isJdk13LumberjackAvailable()) {
logClassName = "org.apache.commons.logging.impl.Jdk13LumberjackLogger";
}
if (logClassName == null) {
logClassName = "org.apache.commons.logging.impl.SimpleLog";
}
return (logClassName);
}
/**
* Return the IMPLEMENTATION NOTE - Race conditions caused by
* calling this method from more than one thread are ignored, because
* the same setLogFactory method (if there is one)
try {
logMethod = logClass.getMethod("setLogFactory",
logMethodSignature);
} catch (Throwable t) {
logMethod = null;
}
// Identify the corresponding constructor to be used
try {
logConstructor = logClass.getConstructor(logConstructorSignature);
return (logConstructor);
} catch (Throwable t) {
throw new LogConfigurationException
("No suitable Log constructor " +
logConstructorSignature+ " for " + logClassName, t);
}
}
/**
* MUST KEEP THIS METHOD PRIVATE.
*
* Exposing this method outside of
* Return |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.