Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 16118 invoked from network); 4 Sep 2005 05:37:59 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 4 Sep 2005 05:37:59 -0000 Received: (qmail 76234 invoked by uid 500); 4 Sep 2005 05:37:57 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 76210 invoked by uid 500); 4 Sep 2005 05:37:57 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 76197 invoked by uid 500); 4 Sep 2005 05:37:56 -0000 Received: (qmail 76194 invoked by uid 99); 4 Sep 2005 05:37:56 -0000 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Sat, 03 Sep 2005 22:37:56 -0700 Received: (qmail 16092 invoked by uid 65534); 4 Sep 2005 05:37:56 -0000 Message-ID: <20050904053756.16091.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r278561 - in /jakarta/commons/sandbox/proxy/trunk/src: java/org/apache/commons/proxy/factory/cglib/ java/org/apache/commons/proxy/factory/javassist/ java/org/apache/commons/proxy/factory/reflect/ java/org/apache/commons/proxy/factory/util/ ... Date: Sun, 04 Sep 2005 05:37:55 -0000 To: commons-cvs@jakarta.apache.org From: jcarman@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: jcarman Date: Sat Sep 3 22:37:49 2005 New Revision: 278561 URL: http://svn.apache.org/viewcvs?rev=278561&view=rev Log: Added support for "invocation handler proxies." Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ReflectionProxyFactory.java jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/util/AbstractProxyFactory.java jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/AbstractProxyFactoryTestCase.java Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java?rev=278561&r1=278560&r2=278561&view=diff ============================================================================== --- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java (original) +++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java Sat Sep 3 22:37:49 2005 @@ -26,6 +26,7 @@ import java.lang.reflect.AccessibleObject; import java.lang.reflect.Method; +import java.lang.reflect.InvocationHandler; /** * A CGLIB-based {@link org.apache.commons.proxy.ProxyFactory} @@ -56,6 +57,30 @@ return enhancer.create(); } + public Object createInvocationHandlerProxy( ClassLoader classLoader, InvocationHandler invocationHandler, + Class... proxyInterfaces ) + { + final Enhancer enhancer = new Enhancer(); + enhancer.setClassLoader( classLoader ); + enhancer.setInterfaces( proxyInterfaces ); + enhancer.setCallback( new InvocationHandlerBridge( invocationHandler ) ); + return enhancer.create(); + } + + private class InvocationHandlerBridge implements net.sf.cglib.proxy.InvocationHandler + { + private final InvocationHandler original; + + public InvocationHandlerBridge( InvocationHandler original ) + { + this.original = original; + } + + public Object invoke( Object object, Method method, Object[] objects ) throws Throwable + { + return original.invoke( object, method, objects ); + } + } private class InterceptorBridge implements net.sf.cglib.proxy.MethodInterceptor { private final MethodInterceptor inner; Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java?rev=278561&r1=278560&r2=278561&view=diff ============================================================================== --- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java (original) +++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java Sat Sep 3 22:37:49 2005 @@ -28,6 +28,7 @@ import org.apache.commons.proxy.factory.util.ProxyClassCache; import java.lang.reflect.Method; +import java.lang.reflect.InvocationHandler; /** * A Javassist-based {@link org.apache.commons.proxy.ProxyFactory} @@ -42,6 +43,7 @@ new DelegatingProxyClassGenerator() ); private static final ProxyClassCache interceptingProxyClassCache = new ProxyClassCache( new InterceptingProxyClassGenerator() ); + private static final ProxyClassCache invocationHandlerProxyClassCache = new ProxyClassCache( new InvocationHandlerProxyClassGenerator() ); public Object createInterceptingProxy( ClassLoader classLoader, Object target, MethodInterceptor interceptor, Class... proxyInterfaces ) @@ -71,6 +73,59 @@ catch( Exception e ) { throw new ProxyFactoryException( "Unable to instantiate proxy from generated proxy class.", e ); + } + } + + public Object createInvocationHandlerProxy( ClassLoader classLoader, InvocationHandler invocationHandler, + Class... proxyInterfaces ) + { + try + { + + final Class clazz = invocationHandlerProxyClassCache.getProxyClass( classLoader, proxyInterfaces ); + final Method[] methods = AbstractProxyClassGenerator.getImplementationMethods( proxyInterfaces ); + return clazz.getConstructor( Method[].class, InvocationHandler.class ).newInstance( methods, invocationHandler ); + } + catch( Exception e ) + { + throw new ProxyFactoryException( "Unable to instantiate proxy from generated proxy class.", e ); + } + } + + private static class InvocationHandlerProxyClassGenerator extends AbstractProxyClassGenerator + { + public Class generateProxyClass( ClassLoader classLoader, Class... proxyInterfaces ) + { + try + { + final CtClass proxyClass = JavassistUtils.createClass(); + final Method[] methods = getImplementationMethods( proxyInterfaces ); + JavassistUtils.addInterfaces( proxyClass, proxyInterfaces ); + JavassistUtils.addField( Method[].class, "methods", proxyClass ); + JavassistUtils.addField( InvocationHandler.class, "invocationHandler", proxyClass ); + final CtConstructor proxyConstructor = new CtConstructor( + JavassistUtils.resolve( + new Class[]{Method[].class, InvocationHandler.class} ), + proxyClass ); + proxyConstructor + .setBody( "{\n\tthis.methods = $1;\n\tthis.invocationHandler = $2; }" ); + proxyClass.addConstructor( proxyConstructor ); + for( int i = 0; i < methods.length; ++i ) + { + final CtMethod method = new CtMethod( JavassistUtils.resolve( methods[i].getReturnType() ), + methods[i].getName(), + JavassistUtils.resolve( methods[i].getParameterTypes() ), + proxyClass ); + final String body = "{\n\t return ( $r ) invocationHandler.invoke( this, methods[" + i + "], $args );\n }"; + method.setBody( body ); + proxyClass.addMethod( method ); + } + return proxyClass.toClass( classLoader ); + } + catch( CannotCompileException e ) + { + throw new ProxyFactoryException( "Could not compile class.", e ); + } } } Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ReflectionProxyFactory.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ReflectionProxyFactory.java?rev=278561&r1=278560&r2=278561&view=diff ============================================================================== --- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ReflectionProxyFactory.java (original) +++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ReflectionProxyFactory.java Sat Sep 3 22:37:49 2005 @@ -21,6 +21,7 @@ import org.apache.commons.proxy.factory.util.AbstractProxyFactory; import java.lang.reflect.Proxy; +import java.lang.reflect.InvocationHandler; /** * A JDK {@link java.lang.reflect.Proxy Proxy}-based {@link org.apache.commons.proxy.ProxyFactory} implementation. @@ -44,4 +45,9 @@ new DelegateProviderInvocationHandler( targetProvider ) ); } + public Object createInvocationHandlerProxy( ClassLoader classLoader, InvocationHandler invocationHandler, + Class... proxyInterfaces ) + { + return Proxy.newProxyInstance( classLoader, proxyInterfaces, invocationHandler ); + } } Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/util/AbstractProxyFactory.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/util/AbstractProxyFactory.java?rev=278561&r1=278560&r2=278561&view=diff ============================================================================== --- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/util/AbstractProxyFactory.java (original) +++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/util/AbstractProxyFactory.java Sat Sep 3 22:37:49 2005 @@ -22,7 +22,6 @@ import org.apache.commons.logging.LogFactory; import org.apache.commons.proxy.ObjectProvider; import org.apache.commons.proxy.ProxyFactory; -import org.apache.commons.proxy.provider.NullProvider; import java.lang.reflect.InvocationHandler; @@ -46,24 +45,18 @@ this.log = log; } - public Object createInterceptingProxy( Object target, MethodInterceptor interceptor, Class... proxyInterfaces ) + public final Object createInterceptingProxy( Object target, MethodInterceptor interceptor, Class... proxyInterfaces ) { return createInterceptingProxy( Thread.currentThread().getContextClassLoader(), target, interceptor, proxyInterfaces ); } - public Object createDelegatingProxy( ObjectProvider targetProvider, Class... proxyInterfaces ) + public final Object createDelegatingProxy( ObjectProvider targetProvider, Class... proxyInterfaces ) { return createDelegatingProxy( Thread.currentThread().getContextClassLoader(), targetProvider, proxyInterfaces ); } - public Object createInvocationHandlerProxy( ClassLoader classLoader, InvocationHandler invocationHandler, - Class... proxyInterfaces ) - { - return createInterceptingProxy( classLoader, new NullProvider(), new InvocationHandlerMethodInterceptor( invocationHandler ), proxyInterfaces ); - } - - public Object createInvocationHandlerProxy( InvocationHandler invocationHandler, Class... proxyInterfaces ) + public final Object createInvocationHandlerProxy( InvocationHandler invocationHandler, Class... proxyInterfaces ) { return createInvocationHandlerProxy( Thread.currentThread().getContextClassLoader(), invocationHandler, proxyInterfaces ); } Modified: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/AbstractProxyFactoryTestCase.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/AbstractProxyFactoryTestCase.java?rev=278561&r1=278560&r2=278561&view=diff ============================================================================== --- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/AbstractProxyFactoryTestCase.java (original) +++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/AbstractProxyFactoryTestCase.java Sat Sep 3 22:37:49 2005 @@ -60,6 +60,7 @@ final Echo echo = ( Echo )factory.createInvocationHandlerProxy( tester, Echo.class ); echo.echoBack( "hello" ); assertEquals( Echo.class.getMethod( "echoBack", String.class ), tester.method ); + assertSame( echo, tester.proxy ); assertNotNull( tester.args ); assertEquals( 1, tester.args.length ); assertEquals( "hello", tester.args[0] ); @@ -245,9 +246,11 @@ { private Object method; private Object[] args; + private Object proxy; public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable { + this.proxy = proxy; this.method = method; this.args = args; return null; --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org