use of org.mule.runtime.core.api.context.MuleContextAware 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.core.api.context.MuleContextAware in project mule by mulesoft.
the class FlowRefFactoryBeanTestCase method dynamicFlowRefSubFlowMessageProcessorChain.
@Test
public void dynamicFlowRefSubFlowMessageProcessorChain() throws Exception {
CoreEvent event = testEvent();
Processor targetSubFlowConstructAware = (Processor) mock(Object.class, INITIALIZABLE_MESSAGE_PROCESSOR);
when(targetSubFlowConstructAware.apply(any(Publisher.class))).thenReturn(just(result));
Processor targetMuleContextAwareAware = (Processor) mock(MuleContextAware.class, INITIALIZABLE_MESSAGE_PROCESSOR);
when(targetMuleContextAwareAware.apply(any(Publisher.class))).thenAnswer(invocationOnMock -> invocationOnMock.getArguments()[0]);
MessageProcessorChain targetSubFlowChain = mock(MessageProcessorChain.class, INITIALIZABLE_MESSAGE_PROCESSOR);
when(targetSubFlowChain.apply(any(Publisher.class))).thenReturn(just(result));
List<Processor> targetSubFlowProcessors = asList(targetSubFlowConstructAware, targetMuleContextAwareAware);
when(targetSubFlowChain.getMessageProcessors()).thenReturn(targetSubFlowProcessors);
SubflowMessageProcessorChainBuilder chainBuilder = new SubflowMessageProcessorChainBuilder();
chainBuilder.chain(targetSubFlowProcessors);
FlowRefFactoryBean flowRefFactoryBean = createDynamicFlowRefFactoryBean(targetSubFlowChain, chainBuilder);
final Processor flowRefProcessor = getFlowRefProcessor(flowRefFactoryBean);
just(event).transform(flowRefProcessor).block();
verify((MuleContextAware) targetMuleContextAwareAware, atLeastOnce()).setMuleContext(mockMuleContext);
}
use of org.mule.runtime.core.api.context.MuleContextAware in project mule by mulesoft.
the class MulePropertyEditorRegistrar method registerCustomEditors.
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(MessageExchangePattern.class, new MessageExchangePatternPropertyEditor());
registry.registerCustomEditor(Date.class, new DatePropertyEditor(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"), new SimpleDateFormat("yyyy-MM-dd"), true));
if (customPropertyEditorsCache == null) {
discoverCustomPropertyEditor();
}
for (Map.Entry<Class<?>, Class<PropertyEditor>> entry : customPropertyEditorsCache.entrySet()) {
try {
final PropertyEditor customEditor = ClassUtils.instantiateClass(entry.getValue());
if (customEditor instanceof MuleContextAware) {
((MuleContextAware) customEditor).setMuleContext(muleContext);
}
registry.registerCustomEditor(entry.getKey(), customEditor);
} catch (Exception e) {
throw new IllegalStateException("Error loading custom property editors", e);
}
}
}
use of org.mule.runtime.core.api.context.MuleContextAware in project mule by mulesoft.
the class FlowRefFactoryBeanTestCase method dynamicFlowRefSubContextAware.
@Test
public void dynamicFlowRefSubContextAware() throws Exception {
CoreEvent event = testEvent();
MuleContextAware targetMuleContextAware = mock(MuleContextAware.class, INITIALIZABLE_MESSAGE_PROCESSOR);
when(((Processor) targetMuleContextAware).apply(any(Publisher.class))).thenReturn(just(result));
FlowRefFactoryBean flowRefFactoryBean = createDynamicFlowRefFactoryBean((Processor) targetMuleContextAware, null);
assertSame(result.getMessage(), getFlowRefProcessor(flowRefFactoryBean).process(event).getMessage());
verify(targetMuleContextAware).setMuleContext(mockMuleContext);
}
use of org.mule.runtime.core.api.context.MuleContextAware in project mule by mulesoft.
the class RegistrationAndInjectionTestCase method muleContextAware.
@Test
public void muleContextAware() throws Exception {
MuleContextAware muleContextAware = mock(MuleContextAware.class);
((MuleContextWithRegistries) muleContext).getRegistry().registerObject(KEY, muleContextAware);
assertRegistered(muleContextAware);
verify(muleContextAware).setMuleContext(muleContext);
}
Aggregations