Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
work in progress.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/proxy/branches/version-2.0-work@964636 13f79535-47bb-0310-9956-ffa450edef68
- Loading branch information
Showing
49 changed files
with
4,599 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>commons-proxy</artifactId> | ||
<groupId>org.apache.commons</groupId> | ||
<version>2.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>commons-proxy-cglib</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>commons-proxy-core</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>cglib</groupId> | ||
<artifactId>cglib-nodep</artifactId> | ||
<version>2.1_3</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>commons-proxy-core</artifactId> | ||
<version>${project.version}</version> | ||
<type>test-jar</type> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-lang</groupId> | ||
<artifactId>commons-lang</artifactId> | ||
<version>2.3</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
package org.apache.commons.proxy.cglib; | ||
|
||
import net.sf.cglib.proxy.Callback; | ||
import net.sf.cglib.proxy.CallbackFilter; | ||
import net.sf.cglib.proxy.Dispatcher; | ||
import net.sf.cglib.proxy.Enhancer; | ||
import net.sf.cglib.proxy.MethodInterceptor; | ||
import net.sf.cglib.proxy.MethodProxy; | ||
import org.apache.commons.proxy.Interceptor; | ||
import org.apache.commons.proxy.Invocation; | ||
import org.apache.commons.proxy.Invoker; | ||
import org.apache.commons.proxy.ObjectProvider; | ||
import org.apache.commons.proxy.impl.AbstractSubclassingProxyFactory; | ||
|
||
import java.io.Serializable; | ||
import java.lang.reflect.Method; | ||
|
||
public class CglibProxyFactory extends AbstractSubclassingProxyFactory | ||
{ | ||
//********************************************************************************************************************** | ||
// Fields | ||
//********************************************************************************************************************** | ||
|
||
private static CallbackFilter callbackFilter = new CglibProxyFactoryCallbackFilter(); | ||
|
||
//********************************************************************************************************************** | ||
// ProxyFactory Implementation | ||
//********************************************************************************************************************** | ||
|
||
public Object createDelegatorProxy(ClassLoader classLoader, ObjectProvider targetProvider, | ||
Class... proxyClasses) | ||
{ | ||
final Enhancer enhancer = new Enhancer(); | ||
enhancer.setClassLoader(classLoader); | ||
enhancer.setInterfaces(toInterfaces(proxyClasses)); | ||
enhancer.setSuperclass(getSuperclass(proxyClasses)); | ||
enhancer.setCallbackFilter(callbackFilter); | ||
enhancer.setCallbacks(new Callback[]{new ObjectProviderDispatcher(targetProvider), new EqualsHandler(), new HashCodeHandler()}); | ||
return enhancer.create(); | ||
} | ||
|
||
public Object createInterceptorProxy(ClassLoader classLoader, Object target, Interceptor interceptor, | ||
Class... proxyClasses) | ||
{ | ||
final Enhancer enhancer = new Enhancer(); | ||
enhancer.setClassLoader(classLoader); | ||
enhancer.setInterfaces(toInterfaces(proxyClasses)); | ||
enhancer.setSuperclass(getSuperclass(proxyClasses)); | ||
enhancer.setCallbackFilter(callbackFilter); | ||
enhancer.setCallbacks(new Callback[]{new InterceptorBridge(target, interceptor), new EqualsHandler(), new HashCodeHandler()}); | ||
return enhancer.create(); | ||
} | ||
|
||
public Object createInvokerProxy(ClassLoader classLoader, Invoker invoker, | ||
Class... proxyClasses) | ||
{ | ||
final Enhancer enhancer = new Enhancer(); | ||
enhancer.setClassLoader(classLoader); | ||
enhancer.setInterfaces(toInterfaces(proxyClasses)); | ||
enhancer.setSuperclass(getSuperclass(proxyClasses)); | ||
enhancer.setCallbackFilter(callbackFilter); | ||
enhancer.setCallbacks(new Callback[]{new InvokerBridge(invoker), new EqualsHandler(), new HashCodeHandler()}); | ||
return enhancer.create(); | ||
} | ||
|
||
//********************************************************************************************************************** | ||
// Inner Classes | ||
//********************************************************************************************************************** | ||
|
||
private static class CglibProxyFactoryCallbackFilter implements CallbackFilter | ||
{ | ||
public int accept(Method method) | ||
{ | ||
if (isEqualsMethod(method)) | ||
{ | ||
return 1; | ||
} | ||
else if (isHashCode(method)) | ||
{ | ||
return 2; | ||
} | ||
else | ||
{ | ||
return 0; | ||
} | ||
} | ||
} | ||
|
||
private static class EqualsHandler implements MethodInterceptor, Serializable | ||
{ | ||
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable | ||
{ | ||
return o == objects[0]; | ||
} | ||
} | ||
|
||
private static class HashCodeHandler implements MethodInterceptor, Serializable | ||
{ | ||
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable | ||
{ | ||
return System.identityHashCode(o); | ||
} | ||
} | ||
|
||
private static class InterceptorBridge implements net.sf.cglib.proxy.MethodInterceptor, Serializable | ||
{ | ||
private final Interceptor inner; | ||
private final Object target; | ||
|
||
public InterceptorBridge(Object target, Interceptor inner) | ||
{ | ||
this.inner = inner; | ||
this.target = target; | ||
} | ||
|
||
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable | ||
{ | ||
return inner.intercept(new MethodProxyInvocation(target, method, args, methodProxy)); | ||
} | ||
} | ||
|
||
private static class InvokerBridge implements net.sf.cglib.proxy.InvocationHandler, Serializable | ||
{ | ||
private final Invoker original; | ||
|
||
public InvokerBridge(Invoker original) | ||
{ | ||
this.original = original; | ||
} | ||
|
||
public Object invoke(Object object, Method method, Object[] objects) throws Throwable | ||
{ | ||
return original.invoke(object, method, objects); | ||
} | ||
} | ||
|
||
private static class MethodProxyInvocation implements Invocation, Serializable | ||
{ | ||
private final MethodProxy methodProxy; | ||
private final Method method; | ||
private final Object[] args; | ||
private final Object target; | ||
|
||
public MethodProxyInvocation(Object target, Method method, Object[] args, MethodProxy methodProxy) | ||
{ | ||
this.target = target; | ||
this.method = method; | ||
this.methodProxy = methodProxy; | ||
this.args = args; | ||
} | ||
|
||
public Method getMethod() | ||
{ | ||
return method; | ||
} | ||
|
||
public Object[] getArguments() | ||
{ | ||
return args; | ||
} | ||
|
||
public Object proceed() throws Throwable | ||
{ | ||
return methodProxy.invoke(target, args); | ||
} | ||
|
||
public Object getProxy() | ||
{ | ||
return target; | ||
} | ||
} | ||
|
||
private static class ObjectProviderDispatcher implements Dispatcher, Serializable | ||
{ | ||
private final ObjectProvider delegateProvider; | ||
|
||
public ObjectProviderDispatcher(ObjectProvider delegateProvider) | ||
{ | ||
this.delegateProvider = delegateProvider; | ||
} | ||
|
||
public Object loadObject() | ||
{ | ||
return delegateProvider.getObject(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.apache.commons.proxy.cglib; | ||
|
||
import org.apache.commons.proxy.AbstractSubclassingProxyFactoryTestCase; | ||
|
||
public class TestCglibProxyFactory extends AbstractSubclassingProxyFactoryTestCase | ||
{ | ||
//********************************************************************************************************************** | ||
// Constructors | ||
//********************************************************************************************************************** | ||
|
||
public TestCglibProxyFactory() | ||
{ | ||
super(new CglibProxyFactory()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>commons-proxy</artifactId> | ||
<groupId>org.apache.commons</groupId> | ||
<version>2.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>commons-proxy-core</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>aopalliance</groupId> | ||
<artifactId>aopalliance</artifactId> | ||
<version>1.0</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>axis</groupId> | ||
<artifactId>axis-jaxrpc</artifactId> | ||
<version>1.2.1</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>hessian</groupId> | ||
<artifactId>hessian</artifactId> | ||
<version>3.0.1</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>burlap</groupId> | ||
<artifactId>burlap</artifactId> | ||
<version>2.1.7</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>xmlrpc</groupId> | ||
<artifactId>xmlrpc</artifactId> | ||
<version>2.0</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-logging</groupId> | ||
<artifactId>commons-logging</artifactId> | ||
<version>1.0.4</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>1.4.0</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-collections</groupId> | ||
<artifactId>commons-collections</artifactId> | ||
<version>3.1</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-codec</groupId> | ||
<artifactId>commons-codec</artifactId> | ||
<version>1.3</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-lang</groupId> | ||
<artifactId>commons-lang</artifactId> | ||
<version>2.3</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>jmock</groupId> | ||
<artifactId>jmock</artifactId> | ||
<version>1.0.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>test-jar</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* "Intercepts" a method invocation. | ||
* | ||
* @author James Carman | ||
* @since 1.0 | ||
*/ | ||
public interface Interceptor extends Serializable | ||
{ | ||
//********************************************************************************************************************** | ||
// Other Methods | ||
//********************************************************************************************************************** | ||
|
||
public Object intercept( Invocation invocation ) throws Throwable; | ||
} |
Oops, something went wrong.