1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.logging.impl;
18
19
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import java.security.AccessController;
24 import java.security.PrivilegedAction;
25 import java.util.Enumeration;
26 import java.util.Hashtable;
27 import java.util.Vector;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogConfigurationException;
31 import org.apache.commons.logging.LogFactory;
32
33
34 /***
35 * <p>Concrete subclass of {@link LogFactory} that implements the
36 * following algorithm to dynamically select a logging implementation
37 * class to instantiate a wrapper for.</p>
38 * <ul>
39 * <li>Use a factory configuration attribute named
40 * <code>org.apache.commons.logging.Log</code> to identify the
41 * requested implementation class.</li>
42 * <li>Use the <code>org.apache.commons.logging.Log</code> system property
43 * to identify the requested implementation class.</li>
44 * <li>If <em>Log4J</em> is available, return an instance of
45 * <code>org.apache.commons.logging.impl.Log4JLogger</code>.</li>
46 * <li>If <em>JDK 1.4 or later</em> is available, return an instance of
47 * <code>org.apache.commons.logging.impl.Jdk14Logger</code>.</li>
48 * <li>Otherwise, return an instance of
49 * <code>org.apache.commons.logging.impl.SimpleLog</code>.</li>
50 * </ul>
51 *
52 * <p>If the selected {@link Log} implementation class has a
53 * <code>setLogFactory()</code> method that accepts a {@link LogFactory}
54 * parameter, this method will be called on each newly created instance
55 * to identify the associated factory. This makes factory configuration
56 * attributes available to the Log instance, if it so desires.</p>
57 *
58 * <p>This factory will remember previously created <code>Log</code> instances
59 * for the same name, and will return them on repeated requests to the
60 * <code>getInstance()</code> method. This implementation ignores any
61 * configured attributes.</p>
62 *
63 * @author Rod Waldhoff
64 * @author Craig R. McClanahan
65 * @author Richard A. Sitze
66 * @version $Revision: 1.33 $ $Date: 2004/03/06 21:52:59 $
67 */
68
69 public class LogFactoryImpl extends LogFactory {
70
71
72
73
74 /***
75 * Public no-arguments constructor required by the lookup mechanism.
76 */
77 public LogFactoryImpl() {
78 super();
79 }
80
81
82
83
84
85 /***
86 * The name of the system property identifying our {@link Log}
87 * implementation class.
88 */
89 public static final String LOG_PROPERTY =
90 "org.apache.commons.logging.Log";
91
92
93 /***
94 * The deprecated system property used for backwards compatibility with
95 * the old {@link org.apache.commons.logging.LogSource} class.
96 */
97 protected static final String LOG_PROPERTY_OLD =
98 "org.apache.commons.logging.log";
99
100
101 /***
102 * <p>The name of the {@link Log} interface class.</p>
103 */
104 private static final String LOG_INTERFACE =
105 "org.apache.commons.logging.Log";
106
107
108
109
110
111 /***
112 * Configuration attributes.
113 */
114 protected Hashtable attributes = new Hashtable();
115
116
117 /***
118 * The {@link org.apache.commons.logging.Log} instances that have
119 * already been created, keyed by logger name.
120 */
121 protected Hashtable instances = new Hashtable();
122
123
124 /***
125 * Name of the class implementing the Log interface.
126 */
127 private String logClassName;
128
129
130 /***
131 * The one-argument constructor of the
132 * {@link org.apache.commons.logging.Log}
133 * implementation class that will be used to create new instances.
134 * This value is initialized by <code>getLogConstructor()</code>,
135 * and then returned repeatedly.
136 */
137 protected Constructor logConstructor = null;
138
139
140 /***
141 * The signature of the Constructor to be used.
142 */
143 protected Class logConstructorSignature[] =
144 { java.lang.String.class };
145
146
147 /***
148 * The one-argument <code>setLogFactory</code> method of the selected
149 * {@link org.apache.commons.logging.Log} method, if it exists.
150 */
151 protected Method logMethod = null;
152
153
154 /***
155 * The signature of the <code>setLogFactory</code> method to be used.
156 */
157 protected Class logMethodSignature[] =
158 { LogFactory.class };
159
160
161
162
163
164 /***
165 * Return the configuration attribute with the specified name (if any),
166 * or <code>null</code> if there is no such attribute.
167 *
168 * @param name Name of the attribute to return
169 */
170 public Object getAttribute(String name) {
171
172 return (attributes.get(name));
173
174 }
175
176
177 /***
178 * Return an array containing the names of all currently defined
179 * configuration attributes. If there are no such attributes, a zero
180 * length array is returned.
181 */
182 public String[] getAttributeNames() {
183
184 Vector names = new Vector();
185 Enumeration keys = attributes.keys();
186 while (keys.hasMoreElements()) {
187 names.addElement((String) keys.nextElement());
188 }
189 String results[] = new String[names.size()];
190 for (int i = 0; i < results.length; i++) {
191 results[i] = (String) names.elementAt(i);
192 }
193 return (results);
194
195 }
196
197
198 /***
199 * Convenience method to derive a name from the specified class and
200 * call <code>getInstance(String)</code> with it.
201 *
202 * @param clazz Class for which a suitable Log name will be derived
203 *
204 * @exception LogConfigurationException if a suitable <code>Log</code>
205 * instance cannot be returned
206 */
207 public Log getInstance(Class clazz) throws LogConfigurationException {
208
209 return (getInstance(clazz.getName()));
210
211 }
212
213
214 /***
215 * <p>Construct (if necessary) and return a <code>Log</code> instance,
216 * using the factory's current set of configuration attributes.</p>
217 *
218 * <p><strong>NOTE</strong> - Depending upon the implementation of
219 * the <code>LogFactory</code> you are using, the <code>Log</code>
220 * instance you are returned may or may not be local to the current
221 * application, and may or may not be returned again on a subsequent
222 * call with the same name argument.</p>
223 *
224 * @param name Logical name of the <code>Log</code> instance to be
225 * returned (the meaning of this name is only known to the underlying
226 * logging implementation that is being wrapped)
227 *
228 * @exception LogConfigurationException if a suitable <code>Log</code>
229 * instance cannot be returned
230 */
231 public Log getInstance(String name) throws LogConfigurationException {
232
233 Log instance = (Log) instances.get(name);
234 if (instance == null) {
235 instance = newInstance(name);
236 instances.put(name, instance);
237 }
238 return (instance);
239
240 }
241
242
243 /***
244 * Release any internal references to previously created
245 * {@link org.apache.commons.logging.Log}
246 * instances returned by this factory. This is useful in environments
247 * like servlet containers, which implement application reloading by
248 * throwing away a ClassLoader. Dangling references to objects in that
249 * class loader would prevent garbage collection.
250 */
251 public void release() {
252
253 instances.clear();
254 }
255
256
257 /***
258 * Remove any configuration attribute associated with the specified name.
259 * If there is no such attribute, no action is taken.
260 *
261 * @param name Name of the attribute to remove
262 */
263 public void removeAttribute(String name) {
264
265 attributes.remove(name);
266
267 }
268
269
270 /***
271 * Set the configuration attribute with the specified name. Calling
272 * this with a <code>null</code> value is equivalent to calling
273 * <code>removeAttribute(name)</code>.
274 *
275 * @param name Name of the attribute to set
276 * @param value Value of the attribute to set, or <code>null</code>
277 * to remove any setting for this attribute
278 */
279 public void setAttribute(String name, Object value) {
280
281 if (value == null) {
282 attributes.remove(name);
283 } else {
284 attributes.put(name, value);
285 }
286
287 }
288
289
290
291
292
293
294 /***
295 * Return the fully qualified Java classname of the {@link Log}
296 * implementation we will be using.
297 */
298 protected String getLogClassName() {
299
300
301 if (logClassName != null) {
302 return logClassName;
303 }
304
305 logClassName = (String) getAttribute(LOG_PROPERTY);
306
307 if (logClassName == null) {
308 logClassName = (String) getAttribute(LOG_PROPERTY_OLD);
309 }
310
311 if (logClassName == null) {
312 try {
313 logClassName = System.getProperty(LOG_PROPERTY);
314 } catch (SecurityException e) {
315 ;
316 }
317 }
318
319 if (logClassName == null) {
320 try {
321 logClassName = System.getProperty(LOG_PROPERTY_OLD);
322 } catch (SecurityException e) {
323 ;
324 }
325 }
326
327 if ((logClassName == null) && isLog4JAvailable()) {
328 logClassName = "org.apache.commons.logging.impl.Log4JLogger";
329 }
330
331 if ((logClassName == null) && isJdk14Available()) {
332 logClassName = "org.apache.commons.logging.impl.Jdk14Logger";
333 }
334
335 if ((logClassName == null) && isJdk13LumberjackAvailable()) {
336 logClassName = "org.apache.commons.logging.impl.Jdk13LumberjackLogger";
337 }
338
339 if (logClassName == null) {
340 logClassName = "org.apache.commons.logging.impl.SimpleLog";
341 }
342
343 return (logClassName);
344
345 }
346
347
348 /***
349 * <p>Return the <code>Constructor</code> that can be called to instantiate
350 * new {@link org.apache.commons.logging.Log} instances.</p>
351 *
352 * <p><strong>IMPLEMENTATION NOTE</strong> - Race conditions caused by
353 * calling this method from more than one thread are ignored, because
354 * the same <code>Constructor</code> instance will ultimately be derived
355 * in all circumstances.</p>
356 *
357 * @exception LogConfigurationException if a suitable constructor
358 * cannot be returned
359 */
360 protected Constructor getLogConstructor()
361 throws LogConfigurationException {
362
363
364 if (logConstructor != null) {
365 return logConstructor;
366 }
367
368 String logClassName = getLogClassName();
369
370
371 Class logClass = null;
372 Class logInterface = null;
373 try {
374 logInterface = this.getClass().getClassLoader().loadClass
375 (LOG_INTERFACE);
376 logClass = loadClass(logClassName);
377 if (logClass == null) {
378 throw new LogConfigurationException
379 ("No suitable Log implementation for " + logClassName);
380 }
381 if (!logInterface.isAssignableFrom(logClass)) {
382 Class interfaces[] = logClass.getInterfaces();
383 for (int i = 0; i < interfaces.length; i++) {
384 if (LOG_INTERFACE.equals(interfaces[i].getName())) {
385 throw new LogConfigurationException
386 ("Invalid class loader hierarchy. " +
387 "You have more than one version of '" +
388 LOG_INTERFACE + "' visible, which is " +
389 "not allowed.");
390 }
391 }
392 throw new LogConfigurationException
393 ("Class " + logClassName + " does not implement '" +
394 LOG_INTERFACE + "'.");
395 }
396 } catch (Throwable t) {
397 throw new LogConfigurationException(t);
398 }
399
400
401 try {
402 logMethod = logClass.getMethod("setLogFactory",
403 logMethodSignature);
404 } catch (Throwable t) {
405 logMethod = null;
406 }
407
408
409 try {
410 logConstructor = logClass.getConstructor(logConstructorSignature);
411 return (logConstructor);
412 } catch (Throwable t) {
413 throw new LogConfigurationException
414 ("No suitable Log constructor " +
415 logConstructorSignature+ " for " + logClassName, t);
416 }
417 }
418
419
420 /***
421 * MUST KEEP THIS METHOD PRIVATE.
422 *
423 * <p>Exposing this method outside of
424 * <code>org.apache.commons.logging.LogFactoryImpl</code>
425 * will create a security violation:
426 * This method uses <code>AccessController.doPrivileged()</code>.
427 * </p>
428 *
429 * Load a class, try first the thread class loader, and
430 * if it fails use the loader that loaded this class.
431 */
432 private static Class loadClass( final String name )
433 throws ClassNotFoundException
434 {
435 Object result = AccessController.doPrivileged(
436 new PrivilegedAction() {
437 public Object run() {
438 ClassLoader threadCL = getContextClassLoader();
439 if (threadCL != null) {
440 try {
441 return threadCL.loadClass(name);
442 } catch( ClassNotFoundException ex ) {
443
444 }
445 }
446 try {
447 return Class.forName( name );
448 } catch (ClassNotFoundException e) {
449 return e;
450 }
451 }
452 });
453
454 if (result instanceof Class)
455 return (Class)result;
456
457 throw (ClassNotFoundException)result;
458 }
459
460
461 /***
462 * Is <em>JDK 1.3 with Lumberjack</em> logging available?
463 */
464 protected boolean isJdk13LumberjackAvailable() {
465
466 try {
467 loadClass("java.util.logging.Logger");
468 loadClass("org.apache.commons.logging.impl.Jdk13LumberjackLogger");
469 return (true);
470 } catch (Throwable t) {
471 return (false);
472 }
473
474 }
475
476
477 /***
478 * <p>Return <code>true</code> if <em>JDK 1.4 or later</em> logging
479 * is available. Also checks that the <code>Throwable</code> class
480 * supports <code>getStackTrace()</code>, which is required by
481 * Jdk14Logger.</p>
482 */
483 protected boolean isJdk14Available() {
484
485 try {
486 loadClass("java.util.logging.Logger");
487 loadClass("org.apache.commons.logging.impl.Jdk14Logger");
488 Class throwable = loadClass("java.lang.Throwable");
489 if (throwable.getDeclaredMethod("getStackTrace", null) == null) {
490 return (false);
491 }
492 return (true);
493 } catch (Throwable t) {
494 return (false);
495 }
496 }
497
498
499 /***
500 * Is a <em>Log4J</em> implementation available?
501 */
502 protected boolean isLog4JAvailable() {
503
504 try {
505 loadClass("org.apache.log4j.Logger");
506 loadClass("org.apache.commons.logging.impl.Log4JLogger");
507 return (true);
508 } catch (Throwable t) {
509 return (false);
510 }
511 }
512
513
514 /***
515 * Create and return a new {@link org.apache.commons.logging.Log}
516 * instance for the specified name.
517 *
518 * @param name Name of the new logger
519 *
520 * @exception LogConfigurationException if a new instance cannot
521 * be created
522 */
523 protected Log newInstance(String name) throws LogConfigurationException {
524
525 Log instance = null;
526 try {
527 Object params[] = new Object[1];
528 params[0] = name;
529 instance = (Log) getLogConstructor().newInstance(params);
530 if (logMethod != null) {
531 params[0] = this;
532 logMethod.invoke(instance, params);
533 }
534 return (instance);
535 } catch (InvocationTargetException e) {
536 Throwable c = e.getTargetException();
537 if (c != null) {
538 throw new LogConfigurationException(c);
539 } else {
540 throw new LogConfigurationException(e);
541 }
542 } catch (Throwable t) {
543 throw new LogConfigurationException(t);
544 }
545
546 }
547
548
549 }