KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > jmx > MBeanServerProxy


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.jmx;
8
9 import java.lang.reflect.InvocationHandler JavaDoc;
10 import java.lang.reflect.Method JavaDoc;
11 import java.lang.reflect.Proxy JavaDoc;
12
13 import javax.management.MBeanServer JavaDoc;
14
15 /**
16  * A proxy for object that provides MBeanServer fonctionnality
17  *
18  * @author Laurent Etiemble
19  * @version $Revision: 1.8 $
20  */

21 public class MBeanServerProxy implements InvocationHandler JavaDoc
22 {
23    /** The class to search the method on */
24    protected Class JavaDoc clazz = null;
25    /** The real object */
26    protected Object JavaDoc object = null;
27    /** Interface implemented by the proxy */
28    private final static Class JavaDoc[] INTERFACES = new Class JavaDoc[]{MBeanServer JavaDoc.class};
29
30
31    /**
32     * Constructor for MBeanServerProxy.
33     *
34     * @param object Real object to proxy
35     * @param clazz Class of the proxied object
36     */

37    public MBeanServerProxy(Object JavaDoc object, Class JavaDoc clazz)
38    {
39       this.object = object;
40       this.clazz = clazz;
41    }
42
43
44    /**
45     * Invocation method. Delegates the method call to the real object.
46     *
47     * @param method Method called
48     * @param args Method arguments
49     * @param obj The method on which the invocation was made
50     * @return The return value of the method
51     * @exception Throwable In case of Exception
52     */

53    public Object JavaDoc invoke(Object JavaDoc obj, Method JavaDoc method, Object JavaDoc[] args)
54       throws Throwable JavaDoc
55    {
56       Method JavaDoc m = this.clazz.getMethod(method.getName(), method.getParameterTypes());
57       if (m != null)
58       {
59          ClassLoader JavaDoc ctxLoader = Thread.currentThread().getContextClassLoader();
60
61          try
62          {
63             Thread.currentThread().setContextClassLoader(this.clazz.getClassLoader());
64             return m.invoke(this.object, args);
65          }
66          finally
67          {
68             Thread.currentThread().setContextClassLoader(ctxLoader);
69          }
70       }
71       return null;
72    }
73
74
75    /**
76     * Factory method to create a proxy
77     *
78     * @param object Real object to proxy
79     * @return The proxy created
80     */

81    public static MBeanServer JavaDoc createMBeanProxy(Object JavaDoc object)
82    {
83       return MBeanServerProxy.createMBeanProxy(object, object.getClass());
84    }
85
86
87    /**
88     * Factory method to create a proxy
89     *
90     * @param object Real object to proxy
91     * @param clazz Class of the proxied object
92     * @return The proxy created
93     */

94    public static MBeanServer JavaDoc createMBeanProxy(Object JavaDoc object, Class JavaDoc clazz)
95    {
96       MBeanServerProxy proxy = new MBeanServerProxy(object, clazz);
97       return (MBeanServer JavaDoc) Proxy.newProxyInstance(MBeanServerProxy.class.getClassLoader(), MBeanServerProxy.INTERFACES, proxy);
98    }
99 }
100
Popular Tags