KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > log4j > GenericHandler


1 /**
2  * Copyright (C) 2001-2003 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.util.monolog.wrapper.log4j;
19
20 import org.apache.log4j.Appender;
21 import org.apache.log4j.Layout;
22 import org.apache.log4j.spi.ErrorHandler;
23 import org.apache.log4j.spi.Filter;
24 import org.apache.log4j.spi.LoggingEvent;
25 import org.objectweb.util.monolog.api.Handler;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 /**
32  * This class is a generic implementation of the Handler interface. This class
33  * delegates all calls on a log4j Appender. It is also an Appender interceptor.
34  * This class can therefore be referenced into the log4j struture as an
35  * Appender.
36  * There are three ways to specify the inner Appender:
37  * <ul>
38  * <li>by the construstor with the appender instance</li>
39  * <li>by the setAppender method with the appender instance</li>
40  * <li>by the setAttribute method with the appender class name. This method
41  * tries to instanciate the class, and initializes the new Appender with all
42  * attribute which has been specified before. Even the filters and the layout
43  * are memorized.</li>
44  *
45  * @author Sebastien Chassande-Barrioz
46  */

47 public class GenericHandler implements Appender, Handler {
48
49     /**
50      * This constant can be used to specify the class name of the inner appender
51      */

52     public static final String JavaDoc APPENDER_CLASS_NAME_ATTR = "appenderClassName";
53
54     /**
55      * The inner appender
56      */

57     protected Appender appender = null;
58
59     /**
60      * The appender name
61      */

62     protected String JavaDoc name = null;
63
64     /**
65      * The properties of the appender
66      */

67     protected HashMap JavaDoc prop = null;
68
69     protected ArrayList JavaDoc filters = null;
70     protected Layout layout = null;
71
72     public GenericHandler() {
73     }
74
75     public GenericHandler(String JavaDoc name) {
76         this.name = name;
77     }
78
79     public GenericHandler(Appender a) {
80         appender = a;
81         prop = new HashMap JavaDoc();
82     }
83
84     public Appender getAppender() {
85         return appender;
86     }
87
88     public void setAppender(Appender a) {
89         appender = a;
90         if (layout != null) {
91             appender.setLayout(layout);
92         }
93         if (filters != null) {
94             for (Iterator JavaDoc it = filters.iterator(); it.hasNext();) {
95                 appender.addFilter((Filter) it.next());
96             }
97         }
98     }
99
100     // IMPLEMENTATION OF THE Appender INTERFACE //
101
//------------------------------------------//
102

103     public String JavaDoc getName() {
104         return name = appender.getName();
105     }
106
107     public void setName(String JavaDoc n) {
108         name = n;
109         appender.setName(n);
110     }
111
112     public String JavaDoc getType() {
113         return "generic";
114     }
115
116     public String JavaDoc[] getAttributeNames() {
117         return (String JavaDoc[]) prop.keySet().toArray(new String JavaDoc[0]);
118     }
119
120     public Object JavaDoc getAttribute(String JavaDoc key) {
121         return prop.get(key);
122     }
123
124     public Object JavaDoc setAttribute(String JavaDoc key, Object JavaDoc value) {
125         if (APPENDER_CLASS_NAME_ATTR.equalsIgnoreCase(key)) {
126             try {
127                 setAppender((Appender)
128                     Class.forName( (String JavaDoc) value).newInstance());
129             }
130             catch (Exception JavaDoc e) {
131             }
132         }
133         return null;
134     }
135
136
137     // IMPLEMENTATION OF THE Appender INTERFACE //
138
//------------------------------------------//
139

140     public void addFilter(Filter newFilter) {
141         if (appender != null) {
142             appender.addFilter(newFilter);
143         }
144         else {
145             if (filters == null)
146                 filters = new ArrayList JavaDoc();
147             filters.add(newFilter);
148         }
149     }
150
151     public void clearFilters() {
152         if (appender != null) {
153             appender.clearFilters();
154         }
155         else {
156             if (filters != null)
157                 filters.clear();
158         }
159     }
160
161     public void close() {
162         if (appender != null) {
163             appender.close();
164         }
165     }
166
167     public void doAppend(LoggingEvent event) {
168         if (appender != null) {
169             appender.doAppend(event);
170         }
171     }
172
173     public void setErrorHandler(ErrorHandler errorHandler) {
174         if (appender != null) {
175             appender.setErrorHandler(errorHandler);
176         }
177     }
178
179     public void setLayout(Layout layout) {
180         if (appender != null) {
181             appender.setLayout(layout);
182         }
183         else {
184             this.layout = layout;
185         }
186     }
187
188     public Filter getFilter() {
189         if (appender != null) {
190             return appender.getFilter();
191         }
192         else if (filters != null && filters.size()>0) {
193             return (Filter) filters.get(0);
194         }
195         return null;
196     }
197
198     public ErrorHandler getErrorHandler() {
199         return (appender != null ? appender.getErrorHandler():null);
200     }
201
202     public Layout getLayout() {
203         return layout;
204     }
205
206     public boolean requiresLayout() {
207         return (appender != null ? appender.requiresLayout() : true);
208     }
209 }
210
Popular Tags