1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.modeler.ant;
19
20 import java.util.Vector;
21
22 import javax.management.MBeanServer;
23 import javax.management.MBeanServerFactory;
24 import javax.management.ObjectName;
25
26 import org.apache.tools.ant.Task;
27
28 /***
29 * Set mbean properties.
30 *
31 */
32 public class JmxInvoke extends Task {
33 String objectName;
34
35 String method;
36 Vector args;
37
38 public JmxInvoke() {
39
40 }
41
42 public void setObjectName(String name) {
43 this.objectName = name;
44 }
45
46 public void setOperation(String method) {
47 this.method = method;
48 }
49
50 public void execute() {
51 try {
52 MBeanServer server=(MBeanServer)project.getReference("jmx.server");
53
54 if (server == null) {
55 if( MBeanServerFactory.findMBeanServer(null).size() > 0 ) {
56 server=(MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);
57 } else {
58 System.out.println("Creating mbean server");
59 server=MBeanServerFactory.createMBeanServer();
60 }
61 project.addReference("jmx.server", server);
62 }
63
64 ObjectName oname=new ObjectName(objectName);
65
66 if( args==null ) {
67 server.invoke(oname, method, null, null);
68 } else {
69 // XXX Use the loader ref, if any
70 Object argsA[]=new Object[ args.size()];
71 String sigA[]=new String[args.size()];
72 for( int i=0; i<args.size(); i++ ) {
73 Arg arg=(Arg)args.elementAt(i);
74 if( arg.type==null )
75 arg.type="java.lang.String";
76 sigA[i]=arg.getType();
77 argsA[i]=arg.getValue();
78 // XXX Deal with not string types - IntrospectionUtils
79 }
80 server.invoke(oname, method, argsA, sigA);
81 }
82 } catch(Exception ex) {
83 ex.printStackTrace();
84 }
85 }
86
87 }