1 16 17 package org.apache.commons.modeler.ant; 18 19 import java.net.URL ; 20 import java.util.ArrayList ; 21 import java.util.List ; 22 23 import javax.management.JMException ; 24 import javax.management.MBeanServer ; 25 import javax.management.MBeanServerFactory ; 26 import javax.management.ObjectName ; 27 import javax.management.loading.MLet ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.Task; 33 34 45 public class MLETTask extends Task { 46 private static Log log = LogFactory.getLog(MLETTask.class); 47 String code; 48 String archive; 49 String codebase; 50 String objectName; 51 ObjectName oname; 52 53 List args=new ArrayList (); 54 List attributes=new ArrayList (); 55 56 String loaderRef; 59 public MLETTask() { 60 } 61 62 public void addArg(Arg arg ) { 63 args.add(arg); 64 } 65 66 public void addAttribute( JmxSet arg ) { 67 attributes.add( arg ); 68 } 69 70 71 public void setCode(String code) { 72 this.code = code; 73 } 74 75 public void setArchive(String archive) { 76 this.archive = archive; 77 } 78 79 public void setCodebase(String codebase) { 80 this.codebase = codebase; 81 } 82 83 public void setName(String name) { 84 this.objectName = name; 85 } 86 87 MBeanServer server; 88 89 public MBeanServer getMBeanServer() { 90 if( server!= null ) return server; 91 92 server=(MBeanServer )project.getReference("jmx.server"); 93 94 if (server != null) return server; 95 96 try { 97 if( MBeanServerFactory.findMBeanServer(null).size() > 0 ) { 98 server=(MBeanServer )MBeanServerFactory.findMBeanServer(null).get(0); 99 } else { 100 server=MBeanServerFactory.createMBeanServer(); 101 102 ObjectName defaultLoader= new ObjectName ("modeler-ant", 104 "loader", "ant"); 105 MLet mlet=new MLet ( new URL [0], this.getClass().getClassLoader()); 106 server.registerMBean(mlet, defaultLoader); 107 108 if( log.isDebugEnabled()) 109 log.debug("Creating mbean server and loader "+ mlet + 110 " " + this.getClass().getClassLoader()); 111 } 112 project.addReference("jmx.server", server); 113 114 } catch( JMException ex ) { 116 log.error("Error creating server", ex); 117 } 118 119 if( log.isDebugEnabled()) log.debug("Using Mserver " + server ); 120 121 return server; 122 } 123 124 boolean modeler=false; 125 126 public void setModeler(boolean modeler) { 127 this.modeler = modeler; 128 } 129 130 protected void bindJmx(String objectName, String code, 131 String arg0, List args) 132 throws Exception 133 { 134 MBeanServer server=getMBeanServer(); 135 oname=new ObjectName ( objectName ); 136 if( modeler ) { 137 Arg codeArg=new Arg(); 138 codeArg.setType("java.lang.String"); 139 codeArg.setValue( code ); 140 if( args==null) args=new ArrayList (); 141 args.add(0, codeArg); 142 code="org.apache.commons.modeler.BaseModelMBean"; 143 } 144 145 Object argsA[]=new Object [ args.size()]; 146 String sigA[]=new String [args.size()]; 147 for( int i=0; i<args.size(); i++ ) { 148 Arg arg=(Arg)args.get(i); 149 if( arg.type==null ) 150 arg.type="java.lang.String"; 151 sigA[i]=arg.getType(); 152 argsA[i]=arg.getValue(); 153 } 155 156 if( args.size()==0 ) { 158 server.createMBean(code, oname); 159 } else { 160 server.createMBean(code, oname, argsA, sigA ); 161 } 162 log.debug( "Created MBEAN " + oname + " " + code); 163 } 164 165 public ObjectName getObjectName() { 166 return oname; 167 } 168 169 public void execute() throws BuildException { 170 try { 171 bindJmx( objectName, code, null, args); 173 174 for( int i=0; i<attributes.size(); i++ ) { 176 JmxSet att=(JmxSet)attributes.get(i); 177 att.setObjectName( oname ); 178 log.info("Setting attribute " + oname + " " + att.getName()); 179 att.execute(); 180 } 181 } catch(Exception ex) { 182 log.error("Can't create mbean " + objectName, ex); 183 } 184 } 185 186 } 187 | Popular Tags |