1 18 19 package org.objectweb.util.monolog.wrapper.log4j; 20 21 import org.objectweb.util.monolog.api.Handler; 22 import org.objectweb.util.monolog.api.MonologFactory; 23 import org.objectweb.util.monolog.wrapper.common.RelatifEnvironmentPathGetter; 24 import org.apache.log4j.AppenderSkeleton; 25 import org.apache.log4j.PatternLayout; 26 import org.apache.log4j.spi.LoggingEvent; 27 28 import java.util.HashMap ; 29 import java.util.Map ; 30 32 import javax.management.ListenerNotFoundException ; 33 import javax.management.MBeanNotificationInfo ; 34 import javax.management.Notification ; 35 import javax.management.NotificationBroadcasterSupport ; 36 import javax.management.NotificationEmitter ; 37 import javax.management.NotificationFilter ; 38 import javax.management.NotificationListener ; 39 40 44 public class JMXHandler extends AppenderSkeleton implements NotificationEmitter , Handler { 45 46 49 protected HashMap prop = null; 50 51 public JMXHandler() { 52 super(); 53 } 54 55 59 public JMXHandler(String name) { 60 super(); 61 setName(name); 62 prop = new HashMap (); 63 } 64 CustomNotificationBroadcasterSupport emitter = new CustomNotificationBroadcasterSupport(); 65 66 public void addNotificationListener(NotificationListener listener, 67 NotificationFilter filter, 68 Object handback) 69 throws IllegalArgumentException { 70 emitter.addNotificationListener( listener, filter, handback ); 71 } 72 73 public void removeNotificationListener( NotificationListener listener ) 74 throws ListenerNotFoundException { 75 emitter.removeNotificationListener( listener ); 76 } 77 78 public void removeNotificationListener( NotificationListener listener, 79 NotificationFilter filter, Object handback) 80 throws ListenerNotFoundException { 81 emitter.removeNotificationListener( listener, filter, handback ); 82 } 83 84 public MBeanNotificationInfo [] getNotificationInfo(){ 85 return emitter.getNotificationInfo(); 86 } 87 88 public Map getAttributes() { 89 return prop; 90 } 91 92 public void setAttributes(Map attributes) { 93 prop.clear(); 94 prop.putAll(attributes); 95 Object mf = prop.get("activation"); 96 if (mf != null) { 97 prop.remove("activation"); 98 setAttribute("activation", mf); 99 } 100 } 101 102 public String getType() { 105 return "jmx"; 106 } 107 108 public String [] getAttributeNames() { 109 return (String []) prop.keySet().toArray(new String [0]); 110 } 111 112 public Object getAttribute(String key) { 113 return prop.get(key); 114 } 115 116 public Object setAttribute(String key, Object value) { 117 if (prop == null) 118 prop = new HashMap (); 119 if (!key.equalsIgnoreCase("activation")) { 120 return prop.put(key, value); 121 } else if (prop.containsKey(key)) { 122 return null; } 124 MonologFactory mf = (MonologFactory) value; 125 String pattern = (String ) prop.get(Handler.PATTERN_ATTRIBUTE); 126 if (pattern != null) { 127 setLayout(new PatternLayout(PatternConverter.monolog2log4j(pattern))); 128 } 129 130 String level = (String ) prop.get(Handler.LEVEL_ATTRIBUTE); 131 if (level != null && level.length() > 0) { 132 int levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl.evaluate(level, mf); 133 setThreshold(org.apache.log4j.Level.toLevel(levelVal)); 134 } 135 136 String output = (String ) prop.get(Handler.OUTPUT_ATTRIBUTE); 137 output = RelatifEnvironmentPathGetter.getRealPath(output); 138 139 super.activateOptions(); 140 return null; 141 } 142 143 private long notificationSequence = 0; 144 145 148 public void doAppend(LoggingEvent event) { 149 Notification notification = new Notification ("Monolog.JMXHandler.Log", 150 "JMXHandler:Type=Log4j",++notificationSequence, 151 System.currentTimeMillis(), event.getMessage().toString()); 152 notification.setUserData(event); 153 emitter.sendNotification(notification); 154 } 155 156 class CustomNotificationBroadcasterSupport extends NotificationBroadcasterSupport { 157 158 public void sendNotification (Notification notification) { 159 super.sendNotification (notification); 160 } 161 } 162 163 166 protected void append(LoggingEvent event) { 167 this.append(event); 168 } 169 170 173 public void close() { 174 if (this != null) { 175 this.close(); 176 } 177 178 } 179 180 183 public boolean requiresLayout() { 184 return false; 186 } 187 188 } 189 190 | Popular Tags |