1 8 package mx4j.tools.adaptor.http; 9 10 import java.io.IOException ; 11 import java.util.Iterator ; 12 import java.util.Map ; 13 import java.util.SortedMap ; 14 import java.util.TreeMap ; 15 import javax.management.Attribute ; 16 import javax.management.JMException ; 17 import javax.management.MBeanAttributeInfo ; 18 import javax.management.MBeanInfo ; 19 import javax.management.MalformedObjectNameException ; 20 import javax.management.ObjectName ; 21 22 import org.w3c.dom.Document ; 23 import org.w3c.dom.Element ; 24 25 33 public class SetAttributesCommandProcessor extends HttpCommandProcessorAdaptor 34 { 35 36 public SetAttributesCommandProcessor() 37 { 38 } 39 40 public Document executeRequest(HttpInputStream in) throws IOException , JMException 41 { 42 Document document = builder.newDocument(); 43 44 Element root = document.createElement("MBeanOperation"); 45 document.appendChild(root); 46 Element operationElement = document.createElement("Operation"); 47 operationElement.setAttribute("operation", "setattributes"); 48 root.appendChild(operationElement); 49 50 String objectVariable = in.getVariable("objectname"); 51 if (objectVariable == null || objectVariable.equals("")) 52 { 53 operationElement.setAttribute("result", "error"); 54 operationElement.setAttribute("errorMsg", "Missing objectname in the request"); 55 return document; 56 } 57 operationElement.setAttribute("objectname", objectVariable); 58 ObjectName name = null; 59 try 60 { 61 name = new ObjectName (objectVariable); 62 } 63 catch (MalformedObjectNameException e) 64 { 65 operationElement.setAttribute("result", "error"); 66 operationElement.setAttribute("errorMsg", "Malformed object name"); 67 return document; 68 } 69 if (server.isRegistered(name)) 70 { 71 Map variables = in.getVariables(); 72 if (variables.containsKey("setall")) 73 { 74 Iterator keys = variables.keySet().iterator(); 75 SortedMap allAttributes = new TreeMap (); 76 while (keys.hasNext()) 77 { 78 String key = (String )keys.next(); 79 if (key.startsWith("value_")) 80 { 81 String attributeVariable = key.substring(6, key.length()); 82 String valueVariable = in.getVariable(key); 83 Element attributeElement = setAttribute(document, attributeVariable, valueVariable, name); 84 allAttributes.put(attributeVariable, attributeElement); 85 operationElement.appendChild(attributeElement); 86 } 87 } 88 keys = allAttributes.keySet().iterator(); 89 while (keys.hasNext()) 90 { 91 Element attributeElement = (Element )allAttributes.get(keys.next()); 92 operationElement.appendChild(attributeElement); 93 } 94 } 95 else 96 { 97 Iterator keys = variables.keySet().iterator(); 98 SortedMap allAttributes = new TreeMap (); 99 while (keys.hasNext()) 100 { 101 String key = (String )keys.next(); 102 if (key.startsWith("set_")) 103 { 104 String attributeVariable = key.substring(4, key.length()); 105 String valueVariable = in.getVariable("value_" + attributeVariable); 106 Element attributeElement = setAttribute(document, attributeVariable, valueVariable, name); 107 allAttributes.put(attributeVariable, attributeElement); 108 } 109 } 110 keys = allAttributes.keySet().iterator(); 111 while (keys.hasNext()) 112 { 113 Element attributeElement = (Element )allAttributes.get(keys.next()); 114 operationElement.appendChild(attributeElement); 115 } 116 } 117 } 119 else 120 { 121 if (name != null) 122 { 123 operationElement.setAttribute("result", "error"); 124 operationElement.setAttribute("errorMsg", "MBean " + name + " not registered"); 125 } 126 } 127 return document; 128 } 129 130 private Element setAttribute(Document document, String attributeVariable, String valueVariable, ObjectName name) throws JMException 131 { 132 Element attributeElement = document.createElement("Attribute"); 133 attributeElement.setAttribute("attribute", attributeVariable); 134 MBeanInfo info = server.getMBeanInfo(name); 135 MBeanAttributeInfo [] attributes = info.getAttributes(); 136 MBeanAttributeInfo targetAttribute = null; 137 if (attributes != null) 138 { 139 for (int i = 0; i < attributes.length; i++) 140 { 141 if (attributes[i].getName().equals(attributeVariable)) 142 { 143 targetAttribute = attributes[i]; 144 break; 145 } 146 } 147 } 148 if (targetAttribute != null) 149 { 150 String type = targetAttribute.getType(); 151 Object value = null; 152 if (valueVariable != null) 153 { 154 try 155 { 156 value = CommandProcessorUtil.createParameterValue(type, valueVariable); 157 } 158 catch (Exception e) 159 { 160 attributeElement.setAttribute("result", "error"); 161 attributeElement.setAttribute("errorMsg", "Value: " + valueVariable + " could not be converted to " + type); 162 } 163 if (value != null) 164 { 165 try 166 { 167 server.setAttribute(name, new Attribute (attributeVariable, value)); 168 attributeElement.setAttribute("result", "success"); 169 attributeElement.setAttribute("value", valueVariable); 170 } 171 catch (Exception e) 172 { 173 attributeElement.setAttribute("result", "error"); 174 attributeElement.setAttribute("errorMsg", e.getMessage()); 175 } 176 } 177 } 178 } 179 else 180 { 181 attributeElement.setAttribute("result", "error"); 182 attributeElement.setAttribute("errorMsg", "Attribute " + attributeVariable + " not found"); 183 } 184 return attributeElement; 185 } 186 187 } 188 | Popular Tags |