1 17 18 package org.apache.avalon.fortress.impl.factory; 19 20 import org.apache.avalon.framework.component.Component; 21 import org.apache.excalibur.mpool.ObjectFactory; 22 23 import java.lang.reflect.Proxy ; 24 25 32 public final class ProxyObjectFactory extends AbstractObjectFactory 33 { 34 40 public ProxyObjectFactory( final ObjectFactory objectFactory ) throws NullPointerException 41 { 42 super( objectFactory ); 43 } 44 45 51 public Object newInstance() 52 throws Exception 53 { 54 final Object object = m_delegateFactory.newInstance(); 55 return createProxy( object ); 56 } 57 58 65 public void dispose( final Object object ) 66 throws Exception 67 { 68 final Object target = getObject( object ); 69 m_delegateFactory.dispose( target ); 70 } 71 72 77 public static Component createProxy( final Object service ) 78 { 79 final Class clazz = service.getClass(); 80 final Class [] workInterfaces = guessWorkInterfaces( clazz ); 81 82 return (Component) Proxy.newProxyInstance( clazz.getClassLoader(), 83 workInterfaces, 84 new PassThroughInvocationHandler( service ) ); 85 } 86 87 95 public static Object getObject( final Object proxy ) 96 97 { 98 if ( null == proxy ) 99 { 100 throw new NullPointerException ( "proxy" ); 101 } 102 103 if ( !Proxy.isProxyClass( proxy.getClass() ) ) 104 { 105 final String message = "object is not a proxy"; 106 throw new IllegalArgumentException ( message ); 107 } 108 109 final PassThroughInvocationHandler handler = 110 (PassThroughInvocationHandler) Proxy.getInvocationHandler( proxy ); 111 return handler.getObject(); 112 } 113 } 114 | Popular Tags |