1 /* Copyright (C) 2009  Egon Willighagen <egonw@users.sf.net>
2  *
3  * Contact: cdk-devel@lists.sourceforge.net
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * as published by the Free Software Foundation; either version 2.1
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 package org.openscience.cdk.tools;
20 
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 
24 /**
25  * Factory used to instantiate a {@link ILoggingTool}. To get an instance, run:
26  * <pre>
27  * public class SomeClass {
28  *   private static ILoggingTool logger;
29  *
30  *   static {
31  *     logger = LoggingToolFactory.createLoggingTool(SomeClass.class);
32  *   }
33  * }
34  * </pre>
35  *
36  * @cdk.module core
37  * @cdk.githash
38  */
39 public class LoggingToolFactory {
40 
41     /** Default logging tool. Currently, the log4j based one. */
42     public final static String                   DEFAULT_LOGGING_TOOL_CLASS = "org.openscience.cdk.tools.LoggingTool";
43     /** Back-up logging tool. Currently, a tool that outputs to System.out. */
44     public final static String                   STDOUT_LOGGING_TOOL_CLASS  = "org.openscience.cdk.tools.SystemOutLoggingTool";
45 
46     private static Class<? extends ILoggingTool> userSetILoggerTool;
47 
48     /**
49      * Sets the {@link ILoggingTool} implementation to be used.
50      *
51      * @param loggingTool The new {@link ILoggingTool}.
52      * @see   #getLoggingToolClass()
53      */
setLoggingToolClass(Class<? extends ILoggingTool> loggingTool)54     public static void setLoggingToolClass(Class<? extends ILoggingTool> loggingTool) {
55         LoggingToolFactory.userSetILoggerTool = loggingTool;
56     }
57 
58     /**
59      * Gets the currently used {@link ILoggingTool} implementation.
60      *
61      * @return The currently used {@link ILoggingTool}.
62      * @see    #setLoggingToolClass(Class)
63      */
getLoggingToolClass()64     public static Class<? extends ILoggingTool> getLoggingToolClass() {
65         return LoggingToolFactory.userSetILoggerTool;
66     }
67 
68     /**
69      * Dynamically create a {@link ILoggingTool} for the given
70      * <code>sourceClass</code>.
71      *
72      * @param  sourceClass Class for which the {@link ILoggingTool} should be
73      *                     constructed.
74      * @return             An {@link ILoggingTool} implementation.
75      */
createLoggingTool(Class<?> sourceClass)76     public static ILoggingTool createLoggingTool(Class<?> sourceClass) {
77         ILoggingTool tool = null;
78         // first attempt the user set ILoggingTool
79         if (userSetILoggerTool != null) {
80             tool = instantiateWithCreateMethod(sourceClass, userSetILoggerTool);
81         }
82         if (tool == null) {
83             tool = initializeLoggingTool(sourceClass, DEFAULT_LOGGING_TOOL_CLASS);
84         }
85         if (tool == null) {
86             tool = initializeLoggingTool(sourceClass, STDOUT_LOGGING_TOOL_CLASS);
87         }
88         return tool;
89     }
90 
initializeLoggingTool(Class<?> sourceClass, String className)91     private static ILoggingTool initializeLoggingTool(Class<?> sourceClass, String className) {
92         try {
93             Class<?> possibleLoggingToolClass = sourceClass.getClassLoader().loadClass(className);
94             if (ILoggingTool.class.isAssignableFrom(possibleLoggingToolClass)) {
95                 return instantiateWithCreateMethod(sourceClass, possibleLoggingToolClass);
96             }
97         } catch (ClassNotFoundException e) {
98         } catch (SecurityException e) {
99         } catch (IllegalArgumentException e) {
100         }
101         return null;
102     }
103 
instantiateWithCreateMethod(Class<?> sourceClass, Class<?> loggingToolClass)104     private static ILoggingTool instantiateWithCreateMethod(Class<?> sourceClass, Class<?> loggingToolClass) {
105         Method createMethod;
106         try {
107             createMethod = loggingToolClass.getMethod("create", Class.class);
108             Object createdLoggingTool = createMethod.invoke(null, sourceClass);
109             if (createdLoggingTool instanceof ILoggingTool) {
110                 return (ILoggingTool) createdLoggingTool;
111             } else {
112                 System.out.println("Expected ILoggingTool, but found a:" + createdLoggingTool.getClass().getName());
113             }
114         } catch (SecurityException e) {
115         } catch (NoSuchMethodException e) {
116         } catch (IllegalArgumentException e) {
117         } catch (IllegalAccessException e) {
118         } catch (InvocationTargetException e) {
119         }
120         return null;
121     }
122 
123 }
124