You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by jc...@apache.org on 2005/08/23 05:02:05 UTC

svn commit: r239304 - 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/ test/org/apache/commons/proxy/interceptor/filter/

Author: jcarman
Date: Mon Aug 22 20:02:01 2005
New Revision: 239304

URL: http://svn.apache.org/viewcvs?rev=239304&view=rev
Log:
Adding FilteredMethodInterceptor

Added:
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/FilteredMethodInterceptor.java
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/MethodFilter.java
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/filter/SimpleMethodFilter.java
    jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestFilteredMethodInterceptor.java
    jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/filter/TestSimpleMethodFilter.java

Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/FilteredMethodInterceptor.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/FilteredMethodInterceptor.java?rev=239304&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/FilteredMethodInterceptor.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/FilteredMethodInterceptor.java Mon Aug 22 20:02:01 2005
@@ -0,0 +1,50 @@
+/*
+ *  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.aopalliance.intercept.MethodInvocation;
+
+/**
+ * Decorates another <code>MethodInterceptor</code> by only calling it if the
+ * method is accepted by the supplied <code>MethodFilter</code>.
+ *
+ * @author James Carman
+ * @version 1.0
+ */
+public class FilteredMethodInterceptor implements MethodInterceptor
+{
+    private final MethodInterceptor inner;
+    private final MethodFilter filter;
+
+    public FilteredMethodInterceptor( MethodInterceptor inner, MethodFilter filter )
+    {
+        this.inner = inner;
+        this.filter = filter;
+    }
+
+    public Object invoke( MethodInvocation methodInvocation ) throws Throwable
+    {
+        if( filter.accepts( methodInvocation.getMethod() ) )
+        {
+            return inner.invoke( methodInvocation );
+        }
+        else
+        {
+            return methodInvocation.proceed();
+        }
+    }
+}

Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/MethodFilter.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/MethodFilter.java?rev=239304&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/MethodFilter.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/MethodFilter.java Mon Aug 22 20:02:01 2005
@@ -0,0 +1,27 @@
+/*
+ *  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 java.lang.reflect.Method;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public interface MethodFilter
+{
+    public boolean accepts( Method method );
+}

Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/filter/SimpleMethodFilter.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/filter/SimpleMethodFilter.java?rev=239304&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/filter/SimpleMethodFilter.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/interceptor/filter/SimpleMethodFilter.java Mon Aug 22 20:02:01 2005
@@ -0,0 +1,42 @@
+/*
+ *  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.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class SimpleMethodFilter implements MethodFilter
+{
+    private final Set<String> methodNames;
+
+    public SimpleMethodFilter( String... methodNames )
+    {
+        this.methodNames = new HashSet<String>( Arrays.asList( methodNames ) );
+    }
+
+    public boolean accepts( Method method )
+    {
+        return methodNames.contains( method.getName() );
+    }
+}

Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestFilteredMethodInterceptor.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestFilteredMethodInterceptor.java?rev=239304&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestFilteredMethodInterceptor.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/TestFilteredMethodInterceptor.java Mon Aug 22 20:02:01 2005
@@ -0,0 +1,42 @@
+/*
+ *  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.apache.commons.proxy.util.Echo;
+import org.apache.commons.proxy.util.EchoImpl;
+import org.apache.commons.proxy.util.SuffixMethodInterceptor;
+import org.apache.commons.proxy.factory.cglib.CglibProxyFactory;
+import org.apache.commons.proxy.interceptor.filter.SimpleMethodFilter;
+import junit.framework.TestCase;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class TestFilteredMethodInterceptor extends TestCase
+{
+    public void testFilterAccepts()
+    {
+        Echo echo = ( Echo ) new MethodInterceptorChain( new FilteredMethodInterceptor( new SuffixMethodInterceptor( "a" ), new SimpleMethodFilter( "echoBack" ) ) ).createProxyProvider( new CglibProxyFactory(), new EchoImpl() ).getObject();
+        assertEquals( "messagea", echo.echoBack( "message" ) );
+    }
+
+    public void testFilterDenies()
+    {
+        Echo echo = ( Echo ) new MethodInterceptorChain( new FilteredMethodInterceptor( new SuffixMethodInterceptor( "a" ), new SimpleMethodFilter() ) ).createProxyProvider(  new CglibProxyFactory(), new EchoImpl() ).getObject();
+        assertEquals( "message", echo.echoBack( "message" ) );
+    }
+}

Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/filter/TestSimpleMethodFilter.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/filter/TestSimpleMethodFilter.java?rev=239304&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/filter/TestSimpleMethodFilter.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/interceptor/filter/TestSimpleMethodFilter.java Mon Aug 22 20:02:01 2005
@@ -0,0 +1,35 @@
+/*
+ *  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 junit.framework.TestCase;
+import org.apache.commons.proxy.util.Echo;
+import org.apache.commons.proxy.util.EchoImpl;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class TestSimpleMethodFilter extends TestCase
+{
+    public void testAccepts() throws Exception
+    {
+        final SimpleMethodFilter filter = new SimpleMethodFilter( "echoBack" );
+        assertTrue( filter.accepts( Echo.class.getMethod( "echoBack", String.class ) ) );
+        assertFalse( filter.accepts( EchoImpl.class.getMethod( "hashCode" ) ) );
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org