001: /**
002: * Copyright (C) 2001-2003 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.util.monolog.wrapper.log4j;
018:
019: import org.apache.log4j.Appender;
020: import org.apache.log4j.Layout;
021: import org.apache.log4j.spi.ErrorHandler;
022: import org.apache.log4j.spi.Filter;
023: import org.apache.log4j.spi.LoggingEvent;
024: import org.objectweb.util.monolog.api.Handler;
025:
026: import java.util.ArrayList;
027: import java.util.HashMap;
028: import java.util.Iterator;
029:
030: /**
031: * This class is a generic implementation of the Handler interface. This class
032: * delegates all calls on a log4j Appender. It is also an Appender interceptor.
033: * This class can therefore be referenced into the log4j struture as an
034: * Appender.
035: * There are three ways to specify the inner Appender:
036: * <ul>
037: * <li>by the construstor with the appender instance</li>
038: * <li>by the setAppender method with the appender instance</li>
039: * <li>by the setAttribute method with the appender class name. This method
040: * tries to instanciate the class, and initializes the new Appender with all
041: * attribute which has been specified before. Even the filters and the layout
042: * are memorized.</li>
043: *
044: * @author Sebastien Chassande-Barrioz
045: */
046: public class GenericHandler implements Appender, Handler {
047:
048: /**
049: * This constant can be used to specify the class name of the inner appender
050: */
051: public static final String APPENDER_CLASS_NAME_ATTR = "appenderClassName";
052:
053: /**
054: * The inner appender
055: */
056: protected Appender appender = null;
057:
058: /**
059: * The appender name
060: */
061: protected String name = null;
062:
063: /**
064: * The properties of the appender
065: */
066: protected HashMap prop = null;
067:
068: protected ArrayList filters = null;
069: protected Layout layout = null;
070:
071: public GenericHandler() {
072: }
073:
074: public GenericHandler(String name) {
075: this .name = name;
076: }
077:
078: public GenericHandler(Appender a) {
079: appender = a;
080: prop = new HashMap();
081: }
082:
083: public Appender getAppender() {
084: return appender;
085: }
086:
087: public void setAppender(Appender a) {
088: appender = a;
089: if (layout != null) {
090: appender.setLayout(layout);
091: }
092: if (filters != null) {
093: for (Iterator it = filters.iterator(); it.hasNext();) {
094: appender.addFilter((Filter) it.next());
095: }
096: }
097: }
098:
099: // IMPLEMENTATION OF THE Appender INTERFACE //
100: //------------------------------------------//
101:
102: public String getName() {
103: return name = appender.getName();
104: }
105:
106: public void setName(String n) {
107: name = n;
108: appender.setName(n);
109: }
110:
111: public String getType() {
112: return "generic";
113: }
114:
115: public String[] getAttributeNames() {
116: return (String[]) prop.keySet().toArray(new String[0]);
117: }
118:
119: public Object getAttribute(String key) {
120: return prop.get(key);
121: }
122:
123: public Object setAttribute(String key, Object value) {
124: if (APPENDER_CLASS_NAME_ATTR.equalsIgnoreCase(key)) {
125: try {
126: setAppender((Appender) Class.forName((String) value)
127: .newInstance());
128: } catch (Exception e) {
129: }
130: }
131: return null;
132: }
133:
134: // IMPLEMENTATION OF THE Appender INTERFACE //
135: //------------------------------------------//
136:
137: public void addFilter(Filter newFilter) {
138: if (appender != null) {
139: appender.addFilter(newFilter);
140: } else {
141: if (filters == null)
142: filters = new ArrayList();
143: filters.add(newFilter);
144: }
145: }
146:
147: public void clearFilters() {
148: if (appender != null) {
149: appender.clearFilters();
150: } else {
151: if (filters != null)
152: filters.clear();
153: }
154: }
155:
156: public void close() {
157: if (appender != null) {
158: appender.close();
159: }
160: }
161:
162: public void doAppend(LoggingEvent event) {
163: if (appender != null) {
164: appender.doAppend(event);
165: }
166: }
167:
168: public void setErrorHandler(ErrorHandler errorHandler) {
169: if (appender != null) {
170: appender.setErrorHandler(errorHandler);
171: }
172: }
173:
174: public void setLayout(Layout layout) {
175: if (appender != null) {
176: appender.setLayout(layout);
177: } else {
178: this .layout = layout;
179: }
180: }
181:
182: public Filter getFilter() {
183: if (appender != null) {
184: return appender.getFilter();
185: } else if (filters != null && filters.size() > 0) {
186: return (Filter) filters.get(0);
187: }
188: return null;
189: }
190:
191: public ErrorHandler getErrorHandler() {
192: return (appender != null ? appender.getErrorHandler() : null);
193: }
194:
195: public Layout getLayout() {
196: return layout;
197: }
198:
199: public boolean requiresLayout() {
200: return (appender != null ? appender.requiresLayout() : true);
201: }
202: }
|