KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > monitor > services > JMXNotificationAppender


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.monitor.services;
23
24 import javax.management.ListenerNotFoundException JavaDoc;
25 import javax.management.MBeanNotificationInfo JavaDoc;
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.NotificationEmitter JavaDoc;
28 import javax.management.NotificationFilter JavaDoc;
29 import javax.management.NotificationListener JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 import org.apache.log4j.AppenderSkeleton;
33 import org.apache.log4j.Layout;
34 import org.apache.log4j.Level;
35 import org.apache.log4j.spi.LoggingEvent;
36 import org.jboss.monitor.alarm.Alarm;
37 import org.jboss.monitor.alarm.AlarmNotification;
38 import org.jboss.mx.util.JBossNotificationBroadcasterSupport;
39 import org.jboss.mx.util.MBeanServerLocator;
40
41
42 /**
43  * A log4j Appender that emits the received log events as JMX Notifications
44  *
45  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
46  * @version $Revision: 37459 $
47  */

48 public class JMXNotificationAppender extends AppenderSkeleton
49    implements JMXNotificationAppenderMBean, NotificationEmitter JavaDoc
50 {
51    public static final String JavaDoc DEFAULT_TYPE = "jboss.alarm.logging";
52    
53    // Private Data --------------------------------------------------
54

55    private MBeanServer JavaDoc server;
56    
57    private ObjectName JavaDoc objectName;
58    
59    private String JavaDoc objectNameString;
60    
61    private String JavaDoc notificationType;
62    
63    private JBossNotificationBroadcasterSupport emitter;
64    
65    // Protected Data ------------------------------------------------
66

67    // Constructors --------------------------------------------------
68

69    /**
70     * CTOR
71     */

72    public JMXNotificationAppender()
73    {
74       notificationType = DEFAULT_TYPE;
75       server = MBeanServerLocator.locateJBoss();
76       emitter = new JBossNotificationBroadcasterSupport();
77       
78       // Default Threadshold
79
setThreshold(Level.WARN);
80    }
81
82    // Attributes ----------------------------------------------------
83

84    public void setObjectName(String JavaDoc objectNameString) throws Exception JavaDoc
85    {
86       // in case we've registered before
87
unregister();
88
89       if (server != null)
90       {
91          objectName = new ObjectName JavaDoc(objectNameString);
92          server.registerMBean(this, objectName);
93       }
94       
95       this.objectNameString = objectNameString;
96    }
97    
98    public String JavaDoc getObjectName()
99    {
100       return objectNameString;
101    }
102    
103    public void setNotificationType(String JavaDoc notificationType)
104    {
105       this.notificationType = notificationType;
106    }
107    
108    public String JavaDoc getNotificationType()
109    {
110       return notificationType;
111    }
112    
113    // NotificationEmitter implementation ----------------------------
114

115    public void addNotificationListener(NotificationListener listener, NotificationFilter JavaDoc filter, Object JavaDoc handback)
116    {
117       // delegate
118
emitter.addNotificationListener(listener, filter, handback);
119    }
120
121    public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException JavaDoc
122    {
123       // delegate
124
emitter.removeNotificationListener(listener);
125    }
126    
127    public void removeNotificationListener(NotificationListener listener, NotificationFilter JavaDoc filter, Object JavaDoc handback)
128       throws ListenerNotFoundException JavaDoc
129    {
130       // delegate
131
emitter.removeNotificationListener(listener, filter, handback);
132    }
133
134    public MBeanNotificationInfo JavaDoc[] getNotificationInfo()
135    {
136       // delegate
137
return emitter.getNotificationInfo();
138    }
139
140    // Appender implementation ---------------------------------------
141

142    public void close()
143    {
144       unregister();
145       
146       server = null;
147       emitter = null;
148       objectName = null;
149       objectNameString = null;
150       notificationType = null;
151    }
152    
153    public boolean requiresLayout()
154    {
155      return true;
156    }
157    
158    // AppenderSkeleton overrides ------------------------------------
159

160    protected void append(LoggingEvent event)
161    {
162       String JavaDoc msg = super.layout.format(event);
163
164       if (super.layout.ignoresThrowable())
165       {
166          String JavaDoc[] ts = event.getThrowableStrRep();
167          if (ts != null)
168          {
169             StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(msg);
170
171             int len = ts.length;
172             for (int i = 0; i < len; i++)
173             {
174                sbuf.append(Layout.LINE_SEP).append(ts[i]);
175             }
176             
177             msg = sbuf.toString();
178          }
179       }
180
181       // Map level to severity
182
Level level = event.getLevel();
183       int severity;
184       
185       if (level == Level.WARN)
186       {
187          severity = Alarm.SEVERITY_WARNING;
188       }
189       else if (level == Level.ERROR)
190       {
191          severity = Alarm.SEVERITY_MAJOR;
192       }
193       else if (level == Level.FATAL)
194       {
195          severity = Alarm.SEVERITY_CRITICAL;
196       }
197       else
198       {
199          severity = Alarm.SEVERITY_UNKNOWN;
200       }
201
202       // create the alarm
203
AlarmNotification alarm = new AlarmNotification(
204             notificationType, this, null, severity, Alarm.STATE_NONE,
205             emitter.nextNotificationSequenceNumber(), event.timeStamp, msg);
206       
207       emitter.sendNotification(alarm);
208    }
209    
210    // Private -------------------------------------------------------
211

212    private void unregister()
213    {
214       if (server != null && objectName != null)
215       {
216          try
217          {
218             server.unregisterMBean(objectName);
219          }
220          catch (Exception JavaDoc ignored)
221          {
222             // ignored
223
}
224       }
225    }
226 }
227
Popular Tags