1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.discovery.log;
18
19 import java.lang.reflect.Method;
20 import java.util.Hashtable;
21 import java.util.Map;
22
23 import org.apache.commons.discovery.DiscoveryException;
24 import org.apache.commons.discovery.tools.ClassUtils;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 @Deprecated
46 public class DiscoveryLogFactory {
47
48 private static LogFactory logFactory = null;
49
50 private static final Map<Class<?>, Class<?>> classRegistry = new Hashtable<Class<?>, Class<?>>();
51
52 private static final Class<?>[] setLogParamClasses = new Class<?>[] { Log.class };
53
54
55
56
57 private static Log log = DiscoveryLogFactory._newLog(DiscoveryLogFactory.class);
58
59
60
61
62
63
64
65 public static Log newLog(Class<?> clazz) {
66
67
68
69 try {
70 Method setLog = ClassUtils.findPublicStaticMethod(clazz,
71 void.class,
72 "setLog",
73 setLogParamClasses);
74
75 if (setLog == null) {
76 String msg = "Internal Error: "
77 + clazz.getName()
78 + " required to implement 'public static void setLog(Log)'";
79 log.fatal(msg);
80 throw new DiscoveryException(msg);
81 }
82 } catch (SecurityException se) {
83 String msg = "Required Security Permissions not present";
84 log.fatal(msg, se);
85 throw new DiscoveryException(msg, se);
86 }
87
88 if (log.isDebugEnabled()) {
89 log.debug("Class meets requirements: " + clazz.getName());
90 }
91
92 return _newLog(clazz);
93 }
94
95
96
97
98
99
100
101 public static Log _newLog(Class<?> clazz) {
102 classRegistry.put(clazz, clazz);
103
104 return (logFactory == null)
105 ? new SimpleLog(clazz.getName())
106 : logFactory.getInstance(clazz.getName());
107 }
108
109
110
111
112
113
114 public static void setLog(Log _log) {
115 log = _log;
116 }
117
118
119
120
121
122
123 public static void setFactory(LogFactory factory) {
124 if (logFactory == null) {
125
126 logFactory = factory;
127
128
129 for (Class<?> clazz : classRegistry.values()) {
130
131 if (log.isDebugEnabled()) {
132 log.debug("Reset Log for: " + clazz.getName());
133 }
134
135 Method setLog = null;
136
137
138
139 try {
140 setLog = clazz.getMethod("setLog", setLogParamClasses);
141 } catch(Exception e) {
142 String msg = "Internal Error: pre-check for " + clazz.getName() + " failed?!";
143 log.fatal(msg, e);
144 throw new DiscoveryException(msg, e);
145 }
146
147 Object[] setLogParam = new Object[] { factory.getInstance(clazz.getName()) };
148
149 try {
150 setLog.invoke(null, setLogParam);
151 } catch(Exception e) {
152 String msg = "Internal Error: setLog failed for " + clazz.getName();
153 log.fatal(msg, e);
154 throw new DiscoveryException(msg, e);
155 }
156 }
157 }
158 }
159
160 }