Return-Path:
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.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 @@
}
/**
- *
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 benull
- * @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: - *
+ * 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