{
if ( !moduleRoot.exists() || !srcOutputRoot.exists() )
throw new IOException( "Directories " + moduleRoot + " or " + srcOutputRoot + " don't exist!");
if ( factoryName == null )
throw new ControlAssemblyException( "Missing context factory names" );
if ( cl == null )
throw new ControlAssemblyException( "Must specify a classloader" );
ClassLoader origCL = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader( cl );
try
{
// Create the requested ControlAssemblyContext.Factory
Class factoryClass = cl.loadClass( factoryName );
ControlAssemblyContext.Factory factory = (ControlAssemblyContext.Factory)factoryClass.newInstance();
// Iterate over control types
Set<String> controlTypes = controlTypeToImpl.keySet();
for ( String ct : controlTypes )
{
// Search for applicable ControlAssemblers as specified on the control impls
String cImpl = controlTypeToImpl.get( ct );
Class cImplClass = cl.loadClass( cImpl );
ControlImplementation a = (ControlImplementation)cImplClass.getAnnotation(ControlImplementation.class);
if ( a == null )
throw new ControlAssemblyException( "Control implementation class=" + cImpl + " missing ControlImplementation annotation" );
// For each non-default ControlAssembler, create one and call it.
Class<? extends ControlAssembler> assemblerClass = a.assembler();
if ( !assemblerClass.equals(DefaultControlAssembler.class) )
{
ControlAssembler assembler = assemblerClass.newInstance();
Set<String> clients = controlTypeToClients.get( ct );
ControlAssemblyContext cac = factory.newInstance(
cl.loadClass(ct), null, clients, moduleRoot, moduleName, srcOutputRoot );
assembler.assemble( cac );
}
}
}
catch ( ControlAssemblyException cae )
{
// just rethrow ControlAssemblyExceptions, which will typically come from user-provided assemblers.
throw cae;
}
catch ( Throwable t )
{
// Not expecting any throwables other than ControlAssemblyExceptions, so consider them as
// unexpected infrastructure issues and wrap them in a CAE.
throw new ControlAssemblyException( "Assembly infrastructure exception", t);
}
finally
{
Thread.currentThread().setContextClassLoader( origCL );
}