Once the initialization phase is complete, the bootstrap controller will call the Application.activate() method to start things up.
Recap of sequence;
Luckily these steps have been simplified with convenience decorators, so that your code becomes simpler.
There are 3 levels of convenience provided, depending on your needs. Singelton, "Pancake"-layering and Full assembly.
Let's take a closer look at how it is put together.
ObjectBuilderFactory factory = assembler.objectBuilderFactory();
Stuff stuff = factory.newObjectBuilder( Stuff.class ).newInstance();
The SingletonAssembler also makes common system resources available from the bootstrap code, such as ObjectBuilderFactory, UnitOfWorkFactory and others. This is possible since there is only one Module.
ApplicationModelSPI model = newApplication( assemblers );
ApplicationSPI runtime = model.newInstance( qi4j.spi() );
runtime.activate();
}
private static ApplicationModelSPI newApplication(
final Assembler[][][] assemblers )
throws AssemblyException
{
return qi4j.newApplicationModel( new ApplicationAssembler()
{
public ApplicationAssembly assemble(
ApplicationAssemblyFactory factory )
throws AssemblyException
{
return factory.newApplicationAssembly( assemblers );
}
} );
}
In principle, you first start the Qi4j runtime, call newApplication with a ApplicationAssembler instance and call activate() on the returned application. The ApplicationAssembler instance will be called with a ApplicationFactory, which is used to create an ApplicationAssembly describing the application structure.
public static void main( String[] args )
throws Exception
{
// Create a Qi4j Runtime
qi4j = new Energy4Java();
application = qi4j.newApplication( new ApplicationAssembler()
{
public ApplicationAssembly assemble(
ApplicationAssemblyFactory applicationFactory )
throws AssemblyException
{
ApplicationAssembly assembly =
applicationFactory.newApplicationAssembly();
buildAssembly( assembly );
return assembly;
}
} );
// activate the application
application.activate();
}
static void buildAssembly( ApplicationAssembly app )
{
LayerAssembly webLayer = createWebLayer( app );
LayerAssembly domainLayer = createDomainLayer( app );
LayerAssembly persistenceLayer = createInfrastructureLayer( app );
LayerAssembly authLayer = createAuth2Layer( app );
LayerAssembly messagingLayer = createMessagingLayer( app );
webLayer.uses( domainLayer );
domainLayer.uses( authLayer );
domainLayer.uses( persistenceLayer );
domainLayer.uses( messagingLayer );
}
static LayerAssembly createWebLayer( ApplicationAssembly app )
{
LayerAssembly layer = app.newLayerAssembly( "web-layer" );
createCustomerWebModule( layer );
return layer;
}
static LayerAssembly createDomainLayer( ApplicationAssembly app )
{
LayerAssembly layer = app.newLayerAssembly( "domain-layer" );
createCustomerDomainModule( layer );
// :
// :
return layer;
}
static LayerAssembly createInfrastructureLayer( ApplicationAssembly app )
{
LayerAssembly layer = app.newLayerAssembly( "infrastructure-layer" );
createPersistenceModule( layer );
return layer;
}
static LayerAssembly createMessagingLayer( ApplicationAssembly app )
{
LayerAssembly layer = app.newLayerAssembly( "messaging-layer" );
createWebServiceModule( layer );
createPersistenceModule( layer );
return layer;
}
static LayerAssembly createAuth2Layer( ApplicationAssembly application )
{
LayerAssembly layer = application.newLayerAssembly( "auth2-layer" );
createAuthModule( layer );
return layer;
}
static void createCustomerWebModule( LayerAssembly layer )
{
ModuleAssembly assembly =
layer.newModuleAssembly( "customer-web-module" );
assembly.addTransients( CustomerViewComposite.class );
assembly.addTransients( CustomerEditComposite.class );
assembly.addTransients( CustomerListViewComposite.class );
assembly.addTransients( CustomerSearchComposite.class );
}
static void createCustomerDomainModule( LayerAssembly layer )
{
ModuleAssembly assembly =
layer.newModuleAssembly( "customer-domain-module" );
assembly.addEntities( CustomerEntity.class );
assembly.addEntities( CountryEntity.class );
assembly.addTransients( AddressComposite.class );
}
static void createAuthModule( LayerAssembly layer )
{
ModuleAssembly assembly = layer.newModuleAssembly( "auth-module" );
assembly.addAssembler( new LdapAuthenticationAssembler() );
assembly.addAssembler( new ThrinkAuthorizationAssembler() );
assembly.addAssembler( new UserTrackingAuditAssembler() );
}
static void createPersistenceModule( LayerAssembly layer )
{
ModuleAssembly assembly =
layer.newModuleAssembly( "persistence-module" );
// Someone has created an assembler for the Neo EntityStore
NeoAssembler neo = new NeoAssembler( "./neostore" );
assembly.addAssembler( neo );
}