rsitze 2002/10/19 10:25:04
Modified: logging/src/java/org/apache/commons/logging/impl
LogFactoryImpl.java
Log:
- code cleanup, refactoring, and corrected a few undiscoved bugs..
- Bugzilla 13157 - Log4j takes undue precedence over Log override.
Revision Changes Path
1.17 +66 -55
jakarta-commons/logging/src/java/org/apache/commons/logging/impl/LogFactoryImpl.java
Index: LogFactoryImpl.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/logging/src/java/org/apache/commons/logging/impl/LogFactoryImpl.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- LogFactoryImpl.java 27 Sep 2002 02:16:44 -0000 1.16
+++ LogFactoryImpl.java 19 Oct 2002 17:25:04 -0000 1.17
@@ -152,6 +152,9 @@
"org.apache.commons.logging.log";
+ private static final String LOG4JLOGIMPL =
+ "org.apache.commons.logging.impl.Log4JCategoryLog".intern();
+
// ----------------------------------------------------- Instance Variables
@@ -169,6 +172,11 @@
/**
+ * 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.
@@ -213,8 +221,8 @@
public Object getAttribute(String name) {
if( proxyFactory != null )
return proxyFactory.getAttribute( name );
- return (attributes.get(name));
+ return attributes.get(name);
}
@@ -236,8 +244,7 @@
for (int i = 0; i < results.length; i++) {
results[i] = (String) names.elementAt(i);
}
- return (results);
-
+ return results;
}
@@ -250,14 +257,11 @@
* @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 {
if( proxyFactory != null )
return proxyFactory.getInstance(clazz);
- return (getInstance(clazz.getName()));
-
+ return getInstance(clazz.getName());
}
@@ -278,9 +282,7 @@
* @exception LogConfigurationException if a suitable <code>Log</code>
* instance cannot be returned
*/
- public Log getInstance(String name)
- throws LogConfigurationException
- {
+ public Log getInstance(String name) throws LogConfigurationException {
if( proxyFactory != null )
return proxyFactory.getInstance(name);
@@ -289,8 +291,7 @@
instance = newInstance(name);
instances.put(name, instance);
}
- return (instance);
-
+ return instance;
}
@@ -307,7 +308,6 @@
proxyFactory.release();
instances.clear();
-
}
@@ -320,7 +320,6 @@
public void removeAttribute(String name) {
if( proxyFactory != null )
proxyFactory.removeAttribute(name);
-
attributes.remove(name);
}
@@ -336,7 +335,7 @@
*/
public void setAttribute(String name, Object value) {
if( proxyFactory != null )
- proxyFactory.setAttribute(name,value);
+ proxyFactory.setAttribute(name, value);
if (value == null) {
attributes.remove(name);
@@ -350,34 +349,19 @@
// ------------------------------------------------------ Protected Methods
- /**
- * <p>Return the <code>Constructor</code> that can be called to instantiate
- * new {@link org.apache.commons.logging.Log} instances.</p>
- *
- * <p><strong>IMPLEMENTATION NOTE</strong> - Race conditions caused by
- * calling this method from more than one thread are ignored, because
- * the same <code>Constructor</code> instance will ultimately be derived
- * in all circumstances.</p>
- *
- * @exception LogConfigurationException if a suitable constructor
- * cannot be returned
- */
- protected Constructor getLogConstructor()
- throws LogConfigurationException {
-
- // Return the previously identified Constructor (if any)
- if (logConstructor != null) {
- return (logConstructor);
- }
+ protected String getLogClassName() {
// Identify the Log implementation class we will be using
- String logClassName = null;
- if (logClassName == null) {
- logClassName = (String) getAttribute(LOG_PROPERTY);
+ 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);
@@ -385,6 +369,7 @@
;
}
}
+
if (logClassName == null) { // @deprecated
try {
logClassName = System.getProperty(LOG_PROPERTY_OLD);
@@ -392,18 +377,46 @@
;
}
}
+
if ((logClassName == null) && isLog4JAvailable()) {
- logClassName =
- "org.apache.commons.logging.impl.Log4JCategoryLog";
+ logClassName = LOG4JLOGIMPL;
}
+
if ((logClassName == null) && isJdk14Available()) {
logClassName =
"org.apache.commons.logging.impl.Jdk14Logger";
}
+
if (logClassName == null) {
logClassName = LOG_DEFAULT;
}
+ return logClassName;
+ }
+
+
+ /**
+ * <p>Return the <code>Constructor</code> that can be called to instantiate
+ * new {@link org.apache.commons.logging.Log} instances.</p>
+ *
+ * <p><strong>IMPLEMENTATION NOTE</strong> - Race conditions caused by
+ * calling this method from more than one thread are ignored, because
+ * the same <code>Constructor</code> instance will ultimately be derived
+ * in all circumstances.</p>
+ *
+ * @exception LogConfigurationException if a suitable constructor
+ * cannot be returned
+ */
+ protected Constructor getLogConstructor()
+ throws LogConfigurationException {
+
+ // Return the previously identified Constructor (if any)
+ if (logConstructor != null) {
+ return logConstructor;
+ }
+
+ String logClassName = getLogClassName();
+
// Attempt to load the Log implementation class
Class logClass = null;
try {
@@ -437,13 +450,15 @@
("No suitable Log constructor " +
logConstructorSignature+ " for " + logClassName, t);
}
-
}
+
/**
* MUST KEEP THIS METHOD PRIVATE
*
- * <p>Exposing this method establishes a security violation.
+ * <p>Exposing this method outside of
+ * <code>org.apache.commons.logging.LogFactoryImpl</code>
+ * will create a security violation:
* This method uses <code>AccessController.doPrivileged()</code>.
* </p>
*
@@ -478,12 +493,13 @@
throw (ClassNotFoundException)result;
}
+
protected void guessConfig() {
- if( isLog4JAvailable() ) {
+ if (getLogClassName() == LOG4JLOGIMPL) {
proxyFactory = null;
try {
Class proxyClass=
- loadClass( "org.apache.commons.logging.impl.Log4jFactory" );
+ loadClass("org.apache.commons.logging.impl.Log4jFactory");
if (proxyClass != null) {
proxyFactory = (LogFactory)proxyClass.newInstance();
}
@@ -494,7 +510,7 @@
// other logger specific initialization
// ...
}
-
+
/**
* Is <em>JDK 1.4 or later</em> logging available?
@@ -508,7 +524,6 @@
} catch (Throwable t) {
return (false);
}
-
}
@@ -524,7 +539,6 @@
} catch (Throwable t) {
return (false);
}
-
}
@@ -537,9 +551,7 @@
* @exception LogConfigurationException if a new instance cannot
* be created
*/
- protected Log newInstance(String name)
- throws LogConfigurationException {
-
+ protected Log newInstance(String name) throws LogConfigurationException {
Log instance = null;
try {
@@ -554,6 +566,5 @@
} catch (Throwable t) {
throw new LogConfigurationException(t);
}
-
}
}
--
To unsubscribe, e-mail: <mailto:commons-dev-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@;jakarta.apache.org>