KickJava   Java API By Example, From Geeks To Geeks.

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


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.log4j;
20
21 import org.apache.log4j.FileAppender;
22 import org.apache.log4j.PatternLayout;
23 import org.objectweb.util.monolog.Monolog;
24 import org.objectweb.util.monolog.api.Handler;
25 import org.objectweb.util.monolog.api.MonologFactory;
26 import org.objectweb.util.monolog.wrapper.common.RelatifEnvironmentPathGetter;
27
28 import java.io.IOException JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * This class is the wrapper to the org.apache.log4j.FileAppender
34  *
35  * @author Sebastien Chassande-Barrioz
36  */

37 public class FileHandler extends FileAppender implements Handler {
38
39     /**
40      * This fields contains the properties of the Handler
41      */

42     protected HashMap JavaDoc prop = null;
43
44     public FileHandler() {
45         super();
46     }
47
48     /**
49      * It Builds a new FileHandler.
50      * @param name is the handler name.
51      */

52     public FileHandler(String JavaDoc name) {
53         super();
54         setName(name);
55         prop = new HashMap JavaDoc();
56     }
57
58     public Map JavaDoc getAttributes() {
59         return prop;
60     }
61
62     public void setAttributes(Map JavaDoc attributes) {
63         prop.clear();
64         prop.putAll(attributes);
65         Object JavaDoc mf = prop.get("activation");
66         if (mf != null) {
67             prop.remove("activation");
68             setAttribute("activation", mf);
69         }
70     }
71
72     // IMPLEMENTATION OF THE Handler INTERFACE //
73
//---------------------------------------------//
74

75     public String JavaDoc getType() {
76         return "file";
77     }
78
79     public String JavaDoc[] getAttributeNames() {
80         return (String JavaDoc[]) prop.keySet().toArray(new String JavaDoc[0]);
81     }
82
83     public Object JavaDoc getAttribute(String JavaDoc key) {
84         return prop.get(key);
85     }
86
87     public Object JavaDoc setAttribute(String JavaDoc key, Object JavaDoc value) {
88         if (prop == null) {
89             prop = new HashMap JavaDoc();
90         }
91         if (!key.equalsIgnoreCase("activation")) {
92             return prop.put(key, value);
93         } else if (prop.containsKey(key)) {
94             return null; //already activated
95
}
96         MonologFactory mf = (MonologFactory) value;
97         String JavaDoc output = (String JavaDoc) prop.get(Handler.OUTPUT_ATTRIBUTE);
98         output = RelatifEnvironmentPathGetter.getRealPath(output);
99         String JavaDoc append = (String JavaDoc) prop.get(Handler.APPEND_MODE_ATTRIBUTE);
100         if (append != null && append.length() > 0) {
101             fileAppend = Boolean.getBoolean(append);
102         } else {
103             fileAppend = true;
104         }
105         
106         String JavaDoc buffersize = (String JavaDoc) prop.get(Handler.BUFFER_ATTRIBUTE);
107         if (buffersize != null && buffersize.length() > 0) {
108             try {
109                 setBufferSize(Integer.valueOf(buffersize).intValue());
110             } catch (NumberFormatException JavaDoc e) {
111                 Monolog.error("Bad specified buffer size for the handler '"
112                         + name + "': " + buffersize, e);
113             }
114         }
115         
116         try {
117             setFile(output, fileAppend, bufferedIO, bufferSize);
118         } catch (IOException JavaDoc e) {
119             Monolog.error("Error during the creation of the handler '"
120                     + name + "': ", e);
121         }
122
123         String JavaDoc pattern = (String JavaDoc) prop.get(Handler.PATTERN_ATTRIBUTE);
124         setLayout(new PatternLayout(PatternConverter.monolog2log4j(pattern)));
125
126         String JavaDoc level = (String JavaDoc) prop.get(Handler.LEVEL_ATTRIBUTE);
127         if (level != null && level.length() > 0) {
128             int levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl.evaluate(level, mf);
129             setThreshold(org.apache.log4j.Level.toLevel(levelVal));
130         }
131         
132         super.activateOptions();
133         return null;
134     }
135
136 }
137
Popular Tags