KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > logging > impl > Log4jFactory


1 /*
2  * ====================================================================
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
29  * Foundation" must not be used to endorse or promote products derived
30  * from this software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache"
34  * nor may "Apache" appear in their names without prior written
35  * permission of the Apache Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  *
56  */

57
58 package org.logicalcobwebs.logging.impl;
59
60 import org.apache.log4j.Category;
61 import org.logicalcobwebs.logging.Log;
62 import org.logicalcobwebs.logging.LogConfigurationException;
63 import org.logicalcobwebs.logging.LogFactory;
64
65 import java.util.Enumeration JavaDoc;
66 import java.util.Hashtable JavaDoc;
67 import java.util.Vector JavaDoc;
68
69 /**
70  * <p>Concrete subclass of {@link org.logicalcobwebs.logging.LogFactory} specific to log4j.
71  *
72  * @author Costin Manolache
73  */

74 public final class Log4jFactory extends LogFactory {
75
76     public Log4jFactory () {
77         super ();
78     }
79
80     /**
81      * The configuration attributes for this {@link org.logicalcobwebs.logging.LogFactory}.
82      */

83     private Hashtable JavaDoc attributes = new Hashtable JavaDoc ();
84
85     // previously returned instances, to avoid creation of proxies
86
private Hashtable JavaDoc instances = new Hashtable JavaDoc ();
87
88     // --------------------------------------------------------- Public Methods
89

90     /**
91      * Return the configuration attribute with the specified name (if any),
92      * or <code>null</code> if there is no such attribute.
93      *
94      * @param name Name of the attribute to return
95      */

96     public Object JavaDoc getAttribute (String JavaDoc name) {
97         return (attributes.get (name));
98     }
99
100     /**
101      * Return an array containing the names of all currently defined
102      * configuration attributes. If there are no such attributes, a zero
103      * length array is returned.
104      */

105     public String JavaDoc[] getAttributeNames () {
106         Vector JavaDoc names = new Vector JavaDoc ();
107         Enumeration JavaDoc keys = attributes.keys ();
108         while (keys.hasMoreElements ()) {
109             names.addElement (keys.nextElement ());
110         }
111         String JavaDoc results[] = new String JavaDoc[names.size ()];
112         for (int i = 0; i < results.length; i++) {
113             results[i] = (String JavaDoc) names.elementAt (i);
114         }
115         return (results);
116     }
117
118     /**
119      * Convenience method to derive a name from the specified class and
120      * call <code>getInstance(String)</code> with it.
121      *
122      * @param clazz Class for which a suitable Log name will be derived
123      *
124      * @exception org.logicalcobwebs.logging.LogConfigurationException if a suitable <code>Log</code>
125      * instance cannot be returned
126      */

127     public Log getInstance (Class JavaDoc clazz)
128             throws LogConfigurationException {
129         Log instance = (Log) instances.get (clazz);
130         if (instance != null) {
131             return instance;
132         }
133
134         instance = new Log4JCategoryLog (Category.getInstance (clazz));
135         instances.put (clazz, instance);
136         return instance;
137     }
138
139     public Log getInstance (String JavaDoc name)
140             throws LogConfigurationException {
141         Log instance = (Log) instances.get (name);
142         if (instance != null) {
143             return instance;
144         }
145
146
147         instance = new Log4JCategoryLog (Category.getInstance (name));
148         instances.put (name, instance);
149         return instance;
150     }
151
152     /**
153      * Release any internal references to previously created {@link org.logicalcobwebs.logging.Log}
154      * instances returned by this factory. This is useful environments
155      * like servlet containers, which implement application reloading by
156      * throwing away a ClassLoader. Dangling references to objects in that
157      * class loader would prevent garbage collection.
158      */

159     public void release () {
160
161         instances.clear ();
162
163         // what's the log4j mechanism to cleanup ???
164
}
165
166     /**
167      * Remove any configuration attribute associated with the specified name.
168      * If there is no such attribute, no action is taken.
169      *
170      * @param name Name of the attribute to remove
171      */

172     public void removeAttribute (String JavaDoc name) {
173         attributes.remove (name);
174     }
175
176     /**
177      * Set the configuration attribute with the specified name. Calling
178      * this with a <code>null</code> value is equivalent to calling
179      * <code>removeAttribute(name)</code>.
180      *
181      * @param name Name of the attribute to set
182      * @param value Value of the attribute to set, or <code>null</code>
183      * to remove any setting for this attribute
184      */

185     public void setAttribute (String JavaDoc name, Object JavaDoc value) {
186         if (value == null) {
187             attributes.remove (name);
188         } else {
189             attributes.put (name, value);
190         }
191     }
192
193 }
194
195
Popular Tags