1 6 7 package org.jfox.pool; 8 9 import java.lang.reflect.InvocationHandler ; 10 import java.lang.reflect.Proxy ; 11 12 15 16 public class ProxyObjectFactory extends SimpleObjectFactory { 17 18 protected Class invokerClass = null; 19 20 26 public ProxyObjectFactory(Class classType) throws Exception { 27 super(classType); 28 this.invokerClass = PoolableObjectInvocationHandler.class; 29 } 30 31 public ProxyObjectFactory(Class classType, Class invokerClass) throws Exception { 32 super(classType); 33 if(PoolableObjectInvocationHandler.class.isAssignableFrom(invokerClass)) { 34 this.invokerClass = invokerClass; 35 } 36 else { 37 throw new Exception (invokerClass.getName() + " not implements " + PoolableObjectInvocationHandler.class.getName()); 38 } 39 } 40 41 public boolean validateObject(PoolableObject object) { 42 if(Proxy.isProxyClass(object.getClass())) { 43 object = ((PoolableObjectInvocationHandler) Proxy.getInvocationHandler(object)).getPoolableObject(); 44 } 45 return super.validateObject(object); 46 } 47 48 public PoolableObject makeObject() throws Exception { 49 PoolableObject obj = (PoolableObject) classType.newInstance(); 51 InvocationHandler handler = (InvocationHandler ) invokerClass.getConstructor(new Class []{PoolableObject.class}).newInstance(new Object []{obj}); 53 54 return (PoolableObject) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), 55 obj.getClass().getInterfaces(), 56 handler); 57 58 } 59 60 public void destroyObject(PoolableObject object) throws Exception { 61 if(Proxy.isProxyClass(object.getClass())) { 62 object = ((PoolableObjectInvocationHandler) Proxy.getInvocationHandler(object)).getPoolableObject(); 63 } 64 object.passivate(); 65 super.destroyObject(object); 66 } 67 68 } 69 | Popular Tags |