Search in sources :

Example 1 with PassivationException

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);
    }
}
Also used : ActivationException(org.qi4j.api.activation.ActivationException) PassivationException(org.qi4j.api.activation.PassivationException) ActivationEvent(org.qi4j.api.activation.ActivationEvent) Activation(org.qi4j.api.activation.Activation) ActivationException(org.qi4j.api.activation.ActivationException) PassivationException(org.qi4j.api.activation.PassivationException) ServiceReference(org.qi4j.api.service.ServiceReference)

Example 2 with PassivationException

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());
    }
}
Also used : ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) PassivationException(org.qi4j.api.activation.PassivationException) ArrayList(java.util.ArrayList) ServiceReference(org.qi4j.api.service.ServiceReference) Test(org.junit.Test)

Example 3 with PassivationException

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);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PassivationException(org.qi4j.api.activation.PassivationException) ActivationEventListener(org.qi4j.api.activation.ActivationEventListener) ActivationEvent(org.qi4j.api.activation.ActivationEvent) ActivationException(org.qi4j.api.activation.ActivationException) PassivationException(org.qi4j.api.activation.PassivationException) ServiceReference(org.qi4j.api.service.ServiceReference)

Aggregations

PassivationException (org.qi4j.api.activation.PassivationException)3 ServiceReference (org.qi4j.api.service.ServiceReference)3 ActivationEvent (org.qi4j.api.activation.ActivationEvent)2 ActivationException (org.qi4j.api.activation.ActivationException)2 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 Test (org.junit.Test)1 Activation (org.qi4j.api.activation.Activation)1 ActivationEventListener (org.qi4j.api.activation.ActivationEventListener)1 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)1 SingletonAssembler (org.qi4j.bootstrap.SingletonAssembler)1