KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > javaLog > 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
19 package org.objectweb.util.monolog.wrapper.javaLog;
20
21 import org.objectweb.util.monolog.api.Handler;
22 import org.objectweb.util.monolog.api.MonologFactory;
23 import org.objectweb.util.monolog.api.BasicLevel;
24 import org.objectweb.util.monolog.wrapper.common.RelatifEnvironmentPathGetter;
25
26 import java.io.UnsupportedEncodingException JavaDoc;
27 import java.util.logging.Filter JavaDoc;
28 import java.util.logging.Formatter JavaDoc;
29 import java.util.logging.LogRecord JavaDoc;
30 import java.util.logging.FileHandler JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.HashMap JavaDoc;
33
34 /**
35  * Is a generic handler implementation used to wrapper java.util.logging.Handler
36  * instance.
37  * @author S.Chassande-Barrioz
38  */

39 public class GenericHandler
40         extends java.util.logging.Handler JavaDoc
41         implements Handler {
42
43     /**
44      * The real handler
45      */

46     public java.util.logging.Handler JavaDoc handler = null;
47
48     /**
49      * the type of the handler (see org.objectweb.util.monolog.api.Handler for
50      * the possible values)
51      */

52     protected String JavaDoc type;
53
54     /**
55      * The name of the handler
56      */

57     protected String JavaDoc name;
58
59     /**
60      * Attributes which has been assigned on the handler
61      */

62     Map JavaDoc attributes = null;
63
64     public GenericHandler() {
65         attributes = new HashMap JavaDoc();
66     }
67
68     public GenericHandler(String JavaDoc name) {
69         this();
70         this.name = name;
71     }
72
73     /**
74      * Builds a generic handler with its name and the type. The real handler
75      * will be instanciated after the configuration step.
76      * @param name is the name of the handler
77      * @param type is the type of the handler
78      */

79     public GenericHandler(String JavaDoc name, String JavaDoc type) {
80         this(name);
81         this.type = type;
82     }
83
84     /**
85      * builds a generic handler since a real handler.
86      * @param name is the name of the handler
87      * @param h is the real handler
88      */

89     public GenericHandler(String JavaDoc name, java.util.logging.Handler JavaDoc h) {
90         this(name);
91         handler = h;
92         if (h instanceof FileHandler JavaDoc) {
93             type = "file";
94         } else if (h instanceof ConsoleHandler) {
95             type = "console";
96         }
97     }
98
99     /**
100      * It retrieves the name of the handler
101      */

102     public String JavaDoc getName() {
103         return name;
104     }
105
106     /**
107      * It assigns the name of the handler
108      */

109     public void setName(String JavaDoc name) {
110         this.name = name;
111     }
112
113     /**
114      * It retrieves the Handler type
115      */

116     public String JavaDoc getType() {
117         return type;
118     }
119
120     /**
121      * It retrieves the attributes of the handler
122      */

123     public String JavaDoc[] getAttributeNames() {
124         return (String JavaDoc[]) attributes.keySet().toArray(new String JavaDoc[0]);
125     }
126
127     /**
128      * It retrieves the value of an attribute value of the handler.
129      * @param name is an attribute name
130      */

131     public Object JavaDoc getAttribute(String JavaDoc n) {
132         return attributes.get(n);
133     }
134
135     /**
136      * It assigns an attributte to the handler.
137      * @param _name is the attribute name
138      * @param value is the attribute value
139      * @return the old value is the attribute was already defined
140      */

141     public Object JavaDoc setAttribute(String JavaDoc _name, Object JavaDoc value) {
142         if (!_name.equalsIgnoreCase("activation")) {
143             return attributes.put(_name, value);
144         }
145         if (type == null) {
146             type = (String JavaDoc) attributes.get("handlertype");
147         }
148         MonologFactory mf = (MonologFactory) value;
149         String JavaDoc output = (String JavaDoc) attributes.get(Handler.OUTPUT_ATTRIBUTE);
150         output = RelatifEnvironmentPathGetter.getRealPath(output);
151         String JavaDoc pattern = (String JavaDoc) attributes.get(Handler.PATTERN_ATTRIBUTE);
152         String JavaDoc level = (String JavaDoc) attributes.get(Handler.LEVEL_ATTRIBUTE);
153         String JavaDoc append = (String JavaDoc) attributes.get(Handler.APPEND_MODE_ATTRIBUTE);
154         String JavaDoc nbfile = (String JavaDoc) attributes.get(Handler.FILE_NUMBER_ATTRIBUTE);
155         String JavaDoc fileSize = (String JavaDoc) attributes.get(Handler.MAX_SIZE_ATTRIBUTE);
156         boolean appendVal = true;
157         if (append != null && append.length() > 0) {
158             appendVal = Boolean.getBoolean(append);
159         }
160         int levelVal = BasicLevel.DEBUG;
161         if (level != null && level.length() > 0) {
162             levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl.evaluate(level, mf);
163         }
164         if ("console".equalsIgnoreCase(type)) {
165             handler = new ConsoleHandler();
166             if (output != null && output.length() >0) {
167                 if (output.equalsIgnoreCase("System.err")) {
168                     ((ConsoleHandler) handler).setOutput(System.err);
169                 } else if (output.equalsIgnoreCase("switch")) {
170                     ((ConsoleHandler) handler).activateSwitching();
171                 } else if (output.equalsIgnoreCase("System.out")) {
172                     ((ConsoleHandler) handler).setOutput(System.out);
173                 }
174             }
175         } else if ("file".equalsIgnoreCase(type)
176             || "rollingfile".equalsIgnoreCase(type)) {
177             int limit = 0;
178             if (fileSize != null && fileSize.length() > 0) {
179                 limit = Integer.parseInt(fileSize);
180             }
181             int count = 1;
182             if (nbfile != null && nbfile.length() > 0) {
183                 count = Integer.parseInt(nbfile);
184             }
185             try {
186                 handler = new FileHandler JavaDoc(output, limit, count, appendVal);
187             } catch (Exception JavaDoc e) {
188                 throw new IllegalStateException JavaDoc("Error when building the handler '"
189                     + name + "': " + e.getMessage());
190             }
191         } else {
192             throw new IllegalStateException JavaDoc("Error when building the handler '"
193                 + name + "': unknwon type: " + type);
194         }
195         handler.setFormatter(new MonologFormatter(pattern));
196         handler.setLevel(LevelImpl.int2Level(levelVal));
197         return null;
198     }
199
200
201     // OVERRIDE THE java.util.logging.Handler CLASS //
202
//----------------------------------------------//
203

204     /**
205      * Close the Handler and free all associated resources.
206      */

207     public void close() {
208         handler.close();
209     }
210
211     /**
212      * Flush any buffered output.
213      */

214     public void flush() {
215         handler.flush();
216     }
217
218     /**
219      * Return the character encoding for this Handler.
220      */

221     public String JavaDoc getEncoding() {
222         return handler.getEncoding();
223     }
224
225     /**
226      * Get the current Filter for this Handler.
227      */

228     public Filter JavaDoc getFilter() {
229         return handler.getFilter();
230     }
231
232     /**
233      * Return the Formatter for this Handler.
234      */

235     public Formatter JavaDoc getFormatter() {
236         return handler.getFormatter();
237     }
238
239     /**
240      * Get the log level specifying which messages will be logged by this Handler.
241      */

242     public java.util.logging.Level JavaDoc getLevel() {
243         System.out.println("handler("+ name + ").getLevel(): " + handler.getLevel().getName());
244         return handler.getLevel();
245     }
246
247     /**
248      * Check if this Handler would actually log a given LogRecord.
249      */

250     public boolean isLoggable(LogRecord JavaDoc record) {
251         //System.out.println("handler("+ name + ").isLoggable(" + record.getLevel().getName() + "): " + handler.isLoggable(record));
252
return handler.isLoggable(record);
253     }
254
255     /**
256      * Publish a LogRecord.
257      */

258     public void publish(LogRecord JavaDoc record) {
259         //System.out.println("handler("+ name + ").publish(" + record.getLevel().getName() + "): " + handler.isLoggable(record));
260
handler.publish(record);
261     }
262
263     /**
264      * Set the character encoding used by this Handler.
265      */

266     public void setEncoding(String JavaDoc encoding)
267             throws SecurityException JavaDoc, UnsupportedEncodingException JavaDoc {
268         handler.setEncoding(encoding);
269     }
270
271     /**
272      * Set the most recent IO exception.
273      */

274     protected void setException(Exception JavaDoc exception) {
275     }
276
277     /**
278      * Set a Filter to control output on this Handler.
279      */

280     public void setFilter(Filter JavaDoc newFilter) {
281         handler.setFilter(newFilter);
282     }
283
284     /**
285      * Set a Formatter.
286      */

287     public void setFormatter(Formatter JavaDoc newFormatter) {
288         handler.setFormatter(newFormatter);
289     }
290
291     /**
292      * Set the log level specifying which message levels will
293      * be logged by this Handler.
294      */

295     public void setLevel(java.util.logging.Level JavaDoc newLevel) {
296         handler.setLevel(newLevel);
297     }
298
299 }
300
Popular Tags