Search in sources :

Example 1 with MuleException

use of org.mule.runtime.api.exception.MuleException 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 MuleException

use of org.mule.runtime.api.exception.MuleException in project mule by mulesoft.

the class SecurityFilterMessageProcessor method initialise.

@Override
public void initialise() throws InitialisationException {
    try {
        muleContext.getInjector().inject(filter);
        initialiseIfNeeded(filter, muleContext);
    } catch (MuleException e) {
        throw new InitialisationException(e, this);
    }
}
Also used : InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MuleException(org.mule.runtime.api.exception.MuleException)

Example 3 with MuleException

use of org.mule.runtime.api.exception.MuleException in project mule by mulesoft.

the class IdempotentRedeliveryPolicy method process.

@Override
public CoreEvent process(CoreEvent event) throws MuleException {
    Optional<Exception> exceptionSeen = empty();
    String messageId = null;
    try {
        messageId = getIdForEvent(event);
    } catch (ExpressionRuntimeException e) {
        logger.warn("The message cannot be processed because the digest could not be generated. Either make the payload serializable or use an expression.");
        return null;
    } catch (Exception ex) {
        exceptionSeen = of(ex);
    }
    Lock lock = lockFactory.createLock(idrId + "-" + messageId);
    lock.lock();
    try {
        RedeliveryCounter counter = findCounter(messageId);
        if (exceptionSeen.isPresent()) {
            throw new MessageRedeliveredException(messageId, counter.counter.get(), maxRedeliveryCount, exceptionSeen.get());
        } else if (counter != null && counter.counter.get() > maxRedeliveryCount) {
            throw new MessageRedeliveredException(messageId, counter.errors, counter.counter.get(), maxRedeliveryCount);
        }
        try {
            CoreEvent returnEvent = processNext(CoreEvent.builder(DefaultEventContext.child((BaseEventContext) event.getContext(), empty()), event).build());
            counter = findCounter(messageId);
            if (counter != null) {
                resetCounter(messageId);
            }
            return returnEvent;
        } catch (Exception ex) {
            if (ex instanceof MessagingException) {
                incrementCounter(messageId, (MessagingException) ex);
                throw ex;
            } else {
                MessagingException me = createMessagingException(event, ex, this);
                incrementCounter(messageId, me);
                throw ex;
            }
        }
    } finally {
        lock.unlock();
    }
}
Also used : MessageRedeliveredException(org.mule.runtime.core.privileged.exception.MessageRedeliveredException) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) MuleException(org.mule.runtime.api.exception.MuleException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) MessageRedeliveredException(org.mule.runtime.core.privileged.exception.MessageRedeliveredException) Lock(java.util.concurrent.locks.Lock)

Example 4 with MuleException

use of org.mule.runtime.api.exception.MuleException in project mule by mulesoft.

the class AbstractOutboundRouter method process.

@Override
public CoreEvent process(final CoreEvent event) throws MuleException {
    ExecutionTemplate<CoreEvent> executionTemplate = createTransactionalExecutionTemplate(muleContext, getTransactionConfig());
    ExecutionCallback<CoreEvent> processingCallback = () -> {
        try {
            return route(event);
        } catch (RoutingException e1) {
            throw e1;
        } catch (Exception e2) {
            throw new RoutingException(AbstractOutboundRouter.this, e2);
        }
    };
    try {
        return executionTemplate.execute(processingCallback);
    } catch (MuleException e) {
        throw e;
    } catch (Exception e) {
        throw new DefaultMuleException(e);
    }
}
Also used : DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) RoutingException(org.mule.runtime.core.privileged.routing.RoutingException) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MuleException(org.mule.runtime.api.exception.MuleException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) DispatchException(org.mule.runtime.core.privileged.connector.DispatchException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) RoutingException(org.mule.runtime.core.privileged.routing.RoutingException) MuleException(org.mule.runtime.api.exception.MuleException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException)

Example 5 with MuleException

use of org.mule.runtime.api.exception.MuleException in project mule by mulesoft.

the class DefaultMuleApplication method stop.

@Override
public void stop() {
    if (this.artifactContext == null || !this.artifactContext.getMuleContext().getLifecycleManager().isDirectTransition(Stoppable.PHASE_NAME)) {
        return;
    }
    if (this.artifactContext == null) {
        withContextClassLoader(null, () -> {
            // app never started, maybe due to a previous error
            if (logger.isInfoEnabled()) {
                logger.info(format("Stopping app '%s' with no mule context", descriptor.getName()));
            }
        });
        status = ApplicationStatus.STOPPED;
        return;
    }
    artifactContext.getMuleContext().getLifecycleManager().checkPhase(Stoppable.PHASE_NAME);
    try {
        withContextClassLoader(null, () -> {
            if (logger.isInfoEnabled()) {
                logger.info(miniSplash(format("Stopping app '%s'", descriptor.getName())));
            }
        });
        this.artifactContext.getMuleContext().stop();
    } catch (MuleException e) {
        throw new DeploymentStopException(createStaticMessage(format("Error stopping application '%s'", descriptor.getName())), e);
    }
}
Also used : DeploymentStopException(org.mule.runtime.deployment.model.api.DeploymentStopException) MuleException(org.mule.runtime.api.exception.MuleException)

Aggregations

MuleException (org.mule.runtime.api.exception.MuleException)68 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)21 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)19 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)19 Test (org.junit.Test)15 DefaultMuleException (org.mule.runtime.api.exception.DefaultMuleException)14 Processor (org.mule.runtime.core.api.processor.Processor)12 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)9 Logger (org.slf4j.Logger)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 BaseEventContext (org.mule.runtime.core.privileged.event.BaseEventContext)6 RegistrationException (org.mule.runtime.core.privileged.registry.RegistrationException)6 Map (java.util.Map)5 ConnectionException (org.mule.runtime.api.connection.ConnectionException)5 MuleContext (org.mule.runtime.core.api.MuleContext)5 LifecycleUtils.stopIfNeeded (org.mule.runtime.core.api.lifecycle.LifecycleUtils.stopIfNeeded)5 Thread.currentThread (java.lang.Thread.currentThread)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Startable (org.mule.runtime.api.lifecycle.Startable)4