1 8 package mx4j.tools.adaptor.http; 9 10 import java.io.IOException ; 11 import java.util.ArrayList ; 12 import java.util.List ; 13 import javax.management.JMException ; 14 import javax.management.MalformedObjectNameException ; 15 import javax.management.ObjectName ; 16 17 import org.w3c.dom.Document ; 18 import org.w3c.dom.Element ; 19 20 26 public class CreateMBeanCommandProcessor extends HttpCommandProcessorAdaptor 27 { 28 29 32 public CreateMBeanCommandProcessor() 33 { 34 } 35 36 37 public Document executeRequest(HttpInputStream in) 38 throws IOException , JMException 39 { 40 Document document = builder.newDocument(); 41 42 Element root = document.createElement("MBeanOperation"); 43 document.appendChild(root); 44 Element operationElement = document.createElement("Operation"); 45 operationElement.setAttribute("name", "create"); 46 root.appendChild(operationElement); 47 48 String objectVariable = in.getVariable("objectname"); 49 String classVariable = in.getVariable("class"); 50 if (objectVariable == null || objectVariable.equals("") 51 || classVariable == null || classVariable.equals("")) 52 { 53 operationElement.setAttribute("result", "error"); 54 operationElement.setAttribute("errorMsg", "Incorrect parameters in the request"); 55 return document; 56 } 57 operationElement.setAttribute("objectname", objectVariable); 58 List types = new ArrayList (); 59 List values = new ArrayList (); 60 int i = 0; 61 boolean unmatchedParameters = false; 62 boolean valid = false; 63 do 64 { 65 String parameterType = in.getVariable("type" + i); 66 String parameterValue = in.getVariable("value" + i); 67 valid = (parameterType != null && parameterValue != null); 68 if (valid) 69 { 70 types.add(parameterType); 71 Object value = null; 72 try 73 { 74 value = CommandProcessorUtil.createParameterValue(parameterType, parameterValue); 75 } 76 catch (Exception e) 77 { 78 operationElement.setAttribute("result", "error"); 79 operationElement.setAttribute("errorMsg", "Parameter " + i + ": " + parameterValue + " cannot be converted to type " + parameterType); 80 return document; 81 } 82 if (value != null) 83 { 84 values.add(value); 85 } 86 } 87 if (parameterType == null ^ parameterValue == null) 88 { 89 unmatchedParameters = true; 90 break; 91 } 92 i++; 93 } 94 while (valid); 95 if (objectVariable == null || objectVariable.equals("")) 96 { 97 operationElement.setAttribute("result", "error"); 98 operationElement.setAttribute("errorMsg", "Incorrect parameters in the request"); 99 return document; 100 } 101 if (unmatchedParameters) 102 { 103 operationElement.setAttribute("result", "error"); 104 operationElement.setAttribute("errorMsg", "count of parameter types doesn't match count of parameter values"); 105 return document; 106 } 107 ObjectName name = null; 108 try 109 { 110 name = new ObjectName (objectVariable); 111 } 112 catch (MalformedObjectNameException e) 113 { 114 operationElement.setAttribute("result", "error"); 115 operationElement.setAttribute("errorMsg", "Malformed object name"); 116 return document; 117 } 118 119 if (server.isRegistered(name)) 120 { 121 operationElement.setAttribute("result", "error"); 122 operationElement.setAttribute("errorMsg", "A MBean with name " + name + " is already registered"); 123 return document; 124 } 125 else 126 { 127 try 128 { 129 if (types.size() > 0) 130 { 131 Object [] params = values.toArray(); 132 String [] signature = new String [types.size()]; 133 types.toArray(signature); 134 server.createMBean(classVariable, name, null, params, signature); 135 } 136 else 137 { 138 server.createMBean(classVariable, name, null); 139 } 140 operationElement.setAttribute("result", "success"); 141 } 142 catch (Exception e) 143 { 144 operationElement.setAttribute("result", "error"); 145 operationElement.setAttribute("errorMsg", e.getMessage()); 146 } 147 } 148 return document; 149 } 150 151 } 152 153 | Popular Tags |