1 17 package org.apache.geronimo.kernel.jmx; 18 19 import java.util.IdentityHashMap ; 20 import javax.management.ObjectName ; 21 22 import net.sf.cglib.proxy.Callback; 23 import net.sf.cglib.proxy.Enhancer; 24 import net.sf.cglib.proxy.MethodInterceptor; 25 import org.apache.geronimo.kernel.Kernel; 26 import org.apache.geronimo.kernel.basic.ProxyMethodInterceptor; 27 import org.apache.geronimo.kernel.proxy.ProxyFactory; 28 import org.apache.geronimo.kernel.proxy.ProxyManager; 29 30 33 public class JMXProxyManager implements ProxyManager { 34 private final Kernel kernel; 35 private final IdentityHashMap interceptors = new IdentityHashMap (); 36 37 public JMXProxyManager(Kernel kernel) { 38 this.kernel = kernel; 39 } 40 41 public synchronized ProxyFactory createProxyFactory(Class type) { 42 assert type != null: "type is null"; 43 return new ManagedProxyFactory(type); 44 } 45 46 public synchronized Object createProxy(ObjectName target, Class type) { 47 assert type != null: "type is null"; 48 assert target != null: "target is null"; 49 50 return createProxyFactory(type).createProxy(target); 51 } 52 53 public synchronized void destroyProxy(Object proxy) { 54 if (proxy == null) { 55 return; 56 } 57 58 ProxyMethodInterceptor methodInterceptor = (ProxyMethodInterceptor) interceptors.remove(proxy); 59 if (methodInterceptor != null) { 60 methodInterceptor.destroy(); 61 } 62 } 63 64 public boolean isProxy(Object proxy) { 65 return interceptors.containsKey(proxy); 66 } 67 68 public synchronized ObjectName getProxyTarget(Object proxy) { 69 ProxyMethodInterceptor methodInterceptor = (ProxyMethodInterceptor) interceptors.remove(proxy); 70 if (methodInterceptor == null) { 71 return null; 72 } 73 return methodInterceptor.getObjectName(); 74 } 75 76 private class ManagedProxyFactory implements ProxyFactory { 77 private final Class type; 78 private final Enhancer enhancer; 79 80 public ManagedProxyFactory(Class type) { 81 enhancer = new Enhancer(); 82 enhancer.setSuperclass(type); 83 enhancer.setCallbackType(MethodInterceptor.class); 84 enhancer.setUseFactory(false); 85 this.type = enhancer.createClass(); 86 } 87 88 public synchronized Object createProxy(ObjectName target) { 89 assert target != null: "target is null"; 90 91 JMXProxyMethodInterceptor interceptor = new JMXProxyMethodInterceptor(type, kernel, target); 92 93 enhancer.setCallbacks(new Callback[]{interceptor}); 95 Object proxy = enhancer.create(); 96 97 interceptors.put(proxy, interceptor); 98 return proxy; 99 } 100 } 101 } 102 | Popular Tags |