1 17 18 package org.apache.avalon.repository.main; 19 20 import java.io.File ; 21 import java.io.InputStream ; 22 import java.io.FileInputStream ; 23 import java.io.IOException ; 24 import java.util.Properties ; 25 26 import javax.naming.directory.Attributes ; 27 28 import org.apache.avalon.repository.Artifact; 29 import org.apache.avalon.repository.Repository; 30 import org.apache.avalon.repository.RepositoryException; 31 import org.apache.avalon.repository.RepositoryRuntimeException; 32 import org.apache.avalon.repository.meta.FactoryDescriptor; 33 import org.apache.avalon.repository.provider.Builder; 34 import org.apache.avalon.repository.provider.InitialContext; 35 import org.apache.avalon.repository.provider.Factory; 36 37 38 45 public class DefaultBuilder extends AbstractBuilder implements Builder 46 { 47 51 60 public static Artifact createImplementationArtifact( 61 ClassLoader classloader, File base, 62 String resource, String key ) throws Exception 63 { 64 return createImplementationArtifact( 65 classloader, null, base, resource, key ); 66 } 67 68 78 public static Artifact createImplementationArtifact( 79 ClassLoader classloader, File system, File base, 80 String resource, String key ) throws Exception 81 { 82 87 String spec = 88 getLocalProperties( base, resource ).getProperty( key ); 89 90 95 if( null == spec ) 96 { 97 spec = 98 getLocalProperties( USER, resource ).getProperty( key ); 99 } 100 101 106 if( null == spec ) 107 { 108 spec = 109 getLocalProperties( system, resource ).getProperty( key ); 110 } 111 112 117 if( null == spec ) 118 { 119 Properties properties = new Properties (); 120 InputStream input = classloader.getResourceAsStream( resource ); 121 if( input == null ) 122 { 123 final String error = 124 "Missing resource: [" + resource + "]"; 125 throw new IllegalStateException ( error ); 126 } 127 properties.load( input ); 128 spec = properties.getProperty( key ); 129 if( spec == null ) 130 { 131 final String error = 132 "Missing property: [" + key + "] in resource: [" + resource + "]"; 133 throw new IllegalStateException ( error ); 134 } 135 } 136 137 141 return Artifact.createArtifact( spec ); 142 } 143 144 148 private static Properties getLocalProperties( 149 File dir, String filename ) throws IOException 150 { 151 Properties properties = new Properties (); 152 if( null == dir ) return properties; 153 File file = new File ( dir, filename ); 154 if( !file.exists() ) return properties; 155 properties.load( new FileInputStream ( file ) ); 156 return properties; 157 } 158 159 private static final File USER = 160 new File ( System.getProperty( "user.home" ) ); 161 162 166 171 private final Repository m_repository; 172 173 177 private final ClassLoader m_classloader; 178 179 184 private final InitialContext m_context; 185 186 189 private final Class m_class; 190 191 194 private final Factory m_delegate; 195 196 200 207 public DefaultBuilder( InitialContext context, Artifact artifact ) 208 throws Exception 209 { 210 this( context, null, artifact ); 211 } 212 213 221 public DefaultBuilder( 222 InitialContext context, ClassLoader classloader, Artifact artifact ) 223 throws Exception 224 { 225 if( null == context ) throw new NullPointerException ( "context" ); 226 if( null == artifact ) throw new NullPointerException ( "artifact" ); 227 228 m_context = context; 229 230 ClassLoader parent = getClassLoader( classloader ); 231 232 m_repository = m_context.getRepository(); 233 234 Attributes attributes = m_repository.getAttributes( artifact ); 235 FactoryDescriptor descriptor = new FactoryDescriptor( attributes ); 236 String classname = descriptor.getFactory(); 237 if( null == classname ) 238 { 239 final String error = 240 "Required property 'avalon.artifact.factory' not present in artifact: [" 241 + artifact + "] under the active repository: [" + m_repository + "]."; 242 throw new IllegalArgumentException ( error ); 243 } 244 245 m_classloader = m_repository.getClassLoader( parent, artifact ); 246 m_class = loadFactoryClass( m_classloader, classname ); 247 248 if( Factory.class.isAssignableFrom( m_class ) ) 249 { 250 try 251 { 252 m_delegate = createDelegate( m_classloader, m_class, m_context ); 253 } 254 catch( Throwable e ) 255 { 256 final String error = 257 "Unable to establish a factory for the supplied artifact:"; 258 StringBuffer buffer = new StringBuffer ( error ); 259 buffer.append( "\n artifact: " + artifact ); 260 buffer.append( "\n build: " + descriptor.getBuild() ); 261 buffer.append( "\n factory: " + descriptor.getFactory() ); 262 buffer.append( "\n source: " 263 + m_class.getProtectionDomain().getCodeSource().getLocation() ); 264 buffer.append( "\n repository: " + m_repository ); 265 throw new RepositoryException( buffer.toString(), e ); 266 } 267 } 268 else 269 { 270 m_delegate = null; 271 } 272 } 273 274 278 282 public Class getFactoryClass() 283 { 284 return m_class; 285 } 286 287 294 public Factory getFactory() 295 { 296 if( null != m_delegate ) 297 { 298 return m_delegate; 299 } 300 else 301 { 302 final String error = 303 "Supplied class [" + m_class.getName() 304 + "] does not implement the Factory interface."; 305 throw new RepositoryRuntimeException( error ); 306 } 307 } 308 309 314 public ClassLoader getClassLoader() 315 { 316 return m_classloader; 317 } 318 319 323 private ClassLoader getClassLoader( ClassLoader classloader ) 324 { 325 if( null != classloader ) return classloader; 326 return DefaultBuilder.class.getClassLoader(); 327 } 328 } 329 | Popular Tags |