KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > util > StandardMBeanProxy


1 /*
2  * Copyright (C) MX4J.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.util;
10
11 import java.lang.reflect.InvocationHandler JavaDoc;
12 import java.lang.reflect.Method JavaDoc;
13 import java.lang.reflect.Proxy JavaDoc;
14 import java.util.List JavaDoc;
15
16 import javax.management.Attribute JavaDoc;
17 import javax.management.MBeanException JavaDoc;
18 import javax.management.MBeanServer JavaDoc;
19 import javax.management.MBeanServerFactory JavaDoc;
20 import javax.management.ObjectName JavaDoc;
21 import javax.management.ReflectionException JavaDoc;
22 import javax.management.RuntimeErrorException JavaDoc;
23 import javax.management.RuntimeMBeanException JavaDoc;
24 import javax.management.RuntimeOperationsException JavaDoc;
25 import javax.management.InstanceNotFoundException JavaDoc;
26 import javax.management.AttributeNotFoundException JavaDoc;
27 import javax.management.InvalidAttributeValueException JavaDoc;
28
29 /**
30  * A utility class that creates proxies for invocation on standard MBeans (does not work for DynamicMBeans) on local MBeanServers. <br>
31  * Usage example:
32  * <pre>
33  * public interface MyServiceMBean {...}
34  * public class MyService implements MyServiceMBean {...}
35  * pulic class Main
36  * {
37  * public static void main(String[] args) throws Exception
38  * {
39  * MBeanServer server = ...;
40  * ObjectName myServiceObjectName = ...;
41  *
42  * MyServiceMBean mbean = (MyServiceMBean)StandardMBeanProxy.create(MyServiceMBean.class, server, myServiceObjectName);
43  *
44  * ...
45  * }
46  * }
47  * </pre>
48  *
49  * @author <a HREF="mailto:biorn_steedom@users.sourceforge.net">Simone Bordet</a>
50  * @version $Revision: 1.8 $
51  * @deprecated Replaced by {@link javax.management.MBeanServerInvocationHandler}
52  */

53 public class StandardMBeanProxy
54 {
55     /**
56      * Creates a proxy with the given MBean interface for an MBean with the specified name
57      * living in the MBeanServer returned by <code>MBeanServerFactory.findMBeanServer(null).get(0)</code>.
58      * @deprecated Replaced by {@link #create(Class mbeanInterface, MBeanServer server, ObjectName name)}
59      */

60     public static Object JavaDoc create(Class JavaDoc mbeanInterface, ObjectName JavaDoc name)
61     {
62         return create(mbeanInterface, null, name);
63     }
64
65     /**
66      * Creates a proxy with the given MBean interface for an MBean with the specified ObjectName
67      * living in the specified local MBeanServer. <br>
68      * Calling this method when the given ObjectName does not represent a registered MBean
69      * in the given MBeanServer results in an exception being thrown.
70      * If the MBean is unregistered after the proxy has been created, an attempt to call any method
71      * on the proxy will result in a {@link java.lang.reflect.UndeclaredThrowableException UndeclaredThrowableException}
72      * being thrown. MBeanServer's behavior would be to throw an InstanceNotFoundException, but this exception
73      * is normally not declared in the throws clause of MBean's management interface, thus resulting
74      * in the UndeclaredThrowableException being thrown instead.
75      */

76     public static Object JavaDoc create(Class JavaDoc mbeanInterface, MBeanServer JavaDoc server, ObjectName JavaDoc name)
77     {
78         if (mbeanInterface == null) throw new IllegalArgumentException JavaDoc("MBean interface cannot be null");
79         if (!mbeanInterface.isInterface()) throw new IllegalArgumentException JavaDoc("Class parameter must be an interface");
80         if (name == null) throw new IllegalArgumentException JavaDoc("MBean name cannot be null");
81
82         if (server == null)
83         {
84             List JavaDoc list = MBeanServerFactory.findMBeanServer(null);
85             if (list.size() > 0)
86                 server = (MBeanServer JavaDoc)list.get(0);
87             else
88                 throw new IllegalArgumentException JavaDoc("Cannot find MBeanServer");
89         }
90
91         // Do this check to reduce InstanceNotFoundExceptions, that are rethrown as UndeclaredThrowableException
92
// and thus are not easily caught by clients.
93
if (!server.isRegistered(name)) throw new IllegalArgumentException JavaDoc("ObjectName " + name + " is not known to MBeanServer " + server);
94
95         // The client must be able to cast the returned object to the mbeanInterface it passes,
96
// so the classloader must be the same
97
ClassLoader JavaDoc loader = mbeanInterface.getClassLoader();
98         return Proxy.newProxyInstance(loader, new Class JavaDoc[]{mbeanInterface}, new LocalHandler(server, name));
99     }
100
101     /**
102      * Base class for MBeanServer invocation handlers. <br>
103      * It handles the translation of method invocation
104      * from the definition of reflection (java.lang.reflect.Method) to
105      * the definition of JMX (method name + signature)
106      */

107     protected static abstract class Handler implements InvocationHandler JavaDoc
108     {
109         protected Handler()
110         {
111         }
112
113         public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc
114         {
115             // Needed for no-parameter methods
116
if (args == null) args = new Object JavaDoc[0];
117
118             // No need to check for consistency between the signature and the args parameter,
119
// since the invocation it is not done by reflection, but statically
120

121             Class JavaDoc[] declared = method.getExceptionTypes();
122
123             if (Utils.isAttributeSetter(method))
124             {
125                 String JavaDoc name = method.getName().substring(3);
126                 Attribute JavaDoc attribute = new Attribute JavaDoc(name, args[0]);
127                 try
128                 {
129                     setAttribute(attribute);
130                     return null;
131                 }
132                 catch (Throwable JavaDoc x)
133                 {
134                     unwrapThrowable(x, declared);
135                 }
136             }
137             else if (Utils.isAttributeGetter(method))
138             {
139                 String JavaDoc n = method.getName();
140                 String JavaDoc name = null;
141                 if (n.startsWith("is"))
142                     name = n.substring(2);
143                 else
144                     name = n.substring(3);
145
146                 try
147                 {
148                     return getAttribute(name);
149                 }
150                 catch (Throwable JavaDoc x)
151                 {
152                     unwrapThrowable(x, declared);
153                 }
154             }
155             else
156             {
157                 Class JavaDoc[] parameters = method.getParameterTypes();
158                 String JavaDoc[] params = new String JavaDoc[parameters.length];
159                 for (int i = 0; i < parameters.length; ++i)
160                 {
161                     params[i] = parameters[i].getName();
162                 }
163
164                 try
165                 {
166                     return invokeOperation(method.getName(), args, params);
167                 }
168                 catch (Throwable JavaDoc x)
169                 {
170                     unwrapThrowable(x, declared);
171                 }
172             }
173
174             return null;
175         }
176
177         /**
178          * Called to invoke setAttribute on a (possibly remote) MBeanServer.
179          */

180         protected abstract void setAttribute(Attribute JavaDoc attribute) throws Exception JavaDoc;
181
182         /**
183          * Called to invoke getAttribute on a (possibly remote) MBeanServer.
184          */

185         protected abstract Object JavaDoc getAttribute(String JavaDoc attribute) throws Exception JavaDoc;
186
187         /**
188          * Called to invoke invoke on a (possibly remote) MBeanServer.
189          */

190         protected abstract Object JavaDoc invokeOperation(String JavaDoc method, Object JavaDoc[] args, String JavaDoc[] params) throws Exception JavaDoc;
191
192         /**
193          * Rethrows as is the given throwable, if it is an instance of one of the given <code>declared</code> classes,
194          * ot tries to (recursively) unwrap it and rethrow it.
195          */

196         protected void unwrapThrowable(Throwable JavaDoc x, Class JavaDoc[] declared) throws Throwable JavaDoc
197         {
198             if (declared != null)
199             {
200                 // See if the exception is declared by the method
201
// If so, just rethrow it.
202
for (int i = 0; i < declared.length; ++i)
203                 {
204                     Class JavaDoc exception = declared[i];
205                     if (exception.isInstance(x)) throw x;
206                 }
207             }
208
209             // The exception is not declared, try to unwrap it
210
if (x instanceof MBeanException JavaDoc)
211             {
212                 unwrapThrowable(((MBeanException JavaDoc)x).getTargetException(), declared);
213             }
214             else if (x instanceof ReflectionException JavaDoc)
215             {
216                 unwrapThrowable(((ReflectionException JavaDoc)x).getTargetException(), declared);
217             }
218             else if (x instanceof RuntimeOperationsException JavaDoc)
219             {
220                 unwrapThrowable(((RuntimeOperationsException JavaDoc)x).getTargetException(), declared);
221             }
222             else if (x instanceof RuntimeMBeanException JavaDoc)
223             {
224                 unwrapThrowable(((RuntimeMBeanException JavaDoc)x).getTargetException(), declared);
225             }
226             else if (x instanceof RuntimeErrorException JavaDoc)
227             {
228                 unwrapThrowable(((RuntimeErrorException JavaDoc)x).getTargetError(), declared);
229             }
230             else
231             {
232                 // Rethrow as is. Since this exception is not declared by the methods of the interface,
233
// if it is checked will be throws as UndclaredThrowableException, if it is unchecked
234
// will be rethrown as is.
235
throw x;
236             }
237         }
238     }
239
240     private static class LocalHandler extends Handler JavaDoc
241     {
242         private MBeanServer JavaDoc m_server;
243         private ObjectName JavaDoc m_name;
244
245         private LocalHandler(MBeanServer JavaDoc server, ObjectName JavaDoc name)
246         {
247             m_server = server;
248             m_name = name;
249         }
250
251         protected void setAttribute(Attribute JavaDoc attribute) throws InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
252         {
253             m_server.setAttribute(m_name, attribute);
254         }
255
256         protected Object JavaDoc getAttribute(String JavaDoc attribute) throws InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
257         {
258             return m_server.getAttribute(m_name, attribute);
259         }
260
261         protected Object JavaDoc invokeOperation(String JavaDoc method, Object JavaDoc[] args, String JavaDoc[] params) throws InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
262         {
263             return m_server.invoke(m_name, method, args, params);
264         }
265     }
266 }
267
Popular Tags