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.javaLog;
018:
019: import org.objectweb.util.monolog.api.Handler;
020: import org.objectweb.util.monolog.api.MonologFactory;
021: import org.objectweb.util.monolog.api.BasicLevel;
022: import org.objectweb.util.monolog.wrapper.common.RelatifEnvironmentPathGetter;
023:
024: import java.io.UnsupportedEncodingException;
025: import java.util.logging.Filter;
026: import java.util.logging.Formatter;
027: import java.util.logging.LogRecord;
028: import java.util.logging.FileHandler;
029: import java.util.Map;
030: import java.util.HashMap;
031:
032: /**
033: * Is a generic handler implementation used to wrapper java.util.logging.Handler
034: * instance.
035: * @author S.Chassande-Barrioz
036: */
037: public class GenericHandler extends java.util.logging.Handler implements
038: Handler {
039:
040: /**
041: * The real handler
042: */
043: public java.util.logging.Handler handler = null;
044:
045: /**
046: * the type of the handler (see org.objectweb.util.monolog.api.Handler for
047: * the possible values)
048: */
049: protected String type;
050:
051: /**
052: * The name of the handler
053: */
054: protected String name;
055:
056: /**
057: * Attributes which has been assigned on the handler
058: */
059: Map attributes = null;
060:
061: public GenericHandler() {
062: attributes = new HashMap();
063: }
064:
065: public GenericHandler(String name) {
066: this ();
067: this .name = name;
068: }
069:
070: /**
071: * Builds a generic handler with its name and the type. The real handler
072: * will be instanciated after the configuration step.
073: * @param name is the name of the handler
074: * @param type is the type of the handler
075: */
076: public GenericHandler(String name, String type) {
077: this (name);
078: this .type = type;
079: }
080:
081: /**
082: * builds a generic handler since a real handler.
083: * @param name is the name of the handler
084: * @param h is the real handler
085: */
086: public GenericHandler(String name, java.util.logging.Handler h) {
087: this (name);
088: handler = h;
089: if (h instanceof FileHandler) {
090: type = "file";
091: } else if (h instanceof ConsoleHandler) {
092: type = "console";
093: }
094: }
095:
096: /**
097: * It retrieves the name of the handler
098: */
099: public String getName() {
100: return name;
101: }
102:
103: /**
104: * It assigns the name of the handler
105: */
106: public void setName(String name) {
107: this .name = name;
108: }
109:
110: /**
111: * It retrieves the Handler type
112: */
113: public String getType() {
114: return type;
115: }
116:
117: /**
118: * It retrieves the attributes of the handler
119: */
120: public String[] getAttributeNames() {
121: return (String[]) attributes.keySet().toArray(new String[0]);
122: }
123:
124: /**
125: * It retrieves the value of an attribute value of the handler.
126: * @param name is an attribute name
127: */
128: public Object getAttribute(String n) {
129: return attributes.get(n);
130: }
131:
132: /**
133: * It assigns an attributte to the handler.
134: * @param _name is the attribute name
135: * @param value is the attribute value
136: * @return the old value is the attribute was already defined
137: */
138: public Object setAttribute(String _name, Object value) {
139: if (!_name.equalsIgnoreCase("activation")) {
140: return attributes.put(_name, value);
141: }
142: if (type == null) {
143: type = (String) attributes.get("handlertype");
144: }
145: MonologFactory mf = (MonologFactory) value;
146: String output = (String) attributes
147: .get(Handler.OUTPUT_ATTRIBUTE);
148: output = RelatifEnvironmentPathGetter.getRealPath(output);
149: String pattern = (String) attributes
150: .get(Handler.PATTERN_ATTRIBUTE);
151: String level = (String) attributes.get(Handler.LEVEL_ATTRIBUTE);
152: String append = (String) attributes
153: .get(Handler.APPEND_MODE_ATTRIBUTE);
154: String nbfile = (String) attributes
155: .get(Handler.FILE_NUMBER_ATTRIBUTE);
156: String fileSize = (String) attributes
157: .get(Handler.MAX_SIZE_ATTRIBUTE);
158: boolean appendVal = true;
159: if (append != null && append.length() > 0) {
160: appendVal = Boolean.getBoolean(append);
161: }
162: int levelVal = BasicLevel.DEBUG;
163: if (level != null && level.length() > 0) {
164: levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl
165: .evaluate(level, mf);
166: }
167: if ("console".equalsIgnoreCase(type)) {
168: handler = new ConsoleHandler();
169: if (output != null && output.length() > 0) {
170: if (output.equalsIgnoreCase("System.err")) {
171: ((ConsoleHandler) handler).setOutput(System.err);
172: } else if (output.equalsIgnoreCase("switch")) {
173: ((ConsoleHandler) handler).activateSwitching();
174: } else if (output.equalsIgnoreCase("System.out")) {
175: ((ConsoleHandler) handler).setOutput(System.out);
176: }
177: }
178: } else if ("file".equalsIgnoreCase(type)
179: || "rollingfile".equalsIgnoreCase(type)) {
180: int limit = 0;
181: if (fileSize != null && fileSize.length() > 0) {
182: limit = Integer.parseInt(fileSize);
183: }
184: int count = 1;
185: if (nbfile != null && nbfile.length() > 0) {
186: count = Integer.parseInt(nbfile);
187: }
188: try {
189: handler = new FileHandler(output, limit, count,
190: appendVal);
191: } catch (Exception e) {
192: throw new IllegalStateException(
193: "Error when building the handler '" + name
194: + "': " + e.getMessage());
195: }
196: } else {
197: throw new IllegalStateException(
198: "Error when building the handler '" + name
199: + "': unknwon type: " + type);
200: }
201: handler.setFormatter(new MonologFormatter(pattern));
202: handler.setLevel(LevelImpl.int2Level(levelVal));
203: return null;
204: }
205:
206: // OVERRIDE THE java.util.logging.Handler CLASS //
207: //----------------------------------------------//
208:
209: /**
210: * Close the Handler and free all associated resources.
211: */
212: public void close() {
213: handler.close();
214: }
215:
216: /**
217: * Flush any buffered output.
218: */
219: public void flush() {
220: handler.flush();
221: }
222:
223: /**
224: * Return the character encoding for this Handler.
225: */
226: public String getEncoding() {
227: return handler.getEncoding();
228: }
229:
230: /**
231: * Get the current Filter for this Handler.
232: */
233: public Filter getFilter() {
234: return handler.getFilter();
235: }
236:
237: /**
238: * Return the Formatter for this Handler.
239: */
240: public Formatter getFormatter() {
241: return handler.getFormatter();
242: }
243:
244: /**
245: * Get the log level specifying which messages will be logged by this Handler.
246: */
247: public java.util.logging.Level getLevel() {
248: System.out.println("handler(" + name + ").getLevel(): "
249: + handler.getLevel().getName());
250: return handler.getLevel();
251: }
252:
253: /**
254: * Check if this Handler would actually log a given LogRecord.
255: */
256: public boolean isLoggable(LogRecord record) {
257: //System.out.println("handler("+ name + ").isLoggable(" + record.getLevel().getName() + "): " + handler.isLoggable(record));
258: return handler.isLoggable(record);
259: }
260:
261: /**
262: * Publish a LogRecord.
263: */
264: public void publish(LogRecord record) {
265: //System.out.println("handler("+ name + ").publish(" + record.getLevel().getName() + "): " + handler.isLoggable(record));
266: handler.publish(record);
267: }
268:
269: /**
270: * Set the character encoding used by this Handler.
271: */
272: public void setEncoding(String encoding) throws SecurityException,
273: UnsupportedEncodingException {
274: handler.setEncoding(encoding);
275: }
276:
277: /**
278: * Set the most recent IO exception.
279: */
280: protected void setException(Exception exception) {
281: }
282:
283: /**
284: * Set a Filter to control output on this Handler.
285: */
286: public void setFilter(Filter newFilter) {
287: handler.setFilter(newFilter);
288: }
289:
290: /**
291: * Set a Formatter.
292: */
293: public void setFormatter(Formatter newFormatter) {
294: handler.setFormatter(newFormatter);
295: }
296:
297: /**
298: * Set the log level specifying which message levels will
299: * be logged by this Handler.
300: */
301: public void setLevel(java.util.logging.Level newLevel) {
302: handler.setLevel(newLevel);
303: }
304:
305: }
|