commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jcar...@apache.org
Subject svn commit: r278555 - in /jakarta/commons/sandbox/proxy/trunk/src: java/org/apache/commons/proxy/ java/org/apache/commons/proxy/factory/util/ java/org/apache/commons/proxy/handler/ test/org/apache/commons/proxy/factory/
Date Sun, 04 Sep 2005 03:59:57 GMT
Author: jcarman
Date: Sat Sep  3 20:59:50 2005
New Revision: 278555

URL: http://svn.apache.org/viewcvs?rev=278555&view=rev
Log:
Added support for "invocation handler proxies."

Added:
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/handler/
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/handler/NullInvocationHandler.java
  (with props)
Modified:
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyFactory.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/ProxyFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyFactory.java?rev=278555&r1=278554&r2=278555&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyFactory.java
(original)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyFactory.java
Sat Sep  3 20:59:50 2005
@@ -18,6 +18,8 @@
 
 import org.aopalliance.intercept.MethodInterceptor;
 
+import java.lang.reflect.InvocationHandler;
+
 /**
  * A <code>ProxyFactory</code> essentially encapsulates a "proxying strategy."
 All <code>commons-proxy</code> proxies
  * are created using a <code>ProxyFactory</code>.  So, to change the proxying
strategy, simply provide a different
@@ -58,8 +60,8 @@
      * Creates a proxy which delegates to the object provided by <code>delegateProvider</code>.
 The proxy will be
      * generated using the current thread's "context class loader."
      *
-     * @param delegateProvider  the delegate provider
-     * @param proxyInterfaces the interfaces that the proxy should implement
+     * @param delegateProvider the delegate provider
+     * @param proxyInterfaces  the interfaces that the proxy should implement
      * @return a proxy which delegates to the object provided by the target object provider
      */
     public Object createDelegatingProxy( ObjectProvider delegateProvider, Class... proxyInterfaces
);
@@ -67,11 +69,32 @@
     /**
      * Creates a proxy which delegates to the object provided by <code>delegateProvider</code>.
      *
-     * @param classLoader     the class loader to use when generating the proxy
-     * @param delegateProvider  the delegate provider
-     * @param proxyInterfaces the interfaces that the proxy should implement
+     * @param classLoader      the class loader to use when generating the proxy
+     * @param delegateProvider the delegate provider
+     * @param proxyInterfaces  the interfaces that the proxy should implement
      * @return a proxy which delegates to the object provided by the target <code>delegateProvider>
      */
     public Object createDelegatingProxy( ClassLoader classLoader, ObjectProvider delegateProvider,
                                          Class... proxyInterfaces );
+
+    /**
+     * Creates a proxy which uses the provided {@link InvocationHandler} to handle all method
invocations.
+     *
+     * @param classLoader       the class loader to use when generating the proxy
+     * @param invocationHandler the invocation handler
+     * @param proxyInterfaces   the interfaces that the proxy should implement
+     * @return a proxy which uses the provided {@link InvocationHandler} to handle all method
invocations
+     */
+    public Object createInvocationHandlerProxy( ClassLoader classLoader, InvocationHandler
invocationHandler,
+                                                Class... proxyInterfaces );
+
+    /**
+     * Creates a proxy which uses the provided {@link InvocationHandler} to handle all method
invocations.  The proxy
+     * will be generated using the current thread's "context class loader."
+     *
+     * @param invocationHandler the invocation handler
+     * @param proxyInterfaces   the interfaces that the proxy should implement
+     * @return a proxy which uses the provided {@link InvocationHandler} to handle all method
invocations
+     */
+    public Object createInvocationHandlerProxy( InvocationHandler invocationHandler, Class...
proxyInterfaces );
 }

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=278555&r1=278554&r2=278555&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 20:59:50 2005
@@ -17,16 +17,14 @@
 package org.apache.commons.proxy.factory.util;
 
 import org.aopalliance.intercept.MethodInterceptor;
+import org.aopalliance.intercept.MethodInvocation;
 import org.apache.commons.logging.Log;
 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.Method;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
+import java.lang.reflect.InvocationHandler;
 
 /**
  * A helpful superclass for {@link org.apache.commons.proxy.ProxyFactory} implementations.
@@ -58,4 +56,32 @@
     {
         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 )
+    {
+        return createInvocationHandlerProxy( Thread.currentThread().getContextClassLoader(),
invocationHandler, proxyInterfaces );
+    }
+
+
+    private static class InvocationHandlerMethodInterceptor implements MethodInterceptor
+    {
+        private final InvocationHandler invocationHandler;
+
+        public InvocationHandlerMethodInterceptor( InvocationHandler invocationHandler )
+        {
+            this.invocationHandler = invocationHandler;
+        }
+
+        public Object invoke( MethodInvocation methodInvocation ) throws Throwable
+        {
+            return invocationHandler.invoke( methodInvocation.getThis(), methodInvocation.getMethod(),
methodInvocation.getArguments() );
+        }
+    }
+
 }

Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/handler/NullInvocationHandler.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/handler/NullInvocationHandler.java?rev=278555&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/handler/NullInvocationHandler.java
(added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/handler/NullInvocationHandler.java
Sat Sep  3 20:59:50 2005
@@ -0,0 +1,35 @@
+/* $Id$
+ *
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.proxy.handler;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+
+/**
+ * An {@link InvocationHandler} implementation which merely returns null for all method invocations.
 This class
+ * is useful for scenarios where the "null object" design pattern is needed.
+ *
+ * @author James Carman
+ * @version 1.0
+ */
+public class NullInvocationHandler implements InvocationHandler
+{
+    public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
+    {
+        return null;
+    }
+}

Propchange: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/handler/NullInvocationHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/handler/NullInvocationHandler.java
------------------------------------------------------------------------------
    svn:keywords = Id

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=278555&r1=278554&r2=278555&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 20:59:50 2005
@@ -20,17 +20,18 @@
 import org.apache.commons.proxy.ProxyFactory;
 import static org.apache.commons.proxy.provider.ProviderUtils.*;
 import org.apache.commons.proxy.util.AbstractTestCase;
+import org.apache.commons.proxy.util.DuplicateEcho;
 import org.apache.commons.proxy.util.Echo;
 import org.apache.commons.proxy.util.EchoImpl;
 import org.apache.commons.proxy.util.SuffixMethodInterceptor;
-import org.apache.commons.proxy.util.DuplicateEcho;
 
 import java.io.IOException;
 import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.util.Arrays;
-import java.util.List;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.SortedSet;
 import java.util.TreeSet;
 
@@ -53,6 +54,17 @@
         set.add( "Hello" );
     }
 
+    public void testInvocationHandlerProxy() throws Exception
+    {
+        final InvocationHandlerTester tester = new InvocationHandlerTester();
+        final Echo echo = ( Echo )factory.createInvocationHandlerProxy( tester, Echo.class
);
+        echo.echoBack( "hello" );
+        assertEquals( Echo.class.getMethod( "echoBack", String.class ), tester.method );
+        assertNotNull( tester.args );
+        assertEquals( 1, tester.args.length );
+        assertEquals( "hello", tester.args[0] );
+    }
+
     public void testDelegatingProxyInterfaceOrder()
     {
         final Echo echo = ( Echo ) factory.createDelegatingProxy( singletonProvider( beanProvider(
EchoImpl.class ) ), Echo.class, DuplicateEcho.class );
@@ -226,6 +238,19 @@
         public Object invoke( MethodInvocation methodInvocation ) throws Throwable
         {
             return methodInvocation.proceed();
+        }
+    }
+
+    private static class InvocationHandlerTester implements InvocationHandler
+    {
+        private Object method;
+        private Object[] args;
+
+        public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
+        {
+            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


Mime
View raw message