Search in sources :

Example 1 with Pair

use of org.mule.runtime.api.util.Pair 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 2 with Pair

use of org.mule.runtime.api.util.Pair in project mule by mulesoft.

the class MessagingExceptionResolver method findRoot.

private Optional<Pair<Throwable, ErrorType>> findRoot(Component obj, MessagingException me, ErrorTypeLocator locator) {
    List<Pair<Throwable, ErrorType>> errors = collectErrors(obj, me, locator);
    if (errors.isEmpty()) {
        return collectCritical(obj, me, locator).stream().findFirst();
    }
    // We look if there is a more specific error in the chain that matches with the root error (is child or has the same error)
    SingleErrorTypeMatcher matcher = new SingleErrorTypeMatcher(errors.get(0).getSecond());
    Reference<Pair<Throwable, ErrorType>> result = new Reference<>();
    errors.forEach(p -> {
        if (matcher.match(p.getSecond())) {
            result.set(p);
        }
    });
    return Optional.ofNullable(result.get());
}
Also used : Reference(org.mule.runtime.api.util.Reference) SingleErrorTypeMatcher(org.mule.runtime.core.api.exception.SingleErrorTypeMatcher) Pair(org.mule.runtime.api.util.Pair)

Example 3 with Pair

use of org.mule.runtime.api.util.Pair in project mule by mulesoft.

the class MessagingExceptionResolver method collectCritical.

private List<Pair<Throwable, ErrorType>> collectCritical(Component obj, MessagingException me, ErrorTypeLocator locator) {
    List<Pair<Throwable, ErrorType>> errors = new LinkedList<>();
    getExceptionsAsList(me).forEach(e -> {
        ErrorType type = errorTypeFromException(obj, locator, e);
        if (isCriticalMuleError(type)) {
            errors.add(new Pair<>(e, type));
        }
    });
    return errors;
}
Also used : ErrorType(org.mule.runtime.api.message.ErrorType) LinkedList(java.util.LinkedList) Pair(org.mule.runtime.api.util.Pair)

Example 4 with Pair

use of org.mule.runtime.api.util.Pair in project mule by mulesoft.

the class ExtensionModelDiscoverer method discoverPluginsExtensionModels.

/**
 * For each artifactPlugin discovers the {@link ExtensionModel}.
 *
 * @param loaderRepository {@link ExtensionModelLoaderRepository} with the available extension loaders.
 * @param artifactPlugins {@link Pair} of {@link ArtifactPluginDescriptor} and {@link ArtifactClassLoader} for artifact plugins
 *        deployed inside the artifact. Non null.
 * @param parentArtifactExtensions {@link Set} of {@link ExtensionModel} to also take into account when parsing extensions
 * @return {@link Set} of {@link Pair} carrying the {@link ArtifactPluginDescriptor} and it's corresponding
 *         {@link ExtensionModel}.
 */
public Set<Pair<ArtifactPluginDescriptor, ExtensionModel>> discoverPluginsExtensionModels(ExtensionModelLoaderRepository loaderRepository, List<Pair<ArtifactPluginDescriptor, ArtifactClassLoader>> artifactPlugins, Set<ExtensionModel> parentArtifactExtensions) {
    final Set<Pair<ArtifactPluginDescriptor, ExtensionModel>> descriptorsWithExtensions = new HashSet<>();
    artifactPlugins.forEach(artifactPlugin -> {
        Set<ExtensionModel> extensions = descriptorsWithExtensions.stream().map(Pair::getSecond).collect(toSet());
        extensions.addAll(parentArtifactExtensions);
        final ArtifactPluginDescriptor artifactPluginDescriptor = artifactPlugin.getFirst();
        Optional<LoaderDescriber> loaderDescriber = artifactPluginDescriptor.getExtensionModelDescriptorProperty();
        ClassLoader artifactClassloader = artifactPlugin.getSecond().getClassLoader();
        String artifactName = artifactPluginDescriptor.getName();
        ExtensionModel extension = loaderDescriber.map(describer -> discoverExtensionThroughJsonDescriber(loaderRepository, describer, extensions, artifactClassloader, artifactName)).orElse(null);
        if (extension != null) {
            descriptorsWithExtensions.add(new Pair<>(artifactPluginDescriptor, extension));
        }
    });
    return descriptorsWithExtensions;
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) ExtensionModelLoaderRepository(org.mule.runtime.module.extension.internal.loader.ExtensionModelLoaderRepository) ArtifactPluginDescriptor(org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor) Collection(java.util.Collection) Set(java.util.Set) String.format(java.lang.String.format) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) SpiServiceRegistry(org.mule.runtime.core.api.registry.SpiServiceRegistry) RuntimeExtensionModelProvider(org.mule.runtime.core.api.extension.RuntimeExtensionModelProvider) HashSet(java.util.HashSet) List(java.util.List) LoaderDescriber(org.mule.runtime.deployment.model.api.plugin.LoaderDescriber) Optional(java.util.Optional) Pair(org.mule.runtime.api.util.Pair) DslResolvingContext.getDefault(org.mule.runtime.api.dsl.DslResolvingContext.getDefault) ExtensionModelLoader(org.mule.runtime.extension.api.loader.ExtensionModelLoader) Collectors.toSet(java.util.stream.Collectors.toSet) MulePluginModel(org.mule.runtime.api.deployment.meta.MulePluginModel) ArtifactPluginDescriptor(org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) LoaderDescriber(org.mule.runtime.deployment.model.api.plugin.LoaderDescriber) Pair(org.mule.runtime.api.util.Pair) HashSet(java.util.HashSet)

Example 5 with Pair

use of org.mule.runtime.api.util.Pair in project mule by mulesoft.

the class ArtifactExtensionManagerFactory method create.

protected ExtensionManager create(MuleContext muleContext, Set<ExtensionModel> parentArtifactExtensions) {
    final ExtensionManager extensionManager = extensionManagerFactory.create(muleContext);
    final Set<ExtensionModel> extensions = new HashSet<>();
    extensionModelDiscoverer.discoverRuntimeExtensionModels().forEach(extensionManager::registerExtension);
    extensions.addAll(extensionModelDiscoverer.discoverPluginsExtensionModels(extensionModelLoaderRepository, artifactPlugins, parentArtifactExtensions).stream().map(Pair::getSecond).collect(toSet()));
    extensions.forEach(extensionManager::registerExtension);
    return extensionManager;
}
Also used : ExtensionManager(org.mule.runtime.core.api.extension.ExtensionManager) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) HashSet(java.util.HashSet) Pair(org.mule.runtime.api.util.Pair)

Aggregations

Pair (org.mule.runtime.api.util.Pair)35 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)12 ArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader)10 LinkedList (java.util.LinkedList)7 ServiceDiscoverer (org.mule.runtime.module.service.api.discoverer.ServiceDiscoverer)7 List (java.util.List)6 Service (org.mule.runtime.api.service.Service)6 Optional (java.util.Optional)5 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)5 ExtensionModel (org.mule.runtime.api.meta.model.ExtensionModel)5 String.format (java.lang.String.format)4 Set (java.util.Set)4 InOrder (org.mockito.InOrder)4 ComponentIdentifier (org.mule.runtime.api.component.ComponentIdentifier)4 I18nMessageFactory.createStaticMessage (org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage)4 ServiceProvider (org.mule.runtime.api.service.ServiceProvider)4 InputStream (java.io.InputStream)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3