01: /**
02: * Copyright (C) 2002
03: */package org.objectweb.util.monolog.wrapper.log4j;
04:
05: import org.apache.log4j.nt.NTEventLogAppender;
06: import org.apache.log4j.PatternLayout;
07: import org.objectweb.util.monolog.api.Handler;
08: import org.objectweb.util.monolog.wrapper.log4j.PatternConverter;
09: import org.objectweb.util.monolog.api.MonologFactory;
10:
11: import java.util.HashMap;
12: import java.util.Map;
13:
14: /**
15: * This class is the wrapper to the org.apache.log4j.nt.NTEventLogAppender
16: *
17: * @author Sebastien Chassande-Barrioz
18: * @author Igor Smirnov
19: */
20: public class NTEventLogHandler extends NTEventLogAppender implements
21: Handler {
22:
23: /**
24: * This fields contains the properties of the Handler
25: */
26: protected HashMap prop = null;
27:
28: public NTEventLogHandler() {
29: super ();
30: }
31:
32: /**
33: * It Builds a new NTEventLogHandler. It is needed to specify an handler
34: * type.
35: * @param name is the handler name.
36: */
37: public NTEventLogHandler(String name) {
38: super ();
39: setName(name);
40: prop = new HashMap();
41: }
42:
43: public Map getAttributes() {
44: return prop;
45: }
46:
47: public void setAttributes(Map attributes) {
48: prop.clear();
49: prop.putAll(attributes);
50: Object mf = prop.get("activation");
51: if (mf != null) {
52: prop.remove("activation");
53: setAttribute("activation", mf);
54: }
55: }
56:
57: // IMPLEMENTATION OF THE Handler INTERFACE //
58: //---------------------------------------------//
59:
60: public String getType() {
61: return "ntevent";
62: }
63:
64: public String[] getAttributeNames() {
65: return (String[]) prop.keySet().toArray(new String[0]);
66: }
67:
68: public Object getAttribute(String key) {
69: return prop.get(key);
70: }
71:
72: public Object setAttribute(String key, Object value) {
73: if (prop == null)
74: prop = new HashMap();
75: if (!key.equalsIgnoreCase("activation")) {
76: return prop.put(key, value);
77: } else if (prop.containsKey(key)) {
78: return null; //already activated
79: }
80: MonologFactory mf = (MonologFactory) value;
81: String source = (String) prop.get("source");
82: if (source != null) {
83: setSource((String) value);
84: }
85: String pattern = (String) prop.get(Handler.PATTERN_ATTRIBUTE);
86: if (pattern != null) {
87: setLayout(new PatternLayout(PatternConverter
88: .monolog2log4j(pattern)));
89: }
90: String level = (String) prop.get(Handler.LEVEL_ATTRIBUTE);
91: if (level != null && level.length() > 0) {
92: int levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl
93: .evaluate(level, mf);
94: setThreshold(org.apache.log4j.Level.toLevel(levelVal));
95: }
96: super.activateOptions();
97: return null;
98: }
99: }
|