Skip to content
Permalink
Browse files
work in progress.
  • Loading branch information
jwcarman committed Jul 15, 2010
1 parent 1a0f4fc commit 8ab6e20ca6db0a78baab76be0b0ef670500a6db6
Showing 49 changed files with 4,599 additions and 88 deletions.
@@ -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>
@@ -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();
}
}
}
@@ -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());
}
}
@@ -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>
@@ -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;
}

0 comments on commit 8ab6e20

Please sign in to comment.