KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > modeler > ant > JmxInvoke


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 JavaDoc;
20
21 import javax.management.MBeanServer JavaDoc;
22 import javax.management.MBeanServerFactory JavaDoc;
23 import javax.management.ObjectName JavaDoc;
24
25 import org.apache.tools.ant.Task;
26
27 /**
28  * Set mbean properties.
29  *
30  */

31 public class JmxInvoke extends Task {
32     String JavaDoc objectName;
33
34     String JavaDoc method;
35     Vector JavaDoc args;
36
37     public JmxInvoke() {
38
39     }
40
41     public void setObjectName(String JavaDoc name) {
42         this.objectName = name;
43     }
44
45     public void setOperation(String JavaDoc method) {
46             this.method = method;
47     }
48
49     public void execute() {
50         try {
51             MBeanServer JavaDoc server=(MBeanServer JavaDoc)project.getReference("jmx.server");
52
53             if (server == null) {
54                 if( MBeanServerFactory.findMBeanServer(null).size() > 0 ) {
55                     server=(MBeanServer JavaDoc)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 JavaDoc oname=new ObjectName JavaDoc(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 JavaDoc argsA[]=new Object JavaDoc[ args.size()];
70                 String JavaDoc sigA[]=new String JavaDoc[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 JavaDoc ex) {
82             ex.printStackTrace();
83         }
84     }
85
86 }
87
Popular Tags