1 8 package mx4j.tools.adaptor.http; 9 10 import java.io.IOException ; 11 import java.util.ArrayList ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import javax.management.JMException ; 15 import javax.management.MBeanInfo ; 16 import javax.management.MBeanOperationInfo ; 17 import javax.management.MBeanParameterInfo ; 18 import javax.management.MalformedObjectNameException ; 19 import javax.management.ObjectName ; 20 21 import org.w3c.dom.Document ; 22 import org.w3c.dom.Element ; 23 24 29 public class InvokeOperationCommandProcessor extends HttpCommandProcessorAdaptor 30 { 31 public InvokeOperationCommandProcessor() 32 { 33 } 34 35 public Document executeRequest(HttpInputStream in) throws IOException , JMException 36 { 37 Document document = builder.newDocument(); 38 39 Element root = document.createElement("MBeanOperation"); 40 document.appendChild(root); 41 Element operationElement = document.createElement("Operation"); 42 operationElement.setAttribute("operation", "invoke"); 43 root.appendChild(operationElement); 44 45 String objectVariable = in.getVariable("objectname"); 46 String operationVariable = in.getVariable("operation"); 47 if (objectVariable == null || objectVariable.equals("") 48 || operationVariable == null || operationVariable.equals("")) 49 { 50 operationElement.setAttribute("result", "error"); 51 operationElement.setAttribute("errorMsg", "Incorrect parameters in the request"); 52 return document; 53 } 54 operationElement.setAttribute("objectname", objectVariable); 55 List types = new ArrayList (); 56 List values = new ArrayList (); 57 int i = 0; 58 boolean unmatchedParameters = false; 59 boolean valid = false; 60 do 61 { 62 String parameterType = in.getVariable("type" + i); 63 String parameterValue = in.getVariable("value" + i); 64 valid = (parameterType != null && parameterValue != null); 65 if (valid) 66 { 67 types.add(parameterType); 68 Object value = null; 69 try 70 { 71 value = CommandProcessorUtil.createParameterValue(parameterType, parameterValue); 72 } 73 74 catch (Exception e) 75 { 76 operationElement.setAttribute("result", "error"); 77 operationElement.setAttribute("errorMsg", "Parameter " + i + ": " + parameterValue + " cannot be converted to type " + parameterType); 78 return document; 79 } 80 if (value != null) 81 { 82 values.add(value); 83 } 84 } 85 if (parameterType == null ^ parameterValue == null) 86 { 87 unmatchedParameters = true; 88 break; 89 } 90 i++; 91 } 92 while (valid); 93 if (objectVariable == null || objectVariable.equals("") || 94 operationVariable == null || operationVariable.equals("")) 95 { 96 operationElement.setAttribute("result", "error"); 97 operationElement.setAttribute("errorMsg", "Incorrect parameters in the request"); 98 return document; 99 } 100 if (unmatchedParameters) 101 { 102 operationElement.setAttribute("result", "error"); 103 operationElement.setAttribute("errorMsg", "count of parameter types doesn't match count of parameter values"); 104 return document; 105 } 106 ObjectName name = null; 107 try 108 { 109 name = new ObjectName (objectVariable); 110 } 111 catch (MalformedObjectNameException e) 112 { 113 operationElement.setAttribute("result", "error"); 114 operationElement.setAttribute("errorMsg", "Malformed object name"); 115 return document; 116 } 117 118 if (server.isRegistered(name)) 119 { 120 MBeanInfo info = server.getMBeanInfo(name); 121 MBeanOperationInfo [] operations = info.getOperations(); 122 boolean match = false; 123 if (operations != null) 124 { 125 for (int j = 0; j < operations.length; j++) 126 { 127 if (operations[j].getName().equals(operationVariable)) 128 { 129 MBeanParameterInfo [] parameters = operations[j].getSignature(); 130 if (parameters.length != types.size()) 131 { 132 continue; 133 } 134 Iterator k = types.iterator(); 135 boolean signatureMatch = true; 136 for (int p = 0; p < types.size(); p++) 137 { 138 if (!parameters[p].getType().equals(k.next())) 139 { 140 signatureMatch = false; 141 break; 142 } 143 } 144 match = signatureMatch; 145 } 146 if (match) 147 { 148 break; 149 } 150 } 151 } 152 if (!match) 153 { 154 operationElement.setAttribute("result", "error"); 155 operationElement.setAttribute("errorMsg", "Operation singature has no match in the MBean"); 156 } 157 else 158 { 159 try 160 { 161 Object [] params = values.toArray(); 162 String [] signature = new String [types.size()]; 163 types.toArray(signature); 164 Object returnValue = server.invoke(name, operationVariable, params, signature); 165 operationElement.setAttribute("result", "success"); 166 if (returnValue != null) 167 { 168 operationElement.setAttribute("returnclass", returnValue.getClass().getName()); 169 operationElement.setAttribute("return", returnValue.toString()); 170 } 171 else 172 { 173 operationElement.setAttribute("returnclass", null); 174 operationElement.setAttribute("return", null); 175 } 176 } 177 catch (Exception e) 178 { 179 operationElement.setAttribute("result", "error"); 180 operationElement.setAttribute("errorMsg", e.getMessage()); 181 } 182 } 183 } 184 else 185 { 186 if (name != null) 187 { 188 operationElement.setAttribute("result", "error"); 189 operationElement.setAttribute("errorMsg", new StringBuffer ("MBean ").append(name).append(" not registered").toString()); 190 } 191 } 192 return document; 193 } 194 195 } 196 | Popular Tags |