Search in sources :

Example 1 with MuleContextAware

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);
}
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 MuleContextAware

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);
}
Also used : MessageProcessorChain(org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain) MuleContextAware(org.mule.runtime.core.api.context.MuleContextAware) Processor(org.mule.runtime.core.api.processor.Processor) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) SubflowMessageProcessorChainBuilder(org.mule.runtime.core.internal.processor.chain.SubflowMessageProcessorChainBuilder) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 3 with MuleContextAware

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);
        }
    }
}
Also used : MuleContextAware(org.mule.runtime.core.api.context.MuleContextAware) PropertyEditor(java.beans.PropertyEditor) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with MuleContextAware

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);
}
Also used : MuleContextAware(org.mule.runtime.core.api.context.MuleContextAware) Processor(org.mule.runtime.core.api.processor.Processor) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 5 with MuleContextAware

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);
}
Also used : MuleContextAware(org.mule.runtime.core.api.context.MuleContextAware) Test(org.junit.Test)

Aggregations

MuleContextAware (org.mule.runtime.core.api.context.MuleContextAware)5 Test (org.junit.Test)3 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)2 Processor (org.mule.runtime.core.api.processor.Processor)2 SmallTest (org.mule.tck.size.SmallTest)2 Publisher (org.reactivestreams.Publisher)2 PropertyEditor (java.beans.PropertyEditor)1 SimpleDateFormat (java.text.SimpleDateFormat)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MuleException (org.mule.runtime.api.exception.MuleException)1 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)1 I18nMessage (org.mule.runtime.api.i18n.I18nMessage)1 Initialisable (org.mule.runtime.api.lifecycle.Initialisable)1 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)1 SubflowMessageProcessorChainBuilder (org.mule.runtime.core.internal.processor.chain.SubflowMessageProcessorChainBuilder)1 MessageProcessorChain (org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain)1