1 16 17 package org.apache.log4j.jmx; 18 19 20 import java.lang.reflect.Constructor ; 21 import org.apache.log4j.*; 22 23 import org.apache.log4j.spi.HierarchyEventListener; 24 import org.apache.log4j.spi.LoggerRepository; 25 import org.apache.log4j.helpers.OptionConverter; 26 27 import java.util.Vector ; 28 import javax.management.MBeanAttributeInfo ; 29 import javax.management.MBeanConstructorInfo ; 30 import javax.management.MBeanNotificationInfo ; 31 import javax.management.MBeanOperationInfo ; 32 import javax.management.MBeanParameterInfo ; 33 34 import javax.management.ObjectName ; 35 import javax.management.MBeanInfo ; 36 import javax.management.Attribute ; 37 38 import javax.management.MBeanException ; 39 import javax.management.AttributeNotFoundException ; 40 import javax.management.RuntimeOperationsException ; 41 import javax.management.ReflectionException ; 42 import javax.management.InvalidAttributeValueException ; 43 import javax.management.NotificationBroadcasterSupport ; 44 import javax.management.NotificationBroadcaster ; 45 import javax.management.Notification ; 46 import javax.management.NotificationListener ; 47 import javax.management.NotificationFilter ; 48 import javax.management.NotificationFilterSupport ; 49 import javax.management.ListenerNotFoundException ; 50 51 public class HierarchyDynamicMBean extends AbstractDynamicMBean 52 implements HierarchyEventListener, 53 NotificationBroadcaster { 54 55 static final String ADD_APPENDER = "addAppender."; 56 static final String THRESHOLD = "threshold"; 57 58 private MBeanConstructorInfo [] dConstructors = new MBeanConstructorInfo [1]; 59 private MBeanOperationInfo [] dOperations = new MBeanOperationInfo [1]; 60 61 private Vector vAttributes = new Vector (); 62 private String dClassName = this.getClass().getName(); 63 private String dDescription = 64 "This MBean acts as a management facade for org.apache.log4j.Hierarchy."; 65 66 private NotificationBroadcasterSupport nbs = new NotificationBroadcasterSupport (); 67 68 69 private LoggerRepository hierarchy; 70 71 private static Logger log = Logger.getLogger(HierarchyDynamicMBean.class); 72 73 public HierarchyDynamicMBean() { 74 hierarchy = LogManager.getLoggerRepository(); 75 buildDynamicMBeanInfo(); 76 } 77 78 private 79 void buildDynamicMBeanInfo() { 80 Constructor [] constructors = this.getClass().getConstructors(); 81 dConstructors[0] = new MBeanConstructorInfo ( 82 "HierarchyDynamicMBean(): Constructs a HierarchyDynamicMBean instance", 83 constructors[0]); 84 85 vAttributes.add(new MBeanAttributeInfo (THRESHOLD, 86 "java.lang.String", 87 "The \"threshold\" state of the hiearchy.", 88 true, 89 true, 90 false)); 91 92 MBeanParameterInfo [] params = new MBeanParameterInfo [1]; 93 params[0] = new MBeanParameterInfo ("name", "java.lang.String", 94 "Create a logger MBean" ); 95 dOperations[0] = new MBeanOperationInfo ("addLoggerMBean", 96 "addLoggerMBean(): add a loggerMBean", 97 params , 98 "javax.management.ObjectName", 99 MBeanOperationInfo.ACTION); 100 } 101 102 103 public 104 ObjectName addLoggerMBean(String name) { 105 Logger cat = LogManager.exists(name); 106 107 if(cat != null) { 108 return addLoggerMBean(cat); 109 } else { 110 return null; 111 } 112 } 113 114 ObjectName addLoggerMBean(Logger logger) { 115 String name = logger.getName(); 116 ObjectName objectName = null; 117 try { 118 LoggerDynamicMBean loggerMBean = new LoggerDynamicMBean(logger); 119 objectName = new ObjectName ("log4j", "logger", name); 120 server.registerMBean(loggerMBean, objectName); 121 122 NotificationFilterSupport nfs = new NotificationFilterSupport (); 123 nfs.enableType(ADD_APPENDER+logger.getName()); 124 125 log.debug("---Adding logger ["+name+"] as listener."); 126 127 nbs.addNotificationListener(loggerMBean, nfs, null); 128 129 130 vAttributes.add(new MBeanAttributeInfo ("logger="+name, 131 "javax.management.ObjectName", 132 "The "+name+" logger.", 133 true, 134 true, false)); 137 138 } catch(Exception e) { 139 log.error("Couls not add loggerMBean for ["+name+"]."); 140 } 141 return objectName; 142 } 143 144 public 145 void addNotificationListener(NotificationListener listener, 146 NotificationFilter filter, 147 java.lang.Object handback) { 148 nbs.addNotificationListener(listener, filter, handback); 149 } 150 151 protected 152 Logger getLogger() { 153 return log; 154 } 155 156 public 157 MBeanInfo getMBeanInfo() { 158 160 MBeanAttributeInfo [] attribs = new MBeanAttributeInfo [vAttributes.size()]; 161 vAttributes.toArray(attribs); 162 163 return new MBeanInfo (dClassName, 164 dDescription, 165 attribs, 166 dConstructors, 167 dOperations, 168 new MBeanNotificationInfo [0]); 169 } 170 171 public 172 MBeanNotificationInfo [] getNotificationInfo(){ 173 return nbs.getNotificationInfo(); 174 } 175 176 public 177 Object invoke(String operationName, 178 Object params[], 179 String signature[]) throws MBeanException , 180 ReflectionException { 181 182 if (operationName == null) { 183 throw new RuntimeOperationsException ( 184 new IllegalArgumentException ("Operation name cannot be null"), 185 "Cannot invoke a null operation in " + dClassName); 186 } 187 189 if(operationName.equals("addLoggerMBean")) { 190 return addLoggerMBean((String )params[0]); 191 } else { 192 throw new ReflectionException ( 193 new NoSuchMethodException (operationName), 194 "Cannot find the operation " + operationName + " in " + dClassName); 195 } 196 197 } 198 199 200 public 201 Object getAttribute(String attributeName) throws AttributeNotFoundException , 202 MBeanException , 203 ReflectionException { 204 205 if (attributeName == null) { 207 throw new RuntimeOperationsException (new IllegalArgumentException ( 208 "Attribute name cannot be null"), 209 "Cannot invoke a getter of " + dClassName + " with null attribute name"); 210 } 211 212 log.debug("Called getAttribute with ["+attributeName+"]."); 213 214 if (attributeName.equals(THRESHOLD)) { 216 return hierarchy.getThreshold(); 217 } else if(attributeName.startsWith("logger")) { 218 int k = attributeName.indexOf("%3D"); 219 String val = attributeName; 220 if(k > 0) { 221 val = attributeName.substring(0, k)+'='+ attributeName.substring(k+3); 222 } 223 try { 224 return new ObjectName ("log4j:"+val); 225 } catch(Exception e) { 226 log.error("Could not create ObjectName" + val); 227 } 228 } 229 230 231 232 throw(new AttributeNotFoundException ("Cannot find " + attributeName + 234 " attribute in " + dClassName)); 235 236 } 237 238 239 public 240 void addAppenderEvent(Category logger, Appender appender) { 241 log.debug("addAppenderEvent called: logger="+logger.getName()+ 242 ", appender="+appender.getName()); 243 Notification n = new Notification (ADD_APPENDER+logger.getName(), this, 0); 244 n.setUserData(appender); 245 log.debug("sending notification."); 246 nbs.sendNotification(n); 247 } 248 249 public 250 void removeAppenderEvent(Category cat, Appender appender) { 251 log.debug("removeAppenderCalled: logger="+cat.getName()+ 252 ", appender="+appender.getName()); 253 } 254 255 public 256 void postRegister(java.lang.Boolean registrationDone) { 257 log.debug("postRegister is called."); 258 hierarchy.addHierarchyEventListener(this); 259 Logger root = hierarchy.getRootLogger(); 260 addLoggerMBean(root); 261 } 262 263 public 264 void removeNotificationListener(NotificationListener listener) 265 throws ListenerNotFoundException { 266 nbs.removeNotificationListener(listener); 267 } 268 269 public 270 void setAttribute(Attribute attribute) throws AttributeNotFoundException , 271 InvalidAttributeValueException , 272 MBeanException , 273 ReflectionException { 274 275 if (attribute == null) { 277 throw new RuntimeOperationsException ( 278 new IllegalArgumentException ("Attribute cannot be null"), 279 "Cannot invoke a setter of "+dClassName+" with null attribute"); 280 } 281 String name = attribute.getName(); 282 Object value = attribute.getValue(); 283 284 if (name == null) { 285 throw new RuntimeOperationsException ( 286 new IllegalArgumentException ("Attribute name cannot be null"), 287 "Cannot invoke the setter of "+dClassName+ 288 " with null attribute name"); 289 } 290 291 if(name.equals(THRESHOLD)) { 292 Level l = OptionConverter.toLevel((String ) value, 293 hierarchy.getThreshold()); 294 hierarchy.setThreshold(l); 295 } 296 297 298 } 299 } 300 | Popular Tags |