use of org.qi4j.bootstrap.AssemblyException in project qi4j-sdk by Qi4j.
the class ApplicationBuilder method newApplication.
/**
* Create and activate a new Application.
* @return Activated Application
* @throws AssemblyException if the assembly failed
* @throws ActivationException if the activation failed
*/
public Application newApplication() throws AssemblyException, ActivationException {
Energy4Java qi4j = new Energy4Java();
ApplicationDescriptor model = qi4j.newApplicationModel(new ApplicationAssembler() {
@Override
public ApplicationAssembly assemble(ApplicationAssemblyFactory factory) throws AssemblyException {
ApplicationAssembly assembly = factory.newApplicationAssembly();
assembly.setName(applicationName);
HashMap<String, LayerAssembly> createdLayers = new HashMap<>();
for (Map.Entry<String, LayerDeclaration> entry : layers.entrySet()) {
LayerAssembly layer = entry.getValue().createLayer(assembly);
createdLayers.put(entry.getKey(), layer);
}
for (LayerDeclaration layer : layers.values()) {
layer.initialize(createdLayers);
}
return assembly;
}
});
Application application = model.newInstance(qi4j.api());
for (ActivationEventListener activationListener : activationListeners) {
application.registerActivationEventListener(activationListener);
}
beforeActivation();
application.activate();
afterActivation();
return application;
}
use of org.qi4j.bootstrap.AssemblyException in project qi4j-sdk by Qi4j.
the class ApplicationDocs method main.
public static void main(String[] args) throws Exception {
qi4j = new Energy4Java();
ApplicationDescriptor model = qi4j.newApplicationModel(new ApplicationAssembler() {
@Override
public ApplicationAssembly assemble(ApplicationAssemblyFactory applicationFactory) throws AssemblyException {
return createAssembly(applicationFactory);
}
});
Application application = model.newInstance(qi4j.spi());
}
use of org.qi4j.bootstrap.AssemblyException in project qi4j-sdk by Qi4j.
the class ApplicationModelFactoryImpl method newApplicationModel.
@Override
public ApplicationDescriptor newApplicationModel(ApplicationAssembly assembly) throws AssemblyException {
AssemblyHelper helper = new AssemblyHelper();
ApplicationAssemblyImpl applicationAssembly = (ApplicationAssemblyImpl) assembly;
ActivatorsModel<Application> applicationActivators = new ActivatorsModel<>(applicationAssembly.activators());
List<LayerModel> layerModels = new ArrayList<>();
final ApplicationModel applicationModel = new ApplicationModel(applicationAssembly.name(), applicationAssembly.version(), applicationAssembly.mode(), applicationAssembly.metaInfo(), applicationActivators, layerModels);
Map<LayerAssembly, LayerModel> mapAssemblyModel = new HashMap<>();
Map<LayerAssembly, List<LayerModel>> mapUsedLayers = new HashMap<>();
// Build all layers
List<LayerAssemblyImpl> layerAssemblies = new ArrayList<>(applicationAssembly.layerAssemblies());
for (LayerAssemblyImpl layerAssembly : layerAssemblies) {
List<LayerModel> usedLayers = new ArrayList<>();
mapUsedLayers.put(layerAssembly, usedLayers);
UsedLayersModel usedLayersModel = new UsedLayersModel(usedLayers);
List<ModuleModel> moduleModels = new ArrayList<>();
String name = layerAssembly.name();
if (name == null) {
throw new AssemblyException("Layer must have name set");
}
ActivatorsModel<Layer> layerActivators = new ActivatorsModel<>(layerAssembly.activators());
LayerModel layerModel = new LayerModel(name, layerAssembly.metaInfo(), usedLayersModel, layerActivators, moduleModels);
for (ModuleAssemblyImpl moduleAssembly : layerAssembly.moduleAssemblies()) {
moduleModels.add(moduleAssembly.assembleModule(helper));
}
mapAssemblyModel.put(layerAssembly, layerModel);
layerModels.add(layerModel);
}
// Populate used layer lists
for (LayerAssemblyImpl layerAssembly : layerAssemblies) {
Set<LayerAssembly> usesLayers = layerAssembly.uses();
List<LayerModel> usedLayers = mapUsedLayers.get(layerAssembly);
for (LayerAssembly usesLayer : usesLayers) {
LayerModel layerModel = mapAssemblyModel.get(usesLayer);
usedLayers.add(layerModel);
}
}
// This will resolve all dependencies
try {
// applicationModel.bind();
applicationModel.accept(new BindingVisitor(applicationModel));
} catch (BindingException e) {
throw new AssemblyException("Unable to bind: " + applicationModel, e);
}
return applicationModel;
}
use of org.qi4j.bootstrap.AssemblyException in project qi4j-sdk by Qi4j.
the class StructureTest method createApplicationUsingApplicationAssembly.
@Test
public void createApplicationUsingApplicationAssembly() throws AssemblyException {
Energy4Java boot = new Energy4Java();
boot.newApplication(new ApplicationAssembler() {
public ApplicationAssembly assemble(ApplicationAssemblyFactory applicationFactory) throws AssemblyException {
ApplicationAssembly applicationAssembly = applicationFactory.newApplicationAssembly();
// Application Layer
LayerAssembly applicationLayer = applicationAssembly.layer("Application");
ModuleAssembly applicationModule = applicationLayer.module("Application");
new DomainApplicationAssembler().assemble(applicationModule);
// View Layer
LayerAssembly viewLayer = applicationAssembly.layer("View");
ModuleAssembly viewModule = viewLayer.module("View");
new ViewAssembler().assemble(viewModule);
viewLayer.uses(applicationLayer);
return applicationAssembly;
}
});
}
use of org.qi4j.bootstrap.AssemblyException in project qi4j-sdk by Qi4j.
the class TypeToCompositeLookupTest method objectsAmbiguousDeclaration.
@Test
public void objectsAmbiguousDeclaration() throws ActivationException, AssemblyException {
Module module = new SingletonAssembler() {
@Override
public void assemble(ModuleAssembly module) throws AssemblyException {
module.objects(SomeOtherFooImpl.class, BasicFooImpl.class);
}
}.module();
assertEquals(CATHEDRAL, module.newObject(SomeOtherFooImpl.class).bar());
assertEquals(BAZAR, module.newObject(BasicFooImpl.class).bar());
try {
module.newObject(Foo.class);
fail("Ambiguous type exception not detected for Objects");
} catch (AmbiguousTypeException expected) {
}
}
Aggregations