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.util.Enumeration;
21 import java.util.Hashtable;
22 import java.util.Vector;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogConfigurationException;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.log4j.Logger;
28
29 /***
30 * <p>Concrete subclass of {@link LogFactory} specific to log4j.
31 *
32 * @deprecated Per discussion on COMMONS-DEV, the behind-the-scenes use
33 * of this class as a proxy factory has been removed. For 1.0, you
34 * can still request it directly if you wish, but it doesn't really
35 * do anything useful, and will be removed in 1.1.
36 *
37 * @author Costin Manolache
38 */
39 public final class Log4jFactory extends LogFactory {
40
41 public Log4jFactory() {
42 super();
43 }
44
45 /***
46 * The configuration attributes for this {@link LogFactory}.
47 */
48 private Hashtable attributes = new Hashtable();
49
50
51 private Hashtable instances = new Hashtable();
52
53
54
55 /***
56 * Return the configuration attribute with the specified name (if any),
57 * or <code>null</code> if there is no such attribute.
58 *
59 * @param name Name of the attribute to return
60 */
61 public Object getAttribute(String name) {
62 return (attributes.get(name));
63 }
64
65
66 /***
67 * Return an array containing the names of all currently defined
68 * configuration attributes. If there are no such attributes, a zero
69 * length array is returned.
70 */
71 public String[] getAttributeNames() {
72 Vector names = new Vector();
73 Enumeration keys = attributes.keys();
74 while (keys.hasMoreElements()) {
75 names.addElement((String) keys.nextElement());
76 }
77 String results[] = new String[names.size()];
78 for (int i = 0; i < results.length; i++) {
79 results[i] = (String) names.elementAt(i);
80 }
81 return (results);
82 }
83
84
85 /***
86 * Convenience method to derive a name from the specified class and
87 * call <code>getInstance(String)</code> with it.
88 *
89 * @param clazz Class for which a suitable Log name will be derived
90 *
91 * @exception LogConfigurationException if a suitable <code>Log</code>
92 * instance cannot be returned
93 */
94 public Log getInstance(Class clazz)
95 throws LogConfigurationException
96 {
97 Log instance = (Log) instances.get(clazz);
98 if( instance != null )
99 return instance;
100
101 instance=new Log4JLogger( Logger.getLogger( clazz ));
102 instances.put( clazz, instance );
103 return instance;
104 }
105
106
107 public Log getInstance(String name)
108 throws LogConfigurationException
109 {
110 Log instance = (Log) instances.get(name);
111 if( instance != null )
112 return instance;
113
114 instance=new Log4JLogger( Logger.getLogger( name ));
115 instances.put( name, instance );
116 return instance;
117 }
118
119
120 /***
121 * Release any internal references to previously created {@link Log}
122 * instances returned by this factory. This is useful in environments
123 * like servlet containers, which implement application reloading by
124 * throwing away a ClassLoader. Dangling references to objects in that
125 * class loader would prevent garbage collection.
126 */
127 public void release() {
128
129 instances.clear();
130
131
132 }
133
134
135 /***
136 * Remove any configuration attribute associated with the specified name.
137 * If there is no such attribute, no action is taken.
138 *
139 * @param name Name of the attribute to remove
140 */
141 public void removeAttribute(String name) {
142 attributes.remove(name);
143 }
144
145
146 /***
147 * Set the configuration attribute with the specified name. Calling
148 * this with a <code>null</code> value is equivalent to calling
149 * <code>removeAttribute(name)</code>.
150 *
151 * @param name Name of the attribute to set
152 * @param value Value of the attribute to set, or <code>null</code>
153 * to remove any setting for this attribute
154 */
155 public void setAttribute(String name, Object value) {
156 if (value == null) {
157 attributes.remove(name);
158 } else {
159 attributes.put(name, value);
160 }
161 }
162
163 }