1 8 9 package mx4j.log; 10 11 import javax.management.NotificationListener ; 12 import javax.management.NotificationFilter ; 13 import javax.management.ListenerNotFoundException ; 14 import javax.management.MBeanNotificationInfo ; 15 import javax.management.Notification ; 16 import javax.management.NotificationBroadcasterSupport ; 17 import javax.management.MBeanRegistration ; 18 import javax.management.ObjectName ; 19 import javax.management.MBeanServer ; 20 21 31 public class LoggerBroadcaster extends NotificationBroadcasterSupport implements MBeanRegistration , LoggerBroadcasterMBean 32 { 33 private long m_sequence; 34 private boolean m_registered; 35 private int m_recursionLevel; 36 37 public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception 38 { 39 return name; 40 } 41 public void postRegister(Boolean registrationDone) 42 { 43 if (!registrationDone.booleanValue()) {return;} 44 m_registered = true; 45 } 46 public void preDeregister() throws Exception {} 47 public void postDeregister() {m_registered = false;} 48 49 public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException 50 { 51 super.removeNotificationListener(listener); 53 } 54 55 public void sendNotification(Notification n) 56 { 57 int maxRecursionLevel = 1; 64 synchronized (this) 65 { 66 if (m_recursionLevel < maxRecursionLevel) 67 { 68 ++m_recursionLevel; 69 super.sendNotification(n); 70 --m_recursionLevel; 71 } 72 } 73 } 74 75 public void start() 76 { 77 Logger logger = createLoggerPrototype(); 78 Log.redirectTo(logger); 79 } 80 81 public void start(String category) 82 { 83 Logger logger = createLoggerPrototype(); 84 Log.redirectTo(logger, category); 85 } 86 87 public void stop() 88 { 89 Log.redirectTo(null); 90 } 91 92 public void stop(String category) 93 { 94 Log.redirectTo(null, category); 95 } 96 97 private boolean isRegistered() {return m_registered;} 98 99 public MBeanNotificationInfo [] getNotificationInfo() 100 { 101 String [] types = new String [] {"mx4j.logger.trace", 102 "mx4j.logger.debug", 103 "mx4j.logger.info", 104 "mx4j.logger.warn", 105 "mx4j.logger.error", 106 "mx4j.logger.fatal"}; 107 MBeanNotificationInfo notifs = new MBeanNotificationInfo (types, "javax.management.Notification", "MX4J Logger MBean notifications"); 108 return new MBeanNotificationInfo [] {notifs}; 109 } 110 111 protected Logger createLoggerPrototype() 112 { 113 return new LoggerNotifier(this); 114 } 115 116 public static class LoggerNotifier extends Logger 117 { 118 private static LoggerBroadcaster m_loggerBroadcaster; 119 120 private LoggerNotifier(LoggerBroadcaster mbean) {m_loggerBroadcaster = mbean;} 121 122 public LoggerNotifier() {} 123 124 protected void log(int priority, Object message, Throwable t) 125 { 126 notify(priority, message, t); 128 } 129 130 private void notify(int priority, Object message, Throwable t) 131 { 132 if (m_loggerBroadcaster.isRegistered()) 133 { 134 long sequence = 0; 135 synchronized (this) {sequence = ++m_loggerBroadcaster.m_sequence;} 136 137 String type = null; 138 switch (priority) 139 { 140 case TRACE: type = "mx4j.logger.trace"; break; 141 case DEBUG: type = "mx4j.logger.debug"; break; 142 case INFO: type = "mx4j.logger.info"; break; 143 case WARN: type = "mx4j.logger.warn"; break; 144 case ERROR: type = "mx4j.logger.error"; break; 145 case FATAL: type = "mx4j.logger.fatal"; break; 146 default: type = "mx4j.logger." + priority; break; 147 } 148 149 String msg = message == null ? "" : message.toString(); 150 151 Notification n = new Notification (type, this, sequence, msg); 153 if (t != null) 154 { 155 n.setUserData(t); 156 } 157 158 m_loggerBroadcaster.sendNotification(n); 159 } 160 } 161 } 162 } 163 | Popular Tags |