use of org.qi4j.api.activation.PassivationException 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.PassivationException in project qi4j-sdk by Qi4j.
the class PassivationTest method givenMixedSuccessFailurePassivationWhenPassivatingExpectAllPassivateMethodsToBeCalled.
@Test
public void givenMixedSuccessFailurePassivationWhenPassivatingExpectAllPassivateMethodsToBeCalled() throws Exception {
SingletonAssembler assembly = new SingletonAssembler() {
public void assemble(ModuleAssembly module) throws AssemblyException {
module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationFailureActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationFailureActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationFailureActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationSuccessActivator.class);
module.addServices(DataAccessService.class).withActivators(PassivationFailureActivator.class);
}
};
ArrayList<Data> datas = new ArrayList<Data>();
Iterable<ServiceReference<DataAccess>> iterable = assembly.module().findServices(DataAccess.class);
for (ServiceReference<DataAccess> service : iterable) {
assertTrue("Service should not be Active before accessed", !service.isActive());
Data data = service.get().data();
if (DataAccessService.class.isInstance(service.get())) {
// Collect the expected successes.
datas.add(data);
}
assertTrue("Data should indicate that the service is activated", data.activated);
assertTrue("Service should be Active after access.", service.isActive());
}
try {
assembly.application().passivate();
fail("PassivationException should have been thrown.");
} catch (PassivationException e) {
// Expected
}
// Still ensure that all services have been shutdown.
for (ServiceReference<DataAccess> service : iterable) {
assertFalse("All services should have been shutdown", service.isActive());
}
}
use of org.qi4j.api.activation.PassivationException 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