1 | // jademx - JADE management using JMX |
2 | // Copyright 2005 Caboodle Networks, Inc. |
3 | // |
4 | // This library is free software; you can redistribute it and/or |
5 | // modify it under the terms of the GNU Lesser General Public |
6 | // License as published by the Free Software Foundation; either |
7 | // version 2.1 of the License, or (at your option) any later version. |
8 | // |
9 | // This library is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | // Lesser General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU Lesser General Public |
15 | // License along with this library; if not, write to the Free Software |
16 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
17 | |
18 | package jade.jademx.server; |
19 | |
20 | import java.lang.reflect.Method; |
21 | import javax.management.MBeanServer; |
22 | |
23 | |
24 | /** |
25 | * JadeMXServer implemented by calling a static method. |
26 | * @author David Bernstein, <a href="http://www.caboodlenetworks.com" |
27 | * >Caboodle Networks, Inc.</a> |
28 | */ |
29 | public class StaticMethod implements JadeMXServer { |
30 | |
31 | /** the platform MBean server */ |
32 | private MBeanServer mBeanServer; |
33 | |
34 | /** class whose method to call to get MBeanServer */ |
35 | private String className; |
36 | |
37 | /** name of method to call to get MBeanServer */ |
38 | private String methodName; |
39 | |
40 | /** signature for method to call to get MBeanServer */ |
41 | private Class signature[]; |
42 | |
43 | /** arguments in call to get MBeanServer */ |
44 | private Object arguments[]; |
45 | |
46 | |
47 | |
48 | /** |
49 | * the JadeMXServer for getting MBeanServer via a static method call |
50 | * @param className name of class with method to call |
51 | * @param methodName name of method to call |
52 | * @param signature method signature |
53 | * @param arguments arguments to call method with |
54 | */ |
55 | protected StaticMethod( |
56 | String className, |
57 | String methodName, |
58 | Class signature[], |
59 | Object arguments[] ) { |
60 | |
61 | this.className = className; |
62 | this.methodName = methodName; |
63 | this.signature = signature; |
64 | this.arguments = arguments; |
65 | |
66 | try { |
67 | Class methodClass = Class.forName( className ); |
68 | Method method = methodClass.getMethod( methodName, signature ); |
69 | mBeanServer = (MBeanServer)method.invoke( null, arguments ); |
70 | } |
71 | catch ( Exception e ) { |
72 | throw new RuntimeException( |
73 | "exception calling static method to get MBeanServer: "+ |
74 | toString(), e ); |
75 | } |
76 | |
77 | |
78 | } |
79 | |
80 | /* (non-Javadoc) |
81 | * @see jade.jademx.server.JadeMXServer#getMBeanServer() |
82 | */ |
83 | public MBeanServer getMBeanServer() { |
84 | return mBeanServer; |
85 | } |
86 | |
87 | /* (non-Javadoc) |
88 | * @see java.lang.Object#toString() |
89 | */ |
90 | public String toString() { |
91 | StringBuffer sb = new StringBuffer(); |
92 | int parmCount = signature.length; |
93 | for ( int i = 0; i < parmCount; i++ ) { |
94 | if ( i > 0 ) { |
95 | sb.append(", "); |
96 | } |
97 | sb.append(signature[i].getCanonicalName()); |
98 | sb.append(" "); |
99 | sb.append(arguments[i].toString()); |
100 | } |
101 | return |
102 | getClass().getCanonicalName() + "[" + className + "." + |
103 | methodName + "(" + sb.toString() + ")]"; |
104 | } |
105 | |
106 | } |