1 22 package org.jboss.mx.mxbean; 23 24 import java.lang.reflect.Method ; 25 import java.lang.reflect.Type ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 29 import javax.management.openmbean.CompositeType ; 30 import javax.management.openmbean.OpenType ; 31 import javax.management.openmbean.SimpleType ; 32 33 import org.jboss.util.UnexpectedThrowable; 34 35 41 public class CompositeTypeMetaData 42 { 43 44 private Class <?> clazz; 45 46 47 private CompositeType compositeType; 48 49 50 private List <ItemMetaData> items = new ArrayList <ItemMetaData>(); 51 52 57 public CompositeTypeMetaData(Class <?> clazz) 58 { 59 this.clazz = clazz; 60 } 61 62 67 public CompositeType getCompositeType() 68 { 69 if (compositeType == null) 70 { 71 String typeName = clazz.getName(); 72 if (items.size() == 0) 73 return generateAnObject(typeName); 74 75 String [] itemNames = new String [items.size()]; 76 OpenType [] itemTypes = new OpenType [items.size()]; 77 for (int i = 0; i < items.size(); ++i) 78 { 79 ItemMetaData item = items.get(i); 80 itemNames[i] = item.getName(); 81 itemTypes[i] = item.getOpenType(); 82 } 83 try 84 { 85 compositeType = new CompositeType (typeName, typeName, itemNames, itemNames, itemTypes); 86 } 87 catch (Throwable t) 88 { 89 throw new UnexpectedThrowable("Error creating composite type for: " + typeName, t); 90 } 91 } 92 return compositeType; 93 } 94 95 98 public void generate() 99 { 100 if (Object .class.equals(clazz)) 101 { 102 compositeType = generateObject(); 103 return; 104 } 105 if (Class .class.equals(clazz)) 106 { 107 compositeType = generateClass(); 108 return; 109 } 110 if (ClassLoader .class.equals(clazz)) 111 { 112 compositeType = generateClassLoader(); 113 return; 114 } 115 116 Method [] methods = clazz.getMethods(); 117 for (Method method : methods) 118 { 119 Type returnType = method.getGenericReturnType(); 120 Class <?> declaring = method.getDeclaringClass(); 121 if (Object .class.equals(declaring) == false) 122 { 123 String key = MXBeanUtils.getCompositeDataKey(method); 124 if (key != null) 125 items.add(new ItemMetaData(key, returnType)); 126 } 127 } 128 } 129 130 135 public static CompositeType generateObject() 136 { 137 return generateAnObject(Object .class.getName()); 138 } 139 140 146 private static CompositeType generateAnObject(String name) 147 { 148 String [] itemNames = { "class" }; 149 OpenType [] openTypes = { generateClass() }; 150 return safeCreateCompositeType(name, itemNames, openTypes); 151 } 152 153 158 public static CompositeType generateClass() 159 { 160 String name = Object .class.getName(); 161 String [] itemNames = { "name" }; 162 OpenType [] openTypes = { SimpleType.STRING }; 163 return safeCreateCompositeType(name, itemNames, openTypes); 164 } 165 166 171 public static CompositeType generateClassLoader() 172 { 173 String name = Object .class.getName(); 174 String [] itemNames = { "name" }; 175 OpenType [] openTypes = { SimpleType.STRING }; 176 return safeCreateCompositeType(name, itemNames, openTypes); 177 } 178 179 187 private static CompositeType safeCreateCompositeType(String name, String [] itemNames, OpenType [] openTypes) 188 { 189 try 190 { 191 return new CompositeType (name, name, itemNames, itemNames, openTypes); 192 } 193 catch (Exception e) 194 { 195 throw new UnexpectedThrowable(e); 196 } 197 } 198 } 199 | Popular Tags |