1
2
3
4
5
6
7
8
9
10 package net.sf.mmapps.commons.logging.impl;
11
12 import java.util.Enumeration;
13 import java.util.Hashtable;
14 import java.util.Vector;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogConfigurationException;
18 import org.apache.commons.logging.LogFactory;
19 import org.mmbase.util.logging.Logging;
20
21 /***
22 * LogFactory for jakarta commons-logging who when used creates MMBase logging backed Log implementations.<br/>
23 * <br/>
24 * <b>Goal:</b> To provide a single log configuration for applications that use both commons-logging and mmbase logging.<br/>
25 * <b>Achievement:</b> By providing a commons-logging factory that uses mmbase-logging.<br/>
26 * <br/>
27 * MMBaseLoggingFactory is a LogFactory for the <a href="http://jakarta.apache.org/commons/logging/">jakarta commons-logging logging</a> api.
28 * MMBaseLoggingFactory uses the MMBase logging mechanism found in <a href="http://www.mmbase.org/api/org/mmbase/util/logging/package-summary.html">org.mmbase.util.logging.Logging</a>
29 * to provide the actual logging.
30 *
31 *
32 * @author Kees Jongenburger
33 * @version $Id: MMBaseLoggingFactory.java,v 1.1 2004/11/21 12:53:19 keesj Exp $
34 */
35 public class MMBaseLoggingFactory extends LogFactory {
36
37 /***
38 * The configuration attributes for this {@link LogFactory}.
39 */
40 private Hashtable attributes = new Hashtable();
41
42
43 private Hashtable instances = new Hashtable();
44
45
46
47 /***
48 * @param name Name of the attribute to return
49 * @return the configuration attribute with the specified name (if any),
50 * or <code>null</code> if there is no such attribute.
51 *
52 */
53 public Object getAttribute(String name) {
54 return (attributes.get(name));
55 }
56
57 /***
58 * @return an array containing the names of all currently defined
59 * configuration attributes. If there are no such attributes, a zero
60 * length array is returned.
61 */
62 public String[] getAttributeNames() {
63 Vector names = new Vector();
64 Enumeration keys = attributes.keys();
65 while (keys.hasMoreElements()) {
66 names.addElement((String) keys.nextElement());
67 }
68 String results[] = new String[names.size()];
69 for (int i = 0; i < results.length; i++) {
70 results[i] = (String) names.elementAt(i);
71 }
72 return (results);
73 }
74
75 /***
76 * This method first first looks in it's internal cache if there is a existing Log for the given class. If that is not the case
77 * the method uses org.mmbase.Version to determine the version of mmbase used and depending on the version
78 * create a {@link MMBase17Logger} or a {@link MMBase18Logger};
79 *
80 * @param clazz the class for witch to create a logger
81 * @return a mmbase backed Log implementationfor the given class
82 */
83 public Log getInstance(Class clazz) throws LogConfigurationException {
84 Log instance = (Log) instances.get(clazz);
85 if (instance != null)
86 return instance;
87
88 if (org.mmbase.Version.getMinor() < 8) {
89 instance = new MMBase17Logger(Logging.getLoggerInstance(clazz));
90 } else {
91 instance = new MMBase18Logger(Logging.getLoggerInstance(clazz));
92 }
93 instances.put(clazz, instance);
94 return instance;
95 }
96
97 /***
98 * This method first first looks in it's internal cache if there is a existing Log with the given name. If that is not the case
99 * the method uses org.mmbase.Version to determine the version of mmbase used and depending on the version
100 * create a {@link MMBase17Logger} or a {@link MMBase18Logger};
101 *
102 * @param clazz the class for witch to create a logger
103 * @return a mmbase backed Log implementation for the given log
104 */
105
106 public Log getInstance(String category) throws LogConfigurationException {
107 Log instance = (Log) instances.get(category);
108 if (instance != null)
109 return instance;
110 if (org.mmbase.Version.getMinor() < 8) {
111 instance = new MMBase17Logger(Logging.getLoggerInstance(category));
112 } else {
113 instance = new MMBase18Logger(Logging.getLoggerInstance(category));
114 }
115 instances.put(category, instance);
116 return instance;
117 }
118
119 /***
120 * Release any internal references to previously created {@link Log}
121 * instances returned by this factory. This is useful in environments
122 * like servlet containers, which implement application reloading by
123 * throwing away a ClassLoader. Dangling references to objects in that
124 * class loader would prevent garbage collection.
125 */
126 public void release() {
127
128 instances.clear();
129 Logging.shutdown();
130 }
131
132 /***
133 * Remove any configuration attribute associated with the specified name.
134 * If there is no such attribute, no action is taken.
135 *
136 * @param name Name of the attribute to remove
137 */
138 public void removeAttribute(String name) {
139 attributes.remove(name);
140 }
141
142 /***
143 * Set the configuration attribute with the specified name. Calling
144 * this with a <code>null</code> value is equivalent to calling
145 * <code>removeAttribute(name)</code>.
146 *
147 * @param name Name of the attribute to set
148 * @param value Value of the attribute to set, or <code>null</code>
149 * to remove any setting for this attribute
150 */
151 public void setAttribute(String name, Object value) {
152 if (value == null) {
153 attributes.remove(name);
154 } else {
155 attributes.put(name, value);
156 }
157 }
158 }