[slf4j-dev] svn commit: r219 - in slf4j/trunk/src: filtered-java/org/slf4j java/org/slf4j java/org/slf4j/spi
ceki at slf4j.org
ceki at slf4j.org
Sun Aug 28 14:18:52 CEST 2005
Author: ceki
Date: Sun Aug 28 14:18:50 2005
New Revision: 219
Added:
slf4j/trunk/src/filtered-java/org/slf4j/StaticLoggerFactoryBinder.java
slf4j/trunk/src/java/org/slf4j/LoggerFactory.java
- copied, changed from r208, slf4j/trunk/src/filtered-java/org/slf4j/LoggerFactory.java
slf4j/trunk/src/java/org/slf4j/SystemPropBinder.java
slf4j/trunk/src/java/org/slf4j/spi/LoggerFactoryBinder.java
Removed:
slf4j/trunk/src/filtered-java/org/slf4j/LoggerFactory.java
Log:
Refactoring of LoggerFactory and ILoggerBinding. The actual binding operation
is now handled by a different class implementing LoggerFactoryBinder.
This modification remains compatible with the previously published interface
of SLF4J.
Added: slf4j/trunk/src/filtered-java/org/slf4j/StaticLoggerFactoryBinder.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/src/filtered-java/org/slf4j/StaticLoggerFactoryBinder.java Sun Aug 28 14:18:50 2005
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2004-2005 SLF4J.ORG
+ * Copyright (c) 2004-2005 QOS.ch
+ *
+ * All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, and/or sell copies of the Software, and to permit persons
+ * to whom the Software is furnished to do so, provided that the above
+ * copyright notice(s) and this permission notice appear in all copies of
+ * the Software and that both the above copyright notice(s) and this
+ * permission notice appear in supporting documentation.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
+ * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+ * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY
+ * SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Except as contained in this notice, the name of a copyright holder
+ * shall not be used in advertising or otherwise to promote the sale, use
+ * or other dealings in this Software without prior written authorization
+ * of the copyright holder.
+ *
+ */
+
+package org.slf4j;
+
+import org.slf4j.spi.LoggerFactoryBinder;
+
+//WARNING
+//WARNING Modifications MUST be made to the original file found at
+//WARNING $SLF4J_HOME/src/filtered-java/org/slf4j/StaticLoggerFactoryBinder.java
+//WARNING
+
+public class StaticLoggerFactoryBinder implements LoggerFactoryBinder {
+
+
+ //
+ // WARNING Do not modify copies, instead modify the original in
+ // $SLF4J_HOME/src/filtered-java/org/slf4j/
+ //
+
+ // Note: @IMPL@ gets substituted at build time by an appropriate Ant task
+ String loggerFactoryClassStr ="org.slf4j.impl. at IMPL@LoggerFactory";
+
+ StaticLoggerFactoryBinder() {
+ }
+
+ // package private
+ public ILoggerFactory getLoggerFactory() {
+ // Note: @IMPL@ gets substituted at build time by an appropriate Ant task
+ return new org.slf4j.impl. at IMPL@LoggerFactory();
+ }
+
+ public String getLoggerFactoryClassStr() {
+ return loggerFactoryClassStr;
+ }
+
+}
Copied: slf4j/trunk/src/java/org/slf4j/LoggerFactory.java (from r208, slf4j/trunk/src/filtered-java/org/slf4j/LoggerFactory.java)
==============================================================================
--- slf4j/trunk/src/filtered-java/org/slf4j/LoggerFactory.java (original)
+++ slf4j/trunk/src/java/org/slf4j/LoggerFactory.java Sun Aug 28 14:18:50 2005
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2004-2005 SLF4J.ORG
+ * Copyright (c) 2004-2005 QOS.ch
*
* All rights reserved.
*
@@ -52,6 +53,8 @@
* @author <a href="http://www.qos.ch/log4j/">Ceki Gülcü</a>
*/
public final class LoggerFactory {
+
+ static StaticLoggerFactoryBinder staticBinder = new StaticLoggerFactoryBinder();
static ILoggerFactory loggerFactory;
// private constructor prevents instantiation
@@ -63,65 +66,26 @@
// $SLF4J_HOME/src/filtered-java/org/slf4j/
//
static {
- String loggerFactoryClassStr = "org.slf4j.impl. at IMPL@LoggerFactory";
- System.out.println("SLF4J built for " + loggerFactoryClassStr);
-
- loggerFactory = getFactoryFromSystemProperties();
+
+ loggerFactory = new SystemPropBinder().getLoggerFactory();
- // if could not get an adapter from the system properties, bind statically
+ // if could get an adapter from the system properties, bind dynamically
if (loggerFactory != null) {
System.out.println("However, SLF4J will use ["+loggerFactory.getClass().getName()
+ "] adapter factory from system properties.");
} else {
- try {
- loggerFactory = new org.slf4j.impl. at IMPL@LoggerFactory();
+ try { // otherwise bind statically
+ loggerFactory = staticBinder.getLoggerFactory();
} catch (Exception e) {
// we should never get here
Util.reportFailure(
- "Could not instantiate instance of class [" + loggerFactoryClassStr + "]",
+ "Could not instantiate instance of class [" + staticBinder.getLoggerFactoryClassStr() + "]",
e);
}
}
}
- /**
- * Fetch the appropriate ILoggerFactory as intructed by the system propties.
- *
- * @return The appropriate ILoggerFactory instance as directed from the
- * system properties
- */
- private static ILoggerFactory getFactoryFromSystemProperties() {
- String factoryFactoryClassName = null;
-
- try {
- factoryFactoryClassName = System.getProperty(Constants.LOGGER_FACTORY_FACTORY_METHOD_NAME);
- if (factoryFactoryClassName == null) {
- return null;
- }
-
- Class factoryFactoryClass = Class.forName(factoryFactoryClassName);
- Class[] EMPTY_CLASS_ARRAY = { };
- java.lang.reflect.Method factoryFactoryMethod =
- factoryFactoryClass.getDeclaredMethod(
- Constants.LOGGER_FACTORY_FACTORY_METHOD_NAME, EMPTY_CLASS_ARRAY);
- ILoggerFactory loggerFactory =
- (ILoggerFactory) factoryFactoryMethod.invoke(null, null);
- return loggerFactory;
- } catch (Throwable t) {
- if (factoryFactoryClassName == null) {
- Util.reportFailure(
- "Failed to fetch " + Constants.LOGGER_FACTORY_FACTORY_METHOD_NAME
- + " system property.", t);
- } else {
- Util.reportFailure(
- "Failed to fetch ILoggerFactory instnace using the "
- + factoryFactoryClassName + " class.", t);
- }
- }
- // we could not get an adapter
- return null;
- }
/**
* Return a logger named according to the name parameter using the
@@ -157,4 +121,8 @@
return loggerFactory;
}
+ public static StaticLoggerFactoryBinder getStaticBinder() {
+ return staticBinder;
+ }
+
}
Added: slf4j/trunk/src/java/org/slf4j/SystemPropBinder.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/src/java/org/slf4j/SystemPropBinder.java Sun Aug 28 14:18:50 2005
@@ -0,0 +1,60 @@
+// TOTO
+
+package org.slf4j;
+
+import org.slf4j.spi.LoggerFactoryBinder;
+
+/**
+ * @author ceki
+ *
+ * TODO To change the template for this generated type comment go to Window -
+ * Preferences - Java - Code Style - Code Templates
+ */
+class SystemPropBinder implements LoggerFactoryBinder {
+ String factoryFactoryClassName = null;
+
+ /**
+ * Fetch the appropriate ILoggerFactory as intructed by the system propties.
+ *
+ * @return The appropriate ILoggerFactory instance as directed from the system
+ * properties
+ */
+ public ILoggerFactory getLoggerFactory() {
+
+ try {
+ if (getLoggerFactoryClassStr() == null) {
+ return null;
+ }
+
+ Class factoryFactoryClass = Class.forName(getLoggerFactoryClassStr());
+ Class[] EMPTY_CLASS_ARRAY = {};
+ java.lang.reflect.Method factoryFactoryMethod = factoryFactoryClass
+ .getDeclaredMethod(Constants.LOGGER_FACTORY_FACTORY_METHOD_NAME,
+ EMPTY_CLASS_ARRAY);
+ ILoggerFactory loggerFactory = (ILoggerFactory) factoryFactoryMethod
+ .invoke(null, null);
+ return loggerFactory;
+ } catch (Exception e) {
+ Util.reportFailure("Failed to fetch ILoggerFactory instnace using the "
+ + factoryFactoryClassName + " class.", e);
+
+ }
+
+ // we could not get an adapter
+ return null;
+ }
+
+ public String getLoggerFactoryClassStr() {
+ if (factoryFactoryClassName == null) {
+ try {
+ factoryFactoryClassName = System
+ .getProperty(Constants.LOGGER_FACTORY_FACTORY_METHOD_NAME);
+ } catch (Exception e) {
+ Util.reportFailure("Failed to fetch "
+ + Constants.LOGGER_FACTORY_FACTORY_METHOD_NAME
+ + " system property.", e);
+ }
+ }
+ return factoryFactoryClassName;
+ }
+}
\ No newline at end of file
Added: slf4j/trunk/src/java/org/slf4j/spi/LoggerFactoryBinder.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/src/java/org/slf4j/spi/LoggerFactoryBinder.java Sun Aug 28 14:18:50 2005
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2004-2005 SLF4J.ORG
+ * Copyright (c) 2004-2005 QOS.ch
+ *
+ * All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, and/or sell copies of the Software, and to permit persons
+ * to whom the Software is furnished to do so, provided that the above
+ * copyright notice(s) and this permission notice appear in all copies of
+ * the Software and that both the above copyright notice(s) and this
+ * permission notice appear in supporting documentation.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
+ * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+ * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY
+ * SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Except as contained in this notice, the name of a copyright holder
+ * shall not be used in advertising or otherwise to promote the sale, use
+ * or other dealings in this Software without prior written authorization
+ * of the copyright holder.
+ *
+ */
+
+package org.slf4j.spi;
+
+import org.slf4j.ILoggerFactory;
+
+/**
+ * An internal interface which helps the static {@link LoggerFactory} class
+ * bind with the appropriate {@link ILoggerFactory} instance.
+ *
+ * @author <a href="http://www.qos.ch/log4j/">Ceki Gülcü</a>
+ */
+public interface LoggerFactoryBinder {
+
+ /**
+ * Return the instance of {@link ILoggerFactory} that {@link LoggerFactory}
+ * should bind to.
+ *
+ * @return the instance of {@link ILoggerFactory} that {@link LoggerFactory}
+ * should bind to.
+ */
+ public ILoggerFactory getLoggerFactory();
+
+ /**
+ * The String form of the {@link ILoggerFactory} object that this
+ * <code>LoggerFactoryBinder</code> instance is <em>intended</em> to return.
+ *
+ * <p>This method allows the developer to intterogate this binder's intention
+ * which may be different from the {@link ILoggerFactory} instance it is able to
+ * yields in practice. The discrepency should only occur in case of errors.
+ *
+ * @return the class name of the intended {@link ILoggerFactory} instance
+ */
+ public String getLoggerFactoryClassStr();
+}
More information about the slf4j-dev
mailing list