1 package org.apache.tools.ant.taskdefs.optional.jmx; 2 3 52 53 54 55 import javax.management.MBeanInfo ; 56 import javax.management.MBeanOperationInfo ; 57 import javax.management.MBeanParameterInfo ; 58 import org.apache.tools.ant.BuildException; 59 import org.apache.tools.ant.taskdefs.optional.jmx.converter.ValueFactory; 60 61 62 72 public class InvokeMBeanTask extends AbstractMBeanTask { 73 74 private String operation; 75 private String resultProperty; 76 77 private java.util.List parameterList = new java.util.LinkedList (); 79 public void setOperation(String operation) { 80 this.operation = operation; 81 } 82 83 88 public void setResultProperty(String resultProperty) { 89 this.resultProperty = resultProperty; 90 } 91 92 97 public String getResultProperty() { 98 if ( (resultProperty == null) || (resultProperty.equals("")) ) { 99 return operation+".result"; 100 } 101 else { 102 return resultProperty; 103 } 104 } 105 106 112 public void addParameter(Parameter param) { 113 parameterList.add(param); 114 } 115 116 121 public static class Parameter { 122 private String type; 123 private StringBuffer value = new StringBuffer (); 124 125 129 public void setValue(String value) { 130 this.value.append(value); 131 } 132 133 public void addText(String text) { 134 this.value.append(text); 135 } 136 137 public String getValue() { 138 return value.toString(); 139 } 140 141 146 public void setType(String type) { 147 this.type = type; 148 } 149 150 public String getType() { 151 return type; 152 } 153 } 154 155 156 162 public MBeanParameterInfo getParameterInfo(MBeanOperationInfo opInfo, String parameterName) { 163 MBeanParameterInfo [] params = opInfo.getSignature(); 164 for (int counter = 0; counter < params.length; counter++) { 165 if (params[counter].getName().equals(parameterName)) { 166 return params[counter]; 167 } 168 } 169 return null; 170 } 171 172 175 public MBeanOperationInfo getOperationInfo(MBeanInfo beanInfo, String operationName, java.util.List parameterList) { 176 MBeanOperationInfo [] operations = beanInfo.getOperations(); 177 for (int counter = 0; counter < operations.length; counter++) { 178 if (operations[counter].getName().equals(operationName)) { 179 if (checkSignature(operations[counter], parameterList)) { 180 return operations[counter]; 181 } 182 } 183 } 184 return null; 185 } 186 187 196 public boolean checkSignature(MBeanOperationInfo operation, java.util.List parameterList) { 197 MBeanParameterInfo [] paramInfos = operation.getSignature(); 198 if (paramInfos.length != parameterList.size()) { 199 return false; 200 } 201 java.util.Iterator paramIt = parameterList.iterator(); 202 for (int counter = 0; counter < paramInfos.length; counter++) { 203 Parameter param = (Parameter) paramIt.next(); 204 if (param.getType() != null && ! param.getType().equals(paramInfos[counter].getType())) { 205 return false; 206 } 207 } 208 return true; 209 } 210 211 217 protected void execute(javax.management.MBeanServer mbserver) throws BuildException { 218 219 try { 220 javax.management.ObjectName mbeanName = getObjectName(); 221 222 if (mbserver.isRegistered(mbeanName)) { 223 MBeanInfo info = mbserver.getMBeanInfo(mbeanName); 224 225 MBeanOperationInfo operation = getOperationInfo(info,this.operation, this.parameterList); 226 if (operation != null) { 227 228 Object [] paramValues = new Object [parameterList.size()]; 229 String [] paramTypes = new String [parameterList.size()]; 230 231 MBeanParameterInfo [] paramInfo = operation.getSignature(); 232 233 if (paramInfo.length == parameterList.size()) { 236 java.util.Iterator paramIt = parameterList.iterator(); 237 for (int counter = 0; counter < paramInfo.length; counter++) { 238 239 Parameter param = (Parameter) paramIt.next(); 240 241 paramTypes[counter] = paramInfo[counter].getType(); 242 paramValues[counter] = ValueFactory.getInstance().valueOf(getProject().replaceProperties(param.getValue()),paramInfo[counter].getType()); 243 } 244 245 Object result = mbserver.invoke(mbeanName,operation.getName(),paramValues,paramTypes); 246 if (result != null) { 247 getProject().setProperty(this.getResultProperty(),ValueFactory.toString(result)); 248 } else { 249 getProject().setProperty(this.getResultProperty(),""); 250 } 251 } else { 252 String message = "Parameter count mismatch. operation["+this.operation+"] requires["+paramInfo.length+"] parameters, but received["+this.parameterList.size()+"] for mbean " + toString(); 253 throw new BuildException(message); 254 } 255 256 } else { 257 String message = "Cannot find operation["+this.operation+"] for mbean " + toString() + " for the given parameters"; 258 throw new BuildException(message); 259 } 260 261 } else { 262 throw new BuildException("Cannot find MBean. " + toString()); 263 } 264 } catch (BuildException x) { 265 throw x; 266 } catch (Exception x) { 267 throw new BuildException(x); 268 } 269 } 270 271 } 272 273 | Popular Tags |