View Javadoc

1   /*
2    * Copyright 2000-2002,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
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  /***
28   * Set mbean properties.
29   *
30   */
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                  // XXX Use the loader ref, if any
69                  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                      // XXX Deal with not string types - IntrospectionUtils
78                  }
79                  server.invoke(oname, method, argsA, sigA);
80              }
81          } catch(Exception ex) {
82              ex.printStackTrace();
83          }
84      }
85  
86  }