01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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
53: .getReference("jmx.server");
54:
55: if (server == null) {
56: if (MBeanServerFactory.findMBeanServer(null).size() > 0) {
57: server = (MBeanServer) MBeanServerFactory
58: .findMBeanServer(null).get(0);
59: } else {
60: System.out.println("Creating mbean server");
61: server = MBeanServerFactory.createMBeanServer();
62: }
63: project.addReference("jmx.server", server);
64: }
65:
66: ObjectName oname = new ObjectName(objectName);
67:
68: if (args == null) {
69: server.invoke(oname, method, null, null);
70: } else {
71: // XXX Use the loader ref, if any
72: Object argsA[] = new Object[args.size()];
73: String sigA[] = new String[args.size()];
74: for (int i = 0; i < args.size(); i++) {
75: Arg arg = (Arg) args.elementAt(i);
76: if (arg.type == null)
77: arg.type = "java.lang.String";
78: sigA[i] = arg.getType();
79: argsA[i] = arg.getValue();
80: // XXX Deal with not string types - IntrospectionUtils
81: }
82: server.invoke(oname, method, argsA, sigA);
83: }
84: } catch (Exception ex) {
85: ex.printStackTrace();
86: }
87: }
88:
89: }
|