Search in sources :

Example 1 with Application

use of org.qi4j.api.structure.Application 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 Application

use of org.qi4j.api.structure.Application in project qi4j-sdk by Qi4j.

the class ApplicationBuilder method main.

/**
     * {@literal main} method that read JSON from STDIN.
     * <p>Passivation exceptions are written to STDERR if any.</p>
     * @param args Unused
     * @throws JSONException if unable to read JSON
     * @throws AssemblyException if the assembly failed
     * @throws ActivationException if the activation failed
     */
public static void main(String[] args) throws JSONException, ActivationException, AssemblyException {
    ApplicationBuilder builder = fromJson(System.in);
    Application application = builder.newApplication();
    Runtime.getRuntime().addShutdownHook(new ApplicationPassivationThread(application, System.err));
}
Also used : Application(org.qi4j.api.structure.Application) ApplicationPassivationThread(org.qi4j.api.activation.ApplicationPassivationThread)

Example 3 with Application

use of org.qi4j.api.structure.Application in project qi4j-sdk by Qi4j.

the class ApplicationBuilderTest method givenJsonInputStreamWhenBuildingApplicationExpectSuccess.

@Test
public void givenJsonInputStreamWhenBuildingApplicationExpectSuccess() throws IOException, JSONException, ActivationException, AssemblyException {
    InputStream input = new ByteArrayInputStream(APPLICATION.getBytes("UTF-8"));
    ApplicationBuilder builder = ApplicationBuilder.fromJson(input);
    Application application = builder.newApplication();
    Module module = application.findModule("layer3", "test module");
    TestService service = module.findService(TestService.class).get();
    assertThat(service.sayHello(), equalTo("Hello Qi4j!"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Module(org.qi4j.api.structure.Module) Application(org.qi4j.api.structure.Application) Test(org.junit.Test)

Example 4 with Application

use of org.qi4j.api.structure.Application in project qi4j-sdk by Qi4j.

the class ApplicationBuilderTest method givenBuilderUseWhenBuildingApplicationExpectSuccess.

@Test
public void givenBuilderUseWhenBuildingApplicationExpectSuccess() throws AssemblyException, ActivationException {
    ApplicationBuilder builder = new ApplicationBuilder("Build from API test.");
    builder.withLayer("layer1").using("layer2").using("layer3");
    builder.withLayer("layer2");
    builder.withLayer("layer3").withModule("test module").withAssemblers(filter(matches(".*ServiceAssembler"), findClasses(getClass())));
    Application application = builder.newApplication();
    Module module = application.findModule("layer3", "test module");
    TestService service = module.findService(TestService.class).get();
    assertThat(service.sayHello(), equalTo("Hello Qi4j!"));
}
Also used : Module(org.qi4j.api.structure.Module) Application(org.qi4j.api.structure.Application) Test(org.junit.Test)

Example 5 with Application

use of org.qi4j.api.structure.Application 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)

Aggregations

Application (org.qi4j.api.structure.Application)50 Test (org.junit.Test)32 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)26 Module (org.qi4j.api.structure.Module)18 Energy4Java (org.qi4j.bootstrap.Energy4Java)18 Assembler (org.qi4j.bootstrap.Assembler)13 ApplicationAssemblerAdapter (org.qi4j.bootstrap.ApplicationAssemblerAdapter)10 SingletonAssembler (org.qi4j.bootstrap.SingletonAssembler)10 AssemblyException (org.qi4j.bootstrap.AssemblyException)8 Function (org.qi4j.functional.Function)7 ApplicationAssembler (org.qi4j.bootstrap.ApplicationAssembler)6 ApplicationAssembly (org.qi4j.bootstrap.ApplicationAssembly)6 UnitOfWorkFactory (org.qi4j.api.unitofwork.UnitOfWorkFactory)5 ApplicationAssemblyFactory (org.qi4j.bootstrap.ApplicationAssemblyFactory)5 LayerAssembly (org.qi4j.bootstrap.LayerAssembly)4 MemoryEntityStoreService (org.qi4j.entitystore.memory.MemoryEntityStoreService)4 UuidIdentityGeneratorService (org.qi4j.spi.uuid.UuidIdentityGeneratorService)4 OrgJsonValueSerializationService (org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationService)4 ArrayList (java.util.ArrayList)3 ApplicationDescriptor (org.qi4j.api.structure.ApplicationDescriptor)3