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;
}
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));
}
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!"));
}
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!"));
}
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());
}
Aggregations