001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.monitor.services;
023:
024: import javax.management.ListenerNotFoundException;
025: import javax.management.MBeanNotificationInfo;
026: import javax.management.MBeanServer;
027: import javax.management.NotificationEmitter;
028: import javax.management.NotificationFilter;
029: import javax.management.NotificationListener;
030: import javax.management.ObjectName;
031:
032: import org.apache.log4j.AppenderSkeleton;
033: import org.apache.log4j.Layout;
034: import org.apache.log4j.Level;
035: import org.apache.log4j.spi.LoggingEvent;
036: import org.jboss.monitor.alarm.Alarm;
037: import org.jboss.monitor.alarm.AlarmNotification;
038: import org.jboss.mx.util.JBossNotificationBroadcasterSupport;
039: import org.jboss.mx.util.MBeanServerLocator;
040:
041: /**
042: * A log4j Appender that emits the received log events as JMX Notifications
043: *
044: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
045: * @version $Revision: 57210 $
046: */
047: public class JMXNotificationAppender extends AppenderSkeleton implements
048: JMXNotificationAppenderMBean, NotificationEmitter {
049: public static final String DEFAULT_TYPE = "jboss.alarm.logging";
050:
051: // Private Data --------------------------------------------------
052:
053: private MBeanServer server;
054:
055: private ObjectName objectName;
056:
057: private String objectNameString;
058:
059: private String notificationType;
060:
061: private JBossNotificationBroadcasterSupport emitter;
062:
063: // Protected Data ------------------------------------------------
064:
065: // Constructors --------------------------------------------------
066:
067: /**
068: * CTOR
069: */
070: public JMXNotificationAppender() {
071: notificationType = DEFAULT_TYPE;
072: server = MBeanServerLocator.locateJBoss();
073: emitter = new JBossNotificationBroadcasterSupport();
074:
075: // Default Threadshold
076: setThreshold(Level.WARN);
077: }
078:
079: // Attributes ----------------------------------------------------
080:
081: public void setObjectName(String objectNameString) throws Exception {
082: // in case we've registered before
083: unregister();
084:
085: if (server != null) {
086: objectName = new ObjectName(objectNameString);
087: server.registerMBean(this , objectName);
088: }
089:
090: this .objectNameString = objectNameString;
091: }
092:
093: public String getObjectName() {
094: return objectNameString;
095: }
096:
097: public void setNotificationType(String notificationType) {
098: this .notificationType = notificationType;
099: }
100:
101: public String getNotificationType() {
102: return notificationType;
103: }
104:
105: // NotificationEmitter implementation ----------------------------
106:
107: public void addNotificationListener(NotificationListener listener,
108: NotificationFilter filter, Object handback) {
109: // delegate
110: emitter.addNotificationListener(listener, filter, handback);
111: }
112:
113: public void removeNotificationListener(NotificationListener listener)
114: throws ListenerNotFoundException {
115: // delegate
116: emitter.removeNotificationListener(listener);
117: }
118:
119: public void removeNotificationListener(
120: NotificationListener listener, NotificationFilter filter,
121: Object handback) throws ListenerNotFoundException {
122: // delegate
123: emitter.removeNotificationListener(listener, filter, handback);
124: }
125:
126: public MBeanNotificationInfo[] getNotificationInfo() {
127: // delegate
128: return emitter.getNotificationInfo();
129: }
130:
131: // Appender implementation ---------------------------------------
132:
133: public void close() {
134: unregister();
135:
136: server = null;
137: emitter = null;
138: objectName = null;
139: objectNameString = null;
140: notificationType = null;
141: }
142:
143: public boolean requiresLayout() {
144: return true;
145: }
146:
147: // AppenderSkeleton overrides ------------------------------------
148:
149: protected void append(LoggingEvent event) {
150: String msg = super .layout.format(event);
151:
152: if (super .layout.ignoresThrowable()) {
153: String[] ts = event.getThrowableStrRep();
154: if (ts != null) {
155: StringBuffer sbuf = new StringBuffer(msg);
156:
157: int len = ts.length;
158: for (int i = 0; i < len; i++) {
159: sbuf.append(Layout.LINE_SEP).append(ts[i]);
160: }
161:
162: msg = sbuf.toString();
163: }
164: }
165:
166: // Map level to severity
167: Level level = event.getLevel();
168: int severity;
169:
170: if (level == Level.WARN) {
171: severity = Alarm.SEVERITY_WARNING;
172: } else if (level == Level.ERROR) {
173: severity = Alarm.SEVERITY_MAJOR;
174: } else if (level == Level.FATAL) {
175: severity = Alarm.SEVERITY_CRITICAL;
176: } else {
177: severity = Alarm.SEVERITY_UNKNOWN;
178: }
179:
180: // create the alarm
181: AlarmNotification alarm = new AlarmNotification(
182: notificationType, this , null, severity,
183: Alarm.STATE_NONE, emitter
184: .nextNotificationSequenceNumber(),
185: event.timeStamp, msg);
186:
187: emitter.sendNotification(alarm);
188: }
189:
190: // Private -------------------------------------------------------
191:
192: private void unregister() {
193: if (server != null && objectName != null) {
194: try {
195: server.unregisterMBean(objectName);
196: } catch (Exception ignored) {
197: // ignored
198: }
199: }
200: }
201: }
|