Search in sources :

Example 1 with Reference

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

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

the class DefaultPolicyStateHandlerTestCase method destroyStateWhenEventIsCompleted.

@Test
public void destroyStateWhenEventIsCompleted() {
    PolicyStateId policyStateExecutionId = new PolicyStateId(TEST_EXECUTION_ID, TEST_POLICY_ID);
    Reference<BiConsumer> subscriberReference = new Reference<>();
    BaseEventContext rootEventContext = eventTestExecutionId.getContext().getRootContext();
    Mockito.doAnswer(invocationOnMock -> {
        subscriberReference.set((BiConsumer) invocationOnMock.getArguments()[0]);
        return null;
    }).when(rootEventContext).onTerminated(any(BiConsumer.class));
    defaultPolicyStateHandler.updateState(policyStateExecutionId, eventTestExecutionId);
    subscriberReference.get().accept(null, null);
    assertThat(defaultPolicyStateHandler.getLatestState(policyStateExecutionId).isPresent(), is(false));
    assertThat(defaultPolicyStateHandler.policyStateIdsByExecutionIdentifier.containsKey(policyStateExecutionId.getExecutionIdentifier()), is(false));
    assertThat(defaultPolicyStateHandler.nextOperationMap.containsKey(policyStateExecutionId.getExecutionIdentifier()), is(false));
    assertThat(defaultPolicyStateHandler.stateMap.containsKey(policyStateExecutionId), is(false));
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) Reference(org.mule.runtime.api.util.Reference) PolicyStateId(org.mule.runtime.core.api.policy.PolicyStateId) BiConsumer(java.util.function.BiConsumer) Test(org.junit.Test)

Example 3 with Reference

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

the class StreamingUtils method withCursoredEvent.

/**
 * Executes the given function {@code f} considering that the given {@code event} might have a {@link CursorProvider} as
 * payload. In that case, this method obtains a cursor from the provider and executes the function.
 * <p>
 * Closing the opened cursor, handling exceptions and return values are all taken care of by this utility method.
 *
 * @param event an {@link CoreEvent}
 * @param f     the function to execute
 * @return the output {@link CoreEvent}
 * @throws MuleException
 */
public static CoreEvent withCursoredEvent(CoreEvent event, CheckedFunction<CoreEvent, CoreEvent> f) throws MuleException {
    if (event.getMessage().getPayload() == null) {
        return event;
    }
    Reference<Throwable> exception = new Reference<>();
    CheckedFunction<CoreEvent, CoreEvent> function = new CheckedFunction<CoreEvent, CoreEvent>() {

        @Override
        public CoreEvent applyChecked(CoreEvent event) throws Throwable {
            return f.apply(event);
        }

        @Override
        public CoreEvent handleException(Throwable throwable) {
            exception.set(unwrap(throwable));
            return null;
        }
    };
    Object payload = event.getMessage().getPayload().getValue();
    CursorProvider cursorProvider = null;
    Cursor cursor = null;
    try {
        if (payload instanceof CursorProvider) {
            cursorProvider = (CursorProvider) payload;
            cursor = cursorProvider.openCursor();
            event = replacePayload(event, cursor);
        }
        CoreEvent value = function.apply(event);
        if (value == null) {
            handlePossibleException(exception);
        } else if (value.getMessage().getPayload().getValue() == cursor) {
            value = replacePayload(value, cursorProvider);
        }
        return value;
    } finally {
        if (cursor != null) {
            closeQuietly(cursor);
        }
    }
}
Also used : CursorProvider(org.mule.runtime.api.streaming.CursorProvider) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Reference(org.mule.runtime.api.util.Reference) Cursor(org.mule.runtime.api.streaming.Cursor) CheckedFunction(org.mule.runtime.core.api.util.func.CheckedFunction)

Example 4 with Reference

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

the class PollingSourceDeclarationEnricher method enrich.

@Override
public void enrich(ExtensionLoadingContext extensionLoadingContext) {
    ClassTypeLoader loader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
    ExtensionDeclarer extensionDeclarer = extensionLoadingContext.getExtensionDeclarer();
    Reference<Boolean> thereArePollingSources = new Reference<>(false);
    new IdempotentDeclarationWalker() {

        @Override
        protected void onSource(SourceDeclaration source) {
            extractType(source).ifPresent(type -> {
                if (type.isAssignableTo(PollingSource.class)) {
                    source.setRunsOnPrimaryNodeOnly(true);
                    ParameterDeclaration parameter = new ParameterDeclaration(SCHEDULING_STRATEGY_PARAMETER_NAME);
                    parameter.setDescription(SCHEDULING_STRATEGY_PARAMETER_DESCRIPTION);
                    parameter.setRequired(true);
                    parameter.setType(loader.load(Scheduler.class), false);
                    parameter.setExpressionSupport(NOT_SUPPORTED);
                    parameter.addModelProperty(new InfrastructureParameterModelProperty(10));
                    parameter.addModelProperty(new QNameModelProperty(new QName(CORE_NAMESPACE, SCHEDULING_STRATEGY_ELEMENT_IDENTIFIER, CORE_PREFIX)));
                    parameter.setDslConfiguration(ParameterDslConfiguration.builder().allowsInlineDefinition(true).allowsReferences(false).allowTopLevelDefinition(false).build());
                    thereArePollingSources.set(true);
                    source.getParameterGroup(DEFAULT_GROUP_NAME).addParameter(parameter);
                }
            });
        }
    }.walk(extensionDeclarer.getDeclaration());
    if (thereArePollingSources.get() && !isSchedulerAlreadyImported(extensionDeclarer.getDeclaration())) {
        ClassTypeLoader typeLoader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
        extensionDeclarer.withImportedType(new ImportedTypeModel((ObjectType) typeLoader.load(Scheduler.class)));
    }
}
Also used : InfrastructureParameterModelProperty(org.mule.runtime.extension.api.property.InfrastructureParameterModelProperty) ParameterDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ParameterDeclaration) CORE_PREFIX(org.mule.runtime.internal.dsl.DslConstants.CORE_PREFIX) ImportedTypeModel(org.mule.runtime.api.meta.model.ImportedTypeModel) PollingSource(org.mule.runtime.extension.api.runtime.source.PollingSource) ExtensionsTypeLoaderFactory(org.mule.runtime.extension.api.declaration.type.ExtensionsTypeLoaderFactory) QNameModelProperty(org.mule.runtime.extension.api.property.QNameModelProperty) CORE_NAMESPACE(org.mule.runtime.internal.dsl.DslConstants.CORE_NAMESPACE) DEFAULT_GROUP_NAME(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel.DEFAULT_GROUP_NAME) MetadataTypeUtils.getTypeId(org.mule.metadata.api.utils.MetadataTypeUtils.getTypeId) ClassTypeLoader(org.mule.metadata.api.ClassTypeLoader) DeclarationEnricher(org.mule.runtime.extension.api.loader.DeclarationEnricher) SourceDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.SourceDeclaration) ParameterDslConfiguration(org.mule.runtime.api.meta.model.ParameterDslConfiguration) SCHEDULING_STRATEGY_PARAMETER_NAME(org.mule.runtime.extension.api.ExtensionConstants.SCHEDULING_STRATEGY_PARAMETER_NAME) ObjectType(org.mule.metadata.api.model.ObjectType) DeclarationEnricherPhase(org.mule.runtime.extension.api.loader.DeclarationEnricherPhase) ExtensionDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclaration) ExtensionLoadingContext(org.mule.runtime.extension.api.loader.ExtensionLoadingContext) STRUCTURE(org.mule.runtime.extension.api.loader.DeclarationEnricherPhase.STRUCTURE) SCHEDULING_STRATEGY_PARAMETER_DESCRIPTION(org.mule.runtime.extension.api.ExtensionConstants.SCHEDULING_STRATEGY_PARAMETER_DESCRIPTION) Scheduler(org.mule.runtime.core.api.source.scheduler.Scheduler) Reference(org.mule.runtime.api.util.Reference) MetadataType(org.mule.metadata.api.model.MetadataType) ExtensionDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer) QName(javax.xml.namespace.QName) NOT_SUPPORTED(org.mule.runtime.api.meta.ExpressionSupport.NOT_SUPPORTED) SCHEDULING_STRATEGY_ELEMENT_IDENTIFIER(org.mule.runtime.internal.dsl.DslConstants.SCHEDULING_STRATEGY_ELEMENT_IDENTIFIER) IdempotentDeclarationWalker(org.mule.runtime.extension.api.declaration.fluent.util.IdempotentDeclarationWalker) QNameModelProperty(org.mule.runtime.extension.api.property.QNameModelProperty) Reference(org.mule.runtime.api.util.Reference) QName(javax.xml.namespace.QName) Scheduler(org.mule.runtime.core.api.source.scheduler.Scheduler) PollingSource(org.mule.runtime.extension.api.runtime.source.PollingSource) InfrastructureParameterModelProperty(org.mule.runtime.extension.api.property.InfrastructureParameterModelProperty) ObjectType(org.mule.metadata.api.model.ObjectType) ClassTypeLoader(org.mule.metadata.api.ClassTypeLoader) ImportedTypeModel(org.mule.runtime.api.meta.model.ImportedTypeModel) ExtensionDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclarer) IdempotentDeclarationWalker(org.mule.runtime.extension.api.declaration.fluent.util.IdempotentDeclarationWalker) ParameterDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ParameterDeclaration) SourceDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.SourceDeclaration)

Example 5 with Reference

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

the class RedeliveryPolicyDeclarationEnricher method enrich.

@Override
public void enrich(ExtensionLoadingContext extensionLoadingContext) {
    final Reference<Boolean> hasObjectStoreParams = new Reference<>(false);
    ExtensionDeclaration extension = extensionLoadingContext.getExtensionDeclarer().getDeclaration();
    new IdempotentDeclarationWalker() {

        @Override
        protected void onSource(SourceDeclaration declaration) {
            addRedeliveryPolicy(declaration);
            hasObjectStoreParams.set(true);
        }
    }.walk(extension);
    if (hasObjectStoreParams.get() && !isObjectStoreAlreadyImported(extension)) {
        ClassTypeLoader typeLoader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
        extension.getImportedTypes().add(new ImportedTypeModel((ObjectType) typeLoader.load(ObjectStore.class)));
    }
}
Also used : ObjectType(org.mule.metadata.api.model.ObjectType) ObjectStore(org.mule.runtime.api.store.ObjectStore) ClassTypeLoader(org.mule.metadata.api.ClassTypeLoader) ImportedTypeModel(org.mule.runtime.api.meta.model.ImportedTypeModel) Reference(org.mule.runtime.api.util.Reference) ExtensionDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclaration) IdempotentDeclarationWalker(org.mule.runtime.extension.api.declaration.fluent.util.IdempotentDeclarationWalker) SourceDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.SourceDeclaration)

Aggregations

Reference (org.mule.runtime.api.util.Reference)29 ObjectType (org.mule.metadata.api.model.ObjectType)12 List (java.util.List)11 MetadataType (org.mule.metadata.api.model.MetadataType)11 Optional (java.util.Optional)10 ArrayType (org.mule.metadata.api.model.ArrayType)8 MetadataTypeVisitor (org.mule.metadata.api.visitor.MetadataTypeVisitor)8 ExtensionModel (org.mule.runtime.api.meta.model.ExtensionModel)8 OperationModel (org.mule.runtime.api.meta.model.operation.OperationModel)8 Map (java.util.Map)7 ClassTypeLoader (org.mule.metadata.api.ClassTypeLoader)7 ConnectionProviderModel (org.mule.runtime.api.meta.model.connection.ConnectionProviderModel)7 ParameterModel (org.mule.runtime.api.meta.model.parameter.ParameterModel)7 ComponentModel (org.mule.runtime.api.meta.model.ComponentModel)6 ConfigurationModel (org.mule.runtime.api.meta.model.config.ConfigurationModel)6 ConstructModel (org.mule.runtime.api.meta.model.construct.ConstructModel)6 String.format (java.lang.String.format)5 Optional.empty (java.util.Optional.empty)5 Collectors.toList (java.util.stream.Collectors.toList)5 ExtensionWalker (org.mule.runtime.api.meta.model.util.ExtensionWalker)5