1 8 9 package mx4j.tools.adaptor.http; 10 11 import java.io.IOException ; 12 import java.lang.reflect.Array ; 13 import java.util.Collection ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 import javax.management.JMException ; 17 import javax.management.MBeanAttributeInfo ; 18 import javax.management.MBeanInfo ; 19 import javax.management.ObjectName ; 20 21 import org.w3c.dom.Document ; 22 import org.w3c.dom.Element ; 23 24 30 public class GetAttributeCommandProcessor extends HttpCommandProcessorAdaptor 31 { 32 public GetAttributeCommandProcessor() 33 { 34 } 35 36 public Document executeRequest(HttpInputStream in) throws IOException , JMException 37 { 38 Document document = builder.newDocument(); 39 40 String name = in.getVariable("objectname"); 41 String attributeVariable = in.getVariable("attribute"); 42 String formatVariable = in.getVariable("format"); 43 ObjectName objectName = null; 44 MBeanAttributeInfo targetAttribute = null; 45 boolean validMBean = false; 47 if (name != null) 48 { 49 objectName = new ObjectName (name); 50 if (server.isRegistered(objectName)) 51 { 52 validMBean = true; 53 } 54 } 55 if (validMBean && attributeVariable != null) 56 { 57 validMBean = false; 58 MBeanInfo info = server.getMBeanInfo(objectName); 59 MBeanAttributeInfo [] attributes = info.getAttributes(); 60 61 if (attributes != null) 62 { 63 for (int i = 0; i < attributes.length; i++) 64 { 65 if (attributes[i].getName().equals(attributeVariable)) 66 { 67 targetAttribute = attributes[i]; 68 validMBean = true; 69 break; 70 } 71 } 72 } 73 } 74 if (validMBean) 75 { 76 Element root = document.createElement("MBean"); 77 document.appendChild(root); 78 79 root.setAttribute("objectname", objectName.toString()); 80 MBeanInfo info = server.getMBeanInfo(objectName); 81 root.setAttribute("classname", info.getClassName()); 82 root.setAttribute("description", info.getDescription()); 83 84 Element attribute = document.createElement("Attribute"); 85 attribute.setAttribute("name", attributeVariable); 86 attribute.setAttribute("classname", targetAttribute.getType()); 87 Object attributeValue = server.getAttribute(objectName, attributeVariable); 88 attribute.setAttribute("isnull", (attributeValue == null) ? "true" : "false"); 89 root.appendChild(attribute); 90 91 if ("array".equals(formatVariable) && attributeValue.getClass().isArray()) 92 { 93 Element array = document.createElement("Array"); 94 array.setAttribute("componentclass", attributeValue.getClass().getComponentType().getName()); 95 int length = Array.getLength(attributeValue); 96 array.setAttribute("length", "" + length); 97 for (int i = 0; i < length; i++) 98 { 99 Element arrayElement = document.createElement("Element"); 100 arrayElement.setAttribute("index", "" + i); 101 if (Array.get(attributeValue, i) != null) 102 { 103 arrayElement.setAttribute("element", Array.get(attributeValue, i).toString()); 104 arrayElement.setAttribute("isnull", "false"); 105 } 106 else 107 { 108 arrayElement.setAttribute("element", "null"); 109 arrayElement.setAttribute("isnull", "true"); 110 } 111 array.appendChild(arrayElement); 112 } 113 attribute.appendChild(array); 114 } 115 else if ("collection".equals(formatVariable) && attributeValue instanceof Collection ) 116 { 117 Collection collection = (Collection )attributeValue; 118 Element collectionElement = document.createElement("Collection"); 119 collectionElement.setAttribute("length", "" + collection.size()); 120 Iterator i = collection.iterator(); 121 int j = 0; 122 while (i.hasNext()) 123 { 124 Element collectionEntry = document.createElement("Element"); 125 collectionEntry.setAttribute("index", "" + j++); 126 Object obj = i.next(); 127 if (obj != null) 128 { 129 collectionEntry.setAttribute("elementclass", obj.getClass().getName()); 130 collectionEntry.setAttribute("element", obj.toString()); 131 } 132 else 133 { 134 collectionEntry.setAttribute("elementclass", "null"); 135 collectionEntry.setAttribute("element", "null"); 136 } 137 collectionElement.appendChild(collectionEntry); 138 } 139 attribute.appendChild(collectionElement); 140 } 141 else if ("map".equals(formatVariable) && attributeValue instanceof Map ) 142 { 143 Map map = (Map )attributeValue; 144 Element mapElement = document.createElement("Map"); 145 mapElement.setAttribute("length", "" + map.size()); 146 Iterator i = map.keySet().iterator(); 147 int j = 0; 148 while (i.hasNext()) 149 { 150 Element mapEntry = document.createElement("Element"); 151 mapEntry.setAttribute("index", "" + j++); 152 Object key = i.next(); 153 Object entry = map.get(key); 154 if (entry != null && key != null) 155 { 156 mapEntry.setAttribute("keyclass", key.getClass().getName()); 157 mapEntry.setAttribute("key", key.toString()); 158 mapEntry.setAttribute("elementclass", entry.getClass().getName()); 159 mapEntry.setAttribute("element", entry.toString()); 160 } 161 else 162 { 163 mapEntry.setAttribute("keyclass", "null"); 164 mapEntry.setAttribute("key", "null"); 165 mapEntry.setAttribute("elementclass", "null"); 166 mapEntry.setAttribute("element", "null"); 167 } 168 mapElement.appendChild(mapEntry); 169 } 170 attribute.appendChild(mapElement); 171 } 172 else 173 { 174 attribute.setAttribute("value", (attributeValue == null) ? "null" : attributeValue.toString()); 175 } 176 177 } 178 return document; 179 } 180 } 181 | Popular Tags |