Search in sources :

Example 6 with MuleContext

use of org.mule.runtime.core.api.MuleContext in project mule by mulesoft.

the class MessagingExceptionResolver method resolve.

/**
 * Resolves a new {@link MessagingException} with the real cause of the problem based on the content of an Incoming
 * {@link MessagingException} with a chain of causes inside it and the current event that the exception is carrying.
 * <p>
 * This method will pick the FIRST cause exception that has a mule or extension KNOWN error as the real cause, if there is not
 * an exception in the causes that match with an Known error type then this method will try to find the error that the current
 * {@link Event} is carrying.
 * <p>
 * When there are multiple exceptions that contains the same root error type, then this method will wrap the one that has
 * highest position in the causes list
 *
 * @return a {@link MessagingException} with the proper {@link Error} associated to it's {@link CoreEvent}
 */
public MessagingException resolve(final MessagingException me, MuleContext context) {
    ErrorTypeLocator locator = ((PrivilegedMuleContext) context).getErrorTypeLocator();
    Optional<Pair<Throwable, ErrorType>> rootCause = findRoot(component, me, locator);
    if (!rootCause.isPresent()) {
        return updateCurrent(me, component, context);
    }
    Throwable root = rootCause.get().getFirst();
    ErrorType rootErrorType = rootCause.get().getSecond();
    Component failingComponent = getFailingProcessor(me, root).orElse(component);
    ErrorType errorType = getErrorMappings(component).stream().filter(m -> m.match(rootErrorType)).findFirst().map(ErrorMapping::getTarget).orElse(rootErrorType);
    Error error = ErrorBuilder.builder(getMessagingExceptionCause(root)).errorType(errorType).build();
    CoreEvent event = CoreEvent.builder(me.getEvent()).error(error).build();
    MessagingException result;
    if (root instanceof MessagingException) {
        ((MessagingException) root).setProcessedEvent(event);
        result = ((MessagingException) root);
    } else {
        result = me instanceof FlowExecutionException ? new FlowExecutionException(event, root, failingComponent) : new MessagingException(event, root, failingComponent);
    }
    if (me.getInfo().containsKey(INFO_ALREADY_LOGGED_KEY)) {
        result.addInfo(INFO_ALREADY_LOGGED_KEY, me.getInfo().get(INFO_ALREADY_LOGGED_KEY));
    }
    return enrich(result, failingComponent, event, context);
}
Also used : EnrichedNotificationInfo(org.mule.runtime.api.notification.EnrichedNotificationInfo) ErrorMapping(org.mule.runtime.core.internal.exception.ErrorMapping) FlowExecutionException(org.mule.runtime.core.internal.policy.FlowExecutionException) ExceptionUtils.getComponentIdentifier(org.mule.runtime.core.api.util.ExceptionUtils.getComponentIdentifier) InternalExceptionUtils.createErrorEvent(org.mule.runtime.core.internal.util.InternalExceptionUtils.createErrorEvent) ExceptionHelper.getExceptionsAsList(org.mule.runtime.api.exception.ExceptionHelper.getExceptionsAsList) Event(org.mule.runtime.api.event.Event) MuleContext(org.mule.runtime.core.api.MuleContext) SingleErrorTypeMatcher(org.mule.runtime.core.api.exception.SingleErrorTypeMatcher) Component(org.mule.runtime.api.component.Component) ExceptionUtils.getMessagingExceptionCause(org.mule.runtime.core.api.util.ExceptionUtils.getMessagingExceptionCause) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) CRITICAL_IDENTIFIER(org.mule.runtime.core.api.exception.Errors.Identifiers.CRITICAL_IDENTIFIER) Pair(org.mule.runtime.api.util.Pair) LinkedList(java.util.LinkedList) Error(org.mule.runtime.api.message.Error) PrivilegedMuleContext(org.mule.runtime.core.privileged.PrivilegedMuleContext) INFO_ALREADY_LOGGED_KEY(org.mule.runtime.api.exception.MuleException.INFO_ALREADY_LOGGED_KEY) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) EnrichedNotificationInfo.createInfo(org.mule.runtime.api.notification.EnrichedNotificationInfo.createInfo) ErrorBuilder(org.mule.runtime.core.internal.message.ErrorBuilder) List(java.util.List) InternalExceptionUtils.getErrorMappings(org.mule.runtime.core.internal.util.InternalExceptionUtils.getErrorMappings) Reference(org.mule.runtime.api.util.Reference) ErrorType(org.mule.runtime.api.message.ErrorType) ExceptionUtils.isUnknownMuleError(org.mule.runtime.core.api.util.ExceptionUtils.isUnknownMuleError) Optional(java.util.Optional) CORE_NAMESPACE_NAME(org.mule.runtime.core.api.exception.Errors.CORE_NAMESPACE_NAME) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) ErrorTypeLocator(org.mule.runtime.core.privileged.exception.ErrorTypeLocator) ErrorType(org.mule.runtime.api.message.ErrorType) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ErrorTypeLocator(org.mule.runtime.core.privileged.exception.ErrorTypeLocator) FlowExecutionException(org.mule.runtime.core.internal.policy.FlowExecutionException) Error(org.mule.runtime.api.message.Error) ExceptionUtils.isUnknownMuleError(org.mule.runtime.core.api.util.ExceptionUtils.isUnknownMuleError) PrivilegedMuleContext(org.mule.runtime.core.privileged.PrivilegedMuleContext) Component(org.mule.runtime.api.component.Component) Pair(org.mule.runtime.api.util.Pair)

Example 7 with MuleContext

use of org.mule.runtime.core.api.MuleContext in project mule by mulesoft.

the class DeploymentMuleContextListenerFactoryTestCase method createsContextListener.

@Test
public void createsContextListener() throws Exception {
    MuleContextListener contextListener = factory.create(APP_NAME);
    MuleContext context = mock(MuleContext.class);
    contextListener.onCreation(context);
    verify(deploymentListener).onArtifactCreated(eq(APP_NAME), any(CustomizationService.class));
}
Also used : MuleContext(org.mule.runtime.core.api.MuleContext) MuleContextListener(org.mule.runtime.core.api.context.notification.MuleContextListener) CustomizationService(org.mule.runtime.api.config.custom.CustomizationService) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 8 with MuleContext

use of org.mule.runtime.core.api.MuleContext in project mule by mulesoft.

the class MuleContextLifecycleTestCase method testIsInValidJdk.

@Test(expected = InitialisationException.class)
public void testIsInValidJdk() throws InitialisationException {
    try {
        JdkVersionUtils.validateJdk();
    } catch (RuntimeException e) {
        fail("Jdk version or vendor is invalid. Update the valid versions");
    }
    String javaVersion = System.setProperty("java.version", "1.5.0_12");
    try {
        try {
            JdkVersionUtils.validateJdk();
            fail("Test is invalid because the Jdk version or vendor is supposed to now be invalid");
        } catch (RuntimeException e) {
        // expected
        }
        MuleContext ctx = ctxBuilder.buildMuleContext();
        assertFalse(ctx.isInitialised());
        assertFalse(ctx.isInitialising());
        assertFalse(ctx.isStarted());
        assertFalse(ctx.isDisposed());
        assertFalse(ctx.isDisposing());
        ctx.initialise();
    } finally {
        System.setProperty("java.version", javaVersion);
    }
}
Also used : MuleContext(org.mule.runtime.core.api.MuleContext) Test(org.junit.Test)

Example 9 with MuleContext

use of org.mule.runtime.core.api.MuleContext in project mule by mulesoft.

the class DefaultExceptionStrategyTestCase method testExceptionNotifications.

// MULE-1627
@Test
public void testExceptionNotifications() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger notificationCount = new AtomicInteger(0);
    ((MuleContextWithRegistries) muleContext).getRegistry().lookupObject(NotificationListenerRegistry.class).registerListener((ExceptionNotificationListener) notification -> {
        if (new IntegerAction(EXCEPTION_ACTION).equals(notification.getAction())) {
            assertEquals("exception", notification.getActionName());
            assertEquals("Wrong info type", TYPE_ERROR, notification.getType());
            notificationCount.incrementAndGet();
            latch.countDown();
        }
    });
    // throwing exception
    InstrumentedExceptionStrategy strategy = new InstrumentedExceptionStrategy(muleContext);
    strategy.setAnnotations(singletonMap(LOCATION_KEY, TEST_CONNECTOR_LOCATION));
    strategy.setMuleContext(muleContext);
    strategy.setNotificationFirer(((MuleContextWithRegistries) muleContext).getRegistry().lookupObject(NotificationDispatcher.class));
    strategy.handleException(new IllegalArgumentException("boom"));
    // Wait for the notifcation event to be fired as they are queue
    latch.await(2000, MILLISECONDS);
    assertEquals(1, notificationCount.get());
}
Also used : Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) NotificationListenerRegistry(org.mule.runtime.api.notification.NotificationListenerRegistry) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) CountDownLatch(java.util.concurrent.CountDownLatch) IntegerAction(org.mule.runtime.api.notification.IntegerAction) MuleContext(org.mule.runtime.core.api.MuleContext) AbstractMuleContextTestCase(org.mule.tck.junit4.AbstractMuleContextTestCase) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TYPE_ERROR(org.mule.runtime.api.notification.AbstractServerNotification.TYPE_ERROR) Collections.singletonMap(java.util.Collections.singletonMap) EXCEPTION_ACTION(org.mule.runtime.api.notification.ExceptionNotification.EXCEPTION_ACTION) ExceptionNotificationListener(org.mule.runtime.api.notification.ExceptionNotificationListener) NotificationDispatcher(org.mule.runtime.api.notification.NotificationDispatcher) LOCATION_KEY(org.mule.runtime.api.component.AbstractComponent.LOCATION_KEY) Assert.assertEquals(org.junit.Assert.assertEquals) IntegerAction(org.mule.runtime.api.notification.IntegerAction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) NotificationListenerRegistry(org.mule.runtime.api.notification.NotificationListenerRegistry) NotificationDispatcher(org.mule.runtime.api.notification.NotificationDispatcher) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 10 with MuleContext

use of org.mule.runtime.core.api.MuleContext in project mule by mulesoft.

the class RegexExpressionLanguageFunctionTestCase method setup.

@Before
public void setup() throws InitialisationException {
    ParserConfiguration parserConfiguration = new ParserConfiguration();
    expressionExecutor = new MVELExpressionExecutor(parserConfiguration);
    muleContext = mock(MuleContext.class);
    context = new MVELExpressionLanguageContext(parserConfiguration, muleContext);
    regexFuntion = new RegexExpressionLanguageFuntion();
    context.declareFunction("regex", regexFuntion);
}
Also used : MuleContext(org.mule.runtime.core.api.MuleContext) MVELExpressionExecutor(org.mule.runtime.core.internal.el.mvel.MVELExpressionExecutor) MVELExpressionLanguageContext(org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageContext) ParserConfiguration(org.mule.mvel2.ParserConfiguration) Before(org.junit.Before)

Aggregations

MuleContext (org.mule.runtime.core.api.MuleContext)46 Test (org.junit.Test)17 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Before (org.junit.Before)8 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)8 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)8 MuleException (org.mule.runtime.api.exception.MuleException)7 LifecycleUtils.initialiseIfNeeded (org.mule.runtime.core.api.lifecycle.LifecycleUtils.initialiseIfNeeded)7 Map (java.util.Map)6 Optional (java.util.Optional)6 I18nMessageFactory.createStaticMessage (org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage)6 ComponentLocation (org.mule.runtime.api.component.location.ComponentLocation)5 ConfigurationBuilder (org.mule.runtime.core.api.config.ConfigurationBuilder)5 ConfigurationException (org.mule.runtime.core.api.config.ConfigurationException)5 HashMap (java.util.HashMap)4 LinkedList (java.util.LinkedList)4 Optional.ofNullable (java.util.Optional.ofNullable)4 MuleConfiguration (org.mule.runtime.core.api.config.MuleConfiguration)4