1 18 package org.objectweb.util.monolog.wrapper.javaLog; 19 import org.objectweb.util.monolog.wrapper.common.OutputStreamSwitcher; 20 22 import java.io.Writer ; 24 import java.util.logging.LogRecord ; 27 import java.util.logging.ErrorManager ; 28 import javax.management.ListenerNotFoundException ; 29 import javax.management.MBeanNotificationInfo ; 30 import javax.management.Notification ; 31 import javax.management.NotificationBroadcasterSupport ; 32 import javax.management.NotificationEmitter ; 33 import javax.management.NotificationFilter ; 34 import javax.management.NotificationListener ; 35 36 45 public class JMXHandler extends java.util.logging.Handler implements NotificationEmitter { 46 47 protected OutputStreamSwitcher oss; 48 private Writer writer; 49 50 public JMXHandler() { 51 super(); 52 } 53 54 CustomNotificationBroadcasterSupport emitter = new CustomNotificationBroadcasterSupport(); 55 56 public void addNotificationListener(NotificationListener listener, 57 NotificationFilter filter, 58 Object handback) 59 throws IllegalArgumentException { 60 emitter.addNotificationListener(listener, filter, handback); 61 } 62 63 public void removeNotificationListener( NotificationListener listener ) 64 throws ListenerNotFoundException { 65 emitter.removeNotificationListener( listener ); 66 } 67 68 public void removeNotificationListener( NotificationListener listener, 69 NotificationFilter filter, Object handback) 70 throws ListenerNotFoundException { 71 emitter.removeNotificationListener( listener, filter, handback ); 72 } 73 74 public MBeanNotificationInfo [] getNotificationInfo(){ 75 return emitter.getNotificationInfo(); 76 } 77 78 private long notificationSequence = 0 ; 79 80 public void publish(LogRecord record) { 81 if (!isLoggable(record)) { 82 return; 83 } 84 String msg = record.getMessage(); 85 Notification notification = new Notification ("Monolog.JMXHandler.Log", 86 "JMXHandler:Type=javaLog",++notificationSequence, 87 System.currentTimeMillis(), msg ); 88 notification.setUserData(record); 89 emitter.sendNotification(notification); 90 } 91 92 class CustomNotificationBroadcasterSupport extends NotificationBroadcasterSupport { 93 public void sendNotification (Notification notification) { 94 super.sendNotification (notification); 95 } 96 public void addNotificationListener(NotificationListener listener, 97 NotificationFilter filter, 98 Object handback) 99 throws IllegalArgumentException { 100 super.addNotificationListener(listener, filter, handback); 101 102 } 103 } 104 105 public void flush() { 106 if (writer != null) { 107 try { 108 writer.flush(); 109 } catch (Exception ex) { 110 reportError(null, ex, ErrorManager.FLUSH_FAILURE); 113 } 114 } 115 } 116 117 public void close() throws SecurityException { 118 flush(); 119 } 120 } 121 | Popular Tags |