KickJava   Java API By Example, From Geeks To Geeks.

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


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.objectweb.util.monolog.api.Handler;
22 import org.objectweb.util.monolog.api.MonologFactory;
23 import org.objectweb.util.monolog.wrapper.common.RelatifEnvironmentPathGetter;
24 import org.apache.log4j.AppenderSkeleton;
25 import org.apache.log4j.PatternLayout;
26 import org.apache.log4j.spi.LoggingEvent;
27
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30 //import java.io.OutputStreamWriter;
31

32 import javax.management.ListenerNotFoundException JavaDoc;
33 import javax.management.MBeanNotificationInfo JavaDoc;
34 import javax.management.Notification JavaDoc;
35 import javax.management.NotificationBroadcasterSupport JavaDoc;
36 import javax.management.NotificationEmitter JavaDoc;
37 import javax.management.NotificationFilter JavaDoc;
38 import javax.management.NotificationListener JavaDoc;
39
40 /**
41  *
42  * @author Sebastien Chassande-Barrioz
43  */

44 public class JMXHandler extends AppenderSkeleton implements NotificationEmitter JavaDoc, Handler {
45
46     /**
47      * This fields contains the properties of the Handler
48      */

49     protected HashMap JavaDoc prop = null;
50
51     public JMXHandler() {
52         super();
53     }
54
55     /**
56      * It Builds a new JMXHandler.
57      * @param name is the handler name.
58      */

59     public JMXHandler(String JavaDoc name) {
60         super();
61         setName(name);
62         prop = new HashMap JavaDoc();
63     }
64     CustomNotificationBroadcasterSupport emitter = new CustomNotificationBroadcasterSupport();
65     
66     public void addNotificationListener(NotificationListener JavaDoc listener,
67             NotificationFilter JavaDoc filter,
68             Object JavaDoc handback)
69             throws IllegalArgumentException JavaDoc {
70         emitter.addNotificationListener( listener, filter, handback );
71     }
72     
73     public void removeNotificationListener( NotificationListener JavaDoc listener )
74     throws ListenerNotFoundException JavaDoc {
75         emitter.removeNotificationListener( listener );
76     }
77     
78     public void removeNotificationListener( NotificationListener JavaDoc listener,
79             NotificationFilter JavaDoc filter, Object JavaDoc handback)
80             throws ListenerNotFoundException JavaDoc {
81         emitter.removeNotificationListener( listener, filter, handback );
82     }
83     
84     public MBeanNotificationInfo JavaDoc[] getNotificationInfo(){
85         return emitter.getNotificationInfo();
86     }
87     
88     public Map JavaDoc getAttributes() {
89         return prop;
90     }
91
92     public void setAttributes(Map JavaDoc attributes) {
93         prop.clear();
94         prop.putAll(attributes);
95         Object JavaDoc mf = prop.get("activation");
96         if (mf != null) {
97             prop.remove("activation");
98             setAttribute("activation", mf);
99         }
100     }
101
102     // IMPLEMENTATION OF THE Handler INTERFACE //
103
//---------------------------------------------//
104
public String JavaDoc getType() {
105         return "jmx";
106     }
107
108     public String JavaDoc[] getAttributeNames() {
109         return (String JavaDoc[]) prop.keySet().toArray(new String JavaDoc[0]);
110     }
111
112     public Object JavaDoc getAttribute(String JavaDoc key) {
113         return prop.get(key);
114     }
115
116     public Object JavaDoc setAttribute(String JavaDoc key, Object JavaDoc value) {
117         if (prop == null)
118             prop = new HashMap JavaDoc();
119         if (!key.equalsIgnoreCase("activation")) {
120             return prop.put(key, value);
121         } else if (prop.containsKey(key)) {
122             return null; //already activated
123
}
124         MonologFactory mf = (MonologFactory) value;
125         String JavaDoc pattern = (String JavaDoc) prop.get(Handler.PATTERN_ATTRIBUTE);
126         if (pattern != null) {
127             setLayout(new PatternLayout(PatternConverter.monolog2log4j(pattern)));
128         }
129         
130         String JavaDoc level = (String JavaDoc) prop.get(Handler.LEVEL_ATTRIBUTE);
131         if (level != null && level.length() > 0) {
132             int levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl.evaluate(level, mf);
133             setThreshold(org.apache.log4j.Level.toLevel(levelVal));
134         }
135         
136         String JavaDoc output = (String JavaDoc) prop.get(Handler.OUTPUT_ATTRIBUTE);
137         output = RelatifEnvironmentPathGetter.getRealPath(output);
138         
139         super.activateOptions();
140         return null;
141     }
142
143     private long notificationSequence = 0;
144     
145     /* (non-Javadoc)
146      * @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
147      */

148     public void doAppend(LoggingEvent event) {
149         Notification JavaDoc notification = new Notification JavaDoc ("Monolog.JMXHandler.Log",
150                 "JMXHandler:Type=Log4j",++notificationSequence,
151                 System.currentTimeMillis(), event.getMessage().toString());
152         notification.setUserData(event);
153         emitter.sendNotification(notification);
154     }
155     
156     class CustomNotificationBroadcasterSupport extends NotificationBroadcasterSupport JavaDoc {
157         
158         public void sendNotification (Notification JavaDoc notification) {
159             super.sendNotification (notification);
160         }
161     }
162
163     /* (non-Javadoc)
164      * @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
165      */

166     protected void append(LoggingEvent event) {
167         this.append(event);
168     }
169
170     /* (non-Javadoc)
171      * @see org.apache.log4j.Appender#close()
172      */

173     public void close() {
174         if (this != null) {
175             this.close();
176         }
177         
178     }
179
180     /* (non-Javadoc)
181      * @see org.apache.log4j.Appender#requiresLayout()
182      */

183     public boolean requiresLayout() {
184         // TODO Auto-generated method stub
185
return false;
186     }
187
188 }
189
190
Popular Tags