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/21 19:38:57 UTC
svn commit: r234282 [2/2] - in /jakarta/commons/sandbox/proxy: ./ trunk/
trunk/src/ trunk/src/java/ trunk/src/java/org/ trunk/src/java/org/apache/
trunk/src/java/org/apache/commons/ trunk/src/java/org/apache/commons/proxy/
trunk/src/java/org/apache/com...
Added: 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=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyFactory.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyFactory.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,78 @@
+/*
+ * 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;
+
+import org.aopalliance.intercept.MethodInterceptor;
+
+/**
+ * A <code>ProxyFactory</code> essentially encapsulates a "proxying strategy." All Syringe proxies are created using
+ * a <code>ProxyFactory</code>. So, to change the proxying strategy, simply provide a different <code>ProxyFactory</code>
+ * implementation.
+ *
+ * @author James Carman
+ * @version 1.0
+ */
+public interface ProxyFactory
+{
+ /**
+ * Creates a proxy which passes through a {@link org.aopalliance.intercept.MethodInterceptor method interceptor} before
+ * eventually reaching the <code>target</code> object.
+ *
+ * @param classLoader the class loader to use when generating the proxy
+ * @param target the target object
+ * @param interceptor the method interceptor
+ * @param proxyInterfaces the interfaces that the proxy should implement.
+ * @return a proxy which passes through a {@link org.aopalliance.intercept.MethodInterceptor method interceptor} before
+ * eventually reaching the <code>target</code> object.
+ */
+ public Object createInterceptorProxy( ClassLoader classLoader, Object target, MethodInterceptor interceptor, Class... proxyInterfaces );
+
+ /**
+ * Creates a proxy which passes through a {@link MethodInterceptor method interceptor} before
+ * eventually reaching the <code>target</code> object. The proxy will be generated using the
+ * current thread's "context class loader."
+ *
+ * @param target the target object
+ * @param interceptor the method interceptor
+ * @param proxyInterfaces the interfaces that the proxy should implement
+ * @return a proxy which passes through a {@link MethodInterceptor method interceptor} before
+ * eventually reaching the <code>target</code> object.
+ */
+ public Object createInterceptorProxy( Object target, MethodInterceptor interceptor, Class... proxyInterfaces );
+
+ /**
+ * Creates a proxy which delegates to the object provided by the target
+ * object provider. The proxy will be generated using the current thread's "context class loader."
+ *
+ * @param targetProvider the target object 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 createProxy( ObjectProvider targetProvider, Class... proxyInterfaces );
+
+ /**
+ * Creates a proxy which delegates to the object provided by the target
+ * object provider.
+ *
+ * @param classLoader the class loader to use when generating the proxy
+ * @param targetProvider the target object 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 createProxy( ClassLoader classLoader, ObjectProvider targetProvider, Class... proxyInterfaces );
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/exception/ObjectProviderException.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/exception/ObjectProviderException.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/exception/ObjectProviderException.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/exception/ObjectProviderException.java Sun Aug 21 10:38:47 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.exception;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class ObjectProviderException extends RuntimeException
+{
+ public ObjectProviderException()
+ {
+ }
+
+ public ObjectProviderException( String message )
+ {
+ super( message );
+ }
+
+ public ObjectProviderException( String message, Throwable cause )
+ {
+ super( message, cause );
+ }
+
+ public ObjectProviderException( Throwable cause )
+ {
+ super( cause );
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/AbstractProxyFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/AbstractProxyFactory.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/AbstractProxyFactory.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/AbstractProxyFactory.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,37 @@
+/*
+ * 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.factory;
+
+import org.aopalliance.intercept.MethodInterceptor;
+import org.apache.commons.proxy.ProxyFactory;
+import org.apache.commons.proxy.ObjectProvider;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public abstract class AbstractProxyFactory implements ProxyFactory
+{
+ public Object createInterceptorProxy( Object target, MethodInterceptor interceptor, Class... proxyInterfaces )
+ {
+ return createInterceptorProxy( Thread.currentThread().getContextClassLoader(), target, interceptor, proxyInterfaces );
+ }
+
+ public Object createProxy( ObjectProvider targetProvider, Class... proxyInterfaces )
+ {
+ return createProxy( Thread.currentThread().getContextClassLoader(), targetProvider, proxyInterfaces );
+ }
+}
\ No newline at end of file
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,126 @@
+/*
+ * 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.factory.cglib;
+
+import net.sf.cglib.proxy.Dispatcher;
+import net.sf.cglib.proxy.Enhancer;
+import net.sf.cglib.proxy.MethodProxy;
+import org.aopalliance.intercept.MethodInterceptor;
+import org.aopalliance.intercept.MethodInvocation;
+import org.apache.commons.proxy.ObjectProvider;
+import org.apache.commons.proxy.factory.AbstractProxyFactory;
+
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Method;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class CglibProxyFactory extends AbstractProxyFactory
+{
+ public Object createInterceptorProxy( ClassLoader classLoader, Object target, MethodInterceptor interceptor, Class... proxyInterfaces )
+ {
+ final Enhancer enhancer = new Enhancer();
+ enhancer.setClassLoader( classLoader );
+ enhancer.setInterfaces( proxyInterfaces );
+ enhancer.setCallback( new InterceptorBridge( target, interceptor ) );
+ return enhancer.create();
+ }
+
+ public Object createProxy( ClassLoader classLoader, ObjectProvider targetProvider, Class... proxyInterfaces )
+ {
+ final Enhancer enhancer = new Enhancer();
+ enhancer.setClassLoader( classLoader );
+ enhancer.setInterfaces( proxyInterfaces );
+ enhancer.setCallback( new ObjectProviderDispatcher( targetProvider ) );
+ return enhancer.create();
+ }
+
+ private class InterceptorBridge implements net.sf.cglib.proxy.MethodInterceptor
+ {
+ private final MethodInterceptor inner;
+ private final Object target;
+
+ public InterceptorBridge( Object target, MethodInterceptor inner )
+ {
+ this.inner = inner;
+ this.target = target;
+ }
+
+ public Object intercept( Object object, Method method, Object[] args, MethodProxy methodProxy ) throws Throwable
+ {
+ return inner.invoke( new MethodProxyMethodInvocation( target, method, args, methodProxy ) );
+ }
+ }
+
+ private class MethodProxyMethodInvocation implements MethodInvocation
+ {
+ private final MethodProxy methodProxy;
+ private final Method method;
+ private final Object[] args;
+ private final Object target;
+
+ public MethodProxyMethodInvocation( 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 getThis()
+ {
+ return null;
+ }
+
+ public AccessibleObject getStaticPart()
+ {
+ return null;
+ }
+ }
+
+ private class ObjectProviderDispatcher implements Dispatcher
+ {
+ private final ObjectProvider objectProvider;
+
+ public ObjectProviderDispatcher( ObjectProvider objectProvider )
+ {
+ this.objectProvider = objectProvider;
+ }
+
+ public Object loadObject()
+ {
+ return objectProvider.getObject();
+ }
+ }
+
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistProxyFactory.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,115 @@
+/*
+ * 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.factory.javassist;
+
+import javassist.CannotCompileException;
+import javassist.ClassPool;
+import javassist.CtClass;
+import javassist.CtConstructor;
+import javassist.CtField;
+import javassist.CtMethod;
+import javassist.NotFoundException;
+import org.aopalliance.intercept.MethodInterceptor;
+import org.apache.commons.proxy.ObjectProvider;
+import org.apache.commons.proxy.exception.ObjectProviderException;
+import org.apache.commons.proxy.factory.AbstractProxyFactory;
+
+import java.lang.reflect.Method;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class JavassistProxyFactory extends AbstractProxyFactory
+{
+ private static int classNumber = 0;
+ private static final ClassPool classPool = ClassPool.getDefault();
+
+ public Object createInterceptorProxy( ClassLoader classLoader, Object target, MethodInterceptor interceptor, Class... proxyInterfaces )
+ {
+ return null;
+ }
+
+ public Object createProxy( ClassLoader classLoader, ObjectProvider targetProvider, Class... proxyInterfaces )
+ {
+ try
+ {
+ final CtClass proxyClass = createClass();
+ final CtField providerField = new CtField( resolve( targetProvider.getClass() ), "provider", proxyClass );
+ proxyClass.addField( providerField );
+ final CtConstructor proxyConstructor = new CtConstructor( resolve( new Class[]{targetProvider.getClass()} ), proxyClass );
+ proxyConstructor.setBody( "{ this.provider = $1; }" );
+ proxyClass.addConstructor( proxyConstructor );
+ for( Class proxyInterface : proxyInterfaces )
+ {
+ proxyClass.addInterface( resolve( proxyInterface ) );
+ final Method[] methods = proxyInterface.getMethods();
+ for( int i = 0; i < methods.length; ++i )
+ {
+ final CtMethod method = new CtMethod( resolve( methods[i].getReturnType() ), methods[i].getName(), resolve( methods[i].getParameterTypes() ), proxyClass );
+ method.setBody( "{ return ( $r ) ( ( " + proxyInterface.getName() + " )provider.getObject() )." + methods[i].getName() + "($$); }" );
+ proxyClass.addMethod( method );
+ }
+ }
+ final Class clazz = proxyClass.toClass( classLoader );
+ return clazz.getConstructor( targetProvider.getClass() ).newInstance( targetProvider );
+ }
+ catch( CannotCompileException e )
+ {
+ throw new ObjectProviderException( "Could not compile class.", e );
+ }
+ catch( NoSuchMethodException e )
+ {
+ throw new ObjectProviderException( "Could not find constructor in generated proxy class.", e );
+ }
+ catch( Exception e )
+ {
+ throw new ObjectProviderException( "Unable to instantiate proxy from generated proxy class.", e );
+ }
+ }
+
+ public static CtClass resolve( Class clazz )
+ {
+ try
+ {
+ return classPool.get( clazz.getName() );
+ }
+ catch( NotFoundException e )
+ {
+ throw new ObjectProviderException( "Unable to find class " + clazz.getName() + " in default Javassist class pool.", e );
+ }
+ }
+
+ public static CtClass[] resolve( Class[] classes )
+ {
+ final CtClass[] ctClasses = new CtClass[classes.length];
+ for( int i = 0; i < ctClasses.length; ++i )
+ {
+ ctClasses[i] = resolve( classes[i] );
+ }
+ return ctClasses;
+ }
+
+ public static CtClass createClass()
+ {
+ return createClass( Object.class );
+ }
+
+ public static CtClass createClass( Class superclass )
+ {
+ return classPool.makeClass( "JavassistUtilsGenerated_" + ( ++classNumber ), resolve( superclass ) );
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/AbstractInvocationHandler.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/AbstractInvocationHandler.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/AbstractInvocationHandler.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/AbstractInvocationHandler.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,37 @@
+/*
+ * 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.factory.reflect;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Proxy;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public abstract class AbstractInvocationHandler implements InvocationHandler
+{
+ public Object createProxy( Class... proxyInterfaces )
+ {
+ return createProxy( Thread.currentThread().getContextClassLoader(), proxyInterfaces );
+ }
+
+ public Object createProxy( ClassLoader classLoader, Class... proxyInterfaces )
+ {
+ return Proxy.newProxyInstance( classLoader, proxyInterfaces, this );
+ }
+}
+
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/DelegatingInvocationHandler.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/DelegatingInvocationHandler.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/DelegatingInvocationHandler.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/DelegatingInvocationHandler.java Sun Aug 21 10:38:47 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.factory.reflect;
+
+import java.lang.reflect.Method;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public abstract class DelegatingInvocationHandler extends AbstractInvocationHandler
+{
+ protected abstract Object getDelegate();
+
+ public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
+ {
+ return method.invoke( getDelegate(), args );
+ }
+
+ public Object createProxy()
+ {
+ return createProxy( getDelegate().getClass().getInterfaces() );
+ }
+
+ public Object createProxy( ClassLoader classLoader )
+ {
+ return createProxy( classLoader, getDelegate().getClass().getInterfaces() );
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/MethodInterceptorInvocationHandler.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/MethodInterceptorInvocationHandler.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/MethodInterceptorInvocationHandler.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/MethodInterceptorInvocationHandler.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,43 @@
+/*
+ * 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.factory.reflect;
+
+import org.aopalliance.intercept.MethodInterceptor;
+
+import java.lang.reflect.Method;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class MethodInterceptorInvocationHandler extends AbstractInvocationHandler
+{
+ private final Object target;
+ private final MethodInterceptor methodInterceptor;
+
+ public MethodInterceptorInvocationHandler( Object target, MethodInterceptor methodInterceptor )
+ {
+ this.target = target;
+ this.methodInterceptor = methodInterceptor;
+ }
+
+ public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
+ {
+ final ReflectionMethodInvocation invocation = new ReflectionMethodInvocation( target, method, args );
+ return methodInterceptor.invoke( invocation );
+ }
+}
+
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ObjectProviderInvocationHandler.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ObjectProviderInvocationHandler.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ObjectProviderInvocationHandler.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ObjectProviderInvocationHandler.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,37 @@
+/*
+ * 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.factory.reflect;
+
+import org.apache.commons.proxy.ObjectProvider;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class ObjectProviderInvocationHandler extends DelegatingInvocationHandler
+{
+ private final ObjectProvider objectProvider;
+
+ public ObjectProviderInvocationHandler( ObjectProvider objectProvider )
+ {
+ this.objectProvider = objectProvider;
+ }
+
+ protected Object getDelegate()
+ {
+ return objectProvider.getObject();
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ReflectionMethodInvocation.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ReflectionMethodInvocation.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ReflectionMethodInvocation.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ReflectionMethodInvocation.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,64 @@
+/*
+ * 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.factory.reflect;
+
+import org.aopalliance.intercept.MethodInvocation;
+
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Method;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+class ReflectionMethodInvocation implements MethodInvocation
+{
+ private final Method method;
+ private final Object[] arguments;
+ private final Object target;
+
+ public ReflectionMethodInvocation( Object target, Method method, Object[] arguments )
+ {
+ this.method = method;
+ this.arguments = arguments;
+ this.target = target;
+ }
+
+ public Object[] getArguments()
+ {
+ return arguments;
+ }
+
+ public Method getMethod()
+ {
+ return method;
+ }
+
+ public Object proceed() throws Throwable
+ {
+ return method.invoke( target, arguments );
+ }
+
+ public Object getThis()
+ {
+ return target;
+ }
+
+ public AccessibleObject getStaticPart()
+ {
+ return method;
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ReflectionProxyFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ReflectionProxyFactory.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ReflectionProxyFactory.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/reflect/ReflectionProxyFactory.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,40 @@
+/*
+ * 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.factory.reflect;
+
+import org.aopalliance.intercept.MethodInterceptor;
+import org.apache.commons.proxy.ObjectProvider;
+import org.apache.commons.proxy.factory.AbstractProxyFactory;
+
+import java.lang.reflect.Proxy;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class ReflectionProxyFactory extends AbstractProxyFactory
+{
+ public Object createInterceptorProxy( ClassLoader classLoader, Object target, MethodInterceptor interceptor, Class... proxyInterfaces )
+ {
+ return new MethodInterceptorInvocationHandler( target, interceptor ).createProxy( classLoader, proxyInterfaces );
+ }
+
+ public Object createProxy( ClassLoader classLoader, ObjectProvider targetProvider, Class... proxyInterfaces )
+ {
+ return Proxy.newProxyInstance( classLoader, proxyInterfaces, new ObjectProviderInvocationHandler( targetProvider ) );
+ }
+
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/AbstractProvider.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/AbstractProvider.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/AbstractProvider.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/AbstractProvider.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,39 @@
+/*
+ * 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.provider;
+
+import org.apache.commons.proxy.ObjectProvider;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public abstract class AbstractProvider<T> implements ObjectProvider<T>
+{
+ private Log log = LogFactory.getLog( getClass() );
+
+ protected Log getLog()
+ {
+ return log;
+ }
+
+ public void setLog( Log log )
+ {
+ this.log = log;
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/BeanProvider.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/BeanProvider.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/BeanProvider.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/BeanProvider.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,52 @@
+/*
+ * 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.provider;
+
+import org.apache.commons.proxy.ObjectProvider;
+import org.apache.commons.proxy.exception.ObjectProviderException;
+import org.apache.commons.proxy.exception.ObjectProviderException;
+
+/**
+ * Uses <code>Class.newInstance()</code> to instantiate an object.
+ *
+ * @author James Carman
+ * @version $Rev: 57 $
+ */
+public class BeanProvider<T> implements ObjectProvider
+{
+ private final Class<? extends T> clazz;
+
+ public BeanProvider( Class<? extends T> clazz )
+ {
+ this.clazz = clazz;
+ }
+
+ public T getObject()
+ {
+ try
+ {
+ return clazz.newInstance();
+ }
+ catch( InstantiationException e )
+ {
+ throw new ObjectProviderException( "Class " + clazz.getName() + " is not concrete.", e );
+ }
+ catch( IllegalAccessException e )
+ {
+ throw new ObjectProviderException( "Constructor for class " + clazz.getName() + " is not accessible.", e );
+ }
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/BurlapProvider.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/BurlapProvider.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/BurlapProvider.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/BurlapProvider.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,52 @@
+/*
+ * 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.provider;
+
+import org.apache.commons.proxy.ObjectProvider;
+import org.apache.commons.proxy.exception.ObjectProviderException;
+import com.caucho.burlap.client.BurlapProxyFactory;
+
+import java.net.MalformedURLException;
+
+/**
+ * Provides a burlap service object.
+ *
+ * @author James Carman
+ * @version 1.0
+ */
+public class BurlapProvider<T> implements ObjectProvider
+{
+ private final Class<T> serviceInterface;
+ private final String url;
+
+ public BurlapProvider( Class<T> serviceInterface, String url )
+ {
+ this.serviceInterface = serviceInterface;
+ this.url = url;
+ }
+
+ public T getObject()
+ {
+ try
+ {
+ return serviceInterface.cast( new BurlapProxyFactory().create( serviceInterface, url ) );
+ }
+ catch( MalformedURLException e )
+ {
+ throw new ObjectProviderException( "Invalid url given.", e );
+ }
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/ConstantProvider.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/ConstantProvider.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/ConstantProvider.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/ConstantProvider.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,39 @@
+/*
+ * 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.provider;
+
+import org.apache.commons.proxy.ObjectProvider;
+
+/**
+ * Always returns the same object.
+ *
+ * @author James Carman
+ * @version 1.0
+ */
+public class ConstantProvider<T> implements ObjectProvider<T>
+{
+ private final T constant;
+
+ public ConstantProvider( T constant )
+ {
+ this.constant = constant;
+ }
+
+ public T getObject()
+ {
+ return constant;
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/HessianProvider.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/HessianProvider.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/HessianProvider.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/HessianProvider.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,52 @@
+/*
+ * 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.provider;
+
+import com.caucho.hessian.client.HessianProxyFactory;
+import org.apache.commons.proxy.ObjectProvider;
+import org.apache.commons.proxy.exception.ObjectProviderException;
+
+import java.net.MalformedURLException;
+
+/**
+ * Provides a hessian service object.
+ *
+ * @author James Carman
+ * @version 1.0
+ */
+public class HessianProvider<T> implements ObjectProvider
+{
+ private final Class<T> serviceInterface;
+ private final String url;
+
+ public HessianProvider( Class<T> serviceInterface, String url )
+ {
+ this.serviceInterface = serviceInterface;
+ this.url = url;
+ }
+
+ public T getObject()
+ {
+ try
+ {
+ return serviceInterface.cast( new HessianProxyFactory().create( serviceInterface, url ) );
+ }
+ catch( MalformedURLException e )
+ {
+ throw new ObjectProviderException( "Invalid url given.", e );
+ }
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/ProviderDecorator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/ProviderDecorator.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/ProviderDecorator.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/ProviderDecorator.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,37 @@
+/*
+ * 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.provider;
+
+import org.apache.commons.proxy.ObjectProvider;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class ProviderDecorator<T> extends AbstractProvider<T>
+{
+ protected ObjectProvider<? extends T> inner;
+
+ public ProviderDecorator( ObjectProvider<? extends T> inner )
+ {
+ this.inner = inner;
+ }
+
+ public T getObject()
+ {
+ return inner.getObject();
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/ProviderUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/ProviderUtils.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/ProviderUtils.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/ProviderUtils.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,45 @@
+/*
+ * 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.provider;
+
+import org.apache.commons.proxy.ObjectProvider;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class ProviderUtils
+{
+ public static <T> ObjectProvider<T> constantProvider( T value )
+ {
+ return new ConstantProvider<T>( value );
+ }
+
+ public static <T> ObjectProvider<T> beanProvider( Class<T> beanClass )
+ {
+ return new BeanProvider<T>( beanClass );
+ }
+
+ public static <T> ObjectProvider<T> singletonProvider( ObjectProvider<T> inner )
+ {
+ return new SingletonProvider<T>( inner );
+ }
+
+ public static <T> ObjectProvider<T> synchronizedProvider( ObjectProvider<T> inner )
+ {
+ return new SynchronizedProvider<T>( inner );
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/RmiObjectProvider.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/RmiObjectProvider.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/RmiObjectProvider.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/RmiObjectProvider.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,128 @@
+/*
+ * 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.provider;
+
+import org.apache.commons.proxy.ObjectProvider;
+import org.apache.commons.proxy.exception.ObjectProviderException;
+
+import java.rmi.AccessException;
+import java.rmi.NotBoundException;
+import java.rmi.RemoteException;
+import java.rmi.registry.LocateRegistry;
+import java.rmi.registry.Registry;
+import java.rmi.server.RMIClientSocketFactory;
+
+/**
+ * Provides an object by looking it up in an RMI registry.
+ *
+ * @author James Carman
+ * @version 1.0
+ */
+public class RmiObjectProvider<T> implements ObjectProvider
+{
+ private String host = "localhost";
+ private int port = Registry.REGISTRY_PORT;
+ private RMIClientSocketFactory clientSocketFactory;
+ private final String name;
+ private final Class<T> serviceInterface;
+
+ public RmiObjectProvider( String name, Class<T> serviceInterface )
+ {
+ this.name = name;
+ this.serviceInterface = serviceInterface;
+ }
+
+ public RmiObjectProvider( String host, String name, Class<T> serviceInterface )
+ {
+ this.host = host;
+ this.name = name;
+ this.serviceInterface = serviceInterface;
+ }
+
+ public RmiObjectProvider( String host, int port, String name, Class<T> serviceInterface )
+ {
+ this.host = host;
+ this.name = name;
+ this.serviceInterface = serviceInterface;
+ this.port = port;
+ }
+
+ public RmiObjectProvider( String host, int port, RMIClientSocketFactory clientSocketFactory, String name, Class<T> serviceInterface )
+ {
+ this.host = host;
+ this.port = port;
+ this.clientSocketFactory = clientSocketFactory;
+ this.name = name;
+ this.serviceInterface = serviceInterface;
+ }
+
+ public T getObject()
+ {
+ Registry reg = null;
+ try
+ {
+ reg = getRegistry();
+ return serviceInterface.cast( reg.lookup( name ) );
+ }
+ catch( NotBoundException e )
+ {
+ throw new ObjectProviderException( "Name " + name + " not found in registry at " + host + ":" + port + ".", e );
+ }
+ catch( AccessException e )
+ {
+ throw new ObjectProviderException( "Registry at " + host + ":" + port + " did not allow lookup.", e );
+ }
+ catch( RemoteException e )
+ {
+ throw new ObjectProviderException( "Unable to lookup service named " + name + " in registry at " + host + ":" + port + "." );
+ }
+
+ }
+
+ private Registry getRegistry()
+ {
+ try
+ {
+ if( clientSocketFactory != null )
+ {
+ return LocateRegistry.getRegistry( host, port, clientSocketFactory );
+ }
+ else
+ {
+ return LocateRegistry.getRegistry( host, port );
+ }
+ }
+ catch( RemoteException e )
+ {
+ throw new ObjectProviderException( "Unable to locate registry at " + host + ":" + port + ".", e );
+ }
+ }
+
+ public void setHost( String host )
+ {
+ this.host = host;
+ }
+
+ public void setPort( int port )
+ {
+ this.port = port;
+ }
+
+ public void setClientSocketFactory( RMIClientSocketFactory clientSocketFactory )
+ {
+ this.clientSocketFactory = clientSocketFactory;
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/SingletonProvider.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/SingletonProvider.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/SingletonProvider.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/SingletonProvider.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,57 @@
+/*
+ * 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.provider;
+
+import org.apache.commons.proxy.ObjectProvider;
+
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+/**
+ * Wraps another object provider, making sure to only call it once, returning the value
+ * returned from the wrapped provider on all subsequent invocations.
+ *
+ * @author James Carman
+ * @version 1.0
+ */
+public class SingletonProvider<T> extends ProviderDecorator<T>
+{
+ private T instance;
+ private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
+
+ public SingletonProvider( ObjectProvider<? extends T> inner )
+ {
+ super( inner );
+ }
+
+ public T getObject()
+ {
+ rwl.readLock().lock();
+ if( instance == null )
+ {
+ rwl.readLock().unlock();
+ rwl.writeLock().lock();
+ if( instance == null )
+ {
+ instance = super.getObject();
+ inner = null; // Garbage collection
+ }
+ rwl.readLock().lock();
+ rwl.writeLock().unlock();
+ }
+ rwl.readLock().unlock();
+ return instance;
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/SynchronizedProvider.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/SynchronizedProvider.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/SynchronizedProvider.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/provider/SynchronizedProvider.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,48 @@
+/*
+ * 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.provider;
+
+import org.apache.commons.proxy.ObjectProvider;
+
+/**
+ * Wraps another object provider, making it synchronized.
+ * @author James Carman
+ * @version 1.0
+ */
+public class SynchronizedProvider<T> extends ProviderDecorator<T>
+{
+ private final Object monitor;
+
+ public SynchronizedProvider( ObjectProvider<T> inner, Object monitor )
+ {
+ super( inner );
+ this.monitor = monitor;
+ }
+
+ public SynchronizedProvider( ObjectProvider<T> inner )
+ {
+ super( inner );
+ monitor = this;
+ }
+
+ public T getObject()
+ {
+ synchronized( monitor )
+ {
+ return super.getObject();
+ }
+ }
+}
Added: 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=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/AbstractProxyFactoryTestCase.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/AbstractProxyFactoryTestCase.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,51 @@
+/*
+ * 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.factory;
+
+import org.apache.commons.proxy.ProxyFactory;
+import org.apache.commons.proxy.provider.BeanProvider;
+import org.apache.commons.proxy.provider.SingletonProvider;
+import org.apache.commons.proxy.util.AbstractTestCase;
+import org.apache.commons.proxy.util.Echo;
+import org.apache.commons.proxy.util.EchoImpl;
+import org.apache.commons.proxy.util.SuffixMethodInterceptor;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public abstract class AbstractProxyFactoryTestCase extends AbstractTestCase
+{
+ private final ProxyFactory factory;
+
+ protected AbstractProxyFactoryTestCase( ProxyFactory factory )
+ {
+ this.factory = factory;
+ }
+
+ public void testCreateProxy()
+ {
+ final Echo echo = ( Echo )factory.createProxy( new SingletonProvider<Echo>( new BeanProvider<Echo>( EchoImpl.class ) ), Echo.class );
+ assertEquals( "message", echo.echoBack( "message" ) );
+ }
+
+ public void testCreateInterceptorProxy()
+ {
+ final Echo target = ( Echo )factory.createProxy( new SingletonProvider<Echo>( new BeanProvider<Echo>( EchoImpl.class ) ), Echo.class );
+ final Echo proxy = ( Echo )factory.createInterceptorProxy( target, new SuffixMethodInterceptor( " suffix" ), Echo.class );
+ assertEquals( "message suffix", proxy.echoBack( "message" ) );
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/cglib/TestCglibProxyFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/cglib/TestCglibProxyFactory.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/cglib/TestCglibProxyFactory.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/cglib/TestCglibProxyFactory.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,30 @@
+/*
+ * 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.factory.cglib;
+
+import org.apache.commons.proxy.factory.AbstractProxyFactoryTestCase;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class TestCglibProxyFactory extends AbstractProxyFactoryTestCase
+{
+ public TestCglibProxyFactory()
+ {
+ super( new CglibProxyFactory() );
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/javassist/TestJavassistProxyFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/javassist/TestJavassistProxyFactory.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/javassist/TestJavassistProxyFactory.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/javassist/TestJavassistProxyFactory.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,31 @@
+/*
+ * 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.factory.javassist;
+
+import org.apache.commons.proxy.factory.AbstractProxyFactoryTestCase;
+import org.apache.commons.proxy.ProxyFactory;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class TestJavassistProxyFactory extends AbstractProxyFactoryTestCase
+{
+ public TestJavassistProxyFactory()
+ {
+ super( new JavassistProxyFactory() );
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/reflect/TestReflectionProxyFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/reflect/TestReflectionProxyFactory.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/reflect/TestReflectionProxyFactory.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/factory/reflect/TestReflectionProxyFactory.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,30 @@
+/*
+ * 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.factory.reflect;
+
+import org.apache.commons.proxy.factory.AbstractProxyFactoryTestCase;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class TestReflectionProxyFactory extends AbstractProxyFactoryTestCase
+{
+ public TestReflectionProxyFactory()
+ {
+ super( new ReflectionProxyFactory() );
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestConstantProvider.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestConstantProvider.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestConstantProvider.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/provider/TestConstantProvider.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,32 @@
+/*
+ * 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.provider;
+
+import org.apache.commons.proxy.util.AbstractTestCase;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class TestConstantProvider extends AbstractTestCase
+{
+ public void testGetObject() throws Exception
+ {
+ final String s = "Hello, World!";
+ final ConstantProvider<String> provider = new ConstantProvider<String>( s );
+ assertSame( s, provider.getObject() );
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/AbstractTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/AbstractTestCase.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/AbstractTestCase.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/AbstractTestCase.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2005 Carman Consulting, Inc. All Rights Reserved.
+ *
+ * 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.util;
+
+import junit.framework.TestCase;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public abstract class AbstractTestCase extends TestCase
+{
+ protected final Log log = LogFactory.getLog( getClass() );
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/Echo.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/Echo.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/Echo.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/Echo.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2005 Carman Consulting, Inc. All Rights Reserved.
+ *
+ * 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.util;
+
+import java.util.Comparator;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public interface Echo
+{
+ public String echoBack( String message );
+ public boolean isInitialized();
+
+ String getStringDependency();
+
+ Integer getIntegerDependency();
+
+ Comparator getComparator();
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/EchoImpl.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/EchoImpl.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/EchoImpl.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/EchoImpl.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2005 Carman Consulting, Inc. All Rights Reserved.
+ *
+ * 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.util;
+
+import java.util.Comparator;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class EchoImpl implements Echo
+{
+ private boolean initialized = false;
+ private String stringDependency;
+ private Integer integerDependency;
+ private Comparator comparator;
+
+ public EchoImpl()
+ {
+ }
+
+ public EchoImpl( String stringDependency, Integer integerDependency, Comparator comparator )
+ {
+ this.stringDependency = stringDependency;
+ this.integerDependency = integerDependency;
+ this.comparator = comparator;
+ }
+
+ public EchoImpl( String stringDependency, Integer integerDependency )
+ {
+ this.stringDependency = stringDependency;
+ this.integerDependency = integerDependency;
+ }
+
+ public EchoImpl( Comparator comparator )
+ {
+ this.comparator = comparator;
+ }
+
+ public String getStringDependency()
+ {
+ return stringDependency;
+ }
+
+ public void setComparator( Comparator comparator )
+ {
+ this.comparator = comparator;
+ }
+
+ public Comparator getComparator()
+ {
+ return comparator;
+ }
+
+ public void setStringDependency( String stringDependency )
+ {
+ this.stringDependency = stringDependency;
+ }
+
+ public Integer getIntegerDependency()
+ {
+ return integerDependency;
+ }
+
+ public void setIntegerDependency( Integer integerDependency )
+ {
+ this.integerDependency = integerDependency;
+ }
+
+ public String echoBack( String message )
+ {
+ return message;
+ }
+
+ public void init()
+ {
+ this.initialized = true;
+ }
+
+ public boolean isInitialized()
+ {
+ return initialized;
+ }
+}
Added: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/SuffixMethodInterceptor.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/SuffixMethodInterceptor.java?rev=234282&view=auto
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/SuffixMethodInterceptor.java (added)
+++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/util/SuffixMethodInterceptor.java Sun Aug 21 10:38:47 2005
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2005 Carman Consulting, Inc. All Rights Reserved.
+ *
+ * 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.util;
+
+import org.aopalliance.intercept.MethodInterceptor;
+import org.aopalliance.intercept.MethodInvocation;
+
+/**
+ * @author James Carman
+ * @version 1.0
+ */
+public class SuffixMethodInterceptor implements MethodInterceptor
+{
+ private final String suffix;
+
+ public SuffixMethodInterceptor( String suffix )
+ {
+ this.suffix = suffix;
+ }
+
+ public Object invoke( MethodInvocation methodInvocation ) throws Throwable
+ {
+ return methodInvocation.proceed() + suffix;
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org