Skip to content

Commit

Permalink
Implemented instantiating custom loggers; example in the unit test class
Browse files Browse the repository at this point in the history
Signed-off-by: Rajarshi Guha <rajarshi.guha@gmail.com>
  • Loading branch information
egonw authored and rajarshi committed Nov 4, 2009
1 parent acf5953 commit 2771eb9
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 20 deletions.
61 changes: 41 additions & 20 deletions src/main/org/openscience/cdk/tools/LoggingToolFactory.java
Expand Up @@ -48,18 +48,18 @@ public class LoggingToolFactory {
public final static String STDOUT_LOGGING_TOOL_CLASS =
"org.openscience.cdk.tools.SystemOutLoggingTool";

private static Class<? extends ILoggingTool> loggingTool;
private static Class<? extends ILoggingTool> userSetILoggerTool;

/**
* Sets the {@link ILoggingTool} implementation to be used.
*
* @param loggingTool The new {@link ILoggingTool}.
* @see #getLoggingToolClass()
*/
@TestMethod("testSetGetLoggingToolClass")
@TestMethod("testSetGetLoggingToolClass,testCustomLogger")
public static void setLoggingToolClass(
Class<? extends ILoggingTool> loggingTool) {
LoggingToolFactory.loggingTool = loggingTool;
LoggingToolFactory.userSetILoggerTool = loggingTool;
}

/**
Expand All @@ -70,7 +70,7 @@ public static void setLoggingToolClass(
*/
@TestMethod("testSetGetLoggingToolClass")
public static Class<? extends ILoggingTool> getLoggingToolClass() {
return LoggingToolFactory.loggingTool;
return LoggingToolFactory.userSetILoggerTool;
}

/**
Expand All @@ -83,9 +83,18 @@ public static Class<? extends ILoggingTool> getLoggingToolClass() {
*/
@TestMethod("testCreateLoggingTool")
public static ILoggingTool createLoggingTool(Class<?> sourceClass) {
ILoggingTool tool = initializeLoggingTool(
sourceClass, DEFAULT_LOGGING_TOOL_CLASS
);
ILoggingTool tool = null;
// first attempt the user set ILoggingTool
if (userSetILoggerTool != null) {
tool = instantiateWithCreateMethod(
sourceClass, userSetILoggerTool
);
}
if (tool == null) {
tool = initializeLoggingTool(
sourceClass, DEFAULT_LOGGING_TOOL_CLASS
);
}
if (tool == null) {
tool = initializeLoggingTool(
sourceClass, STDOUT_LOGGING_TOOL_CLASS
Expand All @@ -100,24 +109,36 @@ private static ILoggingTool initializeLoggingTool(
Class<?> possibleLoggingToolClass = sourceClass.getClassLoader()
.loadClass(className);
if (ILoggingTool.class.isAssignableFrom(possibleLoggingToolClass)) {
Method createMethod = possibleLoggingToolClass.getMethod(
"create", Class.class
);
Object createdLoggingTool = createMethod.invoke(
null, sourceClass
);
if (createdLoggingTool instanceof ILoggingTool) {
return (ILoggingTool)createdLoggingTool;
} else {
System.out.println("Expected ILoggingTool, but found a:"
+ createdLoggingTool.getClass().getName());
}
return instantiateWithCreateMethod(sourceClass,
possibleLoggingToolClass);
}
} catch (ClassNotFoundException e) {
} catch (IllegalAccessException e) {
} catch (SecurityException e) {
} catch (IllegalArgumentException e) {
}
return null;
}

private static ILoggingTool instantiateWithCreateMethod(
Class<?> sourceClass, Class<?> loggingToolClass) {
Method createMethod;
try {
createMethod = loggingToolClass.getMethod(
"create", Class.class
);
Object createdLoggingTool = createMethod.invoke(
null, sourceClass
);
if (createdLoggingTool instanceof ILoggingTool) {
return (ILoggingTool)createdLoggingTool;
} else {
System.out.println("Expected ILoggingTool, but found a:"
+ createdLoggingTool.getClass().getName());
}
} catch (SecurityException e) {
} catch (NoSuchMethodException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
return null;
Expand Down
66 changes: 66 additions & 0 deletions src/test/org/openscience/cdk/tools/LoggingToolFactoryTest.java
Expand Up @@ -50,4 +50,70 @@ public class LoggingToolFactoryTest {
);
Assert.assertTrue(instance instanceof LoggingTool);
}

@Test public void testCustomLogger() {
LoggingToolFactory.setLoggingToolClass(CustomLogger.class);
ILoggingTool instance = LoggingToolFactory.createLoggingTool(
LoggingToolFactoryTest.class
);
Assert.assertTrue(instance instanceof CustomLogger);
}

/**
* Custom dummy logger used in the
* {@link LoggingToolFactoryTest#testCustomLogger()} test to see if
* the custom {@link ILoggingTool} is really being used. It does
* not really implement any method, as the test uses a mere
* <code>instanceof</code> call.
*/
private static class CustomLogger implements ILoggingTool {

private CustomLogger(Class<?> sourceClass) {}

public static ILoggingTool create(Class<?> sourceClass) {
return new CustomLogger(sourceClass);
}

public void debug(Object object) {}
public void debug(Object object, Object object2) {}
public void debug(Object object, int number) {}
public void debug(Object object, double number) {}
public void debug(Object object, boolean bool) {}
public void debug(Object obj, Object obj2, Object obj3) {}
public void debug(Object obj, Object obj2, Object obj3, Object obj4) {}
public void debug(Object obj, Object obj2, Object obj3, Object obj4,
Object obj5) {}
public void dumpClasspath() {}
public void dumpSystemProperties() {}
public void error(Object object) {}
public void error(Object object, int number) {}
public void error(Object object, double number) {}
public void error(Object object, boolean bool) {}
public void error(Object object, Object object2) {}
public void error(Object obj, Object obj2, Object obj3) {}
public void error(Object obj, Object obj2, Object obj3, Object obj4) {}
public void error(Object obj, Object obj2, Object obj3, Object obj4,
Object obj5) {}
public void fatal(Object object) {}
public void info(Object object) {}
public void info(Object object, int number) {}
public void info(Object object, double number) {}
public void info(Object object, boolean bool) {}
public void info(Object object, Object object2) {}
public void info(Object obj, Object obj2, Object obj3) {}
public void info(Object obj, Object obj2, Object obj3, Object obj4) {}
public void info(Object obj, Object obj2, Object obj3, Object obj4,
Object obj5) {}
public boolean isDebugEnabled() { return true; }
public void setStackLength(int length) {}
public void warn(Object object) {}
public void warn(Object object, int number) {}
public void warn(Object object, boolean bool) {}
public void warn(Object object, double number) {}
public void warn(Object object, Object object2) {}
public void warn(Object obj, Object obj2, Object obj3) {}
public void warn(Object obj, Object obj2, Object obj3, Object obj4) {}
public void warn(Object obj, Object obj2, Object obj3, Object obj4,
Object obj5) {}
}
}

0 comments on commit 2771eb9

Please sign in to comment.