|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| MBeanServerProxy.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 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;
|
|
| 10 |
import java.lang.reflect.Method;
|
|
| 11 |
import java.lang.reflect.Proxy;
|
|
| 12 |
|
|
| 13 |
import javax.management.MBeanServer;
|
|
| 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 |
|
| 22 |
{
|
|
| 23 |
/** The class to search the method on */
|
|
| 24 |
protected Class clazz = null; |
|
| 25 |
/** The real object */
|
|
| 26 |
protected Object object = null; |
|
| 27 |
/** Interface implemented by the proxy */
|
|
| 28 |
private final static Class[] INTERFACES = new Class[]{MBeanServer.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 | 0 |
public MBeanServerProxy(Object object, Class clazz)
|
| 38 |
{
|
|
| 39 | 0 |
this.object = object;
|
| 40 | 0 |
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 | 0 |
public Object invoke(Object obj, Method method, Object[] args)
|
| 54 |
throws Throwable
|
|
| 55 |
{
|
|
| 56 | 0 |
Method m = this.clazz.getMethod(method.getName(), method.getParameterTypes());
|
| 57 | 0 |
if (m != null) |
| 58 |
{
|
|
| 59 | 0 |
ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader(); |
| 60 |
|
|
| 61 | 0 |
try
|
| 62 |
{
|
|
| 63 | 0 |
Thread.currentThread().setContextClassLoader(this.clazz.getClassLoader());
|
| 64 | 0 |
return m.invoke(this.object, args); |
| 65 |
} |
|
| 66 |
finally
|
|
| 67 |
{
|
|
| 68 | 0 |
Thread.currentThread().setContextClassLoader(ctxLoader); |
| 69 |
} |
|
| 70 |
} |
|
| 71 | 0 |
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 | 0 |
public static MBeanServer createMBeanProxy(Object object) |
| 82 |
{
|
|
| 83 | 0 |
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 | 0 |
public static MBeanServer createMBeanProxy(Object object, Class clazz) |
| 95 |
{
|
|
| 96 | 0 |
MBeanServerProxy proxy = new MBeanServerProxy(object, clazz);
|
| 97 | 0 |
return (MBeanServer) Proxy.newProxyInstance(MBeanServerProxy.class.getClassLoader(), MBeanServerProxy.INTERFACES, proxy); |
| 98 |
} |
|
| 99 |
} |
|
| 100 |
|
|
||||||||||