01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2007, Geotools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2.1 of the License, or (at your option) any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.util.logging;
17:
18: import java.util.logging.Logger;
19: import org.apache.commons.logging.Log;
20: import org.apache.commons.logging.LogFactory;
21: import org.apache.commons.logging.impl.Jdk14Logger;
22:
23: /**
24: * A factory for loggers that redirect all Java logging events to the Apache's
25: * <A HREF="http://jakarta.apache.org/commons/logging/">Commons-logging</A> framework.
26: *
27: * @since 2.4
28: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/main/java/org/geotools/util/logging/CommonsLoggerFactory.java $
29: * @version $Id: CommonsLoggerFactory.java 27891 2007-11-14 14:10:48Z desruisseaux $
30: * @author Martin Desruisseaux
31: */
32: public class CommonsLoggerFactory extends LoggerFactory {
33: /**
34: * The unique instance of this factory.
35: */
36: private static CommonsLoggerFactory factory;
37:
38: /**
39: * Constructs a default factory.
40: *
41: * @throws NoClassDefFoundError if Apache's {@code Log} class was not found on the classpath.
42: */
43: protected CommonsLoggerFactory() throws NoClassDefFoundError {
44: super (Log.class);
45: }
46:
47: /**
48: * Returns the unique instance of this factory.
49: *
50: * @throws NoClassDefFoundError if Apache's {@code Log} class was not found on the classpath.
51: */
52: public static synchronized CommonsLoggerFactory getInstance()
53: throws NoClassDefFoundError {
54: if (factory == null) {
55: factory = new CommonsLoggerFactory();
56: }
57: return factory;
58: }
59:
60: /**
61: * Returns the implementation to use for the logger of the specified name,
62: * or {@code null} if the logger would delegates to Java logging anyway.
63: */
64: protected Object getImplementation(final String name) {
65: final Log log = LogFactory.getLog(name);
66: if (log instanceof Jdk14Logger) {
67: return null;
68: }
69: return log;
70: }
71:
72: /**
73: * Wraps the specified {@linkplain #getImplementation implementation} in a Java logger.
74: */
75: protected Logger wrap(String name, Object implementation)
76: throws ClassCastException {
77: return new CommonsLogger(name, (Log) implementation);
78: }
79:
80: /**
81: * Returns the {@linkplain #getImplementation implementation} wrapped by the specified logger,
82: * or {@code null} if none.
83: */
84: protected Object unwrap(final Logger logger) {
85: if (logger instanceof CommonsLogger) {
86: return ((CommonsLogger) logger).logger;
87: }
88: return null;
89: }
90: }
|