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