KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > util > monolog > wrapper > javalog > GenericHandler


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
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  * -------------------------------------------------------------------------
19  * $Id: GenericHandler.java 12:18:02 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 /**
23  * Copyright (C) 2001-2003 France Telecom R&D
24  *
25  * This library is free software; you can redistribute it and/or
26  * modify it under the terms of the GNU Lesser General Public
27  * License as published by the Free Software Foundation; either
28  * version 2 of the License, or (at your option) any later version.
29  *
30  * This library is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33  * Lesser General Public License for more details.
34  *
35  * You should have received a copy of the GNU Lesser General Public
36  * License along with this library; if not, write to the Free Software
37  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38  */

39 package org.objectweb.petals.util.monolog.wrapper.javalog;
40
41 import java.io.UnsupportedEncodingException JavaDoc;
42 import java.util.HashMap JavaDoc;
43 import java.util.Map JavaDoc;
44 import java.util.logging.FileHandler JavaDoc;
45 import java.util.logging.Filter JavaDoc;
46 import java.util.logging.Formatter JavaDoc;
47 import java.util.logging.LogRecord JavaDoc;
48
49 import org.objectweb.util.monolog.api.BasicLevel;
50 import org.objectweb.util.monolog.api.Handler;
51 import org.objectweb.util.monolog.api.MonologFactory;
52 import org.objectweb.util.monolog.wrapper.common.RelatifEnvironmentPathGetter;
53 import org.objectweb.util.monolog.wrapper.javaLog.LevelImpl;
54
55 /**
56  * Is a generic handler implementation used to wrapper java.util.logging.Handler
57  * instance.
58  *
59  * @author S.Chassande-Barrioz
60  * @author ddesjardins - eBMWebsourcing
61  */

62 public class GenericHandler extends java.util.logging.Handler JavaDoc implements
63     Handler JavaDoc {
64
65     /**
66      * The real handler
67      */

68     public java.util.logging.Handler JavaDoc handler = null;
69
70     /**
71      * The name of the handler
72      */

73     protected String JavaDoc name;
74
75     /**
76      * the type of the handler (see org.objectweb.util.monolog.api.Handler for
77      * the possible values)
78      */

79     protected String JavaDoc type;
80
81     /**
82      * Attributes which has been assigned on the handler
83      */

84     Map JavaDoc<String JavaDoc, Object JavaDoc> attributes = null;
85
86     public GenericHandler() {
87         attributes = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
88     }
89
90     public GenericHandler(String JavaDoc name) {
91         this();
92         this.name = name;
93     }
94
95     /**
96      * builds a generic handler since a real handler.
97      *
98      * @param name
99      * is the name of the handler
100      * @param h
101      * is the real handler
102      */

103     public GenericHandler(String JavaDoc name, java.util.logging.Handler JavaDoc h) {
104         this(name);
105         handler = h;
106         if (h instanceof FileHandler JavaDoc) {
107             type = "file";
108         } else if (h instanceof ConsoleHandler) {
109             type = "console";
110         }
111     }
112
113     /**
114      * Builds a generic handler with its name and the type. The real handler
115      * will be instanciated after the configuration step.
116      *
117      * @param name
118      * is the name of the handler
119      * @param type
120      * is the type of the handler
121      */

122     public GenericHandler(String JavaDoc name, String JavaDoc type) {
123         this(name);
124         this.type = type;
125     }
126
127     /**
128      * Close the Handler and free all associated resources.
129      */

130     public void close() {
131         handler.close();
132     }
133
134     /**
135      * Flush any buffered output.
136      */

137     public void flush() {
138         handler.flush();
139     }
140
141     /**
142      * It retrieves the value of an attribute value of the handler.
143      *
144      * @param name
145      * is an attribute name
146      */

147     public Object JavaDoc getAttribute(String JavaDoc n) {
148         return attributes.get(n);
149     }
150
151     /**
152      * It retrieves the attributes of the handler
153      */

154     public String JavaDoc[] getAttributeNames() {
155         return (String JavaDoc[]) attributes.keySet().toArray(new String JavaDoc[0]);
156     }
157
158     /**
159      * Return the character encoding for this Handler.
160      */

161     public String JavaDoc getEncoding() {
162         return handler.getEncoding();
163     }
164
165     /**
166      * Get the current Filter for this Handler.
167      */

168     public Filter JavaDoc getFilter() {
169         return handler.getFilter();
170     }
171
172     // OVERRIDE THE java.util.logging.Handler CLASS //
173
// ----------------------------------------------//
174

175     /**
176      * Return the Formatter for this Handler.
177      */

178     public Formatter JavaDoc getFormatter() {
179         return handler.getFormatter();
180     }
181
182     /**
183      * Get the log level specifying which messages will be logged by this
184      * Handler.
185      */

186     public java.util.logging.Level JavaDoc getLevel() {
187         System.out.println("handler(" + name + ").getLevel(): "
188             + handler.getLevel().getName());
189         return handler.getLevel();
190     }
191
192     /**
193      * It retrieves the name of the handler
194      */

195     public String JavaDoc getName() {
196         return name;
197     }
198
199     /**
200      * It retrieves the Handler type
201      */

202     public String JavaDoc getType() {
203         return type;
204     }
205
206     /**
207      * Check if this Handler would actually log a given LogRecord.
208      */

209     public boolean isLoggable(LogRecord JavaDoc record) {
210         // System.out.println("handler("+ name + ").isLoggable(" +
211
// record.getLevel().getName() + "): " + handler.isLoggable(record));
212
return handler.isLoggable(record);
213     }
214
215     /**
216      * Publish a LogRecord.
217      */

218     public void publish(LogRecord JavaDoc record) {
219         // System.out.println("handler("+ name + ").publish(" +
220
// record.getLevel().getName() + "): " + handler.isLoggable(record));
221
handler.publish(record);
222     }
223
224     /**
225      * It assigns an attributte to the handler.
226      *
227      * @param _name
228      * is the attribute name
229      * @param value
230      * is the attribute value
231      * @return the old value is the attribute was already defined
232      */

233     public Object JavaDoc setAttribute(String JavaDoc _name, Object JavaDoc value) {
234         if (!_name.equalsIgnoreCase("activation")) {
235             return attributes.put(_name, value);
236         }
237         if (type == null) {
238             type = (String JavaDoc) attributes.get("handlertype");
239         }
240         MonologFactory mf = (MonologFactory) value;
241         String JavaDoc output = (String JavaDoc) attributes.get(Handler.OUTPUT_ATTRIBUTE);
242         output = RelatifEnvironmentPathGetter.getRealPath(output);
243         String JavaDoc pattern = (String JavaDoc) attributes.get(Handler.PATTERN_ATTRIBUTE);
244         String JavaDoc level = (String JavaDoc) attributes.get(Handler.LEVEL_ATTRIBUTE);
245         String JavaDoc append = (String JavaDoc) attributes.get(Handler.APPEND_MODE_ATTRIBUTE);
246         String JavaDoc nbfile = (String JavaDoc) attributes.get(Handler.FILE_NUMBER_ATTRIBUTE);
247         String JavaDoc fileSize = (String JavaDoc) attributes.get(Handler.MAX_SIZE_ATTRIBUTE);
248         boolean appendVal = true;
249         if (append != null && append.length() > 0) {
250             appendVal = Boolean.getBoolean(append);
251         }
252         int levelVal = BasicLevel.DEBUG;
253         if (level != null && level.length() > 0) {
254             levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl
255                 .evaluate(level, mf);
256         }
257         if ("console".equalsIgnoreCase(type)) {
258             handler = new ConsoleHandler();
259             if (output != null && output.length() > 0) {
260                 if (output.equalsIgnoreCase("System.err")) {
261                     ((ConsoleHandler) handler).setOutput(System.err);
262                 } else if (output.equalsIgnoreCase("switch")) {
263                     ((ConsoleHandler) handler).activateSwitching();
264                 } else if (output.equalsIgnoreCase("System.out")) {
265                     ((ConsoleHandler) handler).setOutput(System.out);
266                 }
267             }
268         } else if ("file".equalsIgnoreCase(type)
269             || "rollingfile".equalsIgnoreCase(type)) {
270             int limit = 0;
271             if (fileSize != null && fileSize.length() > 0) {
272                 limit = Integer.parseInt(fileSize);
273             }
274             int count = 1;
275             if (nbfile != null && nbfile.length() > 0) {
276                 count = Integer.parseInt(nbfile);
277             }
278             try {
279                 handler = new FileHandler JavaDoc(output, limit, count, appendVal);
280             } catch (Exception JavaDoc e) {
281                 throw new IllegalStateException JavaDoc(
282                     "Error when building the handler '" + name + "': "
283                         + e.getMessage());
284             }
285         } else {
286             throw new IllegalStateException JavaDoc("Error when building the handler '"
287                 + name + "': unknwon type: " + type);
288         }
289         handler.setFormatter(new MonologFormatter(pattern));
290         handler.setLevel(LevelImpl.int2Level(levelVal));
291         return null;
292     }
293
294     /**
295      * Set the character encoding used by this Handler.
296      */

297     public void setEncoding(String JavaDoc encoding) throws SecurityException JavaDoc,
298         UnsupportedEncodingException JavaDoc {
299         handler.setEncoding(encoding);
300     }
301
302     /**
303      * Set a Filter to control output on this Handler.
304      */

305     public void setFilter(Filter JavaDoc newFilter) {
306         handler.setFilter(newFilter);
307     }
308
309     /**
310      * Set a Formatter.
311      */

312     public void setFormatter(Formatter JavaDoc newFormatter) {
313         handler.setFormatter(newFormatter);
314     }
315
316     /**
317      * Set the log level specifying which message levels will be logged by this
318      * Handler.
319      */

320     public void setLevel(java.util.logging.Level JavaDoc newLevel) {
321         handler.setLevel(newLevel);
322     }
323
324     /**
325      * It assigns the name of the handler
326      */

327     public void setName(String JavaDoc name) {
328         this.name = name;
329     }
330
331     /**
332      * Set the most recent IO exception.
333      */

334     protected void setException(Exception JavaDoc exception) {
335     }
336
337 }
338
Popular Tags