Search in sources :

Example 1 with AssemblyException

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;
}
Also used : ApplicationAssemblyFactory(org.qi4j.bootstrap.ApplicationAssemblyFactory) HashMap(java.util.HashMap) ApplicationAssembly(org.qi4j.bootstrap.ApplicationAssembly) ApplicationDescriptor(org.qi4j.api.structure.ApplicationDescriptor) LayerAssembly(org.qi4j.bootstrap.LayerAssembly) AssemblyException(org.qi4j.bootstrap.AssemblyException) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) ActivationEventListener(org.qi4j.api.activation.ActivationEventListener) Energy4Java(org.qi4j.bootstrap.Energy4Java) Application(org.qi4j.api.structure.Application)

Example 2 with AssemblyException

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());
}
Also used : ApplicationAssemblyFactory(org.qi4j.bootstrap.ApplicationAssemblyFactory) AssemblyException(org.qi4j.bootstrap.AssemblyException) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) ApplicationAssembly(org.qi4j.bootstrap.ApplicationAssembly) Energy4Java(org.qi4j.bootstrap.Energy4Java) Application(org.qi4j.api.structure.Application) ApplicationDescriptor(org.qi4j.api.structure.ApplicationDescriptor)

Example 3 with AssemblyException

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;
}
Also used : LayerModel(org.qi4j.runtime.structure.LayerModel) UsedLayersModel(org.qi4j.runtime.structure.UsedLayersModel) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ApplicationModel(org.qi4j.runtime.structure.ApplicationModel) ModuleModel(org.qi4j.runtime.structure.ModuleModel) AssemblyException(org.qi4j.bootstrap.AssemblyException) ArrayList(java.util.ArrayList) List(java.util.List) ActivatorsModel(org.qi4j.runtime.activation.ActivatorsModel) Layer(org.qi4j.api.structure.Layer) LayerAssembly(org.qi4j.bootstrap.LayerAssembly) BindingException(org.qi4j.bootstrap.BindingException) Application(org.qi4j.api.structure.Application)

Example 4 with AssemblyException

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;
        }
    });
}
Also used : ApplicationAssemblyFactory(org.qi4j.bootstrap.ApplicationAssemblyFactory) AssemblyException(org.qi4j.bootstrap.AssemblyException) ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) ApplicationAssembly(org.qi4j.bootstrap.ApplicationAssembly) Energy4Java(org.qi4j.bootstrap.Energy4Java) LayerAssembly(org.qi4j.bootstrap.LayerAssembly) Test(org.junit.Test)

Example 5 with AssemblyException

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) {
    }
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) AssemblyException(org.qi4j.bootstrap.AssemblyException) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) Module(org.qi4j.api.structure.Module) AmbiguousTypeException(org.qi4j.api.composite.AmbiguousTypeException) Test(org.junit.Test)

Aggregations

AssemblyException (org.qi4j.bootstrap.AssemblyException)38 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)27 Test (org.junit.Test)23 Module (org.qi4j.api.structure.Module)14 SingletonAssembler (org.qi4j.bootstrap.SingletonAssembler)14 IOException (java.io.IOException)10 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)10 ApplicationAssembler (org.qi4j.bootstrap.ApplicationAssembler)10 Assembler (org.qi4j.bootstrap.Assembler)10 ApplicationAssemblyFactory (org.qi4j.bootstrap.ApplicationAssemblyFactory)9 Application (org.qi4j.api.structure.Application)8 ApplicationAssembly (org.qi4j.bootstrap.ApplicationAssembly)8 Energy4Java (org.qi4j.bootstrap.Energy4Java)8 LayerAssembly (org.qi4j.bootstrap.LayerAssembly)5 EntityTestAssembler (org.qi4j.test.EntityTestAssembler)5 AmbiguousTypeException (org.qi4j.api.composite.AmbiguousTypeException)4 ApplicationDescriptor (org.qi4j.api.structure.ApplicationDescriptor)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 JettyConfiguration (org.qi4j.library.http.JettyConfiguration)2