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);
}
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");
}
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);
}
}
Aggregations