1 16 17 package org.apache.commons.modeler.ant; 18 19 import java.util.Vector ; 20 21 import javax.management.MBeanServer ; 22 import javax.management.MBeanServerFactory ; 23 import javax.management.ObjectName ; 24 25 import org.apache.tools.ant.Task; 26 27 31 public class JmxInvoke extends Task { 32 String objectName; 33 34 String method; 35 Vector args; 36 37 public JmxInvoke() { 38 39 } 40 41 public void setObjectName(String name) { 42 this.objectName = name; 43 } 44 45 public void setOperation(String method) { 46 this.method = method; 47 } 48 49 public void execute() { 50 try { 51 MBeanServer server=(MBeanServer )project.getReference("jmx.server"); 52 53 if (server == null) { 54 if( MBeanServerFactory.findMBeanServer(null).size() > 0 ) { 55 server=(MBeanServer )MBeanServerFactory.findMBeanServer(null).get(0); 56 } else { 57 System.out.println("Creating mbean server"); 58 server=MBeanServerFactory.createMBeanServer(); 59 } 60 project.addReference("jmx.server", server); 61 } 62 63 ObjectName oname=new ObjectName (objectName); 64 65 if( args==null ) { 66 server.invoke(oname, method, null, null); 67 } else { 68 Object argsA[]=new Object [ args.size()]; 70 String sigA[]=new String [args.size()]; 71 for( int i=0; i<args.size(); i++ ) { 72 Arg arg=(Arg)args.elementAt(i); 73 if( arg.type==null ) 74 arg.type="java.lang.String"; 75 sigA[i]=arg.getType(); 76 argsA[i]=arg.getValue(); 77 } 79 server.invoke(oname, method, argsA, sigA); 80 } 81 } catch(Exception ex) { 82 ex.printStackTrace(); 83 } 84 } 85 86 } 87 | Popular Tags |