commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jcar...@apache.org
Subject svn commit: r278760 - in /jakarta/commons/sandbox/proxy/trunk/src: java/org/apache/commons/proxy/interceptor/ java/org/apache/commons/proxy/interceptor/filter/ test/org/apache/commons/proxy/interceptor/
Date Mon, 05 Sep 2005 14:51:14 GMT
Author: jcarman
Date: Mon Sep  5 07:51:08 2005
New Revision: 278760

URL: http://svn.apache.org/viewcvs?rev=278760&view=rev
Log:
Implementing ExecutorMethodInterceptor

Added:
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/AbstractMethodInterceptor.java
  (with props)
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/ExecutorMethodInterceptor.java
  (with props)
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/filter/ReturnTypeFilter.java
  (with props)
    jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestExecutorMethodInterceptor.java
  (with props)

Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/AbstractMethodInterceptor.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/AbstractMethodInterceptor.java?rev=278760&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/AbstractMethodInterceptor.java
(added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/AbstractMethodInterceptor.java
Mon Sep  5 07:51:08 2005
@@ -0,0 +1,40 @@
+/* $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.interceptor;
+
+import org.aopalliance.intercept.MethodInterceptor;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public abstract class AbstractMethodInterceptor implements MethodInterceptor
+{
+    protected Log log = LogFactory.getLog( getClass() );
+
+    public Log getLog()
+    {
+        return log;
+    }
+
+    public void setLog( Log log )
+    {
+        this.log = log;
+    }
+}

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

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

Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/ExecutorMethodInterceptor.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/ExecutorMethodInterceptor.java?rev=278760&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/ExecutorMethodInterceptor.java
(added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/ExecutorMethodInterceptor.java
Mon Sep  5 07:51:08 2005
@@ -0,0 +1,66 @@
+/* $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.interceptor;
+
+import org.aopalliance.intercept.MethodInvocation;
+
+import java.util.concurrent.Executor;
+
+/**
+ * A method interceptor that uses an {@link Executor} to execute the method invocation.
+ * <p />
+ * <b>Note</b>: Only <em>void</em> methods can be intercepted using
this class!  Any attempts to intercept
+ * non-void methods will result in an {@link IllegalArgumentException}.  If the proxy interfaces
include non-void
+ * methods, try using a {@link FilteredMethodInterceptor} to wrap an instance of this class.
+ * @author James Carman
+ * @version 1.0
+ */
+public class ExecutorMethodInterceptor extends AbstractMethodInterceptor
+{
+    private final Executor executor;
+
+    public ExecutorMethodInterceptor( Executor executor )
+    {
+        this.executor = executor;
+    }
+
+    public Object invoke( final MethodInvocation methodInvocation ) throws Throwable
+    {
+        if( Void.TYPE.equals( methodInvocation.getMethod().getReturnType() ) )
+        {
+            executor.execute( new Runnable()
+            {
+                public void run()
+                {
+                    try
+                    {
+                        methodInvocation.proceed();
+                    }
+                    catch( Throwable t )
+                    {
+                        getLog().error( "Method invocation threw an exception.", t );
+                    }
+                }
+            } );
+            return null;
+        }
+        else
+        {
+            throw new IllegalArgumentException( "Only void methods can be executed in a different
thread." );
+        }
+    }
+}

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

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

Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/filter/ReturnTypeFilter.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/filter/ReturnTypeFilter.java?rev=278760&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/filter/ReturnTypeFilter.java
(added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/filter/ReturnTypeFilter.java
Mon Sep  5 07:51:08 2005
@@ -0,0 +1,50 @@
+/* $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.interceptor.filter;
+
+import org.apache.commons.proxy.interceptor.MethodFilter;
+
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Arrays;
+import java.util.Collection;
+import java.lang.reflect.Method;
+
+/**
+ * Filters methods based on their return type.
+ * @author James Carman
+ * @version 1.0
+ */
+public class ReturnTypeFilter implements MethodFilter
+{
+    private final Set<Class> validReturnTypes = new HashSet<Class>();
+
+    public ReturnTypeFilter( Class... validReturnTypes )
+    {
+        this( Arrays.asList( validReturnTypes ) );
+    }
+
+    public ReturnTypeFilter( Collection<Class> validReturnTypes )
+    {
+        this.validReturnTypes.addAll( validReturnTypes );
+    }
+
+    public boolean accepts( Method method )
+    {
+        return validReturnTypes.contains( method.getReturnType() );
+    }
+}

Propchange: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/filter/ReturnTypeFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/filter/ReturnTypeFilter.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestExecutorMethodInterceptor.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestExecutorMethodInterceptor.java?rev=278760&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestExecutorMethodInterceptor.java
(added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestExecutorMethodInterceptor.java
Mon Sep  5 07:51:08 2005
@@ -0,0 +1,105 @@
+/* $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.interceptor;
+
+import junit.framework.TestCase;
+import org.apache.commons.proxy.factory.cglib.CglibProxyFactory;
+import org.apache.commons.proxy.util.Echo;
+import org.apache.commons.proxy.util.EchoImpl;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Executor;
+
+public class TestExecutorMethodInterceptor extends TestCase
+{
+    public void testVoidMethod() throws Exception
+    {
+        final ExecutedEcho impl = new ExecutedEcho();
+        final OneShotExecutor executor = new OneShotExecutor();
+        final Echo proxy = ( Echo ) new CglibProxyFactory()
+                .createInterceptorProxy( impl, new ExecutorMethodInterceptor( executor ),
Echo.class );
+        proxy.echo();
+        executor.getLatch().await();
+        assertEquals( executor.getThread(), impl.getExecutionThread() );
+    }
+
+    public void testNonVoidMethod() throws Exception
+    {
+        final ExecutedEcho impl = new ExecutedEcho();
+        final OneShotExecutor executor = new OneShotExecutor();
+        final Echo proxy = ( Echo ) new CglibProxyFactory()
+                .createInterceptorProxy( impl, new ExecutorMethodInterceptor( executor ),
Echo.class );
+        try
+        {
+            proxy.echoBack( "hello" );
+            fail();
+        }
+        catch( IllegalArgumentException e )
+        {
+        }
+    }
+
+    public static class ExecutedEcho extends EchoImpl
+    {
+        private Thread executionThread;
+
+        public void echo()
+        {
+            executionThread = Thread.currentThread();
+        }
+
+        public Thread getExecutionThread()
+        {
+            return executionThread;
+        }
+    }
+
+    private static class OneShotExecutor implements Executor
+    {
+        private Thread thread;
+        private CountDownLatch latch = new CountDownLatch( 1 );
+
+        public void execute( final Runnable command )
+        {
+            thread = new Thread( new Runnable()
+            {
+                public void run()
+                {
+                    try
+                    {
+                        command.run();
+                    }
+                    finally
+                    {
+                        latch.countDown();
+                    }
+                }
+            } );
+            thread.start();
+        }
+
+        public Thread getThread()
+        {
+            return thread;
+        }
+
+        public CountDownLatch getLatch()
+        {
+            return latch;
+        }
+    }
+}
\ No newline at end of file

Propchange: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestExecutorMethodInterceptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestExecutorMethodInterceptor.java
------------------------------------------------------------------------------
    svn:keywords = Id



---------------------------------------------------------------------
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