1 8 9 package mx4j.tools.adaptor.http; 10 11 import java.io.IOException ; 12 import java.util.Iterator ; 13 import java.util.Set ; 14 import java.util.SortedMap ; 15 import java.util.TreeMap ; 16 import java.util.TreeSet ; 17 import javax.management.JMException ; 18 import javax.management.MBeanAttributeInfo ; 19 import javax.management.MBeanConstructorInfo ; 20 import javax.management.MBeanInfo ; 21 import javax.management.MBeanNotificationInfo ; 22 import javax.management.MBeanOperationInfo ; 23 import javax.management.MBeanParameterInfo ; 24 import javax.management.ObjectName ; 25 import javax.management.modelmbean.ModelMBeanInfo ; 26 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Element ; 29 30 35 public class MBeanCommandProcessor extends HttpCommandProcessorAdaptor 36 { 37 public MBeanCommandProcessor() 38 { 39 } 40 41 public Document executeRequest(HttpInputStream in) throws IOException , JMException 42 { 43 Document document = builder.newDocument(); 44 45 String name = in.getVariable("objectname"); 46 ObjectName objectName = null; 47 48 if (name != null) 49 { 50 objectName = new ObjectName (name); 51 if (!objectName.isPattern()) 52 { 53 if (server.isRegistered(objectName)) 55 { 56 Element mb = createMBeanElement(document, objectName, in); 57 document.appendChild(mb); 58 } 59 } 60 else 61 { 62 Set names = new TreeSet (CommandProcessorUtil.createObjectNameComparator()); 64 names.addAll(server.queryNames(objectName, null)); 65 Element root = document.createElement("Server"); 66 root.setAttribute("pattern", objectName.toString()); 67 for (Iterator it = names.iterator(); it.hasNext();) 68 { 69 Element mb = createMBeanElement(document, (ObjectName )it.next(), in); 70 root.appendChild(mb); 71 } 72 document.appendChild(root); 73 } 74 } 75 return document; 76 } 77 78 private Element createMBeanElement(Document document, ObjectName objectName, HttpInputStream in) 79 throws JMException 80 { 81 Element root = document.createElement("MBean"); 82 83 MBeanInfo info = server.getMBeanInfo(objectName); 84 root.setAttribute("description", info.getDescription()); 85 root.setAttribute("classname", info.getClassName()); 86 root.setAttribute("objectname", objectName.toString()); 87 88 if (info instanceof ModelMBeanInfo ) 89 { 90 root.setAttribute("model", "true"); 91 } 92 if (HttpUtil.booleanVariableValue(in, "attributes", true)) 93 { 94 MBeanAttributeInfo [] attributes = info.getAttributes(); 95 if (attributes != null) 96 { 97 SortedMap sortedAttributes = new TreeMap (); 98 for (int i = 0; i < attributes.length; i++) 99 { 100 Element attribute = document.createElement("Attribute"); 101 attribute.setAttribute("name", attributes[i].getName()); 102 attribute.setAttribute("type", attributes[i].getType()); 103 attribute.setAttribute("description", attributes[i].getDescription()); 104 attribute.setAttribute("strinit", String.valueOf(CommandProcessorUtil.canCreateParameterValue(attributes[i].getType()))); 105 if (attributes[i].isReadable() && attributes[i].isWritable()) 106 { 107 attribute.setAttribute("availability", "RW"); 108 } 109 if (attributes[i].isReadable() && !attributes[i].isWritable()) 110 { 111 attribute.setAttribute("availability", "RO"); 112 } 113 if (!attributes[i].isReadable() && attributes[i].isWritable()) 114 { 115 attribute.setAttribute("availability", "WO"); 116 } 117 try 118 { 119 Object attributeValue = server.getAttribute(objectName, attributes[i].getName()); 120 attribute.setAttribute("isnull", (attributeValue == null) ? "true" : "false"); 121 if (attributeValue != null) 122 { 123 attribute.setAttribute("value", attributeValue.toString()); 124 if (attributeValue.getClass().isArray()) 125 { 126 attribute.setAttribute("aggregation", "array"); 127 } 128 if (attributeValue instanceof java.util.Collection ) 129 { 130 attribute.setAttribute("aggregation", "collection"); 131 } 132 if (attributeValue instanceof java.util.Map ) 133 { 134 attribute.setAttribute("aggregation", "map"); 135 } 136 } 137 else 138 { 139 attribute.setAttribute("value", "null"); 140 } 141 142 } 143 catch (JMException e) 144 { 145 attribute.setAttribute("value", e.getMessage()); 146 } 147 sortedAttributes.put(attributes[i].getName(), attribute); 148 } 149 Iterator keys = sortedAttributes.keySet().iterator(); 150 while (keys.hasNext()) 151 { 152 root.appendChild((Element )sortedAttributes.get(keys.next())); 153 } 154 } 155 } 156 if (HttpUtil.booleanVariableValue(in, "constructors", true)) 157 { 158 MBeanConstructorInfo [] constructors = info.getConstructors(); 159 if (constructors != null) 160 { 161 for (int i = 0; i < constructors.length; i++) 163 { 164 Element constructor = document.createElement("Constructor"); 165 constructor.setAttribute("name", constructors[i].getName()); 166 constructor.setAttribute("description", constructors[i].getDescription()); 167 addParameters(constructor, document, constructors[i].getSignature()); 168 root.appendChild(constructor); 169 } 170 } 171 } 172 if (HttpUtil.booleanVariableValue(in, "operations", true)) 173 { 174 MBeanOperationInfo [] operations = info.getOperations(); 175 if (operations != null) 176 { 177 for (int i = 0; i < operations.length; i++) 178 { 179 Element operation = document.createElement("Operation"); 180 operation.setAttribute("name", operations[i].getName()); 181 operation.setAttribute("description", operations[i].getDescription()); 182 operation.setAttribute("return", operations[i].getReturnType()); 183 switch (operations[i].getImpact()) 184 { 185 case MBeanOperationInfo.UNKNOWN: 186 operation.setAttribute("impact", "unknown"); 187 break; 188 case MBeanOperationInfo.ACTION: 189 operation.setAttribute("impact", "action"); 190 break; 191 case MBeanOperationInfo.INFO: 192 operation.setAttribute("impact", "info"); 193 break; 194 case MBeanOperationInfo.ACTION_INFO: 195 operation.setAttribute("impact", "action_info"); 196 break; 197 } 198 addParameters(operation, document, operations[i].getSignature()); 199 root.appendChild(operation); 200 } 201 } 202 } 203 if (HttpUtil.booleanVariableValue(in, "notifications", true)) 204 { 205 MBeanNotificationInfo [] notifications = info.getNotifications(); 206 if (notifications != null) 207 { 208 for (int i = 0; i < notifications.length; i++) 209 { 210 Element notification = document.createElement("Notification"); 211 notification.setAttribute("name", notifications[i].getName()); 212 notification.setAttribute("description", notifications[i].getDescription()); 213 String [] types = notifications[i].getNotifTypes(); 214 for (int j = 0; j < types.length; j++) 215 { 216 Element type = document.createElement("Type"); 217 type.setAttribute("name", types[j]); 218 notification.appendChild(type); 219 } 220 root.appendChild(notification); 221 } 222 } 223 } 224 return root; 225 } 226 227 protected void addParameters(Element node, Document document, MBeanParameterInfo [] parameters) 228 { 229 for (int j = 0; j < parameters.length; j++) 230 { 231 Element parameter = document.createElement("Parameter"); 232 parameter.setAttribute("name", parameters[j].getName()); 233 parameter.setAttribute("description", parameters[j].getDescription()); 234 parameter.setAttribute("type", parameters[j].getType()); 235 parameter.setAttribute("strinit", String.valueOf(CommandProcessorUtil.canCreateParameterValue(parameters[j].getType()))); 236 parameter.setAttribute("id", "" + j); 238 node.appendChild(parameter); 239 } 240 } 241 242 } 243 | Popular Tags |