1 17 18 package org.apache.avalon.repository.main ; 19 20 import java.lang.reflect.Constructor ; 21 22 import org.apache.avalon.repository.RepositoryException; 23 import org.apache.avalon.repository.provider.Factory; 24 import org.apache.avalon.repository.provider.InitialContext; 25 26 27 34 public abstract class AbstractBuilder 35 { 36 43 protected Class loadFactoryClass( ClassLoader classloader, String factory ) 44 throws RepositoryException 45 { 46 try 47 { 48 return classloader.loadClass( factory ); 49 } 50 catch( ClassNotFoundException e ) 51 { 52 final String error = 53 "Could not find the factory class: " + factory; 54 throw new RepositoryException( error, e ); 55 } 56 catch( Throwable e ) 57 { 58 final String error = 59 "Unable to load factory class: [" 60 + factory 61 + "]."; 62 throw new RepositoryException( error, e ); 63 } 64 } 65 66 83 protected Factory createDelegate( 84 ClassLoader classloader, Class clazz, InitialContext context ) 85 throws RepositoryException 86 { 87 if( null == classloader ) throw new NullPointerException ( "classloader" ); 88 if( null == clazz ) throw new NullPointerException ( "clazz" ); 89 if( null == context ) throw new NullPointerException ( "context" ); 90 91 try 92 { 93 Constructor constructor = 94 clazz.getConstructor( 95 new Class []{ InitialContext.class, ClassLoader .class } ); 96 return createFactory( 97 constructor, new Object []{ context, classloader } ); 98 } 99 catch( NoSuchMethodException e ) 100 { 101 try 102 { 103 Constructor constructor = 104 clazz.getConstructor( 105 new Class []{ InitialContext.class } ); 106 return createFactory( 107 constructor, new Object []{ context } ); 108 } 109 catch( NoSuchMethodException ee ) 110 { 111 try 112 { 113 Constructor constructor = 114 clazz.getConstructor( 115 new Class []{ ClassLoader .class } ); 116 return createFactory( 117 constructor, new Object []{ classloader } ); 118 } 119 catch( NoSuchMethodException eee ) 120 { 121 try 122 { 123 Constructor constructor = 124 clazz.getConstructor( 125 new Class [0] ); 126 return createFactory( 127 constructor, new Object [0] ); 128 } 129 catch( NoSuchMethodException eeee ) 130 { 131 StringBuffer buffer = new StringBuffer (); 132 buffer.append( "Supplied factory class [" ); 133 buffer.append( clazz.getName() ); 134 buffer.append( 135 " ] does not implement a recognized constructor." ); 136 throw new RepositoryException( buffer.toString() ); 137 } 138 } 139 } 140 } 141 } 142 143 152 private Factory createFactory( 153 Constructor constructor, Object [] args ) 154 throws RepositoryException 155 { 156 Class clazz = constructor.getDeclaringClass(); 157 try 158 { 159 return (Factory) constructor.newInstance( args ); 160 } 161 catch( Throwable e ) 162 { 163 final String error = 164 "Error while attempting to instantiate the factory: [" 165 + clazz.getName() 166 + "]."; 167 throw new RepositoryException( error, e ); 168 } 169 } 170 } 171 | Popular Tags |