Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 1009 invoked from network); 15 Oct 2005 14:24:52 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 15 Oct 2005 14:24:52 -0000 Received: (qmail 82228 invoked by uid 500); 15 Oct 2005 14:24:50 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 82184 invoked by uid 500); 15 Oct 2005 14:24:49 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 82173 invoked by uid 500); 15 Oct 2005 14:24:49 -0000 Received: (qmail 82169 invoked by uid 99); 15 Oct 2005 14:24:49 -0000 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Sat, 15 Oct 2005 07:24:49 -0700 Received: (qmail 934 invoked by uid 65534); 15 Oct 2005 14:24:29 -0000 Message-ID: <20051015142429.933.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r321338 - in /jakarta/commons/sandbox/proxy/trunk/src: java/org/apache/commons/proxy/ProxyUtils.java test/org/apache/commons/proxy/TestProxyUtils.java Date: Sat, 15 Oct 2005 14:24:28 -0000 To: commons-cvs@jakarta.apache.org From: jcarman@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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 proxyClasses. + * * @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 proxyClasses. @@ -52,8 +55,9 @@ /** * Creates a "null object" which implements the proxyClasses. + * * @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 proxyClasses. */ @@ -63,20 +67,19 @@ } /** - *

Gets an array of {@link Class} objects representing all interfaces implemented by the given - * class and its superclasses.

+ *

Gets an array of {@link Class} objects representing all interfaces implemented by the given class and its + * superclasses.

+ *

+ *

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.

+ *

+ * Note: Implementation of this method was "borrowed" from Jakarta + * Commons Lang to avoid a dependency.

* - *

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.

- *

- * Note: Implementation of this method was "borrowed" from - * Jakarta Commons Lang to avoid a dependency. - *

- * @param cls the class to look up, may be null - * @return an array of {@link Class} objects representing all interfaces implemented by the given - * class and its superclasses or null if input class is null. + * @param cls the class to look up, may be null + * @return an array of {@link Class} objects representing all interfaces implemented by the given class and its + * superclasses or null 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. - *

- * Examples: - *

    - *
  • getJavaClassName( Object[].class ) == "Object[]"
  • - *
  • getJavaClassName( Object[][].class ) == "Object[][]"
  • - *
  • getJavaClassName( Integer.TYPE ) == "int"
  • - *

    + *

    + * Examples:

    • getJavaClassName( Object[].class ) == "Object[]"
    • getJavaClassName( + * Object[][].class ) == "Object[][]"
    • getJavaClassName( Integer.TYPE ) == "int"
    • + * * @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 {@link ProxyFactory} implementation for the current environment. + * The implementation class search order is as follows: + *
        + *
      • Try to use the type indicated by the system property "commons-proxy.factory"
      • + *
      • Try to use {@link org.apache.commons.proxy.factory.javassist.JavassistProxyFactory}
      • + *
      • Try to use {@link org.apache.commons.proxy.factory.cglib.CglibProxyFactory}
      • + *
      • Default to {@link org.apache.commons.proxy.factory.reflect.ReflectionProxyFactory} (should always be available)
      • + *
      + * @param classLoader the class loader to use to instantiate the proxy factory + * @return an appropriate {@link 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 {@link ProxyFactory} implementation for the current environment. + * The implementation class search order is as follows: + *
        + *
      • Try to the type indicated by the system property "commons-proxy.factory"
      • + *
      • Try to use {@link org.apache.commons.proxy.factory.javassist.JavassistProxyFactory}
      • + *
      • Try to use {@link org.apache.commons.proxy.factory.cglib.CglibProxyFactory}
      • + *
      • Default to {@link org.apache.commons.proxy.factory.reflect.ReflectionProxyFactory} (should always be available)
      • + *
      + *

      + * Note: This implementation uses the current thread's context class loader! + * @return an appropriate {@link 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: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org