Search in sources :

Example 11 with ApplicationAssembler

use of org.qi4j.bootstrap.ApplicationAssembler in project qi4j-sdk by Qi4j.

the class WicketQi4jApplication method getAssembler.

/**
 * Qi4j Assembler
 *
 * To let the custom application class (DCISampleApplication_x) focus on starting up the
 * Wicket environment, I made a convention of having Qi4j Assembler files in an 'assembly'
 * folder beside the custom application class.
 *
 * There's always only one application file, but we could split the assemblage into several
 * files ie. one for each layer. In that case, the Assembler file would be distributing to
 * the individual LayerXAssembler classes.
 *
 * If you like, you can also override this method in the custom application class and simply
 * return an instance of YourAssembler:
 *
 * @Override protected ApplicationAssembler getAssembler() {
 * return new YourAssemblerInAnyPath();
 * }
 */
protected ApplicationAssembler getAssembler() throws Exception {
    String appPath = getClass().getCanonicalName();
    String expectedPathFromApplication = ".assembly.Assembler";
    String assemblerPath = appPath.substring(0, appPath.lastIndexOf(".")) + expectedPathFromApplication;
    try {
        return (ApplicationAssembler) Class.forName(assemblerPath).newInstance();
    } catch (ClassNotFoundException e) {
        throw new Exception("Couldn't find Qi4j assembler in path '" + assemblerPath + "'");
    }
}
Also used : ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException)

Example 12 with ApplicationAssembler

use of org.qi4j.bootstrap.ApplicationAssembler in project qi4j-sdk by Qi4j.

the class DocumentationSupport method assembledWithValuesModuleSerialization.

// END SNIPPET: io
@Test
public // TODO Include in each ValueSerialization extensions documentation
void assembledWithValuesModuleSerialization() throws Exception {
    Application app = new Energy4Java().newApplication(new ApplicationAssembler() {

        @Override
        public ApplicationAssembly assemble(ApplicationAssemblyFactory applicationFactory) throws AssemblyException {
            Assembler[][][] pancakes = new Assembler[][][] { { { new Assembler() {

                @Override
                public void assemble(ModuleAssembly valuesModule) throws AssemblyException {
                    valuesModule.layer().setName("SINGLE-Layer");
                    valuesModule.setName("VALUES-Module");
                    valuesModule.values(SomeValue.class);
                }
            } }, { new Assembler() {

                @Override
                public void assemble(ModuleAssembly servicesModule) throws AssemblyException {
                    servicesModule.setName("SERVICES-Module");
                    Function<Application, Module> valuesModuleFinder = new Function<Application, Module>() {

                        @Override
                        public Module map(Application app) {
                            return app.findModule("SINGLE-Layer", "VALUES-Module");
                        }
                    };
                    new OrgJsonValueSerializationAssembler().withValuesModuleFinder(valuesModuleFinder).assemble(servicesModule);
                }
            } } } };
            return applicationFactory.newApplicationAssembly(pancakes);
        }
    });
    app.activate();
    try {
        Module valuesModule = app.findModule("SINGLE-Layer", "VALUES-Module");
        SomeValue someValue = someNewValueInstance(valuesModule);
        Module servicesModule = app.findModule("SINGLE-Layer", "SERVICES-Module");
        ValueSerialization valueSerialization = servicesModule.findService(ValueSerialization.class).get();
        String json = valueSerialization.serialize(someValue);
        assertThat(json, equalTo("{\"foo\":\"bar\"}"));
        SomeValue someNewValue = valueSerialization.deserialize(SomeValue.class, json);
        assertThat(someNewValue, equalTo(someValue));
    } finally {
        app.passivate();
    }
}
Also used : ApplicationAssemblyFactory(org.qi4j.bootstrap.ApplicationAssemblyFactory) ApplicationAssembly(org.qi4j.bootstrap.ApplicationAssembly) AssemblyException(org.qi4j.bootstrap.AssemblyException) ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) Function(org.qi4j.functional.Function) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) Energy4Java(org.qi4j.bootstrap.Energy4Java) Assembler(org.qi4j.bootstrap.Assembler) OrgJsonValueSerializationAssembler(org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationAssembler) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) Module(org.qi4j.api.structure.Module) Application(org.qi4j.api.structure.Application) OrgJsonValueSerializationAssembler(org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationAssembler) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 13 with ApplicationAssembler

use of org.qi4j.bootstrap.ApplicationAssembler in project qi4j-sdk by Qi4j.

the class IssueTest method testLayersCanBeCreatedInOrderDifferentFromTheirDependency.

@Test
public void testLayersCanBeCreatedInOrderDifferentFromTheirDependency() throws AssemblyException {
    Energy4Java qi4j = new Energy4Java();
    Application app = qi4j.newApplication(new ApplicationAssembler() {

        public ApplicationAssembly assemble(ApplicationAssemblyFactory applicationFactory) throws AssemblyException {
            ApplicationAssembly assembly = applicationFactory.newApplicationAssembly();
            LayerAssembly domainLayer = assembly.layer(null);
            domainLayer.setName("Domain");
            LayerAssembly infrastructureLayer = assembly.layer(null);
            infrastructureLayer.setName("Infrastructure");
            domainLayer.uses(infrastructureLayer);
            return assembly;
        }
    });
    ApplicationDescriptor model = app.descriptor();
    model.accept(new HierarchicalVisitorAdapter<Object, Object, RuntimeException>() {

        @Override
        public boolean visitEnter(Object visited) throws RuntimeException {
            return visited instanceof ApplicationDescriptor;
        }

        @Override
        public boolean visitLeave(Object visited) throws RuntimeException {
            return visited instanceof LayerDescriptor;
        }

        @Override
        public boolean visit(Object visited) throws RuntimeException {
            if (visited instanceof LayerDescriptor) {
                Iterable<? extends LayerDescriptor> usedLayers = ((LayerDescriptor) visited).usedLayers().layers();
                for (LayerDescriptor usedLayerModel : usedLayers) {
                    Assert.assertNotNull("Used layer model is null", usedLayerModel);
                }
            }
            return false;
        }
    });
}
Also used : ApplicationAssemblyFactory(org.qi4j.bootstrap.ApplicationAssemblyFactory) ApplicationAssembly(org.qi4j.bootstrap.ApplicationAssembly) LayerAssembly(org.qi4j.bootstrap.LayerAssembly) ApplicationDescriptor(org.qi4j.api.structure.ApplicationDescriptor) AssemblyException(org.qi4j.bootstrap.AssemblyException) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) Energy4Java(org.qi4j.bootstrap.Energy4Java) Application(org.qi4j.api.structure.Application) LayerDescriptor(org.qi4j.api.structure.LayerDescriptor) Test(org.junit.Test)

Example 14 with ApplicationAssembler

use of org.qi4j.bootstrap.ApplicationAssembler in project qi4j-sdk by Qi4j.

the class PrivateEntityUnitOfWorkTest method givenAppWithPrivateEntityWhenUnitOfWorkCanSeeItThenCanCommit.

@Test
public void givenAppWithPrivateEntityWhenUnitOfWorkCanSeeItThenCanCommit() throws Exception {
    System.setProperty("qi4j.compacttrace", "off");
    Energy4Java is = new Energy4Java();
    Application app = is.newApplication(new ApplicationAssembler() {

        public ApplicationAssembly assemble(ApplicationAssemblyFactory applicationFactory) throws AssemblyException {
            return applicationFactory.newApplicationAssembly(new Assembler[][][] { { { new Assembler() {

                public void assemble(ModuleAssembly module) throws AssemblyException {
                    module.objects(PrivateEntityUnitOfWorkTest.class);
                }
            } } }, { { new Assembler() {

                public void assemble(ModuleAssembly module) throws AssemblyException {
                    module.entities(ProductEntity.class);
                    module.entities(ProductCatalogEntity.class).visibleIn(application);
                    module.values(ProductInfo.class);
                    new EntityTestAssembler().assemble(module);
                }
            } } } });
        }
    });
    app.activate();
    Module module = app.findModule("Layer 1", "Module 1");
    module.injectTo(this);
    UnitOfWork unitOfWork = uowf.newUnitOfWork();
    try {
        unitOfWork.newEntity(ProductEntity.class);
        fail("Should not be able to create product here");
    } catch (EntityTypeNotFoundException e) {
        // Ok
        ProductCatalog catalog = unitOfWork.newEntity(ProductCatalog.class, "1");
        unitOfWork.complete();
    }
    unitOfWork = uowf.newUnitOfWork();
    String id;
    try {
        ProductCatalog catalog = unitOfWork.get(ProductCatalog.class, "1");
        id = ((Identity) catalog.newProduct()).identity().get();
        unitOfWork.complete();
    } finally {
        unitOfWork.discard();
    }
    unitOfWork = module.newUnitOfWork();
    try {
        ProductCatalog catalog = unitOfWork.get(ProductCatalog.class, "1");
        Product product = catalog.findProduct(id);
        product.price().set(100);
        unitOfWork.complete();
    } finally {
        unitOfWork.discard();
    }
}
Also used : ApplicationAssemblyFactory(org.qi4j.bootstrap.ApplicationAssemblyFactory) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) ApplicationAssembly(org.qi4j.bootstrap.ApplicationAssembly) AssemblyException(org.qi4j.bootstrap.AssemblyException) ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) EntityTestAssembler(org.qi4j.test.EntityTestAssembler) Energy4Java(org.qi4j.bootstrap.Energy4Java) Assembler(org.qi4j.bootstrap.Assembler) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) EntityTestAssembler(org.qi4j.test.EntityTestAssembler) Module(org.qi4j.api.structure.Module) Identity(org.qi4j.api.entity.Identity) Application(org.qi4j.api.structure.Application) Test(org.junit.Test)

Example 15 with ApplicationAssembler

use of org.qi4j.bootstrap.ApplicationAssembler in project qi4j-sdk by Qi4j.

the class Qi4jApplicationBootstrapListener method createNewApplication.

private Application createNewApplication(ServletContext context) {
    Energy4Java qi4j = new Energy4Java();
    // Try create assembler
    final ApplicationAssembler assembler = createAssembler();
    if (assembler != null) {
        try {
            return qi4j.newApplication(assembler);
        } catch (AssemblyException e) {
            throw new IllegalStateException(e);
        }
    }
    return null;
}
Also used : AssemblyException(org.qi4j.bootstrap.AssemblyException) ApplicationAssembler(org.qi4j.bootstrap.ApplicationAssembler) Energy4Java(org.qi4j.bootstrap.Energy4Java)

Aggregations

ApplicationAssembler (org.qi4j.bootstrap.ApplicationAssembler)15 AssemblyException (org.qi4j.bootstrap.AssemblyException)10 Energy4Java (org.qi4j.bootstrap.Energy4Java)10 ApplicationAssemblyFactory (org.qi4j.bootstrap.ApplicationAssemblyFactory)9 ApplicationAssembly (org.qi4j.bootstrap.ApplicationAssembly)8 Application (org.qi4j.api.structure.Application)6 Test (org.junit.Test)5 ApplicationDescriptor (org.qi4j.api.structure.ApplicationDescriptor)5 LayerAssembly (org.qi4j.bootstrap.LayerAssembly)4 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)4 Module (org.qi4j.api.structure.Module)3 Assembler (org.qi4j.bootstrap.Assembler)3 ConcurrentEntityModificationException (org.qi4j.api.unitofwork.ConcurrentEntityModificationException)2 UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)2 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 ServletException (javax.servlet.ServletException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1