use of org.qi4j.api.activation.ActivationEventListener 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.activation.ActivationEventListener in project qi4j-sdk by Qi4j.
the class FileConfigurationDataWiper method registerApplicationPassivationDataWiper.
public static void registerApplicationPassivationDataWiper(FileConfiguration fileConfig, Application application) {
final List<File> dataDirectories = new ArrayList<File>();
dataDirectories.add(fileConfig.configurationDirectory());
dataDirectories.add(fileConfig.cacheDirectory());
dataDirectories.add(fileConfig.dataDirectory());
dataDirectories.add(fileConfig.logDirectory());
dataDirectories.add(fileConfig.temporaryDirectory());
application.registerActivationEventListener(new ActivationEventListener() {
@Override
public void onEvent(ActivationEvent event) {
if (event.type() == ActivationEvent.EventType.PASSIVATED && Application.class.isAssignableFrom(event.source().getClass())) {
for (File dataDir : dataDirectories) {
if (!delete(dataDir)) {
System.err.println("Unable to delete " + dataDir);
}
}
}
}
});
}
use of org.qi4j.api.activation.ActivationEventListener 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.activation.ActivationEventListener in project qi4j-sdk by Qi4j.
the class ActivationDelegate method passivate.
@SuppressWarnings("unchecked")
public void passivate(Runnable callback) throws PassivationException {
Set<Exception> exceptions = new LinkedHashSet<>();
// Before Passivation Events
if (fireEvents) {
ActivationEvent event = new ActivationEvent(target, PASSIVATING);
for (ActivationEventListener listener : listeners) {
try {
listener.onEvent(event);
} catch (Exception ex) {
if (ex instanceof PassivationException) {
exceptions.addAll(((PassivationException) ex).causes());
} else {
exceptions.add(ex);
}
}
}
}
// Before Passivation for Activators
if (targetActivators != null) {
try {
targetActivators.beforePassivation(target);
} catch (PassivationException ex) {
exceptions.addAll(ex.causes());
} catch (Exception ex) {
exceptions.add(ex);
}
}
// Passivation
while (!activeChildren.isEmpty()) {
passivateOneChild(exceptions);
}
// Internal Passivation Callback
if (callback != null) {
try {
callback.run();
} catch (Exception ex) {
if (ex instanceof PassivationException) {
exceptions.addAll(((PassivationException) ex).causes());
} else {
exceptions.add(ex);
}
}
}
// After Passivation for Activators
if (targetActivators != null) {
try {
targetActivators.afterPassivation(target instanceof ServiceReference ? new PassiveServiceReference((ServiceReference) target) : target);
} catch (PassivationException ex) {
exceptions.addAll(ex.causes());
} catch (Exception ex) {
exceptions.add(ex);
}
}
targetActivators = null;
// After Passivation Events
if (fireEvents) {
ActivationEvent event = new ActivationEvent(target, PASSIVATED);
for (ActivationEventListener listener : listeners) {
try {
listener.onEvent(event);
} catch (Exception ex) {
if (ex instanceof PassivationException) {
exceptions.addAll(((PassivationException) ex).causes());
} else {
exceptions.add(ex);
}
}
}
}
// Error handling
if (exceptions.isEmpty()) {
return;
}
throw new PassivationException(exceptions);
}
Aggregations