1 8 package mx4j.tools.adaptor.http; 9 10 import java.io.IOException ; 11 import javax.management.Attribute ; 12 import javax.management.JMException ; 13 import javax.management.MBeanAttributeInfo ; 14 import javax.management.MBeanInfo ; 15 import javax.management.MalformedObjectNameException ; 16 import javax.management.ObjectName ; 17 18 import org.w3c.dom.Document ; 19 import org.w3c.dom.Element ; 20 21 27 public class SetAttributeCommandProcessor extends HttpCommandProcessorAdaptor 28 { 29 30 public SetAttributeCommandProcessor() 31 { 32 } 33 34 public Document executeRequest(HttpInputStream in) throws IOException , JMException 35 { 36 Document document = builder.newDocument(); 37 38 Element root = document.createElement("MBeanOperation"); 39 document.appendChild(root); 40 Element operationElement = document.createElement("Operation"); 41 operationElement.setAttribute("operation", "setattribute"); 42 root.appendChild(operationElement); 43 44 String objectVariable = in.getVariable("objectname"); 45 String attributeVariable = in.getVariable("attribute"); 46 String valueVariable = in.getVariable("value"); 47 if (objectVariable == null || objectVariable.equals("") || 48 attributeVariable == null || attributeVariable.equals("") || 49 valueVariable == null) 50 { 51 operationElement.setAttribute("result", "error"); 52 operationElement.setAttribute("errorMsg", "Incorrect parameters in the request"); 53 return document; 54 } 55 operationElement.setAttribute("objectname", objectVariable); 56 ObjectName name = null; 57 try 58 { 59 name = new ObjectName (objectVariable); 60 } 61 catch (MalformedObjectNameException e) 62 { 63 operationElement.setAttribute("result", "error"); 64 operationElement.setAttribute("errorMsg", "Malformed object name"); 65 return document; 66 } 67 68 if (server.isRegistered(name)) 69 { 70 MBeanInfo info = server.getMBeanInfo(name); 71 MBeanAttributeInfo [] attributes = info.getAttributes(); 72 MBeanAttributeInfo targetAttribute = null; 73 if (attributes != null) 74 { 75 for (int i = 0; i < attributes.length; i++) 76 { 77 if (attributes[i].getName().equals(attributeVariable)) 78 { 79 targetAttribute = attributes[i]; 80 break; 81 } 82 } 83 } 84 if (targetAttribute != null) 85 { 86 String type = targetAttribute.getType(); 87 Object value = null; 88 if (valueVariable != null) 89 { 90 try 91 { 92 value = CommandProcessorUtil.createParameterValue(type, valueVariable); 93 } 94 catch (Exception e) 95 { 96 operationElement.setAttribute("result", "error"); 97 operationElement.setAttribute("errorMsg", "Value: " + valueVariable + " could not be converted to " + type); 98 } 99 if (value != null) 100 { 101 try 102 { 103 server.setAttribute(name, new Attribute (attributeVariable, value)); 104 operationElement.setAttribute("result", "success"); 105 } 106 catch (Exception e) 107 { 108 operationElement.setAttribute("result", "error"); 109 operationElement.setAttribute("errorMsg", e.getMessage()); 110 } 111 } 112 } 113 } 114 else 115 { 116 operationElement.setAttribute("result", "error"); 117 operationElement.setAttribute("errorMsg", "Attribute " + attributeVariable + " not found"); 118 } 119 } 120 else 121 { 122 if (name != null) 123 { 124 operationElement.setAttribute("result", "error"); 125 operationElement.setAttribute("errorMsg", "MBean " + name + " not registered"); 126 } 127 } 128 return document; 129 } 130 131 } 132 | Popular Tags |