1 22 package org.jboss.monitor.services; 23 24 import javax.management.ListenerNotFoundException ; 25 import javax.management.MBeanNotificationInfo ; 26 import javax.management.MBeanServer ; 27 import javax.management.NotificationEmitter ; 28 import javax.management.NotificationFilter ; 29 import javax.management.NotificationListener ; 30 import javax.management.ObjectName ; 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 48 public class JMXNotificationAppender extends AppenderSkeleton 49 implements JMXNotificationAppenderMBean, NotificationEmitter 50 { 51 public static final String DEFAULT_TYPE = "jboss.alarm.logging"; 52 53 55 private MBeanServer server; 56 57 private ObjectName objectName; 58 59 private String objectNameString; 60 61 private String notificationType; 62 63 private JBossNotificationBroadcasterSupport emitter; 64 65 67 69 72 public JMXNotificationAppender() 73 { 74 notificationType = DEFAULT_TYPE; 75 server = MBeanServerLocator.locateJBoss(); 76 emitter = new JBossNotificationBroadcasterSupport(); 77 78 setThreshold(Level.WARN); 80 } 81 82 84 public void setObjectName(String objectNameString) throws Exception 85 { 86 unregister(); 88 89 if (server != null) 90 { 91 objectName = new ObjectName (objectNameString); 92 server.registerMBean(this, objectName); 93 } 94 95 this.objectNameString = objectNameString; 96 } 97 98 public String getObjectName() 99 { 100 return objectNameString; 101 } 102 103 public void setNotificationType(String notificationType) 104 { 105 this.notificationType = notificationType; 106 } 107 108 public String getNotificationType() 109 { 110 return notificationType; 111 } 112 113 115 public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) 116 { 117 emitter.addNotificationListener(listener, filter, handback); 119 } 120 121 public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException 122 { 123 emitter.removeNotificationListener(listener); 125 } 126 127 public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) 128 throws ListenerNotFoundException 129 { 130 emitter.removeNotificationListener(listener, filter, handback); 132 } 133 134 public MBeanNotificationInfo [] getNotificationInfo() 135 { 136 return emitter.getNotificationInfo(); 138 } 139 140 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 160 protected void append(LoggingEvent event) 161 { 162 String msg = super.layout.format(event); 163 164 if (super.layout.ignoresThrowable()) 165 { 166 String [] ts = event.getThrowableStrRep(); 167 if (ts != null) 168 { 169 StringBuffer sbuf = new StringBuffer (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 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 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 212 private void unregister() 213 { 214 if (server != null && objectName != null) 215 { 216 try 217 { 218 server.unregisterMBean(objectName); 219 } 220 catch (Exception ignored) 221 { 222 } 224 } 225 } 226 } 227 | Popular Tags |