1 package org.apache.beehive.controls.runtime.assembly; 2 3 20 21 import org.apache.beehive.controls.api.bean.ControlImplementation; 22 import org.apache.beehive.controls.api.assembly.ControlAssemblyContext; 23 import org.apache.beehive.controls.api.assembly.ControlAssemblyException; 24 import org.apache.beehive.controls.api.assembly.ControlAssembler; 25 import org.apache.beehive.controls.api.assembly.DefaultControlAssembler; 26 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.util.Map ; 30 import java.util.Set ; 31 32 35 public class Assembler 36 { 37 52 public static void assemble( File moduleRoot, 53 String moduleName, 54 File srcOutputRoot, 55 String factoryName, 56 Map <String ,String > controlTypeToImpl, 57 Map <String ,Set <String >> controlTypeToClients, 58 ClassLoader cl ) 59 throws ControlAssemblyException, IOException 60 { 61 if ( !moduleRoot.exists() || !srcOutputRoot.exists() ) 62 throw new IOException ( "Directories " + moduleRoot + " or " + srcOutputRoot + " don't exist!"); 63 64 if ( factoryName == null ) 65 throw new ControlAssemblyException( "Missing context factory names" ); 66 67 if ( cl == null ) 68 throw new ControlAssemblyException( "Must specify a classloader" ); 69 70 ClassLoader origCL = Thread.currentThread().getContextClassLoader(); 71 Thread.currentThread().setContextClassLoader( cl ); 72 73 try 74 { 75 Class factoryClass = cl.loadClass( factoryName ); 77 ControlAssemblyContext.Factory factory = (ControlAssemblyContext.Factory)factoryClass.newInstance(); 78 79 Set <String > controlTypes = controlTypeToImpl.keySet(); 81 for ( String ct : controlTypes ) 82 { 83 String cImpl = controlTypeToImpl.get( ct ); 85 Class cImplClass = cl.loadClass( cImpl ); 86 87 ControlImplementation a = (ControlImplementation)cImplClass.getAnnotation(ControlImplementation.class); 88 if ( a == null ) 89 throw new ControlAssemblyException( "Control implementation class=" + cImpl + " missing ControlImplementation annotation" ); 90 91 Class <? extends ControlAssembler> assemblerClass = a.assembler(); 93 if ( !assemblerClass.equals(DefaultControlAssembler.class) ) 94 { 95 ControlAssembler assembler = assemblerClass.newInstance(); 96 Set <String > clients = controlTypeToClients.get( ct ); 97 ControlAssemblyContext cac = factory.newInstance( 98 cl.loadClass(ct), null, clients, moduleRoot, moduleName, srcOutputRoot ); 99 assembler.assemble( cac ); 100 } 101 } 102 } 103 catch ( ControlAssemblyException cae ) 104 { 105 throw cae; 107 } 108 catch ( Throwable t ) 109 { 110 throw new ControlAssemblyException( "Assembly infrastructure exception", t); 113 } 114 finally 115 { 116 Thread.currentThread().setContextClassLoader( origCL ); 117 } 118 } 119 } 120 | Popular Tags |