Source code: de/danet/an/util/log4j/ApplLog4jFactory.java
1 /*
2 * This file is part of the WfMCore/WfMOpen project.
3 * Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
4 * All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * $Id: ApplLog4jFactory.java,v 1.2 2003/06/27 08:51:46 lipp Exp $
21 *
22 * $Log: ApplLog4jFactory.java,v $
23 * Revision 1.2 2003/06/27 08:51:46 lipp
24 * Fixed copyright/license information.
25 *
26 * Revision 1.1 2003/03/28 10:09:20 lipp
27 * Intriduced logging using commons-logging.
28 *
29 */
30 package de.danet.an.util.log4j;
31
32 import java.util.Enumeration;
33 import java.util.Hashtable;
34 import java.util.Vector;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogConfigurationException;
38 import org.apache.commons.logging.LogFactory;
39 import org.apache.commons.logging.impl.Log4JCategoryLog;
40
41 /**
42 * This class provides an alternate access to the log4j logging
43 * library for Apache commons logging.. The necessity arises from the
44 * usage of log4j as logging package in the JBoss application
45 * server. As log4j is already included in JBoss' classpath and
46 * configured by JBoss, we cannot have really independent application
47 * level logging.<P>
48 *
49 * The central problem is the static default hierarchy used by
50 * <code>org.apache.log4j.Category</code>. For a distinct application
51 * level logging we need an alternate hierarchy. This factory uses
52 * such an alternate hierarchy.<P>
53 *
54 * This implementation is based on the <code>Log4JFactory</code> from
55 * Apache.
56 *
57 * @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
58 * @version $Revision: 1.2 $
59 */
60
61 public class ApplLog4jFactory extends LogFactory {
62
63 /**
64 * The configuration attributes for this {@link LogFactory}.
65 */
66 private Hashtable attributes = new Hashtable();
67
68 // previously returned instances, to avoid creation of proxies
69 private Hashtable instances = new Hashtable();
70
71 /**
72 * Return the configuration attribute with the specified name (if any),
73 * or <code>null</code> if there is no such attribute.
74 *
75 * @param name Name of the attribute to return
76 */
77 public Object getAttribute(String name) {
78 return (attributes.get(name));
79 }
80
81 /**
82 * Return an array containing the names of all currently defined
83 * configuration attributes. If there are no such attributes, a zero
84 * length array is returned.
85 */
86 public String[] getAttributeNames() {
87 Vector names = new Vector();
88 Enumeration keys = attributes.keys();
89 while (keys.hasMoreElements()) {
90 names.addElement((String) keys.nextElement());
91 }
92 String results[] = new String[names.size()];
93 for (int i = 0; i < results.length; i++) {
94 results[i] = (String) names.elementAt(i);
95 }
96 return (results);
97 }
98
99 /**
100 * Convenience method to derive a name from the specified class and
101 * call <code>getInstance(String)</code> with it.
102 *
103 * @param clazz Class for which a suitable Log name will be derived
104 *
105 * @exception LogConfigurationException if a suitable <code>Log</code>
106 * instance cannot be returned
107 */
108 public Log getInstance(Class clazz)
109 throws LogConfigurationException
110 {
111 Log instance = (Log) instances.get(clazz);
112 if(instance != null)
113 return instance;
114
115 instance=new Log4JCategoryLog(ApplLogger.getLogger(clazz));
116 instances.put(clazz, instance);
117 return instance;
118 }
119
120 public Log getInstance(String name)
121 throws LogConfigurationException
122 {
123 Log instance = (Log) instances.get(name);
124 if( instance != null )
125 return instance;
126
127 instance=new Log4JCategoryLog(ApplLogger.getLogger(name));
128 instances.put(name, instance);
129 return instance;
130 }
131
132 /**
133 * Release any internal references to previously created {@link Log}
134 * instances returned by this factory. This is useful environments
135 * like servlet containers, which implement application reloading by
136 * throwing away a ClassLoader. Dangling references to objects in that
137 * class loader would prevent garbage collection.
138 */
139 public void release() {
140 instances.clear();
141 // what's the log4j mechanism to cleanup ???
142 }
143
144
145 /**
146 * Remove any configuration attribute associated with the specified name.
147 * If there is no such attribute, no action is taken.
148 *
149 * @param name Name of the attribute to remove
150 */
151 public void removeAttribute(String name) {
152 attributes.remove(name);
153 }
154
155
156 /**
157 * Set the configuration attribute with the specified name. Calling
158 * this with a <code>null</code> value is equivalent to calling
159 * <code>removeAttribute(name)</code>.
160 *
161 * @param name Name of the attribute to set
162 * @param value Value of the attribute to set, or <code>null</code>
163 * to remove any setting for this attribute
164 */
165 public void setAttribute(String name, Object value) {
166 if (value == null) {
167 attributes.remove(name);
168 } else {
169 attributes.put(name, value);
170 }
171 }
172
173
174 }