use of org.qi4j.api.activation.ActivationEvent in project qi4j-sdk by Qi4j.
the class ActivationDelegate method activate.
@SuppressWarnings("unchecked")
public void activate(ActivatorsInstance targetActivators, Iterable<? extends Activation> children, Runnable callback) throws ActivationException {
if (this.targetActivators != null) {
throw new IllegalStateException("Activation.activate() called multiple times " + "or without calling passivate() first!");
}
try {
// Before Activation Events
if (fireEvents) {
fireEvent(new ActivationEvent(target, ACTIVATING));
}
// Before Activation for Activators
targetActivators.beforeActivation(target instanceof ServiceReference ? new PassiveServiceReference((ServiceReference) target) : target);
// Activation
for (Activation child : children) {
if (!activeChildren.contains(child)) {
child.activate();
}
activeChildren.addFirst(child);
}
// Internal Activation Callback
if (callback != null) {
callback.run();
}
// After Activation
targetActivators.afterActivation(target);
// After Activation Events
if (fireEvents) {
fireEvent(new ActivationEvent(target, ACTIVATED));
}
// Activated
this.targetActivators = targetActivators;
} catch (Exception e) {
// Passivate actives
try {
passivate();
} catch (PassivationException e1) {
ActivationException activationEx = new ActivationException("Unable to Activate application.", e);
activationEx.addSuppressed(e1);
throw activationEx;
}
if (e instanceof ActivationException) {
throw ((ActivationException) e);
}
throw new ActivationException("Unable to Activate application.", e);
}
}
use of org.qi4j.api.activation.ActivationEvent 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.ActivationEvent 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.ActivationEvent 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