001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: ApplLog4jFactory.java,v 1.3 2007/03/27 21:59:43 mlipp Exp $
021: *
022: * $Log: ApplLog4jFactory.java,v $
023: * Revision 1.3 2007/03/27 21:59:43 mlipp
024: * Fixed lots of checkstyle warnings.
025: *
026: * Revision 1.2 2006/09/29 12:32:13 drmlipp
027: * Consistently using WfMOpen as projct name now.
028: *
029: * Revision 1.1.1.2 2004/08/18 15:17:35 drmlipp
030: * Update to 1.2
031: *
032: * Revision 1.3 2004/06/22 11:52:04 lipp
033: * Some fixes to match new commons-logging.
034: *
035: * Revision 1.2 2003/06/27 08:51:46 lipp
036: * Fixed copyright/license information.
037: *
038: * Revision 1.1 2003/03/28 10:09:20 lipp
039: * Intriduced logging using commons-logging.
040: *
041: */
042: package de.danet.an.util.log4j;
043:
044: import java.util.Enumeration;
045: import java.util.Hashtable;
046: import java.util.Vector;
047:
048: import org.apache.commons.logging.Log;
049: import org.apache.commons.logging.LogConfigurationException;
050: import org.apache.commons.logging.LogFactory;
051: import org.apache.commons.logging.impl.Log4JLogger;
052:
053: /**
054: * This class provides an alternate access to the log4j logging
055: * library for Apache commons logging.. The necessity arises from the
056: * usage of log4j as logging package in the JBoss application
057: * server. As log4j is already included in JBoss' classpath and
058: * configured by JBoss, we cannot have really independent application
059: * level logging.<P>
060: *
061: * The central problem is the static default hierarchy used by
062: * <code>org.apache.log4j.Category</code>. For a distinct application
063: * level logging we need an alternate hierarchy. This factory uses
064: * such an alternate hierarchy.<P>
065: *
066: * This implementation is based on the <code>Log4JFactory</code> from
067: * Apache.
068: *
069: * @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
070: * @version $Revision: 1.3 $
071: */
072:
073: public class ApplLog4jFactory extends LogFactory {
074:
075: /**
076: * The configuration attributes for this {@link LogFactory}.
077: */
078: private Hashtable attributes = new Hashtable();
079:
080: // previously returned instances, to avoid creation of proxies
081: private Hashtable instances = new Hashtable();
082:
083: /**
084: * Return the configuration attribute with the specified name (if any),
085: * or <code>null</code> if there is no such attribute.
086: *
087: * @param name Name of the attribute to return
088: */
089: public Object getAttribute(String name) {
090: return (attributes.get(name));
091: }
092:
093: /**
094: * Return an array containing the names of all currently defined
095: * configuration attributes. If there are no such attributes, a zero
096: * length array is returned.
097: */
098: public String[] getAttributeNames() {
099: Vector names = new Vector();
100: Enumeration keys = attributes.keys();
101: while (keys.hasMoreElements()) {
102: names.addElement((String) keys.nextElement());
103: }
104: String[] results = new String[names.size()];
105: for (int i = 0; i < results.length; i++) {
106: results[i] = (String) names.elementAt(i);
107: }
108: return (results);
109: }
110:
111: /**
112: * Convenience method to derive a name from the specified class and
113: * call <code>getInstance(String)</code> with it.
114: *
115: * @param clazz Class for which a suitable Log name will be derived
116: *
117: * @exception LogConfigurationException if a suitable <code>Log</code>
118: * instance cannot be returned
119: */
120: public Log getInstance(Class clazz)
121: throws LogConfigurationException {
122: Log instance = (Log) instances.get(clazz);
123: if (instance != null) {
124: return instance;
125: }
126:
127: instance = new Log4JLogger(ApplLogger.getLogger(clazz));
128: instances.put(clazz, instance);
129: return instance;
130: }
131:
132: public Log getInstance(String name)
133: throws LogConfigurationException {
134: Log instance = (Log) instances.get(name);
135: if (instance != null) {
136: return instance;
137: }
138:
139: instance = new Log4JLogger(ApplLogger.getLogger(name));
140: instances.put(name, instance);
141: return instance;
142: }
143:
144: /**
145: * Release any internal references to previously created {@link Log}
146: * instances returned by this factory. This is useful environments
147: * like servlet containers, which implement application reloading by
148: * throwing away a ClassLoader. Dangling references to objects in that
149: * class loader would prevent garbage collection.
150: */
151: public void release() {
152: instances.clear();
153: // what's the log4j mechanism to cleanup ???
154: }
155:
156: /**
157: * Remove any configuration attribute associated with the specified name.
158: * If there is no such attribute, no action is taken.
159: *
160: * @param name Name of the attribute to remove
161: */
162: public void removeAttribute(String name) {
163: attributes.remove(name);
164: }
165:
166: /**
167: * Set the configuration attribute with the specified name. Calling
168: * this with a <code>null</code> value is equivalent to calling
169: * <code>removeAttribute(name)</code>.
170: *
171: * @param name Name of the attribute to set
172: * @param value Value of the attribute to set, or <code>null</code>
173: * to remove any setting for this attribute
174: */
175: public void setAttribute(String name, Object value) {
176: if (value == null) {
177: attributes.remove(name);
178: } else {
179: attributes.put(name, value);
180: }
181: }
182:
183: }
|