1 9 package org.jboss.portal.common.mx; 10 11 import java.beans.BeanInfo ; 12 import java.beans.Introspector ; 13 import java.beans.MethodDescriptor ; 14 import java.beans.PropertyDescriptor ; 15 import java.lang.reflect.Method ; 16 import java.util.ArrayList ; 17 import java.util.HashMap ; 18 import java.util.Map ; 19 20 import javax.management.Descriptor ; 21 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 22 import javax.management.modelmbean.ModelMBeanConstructorInfo ; 23 import javax.management.modelmbean.ModelMBeanInfo ; 24 import javax.management.modelmbean.ModelMBeanInfoSupport ; 25 import javax.management.modelmbean.ModelMBeanNotificationInfo ; 26 import javax.management.modelmbean.ModelMBeanOperationInfo ; 27 28 import org.apache.log4j.Logger; 29 30 34 public class JavaBeanModelMBeanBuilder 35 { 36 37 private final static String CURRENCY_TIME_LIMIT = "currencyTimeLimit"; 38 private final static String GET_METHOD = "getMethod"; 39 private final static String SET_METHOD = "setMethod"; 40 private final static String PERSIST_POLICY = "persistPolicy"; 41 private final static String ROLE = "role"; 42 43 private static final Logger log = Logger.getLogger(JavaBeanModelMBeanBuilder.class); 44 45 private ArrayList mmais; 46 private ArrayList mmois; 47 private Class from; 48 private Class to; 49 50 public JavaBeanModelMBeanBuilder(Class from, Class to) throws Exception 51 { 52 BeanInfo info = Introspector.getBeanInfo(from, to); 54 55 this.from = from; 57 this.to = to; 58 this.mmais = new ArrayList (info.getPropertyDescriptors().length); 59 this.mmois = new ArrayList (info.getMethodDescriptors().length); 60 61 Map roles = new HashMap (); 63 64 PropertyDescriptor [] propertyDescs = info.getPropertyDescriptors(); 66 for (int i = 0;i < propertyDescs.length;i++) 67 { 68 PropertyDescriptor propertyDesc = propertyDescs[i]; 69 String name = Character.toUpperCase(propertyDesc.getName().charAt(0)) + propertyDesc.getName().substring(1); 70 Method getter = propertyDesc.getReadMethod(); 71 Method setter = propertyDesc.getWriteMethod(); 72 73 if (getter != null) 75 { 76 roles.put(getter, "getter"); 77 } 78 if (setter != null) 79 { 80 roles.put(setter, "setter"); 81 } 82 83 ModelMBeanAttributeInfo mmai = new ModelMBeanAttributeInfo ( 85 name, 86 "Javabean introspected attribute", 87 getter, 88 setter); 89 90 Descriptor desc = mmai.getDescriptor(); 92 desc.setField(CURRENCY_TIME_LIMIT, "0"); 93 desc.setField(PERSIST_POLICY, "Never"); 94 if (getter != null) 95 { 96 desc.setField(GET_METHOD, getter.getName()); 97 } 98 if (setter != null) 99 { 100 desc.setField(SET_METHOD, setter.getName()); 101 } 102 mmai.setDescriptor(desc); 103 104 mmais.add(mmai); 106 } 107 108 MethodDescriptor [] methodDescs = info.getMethodDescriptors(); 110 for (int i = 0;i < methodDescs.length;i++) 111 { 112 MethodDescriptor methodDesc = methodDescs[i]; 113 Method method = methodDesc.getMethod(); 114 115 ModelMBeanOperationInfo mmoi = new ModelMBeanOperationInfo ("Javabean introspected method", method); 117 118 Descriptor desc = mmoi.getDescriptor(); 120 String role = (String )roles.get(method); 121 desc.setField(ROLE, role != null ? role : "operation"); 122 mmoi.setDescriptor(desc); 123 124 mmois.add(mmoi); 126 } 127 } 128 129 132 public void remove(Class itf) 133 { 134 throw new UnsupportedOperationException ("To be implemented if usefull, just a placeholder now"); 135 } 136 137 140 public ModelMBeanInfo getInfo() 141 { 142 return new ModelMBeanInfoSupport ( 143 from.getName(), 144 "Javabean model mbean", 145 (ModelMBeanAttributeInfo [])mmais.toArray(new ModelMBeanAttributeInfo [mmais.size()]), 146 new ModelMBeanConstructorInfo [0], 147 (ModelMBeanOperationInfo [])mmois.toArray(new ModelMBeanOperationInfo [mmois.size()]), 148 new ModelMBeanNotificationInfo [0] 149 ); 150 } 151 152 public static final ModelMBeanInfo build(Class from, Class to) throws Exception 153 { 154 return new JavaBeanModelMBeanBuilder(from, to).getInfo(); 155 } 156 157 public static final ModelMBeanInfo build(Object o) throws Exception 158 { 159 return new JavaBeanModelMBeanBuilder(o.getClass(), Object .class).getInfo(); 160 } 161 162 163 } 164 | Popular Tags |