KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (C) 2002
3  */

4
5 package org.objectweb.util.monolog.wrapper.log4j;
6
7 import org.apache.log4j.nt.NTEventLogAppender;
8 import org.apache.log4j.PatternLayout;
9 import org.objectweb.util.monolog.api.Handler;
10 import org.objectweb.util.monolog.wrapper.log4j.PatternConverter;
11 import org.objectweb.util.monolog.api.MonologFactory;
12
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 /**
18  * This class is the wrapper to the org.apache.log4j.nt.NTEventLogAppender
19  *
20  * @author Sebastien Chassande-Barrioz
21  * @author Igor Smirnov
22  */

23 public class NTEventLogHandler extends NTEventLogAppender implements Handler {
24
25     /**
26      * This fields contains the properties of the Handler
27      */

28     protected HashMap JavaDoc prop = null;
29
30     public NTEventLogHandler() {
31         super();
32     }
33
34     /**
35      * It Builds a new NTEventLogHandler. It is needed to specify an handler
36      * type.
37      * @param name is the handler name.
38      */

39     public NTEventLogHandler(String JavaDoc name) {
40         super();
41         setName(name);
42         prop = new HashMap JavaDoc();
43     }
44
45     public Map JavaDoc getAttributes() {
46         return prop;
47     }
48
49     public void setAttributes(Map JavaDoc attributes) {
50         prop.clear();
51         prop.putAll(attributes);
52         Object JavaDoc mf = prop.get("activation");
53         if (mf != null) {
54             prop.remove("activation");
55             setAttribute("activation", mf);
56         }
57     }
58
59     // IMPLEMENTATION OF THE Handler INTERFACE //
60
//---------------------------------------------//
61

62     public String JavaDoc getType() {
63         return "ntevent";
64     }
65
66     public String JavaDoc[] getAttributeNames() {
67         return (String JavaDoc[]) prop.keySet().toArray(new String JavaDoc[0]);
68     }
69
70     public Object JavaDoc getAttribute(String JavaDoc key) {
71         return prop.get(key);
72     }
73
74     public Object JavaDoc setAttribute(String JavaDoc key, Object JavaDoc value) {
75         if (prop == null)
76             prop = new HashMap JavaDoc();
77         if (!key.equalsIgnoreCase("activation")) {
78             return prop.put(key, value);
79         } else if (prop.containsKey(key)) {
80             return null; //already activated
81
}
82         MonologFactory mf = (MonologFactory) value;
83         String JavaDoc source = (String JavaDoc) prop.get("source");
84         if (source != null) {
85             setSource((String JavaDoc) value);
86         }
87         String JavaDoc pattern = (String JavaDoc) prop.get(Handler.PATTERN_ATTRIBUTE);
88         if (pattern != null) {
89             setLayout(new PatternLayout(PatternConverter.monolog2log4j(pattern)));
90         }
91         String JavaDoc level = (String JavaDoc) prop.get(Handler.LEVEL_ATTRIBUTE);
92         if (level != null && level.length() > 0) {
93             int levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl.evaluate(level, mf);
94             setThreshold(org.apache.log4j.Level.toLevel(levelVal));
95         }
96         super.activateOptions();
97         return null;
98     }
99 }
100
Popular Tags