use of org.qi4j.api.structure.Application in project qi4j-sdk by Qi4j.
the class DividendsMain method main.
public static void main(String[] args) throws Exception {
SingletonAssembler assembler = new SingletonAssembler() {
public void assemble(ModuleAssembly module) throws AssemblyException {
module.services(ProjectedDividendsService.class).instantiateOnStartup();
module.values(DivStream.class, DivPoint.class);
new CxfAssembler().assemble(module);
}
};
final Application application = assembler.application();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
application.passivate();
} catch (Exception e) {
System.err.println("Problem shutting down Qi4j");
e.printStackTrace();
}
}
});
}
use of org.qi4j.api.structure.Application in project qi4j-sdk by Qi4j.
the class LiquibaseServiceTest method testLiquibase.
@Test
public void testLiquibase() throws SQLException, IOException, ActivationException, AssemblyException {
final SingletonAssembler assembler = new SingletonAssembler() {
@Override
public void assemble(ModuleAssembly module) throws AssemblyException {
ModuleAssembly configModule = module;
// Create in-memory store for configurations
new EntityTestAssembler().assemble(configModule);
new C3P0DataSourceServiceAssembler().identifiedBy("datasource-service").withConfig(configModule, Visibility.layer).assemble(module);
new DataSourceAssembler().withDataSourceServiceIdentity("datasource-service").identifiedBy("testds-liquibase").withCircuitBreaker().assemble(module);
module.values(SomeValue.class);
// Set up Liquibase service that will create the tables
// START SNIPPET: assembly
new LiquibaseAssembler().withConfig(configModule, Visibility.layer).assemble(module);
// END SNIPPET: assembly
module.forMixin(LiquibaseConfiguration.class).declareDefaults().enabled().set(true);
module.forMixin(LiquibaseConfiguration.class).declareDefaults().changeLog().set("changelog.xml");
}
@Override
public void beforeActivation(Application application) {
application.registerActivationEventListener(new ActivationEventListener() {
@Override
public void onEvent(ActivationEvent event) {
System.out.println(event);
}
});
}
};
Module module = assembler.module();
// START SNIPPET: io
// Look up the DataSource
DataSource ds = module.findService(DataSource.class).get();
// Instanciate Databases helper
Databases database = new Databases(ds);
// Assert that insertion works
assertTrue(database.update("insert into test values ('someid', 'bar')") == 1);
// END SNIPPET: io
database.query("select * from test", new Databases.ResultSetVisitor() {
@Override
public boolean visit(ResultSet visited) throws SQLException {
assertThat(visited.getString("id"), equalTo("someid"));
assertThat(visited.getString("foo"), equalTo("bar"));
return true;
}
});
Function<ResultSet, SomeValue> toValue = new Function<ResultSet, SomeValue>() {
@Override
public SomeValue map(ResultSet resultSet) {
ValueBuilder<SomeValue> builder = assembler.module().newValueBuilder(SomeValue.class);
try {
builder.prototype().id().set(resultSet.getString("id"));
builder.prototype().foo().set(resultSet.getString("foo"));
} catch (SQLException e) {
throw new IllegalArgumentException("Could not convert to SomeValue", e);
}
return builder.newInstance();
}
};
// START SNIPPET: io
// Select rows and load them in a List
List<SomeValue> rows = new ArrayList<SomeValue>();
database.query("select * from test").transferTo(map(toValue, collection(rows)));
// Transfer all rows to System.out
Inputs.iterable(rows).transferTo(Outputs.systemOut());
// END SNIPPET: io
}
use of org.qi4j.api.structure.Application in project qi4j-sdk by Qi4j.
the class Main method main.
public static void main(String[] args) throws Exception {
Energy4Java is = new Energy4Java();
Server server = new Server(Protocol.HTTP, 8888);
Application app = is.newApplication(new ForumAssembler(), new MetadataService());
app.activate();
ContextRestlet restlet = app.findModule("REST", "Restlet").newObject(ContextRestlet.class, new org.restlet.Context());
ChallengeAuthenticator guard = new ChallengeAuthenticator(null, ChallengeScheme.HTTP_BASIC, "testRealm");
MapVerifier mapVerifier = new MapVerifier();
mapVerifier.getLocalSecrets().put("rickard", "secret".toCharArray());
guard.setVerifier(mapVerifier);
guard.setNext(restlet);
server.setNext(restlet);
server.start();
}
use of org.qi4j.api.structure.Application in project qi4j-sdk by Qi4j.
the class QuikitServlet method createApplicationAssembler.
private ApplicationAssembler createApplicationAssembler(ServletConfig config) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
String assemblerClassname = config.getInitParameter("qi4j-assembler");
ClassLoader loader = getClass().getClassLoader();
Class<?> assemblerClass = loader.loadClass(assemblerClassname);
ApplicationAssembler assembler;
Constructor cons = assemblerClass.getConstructor(Application.Mode.class);
if (cons == null) {
assembler = (ApplicationAssembler) assemblerClass.newInstance();
} else {
Application.Mode mode;
String modeSetting = config.getInitParameter("qi4j-application-mode");
if (modeSetting == null) {
mode = Application.Mode.development;
} else {
mode = Application.Mode.valueOf(modeSetting);
}
assembler = (ApplicationAssembler) cons.newInstance(mode);
}
return assembler;
}
use of org.qi4j.api.structure.Application in project qi4j-sdk by Qi4j.
the class ApplicationBuilderTest method givenJsonWhenBuildingApplicationExpectSuccess.
@Test
public void givenJsonWhenBuildingApplicationExpectSuccess() throws JSONException, ActivationException, AssemblyException {
ApplicationBuilder builder = ApplicationBuilder.fromJson(APPLICATION);
Application application = builder.newApplication();
Module module = application.findModule("layer3", "test module");
TestService service = module.findService(TestService.class).get();
assertThat(service.sayHello(), equalTo("Hello Qi4j!"));
}
Aggregations