1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.logging.impl;
18  
19  import java.util.Enumeration;
20  import java.util.HashMap;
21  import java.util.Hashtable;
22  import java.util.Map;
23  import java.util.Vector;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogConfigurationException;
27  import org.apache.commons.logging.LogFactory;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.slf4j.spi.LocationAwareLogger;
31  
32  /**
33   * <p>
34   * Concrete subclass of {@link LogFactory} which always delegates to the
35   * {@link LoggerFactory org.slf4j.LoggerFactory} class.
36   * 
37   * <p>
38   * This factory generates instances of {@link SLF4JLog}. It will remember
39   * previously created instances for the same name, and will return them on
40   * repeated requests to the <code>getInstance()</code> method.
41   * 
42   * <p>
43   * This implementation ignores any configured attributes.
44   * </p>
45   * 
46   * @author Rod Waldhoff
47   * @author Craig R. McClanahan
48   * @author Richard A. Sitze
49   * @author Ceki G&uuml;lc&uuml;
50   */
51  
52  public class SLF4JLogFactory extends LogFactory {
53  
54    // ----------------------------------------------------------- Constructors
55  
56    /**
57     * The {@link org.apache.commons.logging.Log}instances that have already been
58     * created, keyed by logger name.
59     */
60    Map loggerMap;
61  
62    /**
63     * Public no-arguments constructor required by the lookup mechanism.
64     */
65    public SLF4JLogFactory() {
66      loggerMap = new HashMap();
67    }
68  
69    // ----------------------------------------------------- Manifest Constants
70  
71    /**
72     * The name of the system property identifying our {@link Log}implementation
73     * class.
74     */
75    public static final String LOG_PROPERTY = "org.apache.commons.logging.Log";
76  
77    // ----------------------------------------------------- Instance Variables
78  
79    /**
80     * Configuration attributes.
81     */
82    protected Hashtable attributes = new Hashtable();
83  
84    // --------------------------------------------------------- Public Methods
85  
86    /**
87     * Return the configuration attribute with the specified name (if any), or
88     * <code>null</code> if there is no such attribute.
89     * 
90     * @param name
91     *          Name of the attribute to return
92     */
93    public Object getAttribute(String name) {
94  
95      return (attributes.get(name));
96  
97    }
98  
99    /**
100    * Return an array containing the names of all currently defined configuration
101    * attributes. If there are no such attributes, a zero length array is
102    * returned.
103    */
104   public String[] getAttributeNames() {
105 
106     Vector names = new Vector();
107     Enumeration keys = attributes.keys();
108     while (keys.hasMoreElements()) {
109       names.addElement((String) keys.nextElement());
110     }
111     String results[] = new String[names.size()];
112     for (int i = 0; i < results.length; i++) {
113       results[i] = (String) names.elementAt(i);
114     }
115     return (results);
116 
117   }
118 
119   /**
120    * Convenience method to derive a name from the specified class and call
121    * <code>getInstance(String)</code> with it.
122    * 
123    * @param clazz
124    *          Class for which a suitable Log name will be derived
125    * 
126    * @exception LogConfigurationException
127    *              if a suitable <code>Log</code> instance cannot be returned
128    */
129   public Log getInstance(Class clazz) throws LogConfigurationException {
130 
131     return (getInstance(clazz.getName()));
132 
133   }
134 
135   /**
136    * <p>
137    * Construct (if necessary) and return a <code>Log</code> instance, using
138    * the factory's current set of configuration attributes.
139    * </p>
140    * 
141    * @param name
142    *          Logical name of the <code>Log</code> instance to be returned
143    *          (the meaning of this name is only known to the underlying logging
144    *          implementation that is being wrapped)
145    * 
146    * @exception LogConfigurationException
147    *              if a suitable <code>Log</code> instance cannot be returned
148    */
149   public Log getInstance(String name) throws LogConfigurationException {
150     Log instance = null;
151     // protect against concurrent access of loggerMap
152     synchronized (this) {
153       instance = (Log) loggerMap.get(name);
154       if (instance == null) {
155         Logger logger = LoggerFactory.getLogger(name);
156         if(logger instanceof LocationAwareLogger) {
157           instance = new SLF4JLocationAwareLog((LocationAwareLogger) logger);
158         } else {
159           instance = new SLF4JLog(logger);
160         }
161         loggerMap.put(name, instance);
162       }
163     }
164     return (instance);
165 
166   }
167 
168   /**
169    * Release any internal references to previously created
170    * {@link org.apache.commons.logging.Log}instances returned by this factory.
171    * This is useful in environments like servlet containers, which implement
172    * application reloading by throwing away a ClassLoader. Dangling references
173    * to objects in that class loader would prevent garbage collection.
174    */
175   public void release() {
176     // This method is never called by jcl-over-slf4j classes. However,
177     // in certain deployment scenarios, in particular if jcl-over-slf4j.jar
178     // is
179     // in the the web-app class loader and the official commons-logging.jar is
180     // deployed in some parent class loader (e.g. commons/lib), then it is
181     // possible
182     // for the parent class loader to mask the classes shipping in
183     // jcl-over-slf4j.jar.
184     System.out.println("WARN: The method " + SLF4JLogFactory.class
185         + "#release() was invoked.");
186     System.out
187         .println("WARN: Please see http://www.slf4j.org/codes.html#release for an explanation.");
188     System.out.flush();
189   }
190 
191   /**
192    * Remove any configuration attribute associated with the specified name. If
193    * there is no such attribute, no action is taken.
194    * 
195    * @param name
196    *          Name of the attribute to remove
197    */
198   public void removeAttribute(String name) {
199     attributes.remove(name);
200   }
201 
202   /**
203    * Set the configuration attribute with the specified name. Calling this with
204    * a <code>null</code> value is equivalent to calling
205    * <code>removeAttribute(name)</code>.
206    * 
207    * @param name
208    *          Name of the attribute to set
209    * @param value
210    *          Value of the attribute to set, or <code>null</code> to remove
211    *          any setting for this attribute
212    */
213   public void setAttribute(String name, Object value) {
214 
215     if (value == null) {
216       attributes.remove(name);
217     } else {
218       attributes.put(name, value);
219     }
220 
221   }
222 }