Author: jcarman Date: Sat Oct 15 07:24:23 2005 New Revision: 321338 URL: http://svn.apache.org/viewcvs?rev=321338&view=rev Log: Introduced getProxyFactory() method.
Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyUtils.java jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/TestProxyUtils.java Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyUtils.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyUtils.java?rev=321338&r1=321337&r2=321338&view=diff ============================================================================== --- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyUtils.java (original) +++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/ProxyUtils.java Sat Oct 15 07:24:23 2005 @@ -16,6 +16,7 @@ */ package org.apache.commons.proxy; +import org.apache.commons.proxy.exception.ProxyFactoryException; import org.apache.commons.proxy.handler.NullInvocationHandler; import java.util.ArrayList; @@ -32,6 +33,7 @@ // Fields //---------------------------------------------------------------------------------------------------------------------- + public static final String PROXY_FACTORY_PROPERTY = "commons-proxy.factory"; public static final Object[] EMPTY_ARGUMENTS = new Object[0]; public static final Class[] EMPTY_ARGUMENT_TYPES = new Class[0]; @@ -41,6 +43,7 @@ /** * Creates a "null object" which implements the <code>proxyClasses</code>. + * * @param proxyFactory the proxy factory to be used to create the proxy object * @param proxyClasses the proxy interfaces * @return a "null object" which implements the <code>proxyClasses</code>. @@ -52,8 +55,9 @@ /** * Creates a "null object" which implements the <code>proxyClasses</code>. + * * @param proxyFactory the proxy factory to be used to create the proxy object - * @param classLoader the class loader to be used by the proxy factory to create the proxy object + * @param classLoader the class loader to be used by the proxy factory to create the proxy object * @param proxyClasses the proxy interfaces * @return a "null object" which implements the <code>proxyClasses</code>. */ @@ -63,20 +67,19 @@ } /** - * <p>Gets an array of [EMAIL PROTECTED] Class} objects representing all interfaces implemented by the given - * class and its superclasses.</p> + * <p>Gets an array of [EMAIL PROTECTED] Class} objects representing all interfaces implemented by the given class and its + * superclasses.</p> + * <p/> + * <p>The order is determined by looking through each interface in turn as declared in the source file and following + * its hierarchy up. Then each superclass is considered in the same way. Later duplicates are ignored, so the order + * is maintained.</p> + * <p/> + * <b>Note</b>: Implementation of this method was "borrowed" from <a href="http://jakarta.apache.org/commons/lang/">Jakarta + * Commons Lang</a> to avoid a dependency. </p> * - * <p>The order is determined by looking through each interface in turn as - * declared in the source file and following its hierarchy up. Then each - * superclass is considered in the same way. Later duplicates are ignored, - * so the order is maintained.</p> - * <p> - * <b>Note</b>: Implementation of this method was "borrowed" from - * <a href="http://jakarta.apache.org/commons/lang/">Jakarta Commons Lang</a> to avoid a dependency. - * </p> - * @param cls the class to look up, may be <code>null</code> - * @return an array of [EMAIL PROTECTED] Class} objects representing all interfaces implemented by the given - * class and its superclasses or <code>null</code> if input class is null. + * @param cls the class to look up, may be <code>null</code> + * @return an array of [EMAIL PROTECTED] Class} objects representing all interfaces implemented by the given class and its + * superclasses or <code>null</code> if input class is null. */ public static Class[] getAllInterfaces( Class cls ) { @@ -117,13 +120,10 @@ /** * Returns the class name as you would expect to see it in Java code. - * <p> - * <b>Examples:</b> - * <ul> - * <li>getJavaClassName( Object[].class ) == "Object[]"</li> - * <li>getJavaClassName( Object[][].class ) == "Object[][]"</li> - * <li>getJavaClassName( Integer.TYPE ) == "int"</li> - * </p> + * <p/> + * <b>Examples:</b> <ul> <li>getJavaClassName( Object[].class ) == "Object[]"</li> <li>getJavaClassName( + * Object[][].class ) == "Object[][]"</li> <li>getJavaClassName( Integer.TYPE ) == "int"</li> </p> + * * @param clazz the class * @return the class' name as you would expect to see it in Java code */ @@ -134,6 +134,83 @@ return getJavaClassName( clazz.getComponentType() ) + "[]"; } return clazz.getName(); + } + + /** + * Returns an appropriate [EMAIL PROTECTED] ProxyFactory} implementation for the current environment. + * The implementation class search order is as follows: + * <ul> + * <li>Try to use the type indicated by the system property "commons-proxy.factory"</li> + * <li>Try to use [EMAIL PROTECTED] org.apache.commons.proxy.factory.javassist.JavassistProxyFactory}</li> + * <li>Try to use [EMAIL PROTECTED] org.apache.commons.proxy.factory.cglib.CglibProxyFactory}</li> + * <li>Default to [EMAIL PROTECTED] org.apache.commons.proxy.factory.reflect.ReflectionProxyFactory} (should always be available)</li> + * </ul> + * @param classLoader the class loader to use to instantiate the proxy factory + * @return an appropriate [EMAIL PROTECTED] ProxyFactory} implementation for the current environment + */ + public static ProxyFactory getProxyFactory( ClassLoader classLoader ) + { + final String[] classNames = new String[]{ System.getProperty( PROXY_FACTORY_PROPERTY ), + "org.apache.commons.proxy.factory.javassist.JavassistProxyFactory", + "org.apache.commons.proxy.factory.cglib.CglibProxyFactory", + "org.apache.commons.proxy.factory.reflect.ReflectionProxyFactory" }; + for( int i = 0; i < classNames.length; i++ ) + { + final String className = classNames[i]; + final ProxyFactory factory = instantiateProxyFactory( className, classLoader ); + if( factory != null ) + { + return factory; + } + } + throw new ProxyFactoryException( "Unable to find a suitable proxy factory implementation class." ); + } + + /** + * Returns an appropriate [EMAIL PROTECTED] ProxyFactory} implementation for the current environment. + * The implementation class search order is as follows: + * <ul> + * <li>Try to the type indicated by the system property "commons-proxy.factory"</li> + * <li>Try to use [EMAIL PROTECTED] org.apache.commons.proxy.factory.javassist.JavassistProxyFactory}</li> + * <li>Try to use [EMAIL PROTECTED] org.apache.commons.proxy.factory.cglib.CglibProxyFactory}</li> + * <li>Default to [EMAIL PROTECTED] org.apache.commons.proxy.factory.reflect.ReflectionProxyFactory} (should always be available)</li> + * </ul> + * <p> + * <b>Note</b>: This implementation uses the current thread's context class loader! + * @return an appropriate [EMAIL PROTECTED] ProxyFactory} implementation for the current environment + */ + public static ProxyFactory getProxyFactory() + { + return getProxyFactory( Thread.currentThread().getContextClassLoader() ); + } + + private static ProxyFactory instantiateProxyFactory( String className, ClassLoader classLoader ) + { + try + { + if( className == null ) + { + return null; + } + Class proxyFactoryClass = classLoader.loadClass( className ); + if( proxyFactoryClass.isAssignableFrom( ProxyFactory.class ) ) + { + return null; + } + return ( ProxyFactory ) proxyFactoryClass.newInstance(); + } + catch( IllegalAccessException e ) + { + return null; + } + catch( InstantiationException e ) + { + return null; + } + catch( ClassNotFoundException e ) + { + return null; + } } } Modified: jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/TestProxyUtils.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/TestProxyUtils.java?rev=321338&r1=321337&r2=321338&view=diff ============================================================================== --- jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/TestProxyUtils.java (original) +++ jakarta/commons/sandbox/proxy/trunk/src/test/org/apache/commons/proxy/TestProxyUtils.java Sat Oct 15 07:24:23 2005 @@ -16,7 +16,9 @@ */ package org.apache.commons.proxy; import junit.framework.TestCase; +import org.apache.commons.proxy.factory.cglib.CglibProxyFactory; import org.apache.commons.proxy.factory.javassist.JavassistProxyFactory; +import org.apache.commons.proxy.factory.reflect.ReflectionProxyFactory; import org.apache.commons.proxy.util.Echo; public class TestProxyUtils extends TestCase @@ -50,5 +52,39 @@ assertEquals( "byte", ProxyUtils.getJavaClassName( Byte.TYPE ) ); assertEquals( "char", ProxyUtils.getJavaClassName( Character.TYPE ) ); assertEquals( "boolean", ProxyUtils.getJavaClassName( Boolean.TYPE ) ); + } + + public void testGetProxyFactory() throws Exception + { + assertTrue( ProxyUtils.getProxyFactory() instanceof JavassistProxyFactory ); + System.setProperty( ProxyUtils.PROXY_FACTORY_PROPERTY, CglibProxyFactory.class.getName() ); + assertTrue( ProxyUtils.getProxyFactory() instanceof CglibProxyFactory ); + System.setProperty( ProxyUtils.PROXY_FACTORY_PROPERTY, ReflectionProxyFactory.class.getName() ); + assertTrue( ProxyUtils.getProxyFactory() instanceof ReflectionProxyFactory ); + System.setProperty( ProxyUtils.PROXY_FACTORY_PROPERTY, "" ); + ClassLoader cl = new IsolatingClassLoader( JavassistProxyFactory.class, Thread.currentThread().getContextClassLoader() ); + assertTrue( ProxyUtils.getProxyFactory( cl ) instanceof CglibProxyFactory ); + cl = new IsolatingClassLoader( CglibProxyFactory.class, cl ); + assertTrue( ProxyUtils.getProxyFactory( cl ) instanceof ReflectionProxyFactory ); + } + + private static class IsolatingClassLoader extends ClassLoader + { + private Class isolatedClass; + + public IsolatingClassLoader( Class isolatedClass, ClassLoader parent ) + { + super( parent ); + this.isolatedClass = isolatedClass; + } + + public Class loadClass( String name ) throws ClassNotFoundException + { + if( isolatedClass.getName().equals( name ) ) + { + throw new ClassNotFoundException( name + " not found." ); + } + return getParent().loadClass( name ); + } } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]