1 17 18 package org.apache.avalon.fortress.impl.factory; 19 20 import org.apache.excalibur.mpool.ObjectFactory; 21 22 import java.lang.reflect.Constructor ; 23 24 32 public final class ProxyManager 33 { 34 public static final int DISCOVER = 0; 35 public static final int NONE = 1; 36 public static final int BCEL = 2; 37 public static final int PROXY = 3; 38 private static final String BCEL_CLASS = "org.apache.bcel.classfile.JavaClass"; 39 private static final String BCEL_WRAPPER = "org.apache.avalon.fortress.impl.factory.WrapperObjectFactory"; 40 private static final String PROXY_WRAPPER = "org.apache.avalon.fortress.impl.factory.ProxyObjectFactory"; 41 private static final String NOOP_WRAPPER = "org.apache.avalon.fortress.impl.factory.NoopObjectFactory"; 42 private static final boolean m_isBCELPresent; 43 private final String m_wrapperClassName; 44 private Class m_factoryClass; 45 46 static 47 { 48 boolean bcelPresent = false; 49 try 50 { 51 Thread.currentThread().getContextClassLoader().loadClass( BCEL_CLASS ); 52 Thread.currentThread().getContextClassLoader().loadClass( BCEL_WRAPPER ); 53 bcelPresent = true; 54 } 55 catch ( ClassNotFoundException cfne ) 56 { 57 } 59 60 m_isBCELPresent = bcelPresent; 61 } 62 63 public ProxyManager( final int type ) throws Exception 64 { 65 switch (type) 66 { 67 case NONE: 68 m_wrapperClassName = NOOP_WRAPPER; 69 break; 70 71 case BCEL: 72 if ( ! m_isBCELPresent ) throw new IllegalStateException ("BCEL is not available"); 73 m_wrapperClassName = BCEL_WRAPPER; 74 break; 75 76 case PROXY: 77 m_wrapperClassName = PROXY_WRAPPER; 78 break; 79 80 default: if ( m_isBCELPresent ) 82 { 83 m_wrapperClassName = BCEL_WRAPPER; 84 } 85 else 86 { 87 m_wrapperClassName = PROXY_WRAPPER; 88 } 89 break; 90 } 91 } 92 93 public ObjectFactory getWrappedObjectFactory( final ObjectFactory source ) throws Exception 94 { 95 if ( null == m_factoryClass ) 96 { 97 final ClassLoader loader = source.getClass().getClassLoader(); 98 99 m_factoryClass = loader.loadClass( m_wrapperClassName ); 100 } 101 102 final Constructor constr = m_factoryClass.getConstructor( new Class []{ObjectFactory.class} ); 103 return (ObjectFactory) constr.newInstance( new Object []{source} ); 104 } 105 } 106 | Popular Tags |