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.Layout; 23 import org.apache.log4j.helpers.OptionConverter; 24 import org.apache.log4j.spi.OptionHandler; 25 26 import java.util.Vector ; 27 import java.util.Hashtable ; 28 import java.lang.reflect.Method ; 29 import javax.management.MBeanAttributeInfo ; 30 import javax.management.MBeanConstructorInfo ; 31 import javax.management.MBeanNotificationInfo ; 32 import javax.management.MBeanInfo ; 33 import javax.management.Attribute ; 34 35 import javax.management.MBeanException ; 36 import javax.management.AttributeNotFoundException ; 37 import javax.management.RuntimeOperationsException ; 38 import javax.management.ReflectionException ; 39 import javax.management.InvalidAttributeValueException ; 40 import javax.management.MBeanOperationInfo ; 41 import javax.management.MBeanParameterInfo ; 42 43 import java.beans.Introspector ; 44 import java.beans.BeanInfo ; 45 import java.beans.PropertyDescriptor ; 46 import java.beans.IntrospectionException ; 47 48 public class LayoutDynamicMBean extends AbstractDynamicMBean { 49 50 private MBeanConstructorInfo [] dConstructors = new MBeanConstructorInfo [1]; 51 private Vector dAttributes = new Vector (); 52 private String dClassName = this.getClass().getName(); 53 54 private Hashtable dynamicProps = new Hashtable (5); 55 private MBeanOperationInfo [] dOperations = new MBeanOperationInfo [1]; 56 private String dDescription = 57 "This MBean acts as a management facade for log4j layouts."; 58 59 private static Logger cat = Logger.getLogger(LayoutDynamicMBean.class); 61 62 private Layout layout; 64 65 public LayoutDynamicMBean(Layout layout) throws IntrospectionException { 66 this.layout = layout; 67 buildDynamicMBeanInfo(); 68 } 69 70 private 71 void buildDynamicMBeanInfo() throws IntrospectionException { 72 Constructor [] constructors = this.getClass().getConstructors(); 73 dConstructors[0] = new MBeanConstructorInfo ( 74 "LayoutDynamicMBean(): Constructs a LayoutDynamicMBean instance", 75 constructors[0]); 76 77 78 BeanInfo bi = Introspector.getBeanInfo(layout.getClass()); 79 PropertyDescriptor [] pd = bi.getPropertyDescriptors(); 80 81 int size = pd.length; 82 83 for(int i = 0; i < size; i++) { 84 String name = pd[i].getName(); 85 Method readMethod = pd[i].getReadMethod(); 86 Method writeMethod = pd[i].getWriteMethod(); 87 if(readMethod != null) { 88 Class returnClass = readMethod.getReturnType(); 89 if(isSupportedType(returnClass)) { 90 String returnClassName; 91 if(returnClass.isAssignableFrom(Level.class)) { 92 returnClassName = "java.lang.String"; 93 } else { 94 returnClassName = returnClass.getName(); 95 } 96 97 dAttributes.add(new MBeanAttributeInfo (name, 98 returnClassName, 99 "Dynamic", 100 true, 101 writeMethod != null, 102 false)); 103 dynamicProps.put(name, new MethodUnion(readMethod, writeMethod)); 104 } 105 } 106 } 107 108 MBeanParameterInfo [] params = new MBeanParameterInfo [0]; 109 110 dOperations[0] = new MBeanOperationInfo ("activateOptions", 111 "activateOptions(): add an layout", 112 params, 113 "void", 114 MBeanOperationInfo.ACTION); 115 } 116 117 private 118 boolean isSupportedType(Class clazz) { 119 if(clazz.isPrimitive()) { 120 return true; 121 } 122 123 if(clazz == String .class) { 124 return true; 125 } 126 if(clazz.isAssignableFrom(Level.class)) { 127 return true; 128 } 129 130 return false; 131 } 132 133 134 135 public 136 MBeanInfo getMBeanInfo() { 137 cat.debug("getMBeanInfo called."); 138 139 MBeanAttributeInfo [] attribs = new MBeanAttributeInfo [dAttributes.size()]; 140 dAttributes.toArray(attribs); 141 142 return new MBeanInfo (dClassName, 143 dDescription, 144 attribs, 145 dConstructors, 146 dOperations, 147 new MBeanNotificationInfo [0]); 148 } 149 150 public 151 Object invoke(String operationName, Object params[], String signature[]) 152 throws MBeanException , 153 ReflectionException { 154 155 if(operationName.equals("activateOptions") && 156 layout instanceof OptionHandler) { 157 OptionHandler oh = (OptionHandler) layout; 158 oh.activateOptions(); 159 return "Options activated."; 160 } 161 return null; 162 } 163 164 protected 165 Logger getLogger() { 166 return cat; 167 } 168 169 170 public 171 Object getAttribute(String attributeName) throws AttributeNotFoundException , 172 MBeanException , 173 ReflectionException { 174 175 if (attributeName == null) { 177 throw new RuntimeOperationsException (new IllegalArgumentException ( 178 "Attribute name cannot be null"), 179 "Cannot invoke a getter of " + dClassName + " with null attribute name"); 180 } 181 182 183 MethodUnion mu = (MethodUnion) dynamicProps.get(attributeName); 184 185 cat.debug("----name="+attributeName+", mu="+mu); 186 187 if(mu != null && mu.readMethod != null) { 188 try { 189 return mu.readMethod.invoke(layout, null); 190 } catch(Exception e) { 191 return null; 192 } 193 } 194 195 196 197 throw(new AttributeNotFoundException ("Cannot find " + attributeName + 199 " attribute in " + dClassName)); 200 201 } 202 203 204 public 205 void setAttribute(Attribute attribute) throws AttributeNotFoundException , 206 InvalidAttributeValueException , 207 MBeanException , 208 ReflectionException { 209 210 if (attribute == null) { 212 throw new RuntimeOperationsException ( 213 new IllegalArgumentException ("Attribute cannot be null"), 214 "Cannot invoke a setter of " + dClassName + 215 " with null attribute"); 216 } 217 String name = attribute.getName(); 218 Object value = attribute.getValue(); 219 220 if (name == null) { 221 throw new RuntimeOperationsException ( 222 new IllegalArgumentException ("Attribute name cannot be null"), 223 "Cannot invoke the setter of "+dClassName+ 224 " with null attribute name"); 225 } 226 227 228 229 MethodUnion mu = (MethodUnion) dynamicProps.get(name); 230 231 if(mu != null && mu.writeMethod != null) { 232 Object [] o = new Object [1]; 233 234 Class [] params = mu.writeMethod.getParameterTypes(); 235 if(params[0] == org.apache.log4j.Priority.class) { 236 value = OptionConverter.toLevel((String ) value, 237 (Level) getAttribute(name)); 238 } 239 o[0] = value; 240 241 try { 242 mu.writeMethod.invoke(layout, o); 243 244 } catch(Exception e) { 245 cat.error("FIXME", e); 246 } 247 } else { 248 throw(new AttributeNotFoundException ("Attribute " + name + 249 " not found in " + 250 this.getClass().getName())); 251 } 252 } 253 } 254 255 256 | Popular Tags |