Search in sources :

Example 1 with Initialisable

use of org.mule.runtime.api.lifecycle.Initialisable in project mule by mulesoft.

the class LifecycleUtils method initialiseIfNeeded.

/**
 * The same as {@link #initialiseIfNeeded(Object)}, only that before checking for {@code object} being {@link Initialisable}, it
 * uses the given {@code muleContext} to perform further initialization.
 * <p>
 * It checks if the {@code object} implements {@link MuleContextAware}, in which case it will invoke
 * {@link MuleContextAware#setMuleContext(MuleContext)} with the given {@code muleContext}.
 * <p>
 * Also depending on the value of the {@code inject} argument, it will perform dependency injection on the {@code object}
 *
 * @param object the object you're trying to initialise
 * @param inject whether it should perform dependency injection on the {@code object} before actually initialising it
 * @param muleContext a {@link MuleContext}
 * @throws InitialisationException
 * @throws IllegalArgumentException if {@code MuleContext} is {@code null}
 */
public static void initialiseIfNeeded(Object object, boolean inject, MuleContext muleContext) throws InitialisationException {
    checkArgument(muleContext != null, "muleContext cannot be null");
    object = unwrap(object);
    if (object == null) {
        return;
    }
    if (object instanceof MuleContextAware) {
        ((MuleContextAware) object).setMuleContext(muleContext);
    }
    if (inject) {
        try {
            muleContext.getInjector().inject(object);
        } catch (MuleException e) {
            I18nMessage message = createStaticMessage(format("Found exception trying to inject object of type '%s' on initialising phase", object.getClass().getName()));
            if (object instanceof Initialisable) {
                throw new InitialisationException(message, e, (Initialisable) object);
            }
            throw new MuleRuntimeException(message, e);
        }
    }
    initialiseIfNeeded(object);
}
Also used : MuleContextAware(org.mule.runtime.core.api.context.MuleContextAware) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) I18nMessage(org.mule.runtime.api.i18n.I18nMessage) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MuleException(org.mule.runtime.api.exception.MuleException)

Example 2 with Initialisable

use of org.mule.runtime.api.lifecycle.Initialisable in project mule by mulesoft.

the class SpringLifecycleCallbackTestCase method phaseAppliesInDependencyOrder.

@Test
public void phaseAppliesInDependencyOrder() throws Exception {
    Map<String, Initialisable> objects = new LinkedHashMap<>();
    for (int i = 1; i <= 5; i++) {
        final String key = String.valueOf(i);
        Initialisable object = newInitialisable();
        objects.put(key, object);
        when(springRegistry.get(key)).thenReturn(object);
    }
    Map<String, ?> childsOf1 = new LinkedHashMap<>(objects);
    childsOf1.remove("1");
    childsOf1.remove("4");
    childsOf1.remove("5");
    Map<String, Object> childsOf4 = new LinkedHashMap<>();
    childsOf4.put("5", objects.get("5"));
    when(springRegistry.getBeanDependencyResolver()).thenReturn(new DefaultBeanDependencyResolver(mock(ConfigurationDependencyResolver.class, RETURNS_DEEP_STUBS), springRegistry));
    when(springRegistry.getDependencies("1")).thenReturn((Map<String, Object>) childsOf1);
    when(springRegistry.getDependencies("4")).thenReturn(childsOf4);
    when(springRegistry.lookupEntriesForLifecycle(Initialisable.class)).thenReturn(objects);
    InOrder inOrder = inOrder(objects.values().toArray());
    callback.onTransition(Initialisable.PHASE_NAME, springRegistry);
    verifyInitialisation(inOrder, objects, "2", "3", "1", "5", "4");
}
Also used : Initialisable(org.mule.runtime.api.lifecycle.Initialisable) InOrder(org.mockito.InOrder) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 3 with Initialisable

use of org.mule.runtime.api.lifecycle.Initialisable in project mule by mulesoft.

the class AbstractRegistry method initialise.

@Override
public final void initialise() throws InitialisationException {
    if (id == null) {
        logger.warn("No unique id has been set on this registry");
        id = UUID.getUUID();
    }
    try {
        doInitialise();
    } catch (InitialisationException e) {
        throw e;
    } catch (Exception e) {
        throw new InitialisationException(e, this);
    }
    try {
        fireLifecycle(Initialisable.PHASE_NAME);
    } catch (InitialisationException e) {
        throw e;
    } catch (LifecycleException e) {
        if (e.getComponent() instanceof Initialisable) {
            throw new InitialisationException(e, (Initialisable) e.getComponent());
        }
        throw new InitialisationException(e, this);
    }
}
Also used : LifecycleException(org.mule.runtime.api.lifecycle.LifecycleException) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) LifecycleException(org.mule.runtime.api.lifecycle.LifecycleException) RegistrationException(org.mule.runtime.core.privileged.registry.RegistrationException) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) MuleException(org.mule.runtime.api.exception.MuleException)

Aggregations

Initialisable (org.mule.runtime.api.lifecycle.Initialisable)3 MuleException (org.mule.runtime.api.exception.MuleException)2 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)2 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)2 LinkedHashMap (java.util.LinkedHashMap)1 Test (org.junit.Test)1 InOrder (org.mockito.InOrder)1 I18nMessage (org.mule.runtime.api.i18n.I18nMessage)1 LifecycleException (org.mule.runtime.api.lifecycle.LifecycleException)1 MuleContextAware (org.mule.runtime.core.api.context.MuleContextAware)1 RegistrationException (org.mule.runtime.core.privileged.registry.RegistrationException)1 SmallTest (org.mule.tck.size.SmallTest)1