1 61 62 63 package org.apache.commons.discovery.log; 64 65 import java.lang.reflect.Method ; 66 import java.lang.reflect.Modifier ; 67 import java.util.Enumeration ; 68 import java.util.Hashtable ; 69 70 import org.apache.commons.discovery.DiscoveryException; 71 import org.apache.commons.discovery.tools.ClassUtils; 72 import org.apache.commons.logging.Log; 73 import org.apache.commons.logging.LogFactory; 74 75 76 97 public class DiscoveryLogFactory { 98 private static LogFactory logFactory = null; 99 private static final Hashtable classRegistry = new Hashtable (); 100 private static final Class [] setLogParamClasses = new Class [] { Log.class }; 101 102 105 private static Log log = DiscoveryLogFactory._newLog(DiscoveryLogFactory.class); 106 107 109 public static Log newLog(Class clazz) { 110 113 try { 114 Method setLog = ClassUtils.findPublicStaticMethod(clazz, 115 void.class, 116 "setLog", 117 setLogParamClasses); 118 119 if (setLog == null) { 120 String msg = "Internal Error: " + clazz.getName() + " required to implement 'public static void setLog(Log)'"; 121 log.fatal(msg); 122 throw new DiscoveryException(msg); 123 } 124 } catch (SecurityException se) { 125 String msg = "Required Security Permissions not present"; 126 log.fatal(msg, se); 127 throw new DiscoveryException(msg, se); 128 } 129 130 if (log.isDebugEnabled()) 131 log.debug("Class meets requirements: " + clazz.getName()); 132 133 return _newLog(clazz); 134 } 135 136 139 public static Log _newLog(Class clazz) { 140 classRegistry.put(clazz, clazz); 141 142 return (logFactory == null) 143 ? new SimpleLog(clazz.getName()) 144 : logFactory.getInstance(clazz.getName()); 145 } 146 147 public static void setLog(Log _log) { 148 log = _log; 149 } 150 151 154 public static void setFactory(LogFactory factory) { 155 if (logFactory == null) { 156 logFactory = factory; 158 159 Enumeration elements = classRegistry.elements(); 161 while (elements.hasMoreElements()) { 162 Class clazz = (Class )elements.nextElement(); 163 164 if (log.isDebugEnabled()) 165 log.debug("Reset Log for: " + clazz.getName()); 166 167 Method setLog = null; 168 169 try { 172 setLog = clazz.getMethod("setLog", setLogParamClasses); 173 } catch(Exception e) { 174 String msg = "Internal Error: pre-check for " + clazz.getName() + " failed?!"; 175 log.fatal(msg, e); 176 throw new DiscoveryException(msg, e); 177 } 178 179 Object [] setLogParam = new Object [] { factory.getInstance(clazz.getName()) }; 180 181 try { 182 setLog.invoke(null, setLogParam); 183 } catch(Exception e) { 184 String msg = "Internal Error: setLog failed for " + clazz.getName(); 185 log.fatal(msg, e); 186 throw new DiscoveryException(msg, e); 187 } 188 } 189 } 190 } 191 } 192 | Popular Tags |