1 16 17 package org.apache.log4j.jmx; 18 19 import java.lang.reflect.Constructor ; 20 import org.apache.log4j.Logger; 21 import org.apache.log4j.Level; 22 import org.apache.log4j.Appender; 23 import org.apache.log4j.helpers.OptionConverter; 24 25 import java.util.Vector ; 26 import java.util.Enumeration ; 27 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 import javax.management.ObjectName ; 34 import javax.management.MBeanInfo ; 35 import javax.management.Attribute ; 36 37 import javax.management.MBeanException ; 38 import javax.management.AttributeNotFoundException ; 39 import javax.management.RuntimeOperationsException ; 40 import javax.management.ReflectionException ; 41 import javax.management.InvalidAttributeValueException ; 42 import javax.management.NotificationListener ; 43 import javax.management.Notification ; 44 45 public class LoggerDynamicMBean extends AbstractDynamicMBean 46 implements NotificationListener { 47 48 private MBeanConstructorInfo [] dConstructors = new MBeanConstructorInfo [1]; 49 private MBeanOperationInfo [] dOperations = new MBeanOperationInfo [1]; 50 51 private Vector dAttributes = new Vector (); 52 private String dClassName = this.getClass().getName(); 53 54 private String dDescription = 55 "This MBean acts as a management facade for a org.apache.log4j.Logger instance."; 56 57 private static Logger cat = Logger.getLogger(LoggerDynamicMBean.class); 59 60 private Logger logger; 62 63 public LoggerDynamicMBean(Logger logger) { 64 this.logger = logger; 65 buildDynamicMBeanInfo(); 66 } 67 68 public 69 void handleNotification(Notification notification, Object handback) { 70 cat.debug("Received notification: "+notification.getType()); 71 registerAppenderMBean((Appender) notification.getUserData() ); 72 73 74 } 75 76 private 77 void buildDynamicMBeanInfo() { 78 Constructor [] constructors = this.getClass().getConstructors(); 79 dConstructors[0] = new MBeanConstructorInfo ( 80 "HierarchyDynamicMBean(): Constructs a HierarchyDynamicMBean instance", 81 constructors[0]); 82 83 dAttributes.add(new MBeanAttributeInfo ("name", 84 "java.lang.String", 85 "The name of this Logger.", 86 true, 87 false, 88 false)); 89 90 dAttributes.add(new MBeanAttributeInfo ("priority", 91 "java.lang.String", 92 "The priority of this logger.", 93 true, 94 true, 95 false)); 96 97 98 99 100 101 MBeanParameterInfo [] params = new MBeanParameterInfo [2]; 102 params[0] = new MBeanParameterInfo ("class name", "java.lang.String", 103 "add an appender to this logger"); 104 params[1] = new MBeanParameterInfo ("appender name", "java.lang.String", 105 "name of the appender"); 106 107 dOperations[0] = new MBeanOperationInfo ("addAppender", 108 "addAppender(): add an appender", 109 params, 110 "void", 111 MBeanOperationInfo.ACTION); 112 } 113 114 protected 115 Logger getLogger() { 116 return logger; 117 } 118 119 120 public 121 MBeanInfo getMBeanInfo() { 122 124 MBeanAttributeInfo [] attribs = new MBeanAttributeInfo [dAttributes.size()]; 125 dAttributes.toArray(attribs); 126 127 MBeanInfo mb = new MBeanInfo (dClassName, 128 dDescription, 129 attribs, 130 dConstructors, 131 dOperations, 132 new MBeanNotificationInfo [0]); 133 return mb; 135 } 136 137 public 138 Object invoke(String operationName, Object params[], String signature[]) 139 throws MBeanException , 140 ReflectionException { 141 142 if(operationName.equals("addAppender")) { 143 addAppender((String ) params[0], (String ) params[1]); 144 return "Hello world."; 145 } 146 147 return null; 148 } 149 150 151 public 152 Object getAttribute(String attributeName) throws AttributeNotFoundException , 153 MBeanException , 154 ReflectionException { 155 156 if (attributeName == null) { 158 throw new RuntimeOperationsException (new IllegalArgumentException ( 159 "Attribute name cannot be null"), 160 "Cannot invoke a getter of " + dClassName + " with null attribute name"); 161 } 162 163 if (attributeName.equals("name")) { 165 return logger.getName(); 166 } else if(attributeName.equals("priority")) { 167 Level l = logger.getLevel(); 168 if(l == null) { 169 return null; 170 } else { 171 return l.toString(); 172 } 173 } else if(attributeName.startsWith("appender=")) { 174 try { 175 return new ObjectName ("log4j:"+attributeName ); 176 } catch(Exception e) { 177 cat.error("Could not create ObjectName" + attributeName); 178 } 179 } 180 181 182 throw(new AttributeNotFoundException ("Cannot find " + attributeName + 184 " attribute in " + dClassName)); 185 186 } 187 188 189 void addAppender(String appenderClass, String appenderName) { 190 cat.debug("addAppender called with "+appenderClass+", "+appenderName); 191 Appender appender = (Appender) 192 OptionConverter.instantiateByClassName(appenderClass, 193 org.apache.log4j.Appender.class, 194 null); 195 appender.setName(appenderName); 196 logger.addAppender(appender); 197 198 200 } 201 202 203 public 204 void setAttribute(Attribute attribute) throws AttributeNotFoundException , 205 InvalidAttributeValueException , 206 MBeanException , 207 ReflectionException { 208 209 if (attribute == null) { 211 throw new RuntimeOperationsException ( 212 new IllegalArgumentException ("Attribute cannot be null"), 213 "Cannot invoke a setter of " + dClassName + 214 " with null attribute"); 215 } 216 String name = attribute.getName(); 217 Object value = attribute.getValue(); 218 219 if (name == null) { 220 throw new RuntimeOperationsException ( 221 new IllegalArgumentException ("Attribute name cannot be null"), 222 "Cannot invoke the setter of "+dClassName+ 223 " with null attribute name"); 224 } 225 226 227 if(name.equals("priority")) { 228 if (value instanceof String ) { 229 String s = (String ) value; 230 Level p = logger.getLevel(); 231 if(s.equalsIgnoreCase("NULL")) { 232 p = null; 233 } else { 234 p = OptionConverter.toLevel(s, p); 235 } 236 logger.setLevel(p); 237 } 238 } else { 239 throw(new AttributeNotFoundException ("Attribute " + name + 240 " not found in " + 241 this.getClass().getName())); 242 } 243 } 244 245 void appenderMBeanRegistration() { 246 Enumeration enumeration = logger.getAllAppenders(); 247 while(enumeration.hasMoreElements()) { 248 Appender appender = (Appender) enumeration.nextElement(); 249 registerAppenderMBean(appender); 250 } 251 } 252 253 void registerAppenderMBean(Appender appender) { 254 String name = appender.getName(); 255 cat.debug("Adding AppenderMBean for appender named "+name); 256 ObjectName objectName = null; 257 try { 258 AppenderDynamicMBean appenderMBean = new AppenderDynamicMBean(appender); 259 objectName = new ObjectName ("log4j", "appender", name); 260 server.registerMBean(appenderMBean, objectName); 261 262 dAttributes.add(new MBeanAttributeInfo ("appender="+name, 263 "javax.management.ObjectName", 264 "The "+name+" appender.", 265 true, 266 true, 267 false)); 268 269 } catch(Exception e) { 270 cat.error("Could not add appenderMBean for ["+name+"].", e); 271 } 272 } 273 274 public 275 void postRegister(java.lang.Boolean registrationDone) { 276 appenderMBeanRegistration(); 277 } 278 } 279 | Popular Tags |