Search in sources :

Example 1 with AFTER_NEXT

use of org.mule.runtime.api.notification.PolicyNotification.AFTER_NEXT in project mule by mulesoft.

the class PolicyNextActionMessageProcessor method apply.

@Override
public Publisher<CoreEvent> apply(Publisher<CoreEvent> publisher) {
    return from(publisher).doOnNext(coreEvent -> logExecuteNextEvent("Before execute-next", coreEvent.getContext(), coreEvent.getMessage(), this.muleContext.getConfiguration().getId())).flatMap(event -> {
        Processor nextOperation = policyStateHandler.retrieveNextOperation(event.getContext().getCorrelationId());
        if (nextOperation == null) {
            return error(new MuleRuntimeException(createStaticMessage("There's no next operation configured for event context id " + event.getContext().getCorrelationId())));
        }
        popBeforeNextFlowFlowStackElement().accept(event);
        notificationHelper.notification(BEFORE_NEXT).accept(event);
        return from(processWithChildContext(event, nextOperation, ofNullable(getLocation()))).doOnSuccessOrError(notificationHelper.successOrErrorNotification(AFTER_NEXT).andThen((ev, t) -> pushAfterNextFlowStackElement().accept(event))).onErrorResume(MessagingException.class, t -> {
            PolicyStateId policyStateId = new PolicyStateId(event.getContext().getCorrelationId(), muleContext.getConfiguration().getId());
            policyStateHandler.getLatestState(policyStateId).ifPresent(latestStateEvent -> t.setProcessedEvent(policyEventConverter.createEvent((PrivilegedEvent) t.getEvent(), (PrivilegedEvent) latestStateEvent)));
            // Given we've used child context to ensure AFTER_NEXT notifications are fired at exactly the right time we need
            // to propagate the error to parent context manually.
            ((BaseEventContext) event.getContext()).error(resolveMessagingException(t.getFailingComponent(), muleContext).apply(t));
            return empty();
        }).doOnNext(coreEvent -> logExecuteNextEvent("After execute-next", coreEvent.getContext(), coreEvent.getMessage(), this.muleContext.getConfiguration().getId()));
    });
}
Also used : PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) Message(org.mule.runtime.api.message.Message) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) Processor(org.mule.runtime.core.api.processor.Processor) Function(java.util.function.Function) BEFORE_NEXT(org.mule.runtime.api.notification.PolicyNotification.BEFORE_NEXT) Inject(javax.inject.Inject) MuleContext(org.mule.runtime.core.api.MuleContext) MuleException(org.mule.runtime.api.exception.MuleException) Component(org.mule.runtime.api.component.Component) Mono.from(reactor.core.publisher.Mono.from) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Mono.empty(reactor.core.publisher.Mono.empty) Mono.error(reactor.core.publisher.Mono.error) AFTER_NEXT(org.mule.runtime.api.notification.PolicyNotification.AFTER_NEXT) EventContext(org.mule.runtime.api.event.EventContext) MessagingExceptionResolver(org.mule.runtime.core.internal.util.MessagingExceptionResolver) PolicyStateId(org.mule.runtime.core.api.policy.PolicyStateId) PolicyStateHandler(org.mule.runtime.core.api.policy.PolicyStateHandler) Logger(org.slf4j.Logger) MessageProcessors.processToApply(org.mule.runtime.core.privileged.processor.MessageProcessors.processToApply) Optional.ofNullable(java.util.Optional.ofNullable) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) Consumer(java.util.function.Consumer) MessageProcessors.processWithChildContext(org.mule.runtime.core.privileged.processor.MessageProcessors.processWithChildContext) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) DefaultFlowCallStack(org.mule.runtime.core.internal.context.notification.DefaultFlowCallStack) FlowStackElement(org.mule.runtime.core.api.context.notification.FlowStackElement) Processor(org.mule.runtime.core.api.processor.Processor) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) PolicyStateId(org.mule.runtime.core.api.policy.PolicyStateId) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException)

Example 2 with AFTER_NEXT

use of org.mule.runtime.api.notification.PolicyNotification.AFTER_NEXT in project mule by mulesoft.

the class ApplicationPolicyDeploymentTestCase method appliesApplicationPolicyWithNotificationListener.

@Test
public void appliesApplicationPolicyWithNotificationListener() throws Exception {
    policyManager.registerPolicyTemplate(fooPolicyFileBuilder.getArtifactFile());
    ApplicationFileBuilder applicationFileBuilder = createExtensionApplicationWithServices(APP_WITH_EXTENSION_PLUGIN_CONFIG, helloExtensionV1Plugin);
    addPackedAppFromBuilder(applicationFileBuilder);
    startDeployment();
    assertApplicationDeploymentSuccess(applicationDeploymentListener, applicationFileBuilder.getId());
    List<Integer> notificationListenerActionIds = new ArrayList<>();
    PolicyNotificationListener<PolicyNotification> notificationListener = notification -> notificationListenerActionIds.add(notification.getAction().getActionId());
    policyManager.addPolicy(applicationFileBuilder.getId(), fooPolicyFileBuilder.getArtifactId(), new PolicyParametrization(FOO_POLICY_ID, pointparameters -> true, 1, singletonMap(POLICY_PROPERTY_KEY, POLICY_PROPERTY_VALUE), getResourceFile("/fooPolicy.xml"), singletonList(notificationListener)));
    executeApplicationFlow("main");
    assertThat(invocationCount, equalTo(1));
    new PollingProber(POLICY_NOTIFICATION_TIMEOUT, 100).check(new JUnitProbe() {

        @Override
        protected boolean test() throws Exception {
            assertThat(notificationListenerActionIds, hasSize(4));
            assertThat(notificationListenerActionIds, hasItems(PROCESS_START, BEFORE_NEXT, AFTER_NEXT, PROCESS_END));
            return true;
        }
    });
}
Also used : URISyntaxException(java.net.URISyntaxException) ArtifactPluginFileBuilder(org.mule.runtime.module.deployment.impl.internal.builder.ArtifactPluginFileBuilder) Matchers.hasItems(org.hamcrest.Matchers.hasItems) PollingProber(org.mule.tck.probe.PollingProber) Collections.singletonList(java.util.Collections.singletonList) BEFORE_NEXT(org.mule.runtime.api.notification.PolicyNotification.BEFORE_NEXT) PROCESS_END(org.mule.runtime.api.notification.PolicyNotification.PROCESS_END) TestPolicyProcessor.policyParametrization(org.mule.runtime.module.deployment.internal.TestPolicyProcessor.policyParametrization) Arrays.asList(java.util.Arrays.asList) Is.is(org.hamcrest.core.Is.is) AbstractSecurityProvider(org.mule.runtime.core.api.security.AbstractSecurityProvider) Assert.fail(org.junit.Assert.fail) PolicyFileBuilder(org.mule.runtime.module.deployment.impl.internal.builder.PolicyFileBuilder) JUnitProbe(org.mule.tck.probe.JUnitProbe) AFTER_NEXT(org.mule.runtime.api.notification.PolicyNotification.AFTER_NEXT) PolicyPointcut(org.mule.runtime.core.api.policy.PolicyPointcut) MulePluginModel(org.mule.runtime.api.deployment.meta.MulePluginModel) EXPORTED_RESOURCE_PROPERTY(org.mule.runtime.container.internal.ClasspathModuleDiscoverer.EXPORTED_RESOURCE_PROPERTY) MULE_LOADER_ID(org.mule.runtime.deployment.model.api.artifact.ArtifactDescriptorConstants.MULE_LOADER_ID) Collections.emptyList(java.util.Collections.emptyList) PolicyNotification(org.mule.runtime.api.notification.PolicyNotification) PolicyNotificationListener(org.mule.runtime.api.notification.PolicyNotificationListener) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ApplicationFileBuilder(org.mule.runtime.module.deployment.impl.internal.builder.ApplicationFileBuilder) CompilerUtils(org.mule.tck.util.CompilerUtils) PolicyRegistrationException(org.mule.runtime.deployment.model.api.policy.PolicyRegistrationException) JAVA_LOADER_ID(org.mule.runtime.module.extension.api.loader.java.DefaultJavaExtensionModelLoader.JAVA_LOADER_ID) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) MULE(org.mule.runtime.api.deployment.meta.Product.MULE) PROCESS_START(org.mule.runtime.api.notification.PolicyNotification.PROCESS_START) MuleArtifactLoaderDescriptor(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptor) EXPORTED_RESOURCES(org.mule.runtime.deployment.model.api.artifact.ArtifactDescriptorConstants.EXPORTED_RESOURCES) BeforeClass(org.junit.BeforeClass) MulePolicyModel(org.mule.runtime.api.deployment.meta.MulePolicyModel) ArrayList(java.util.ArrayList) PROPERTIES_BUNDLE_DESCRIPTOR_LOADER_ID(org.mule.runtime.module.deployment.impl.internal.policy.PropertiesBundleDescriptorLoader.PROPERTIES_BUNDLE_DESCRIPTOR_LOADER_ID) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Collections.singletonMap(java.util.Collections.singletonMap) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TestPolicyProcessor.invocationCount(org.mule.runtime.module.deployment.internal.TestPolicyProcessor.invocationCount) Collections.emptyMap(java.util.Collections.emptyMap) SecurityException(org.mule.runtime.api.security.SecurityException) XmlExtensionModelLoader(org.mule.runtime.extension.api.loader.xml.XmlExtensionModelLoader) PolicyParametrization(org.mule.runtime.core.api.policy.PolicyParametrization) JarFileBuilder(org.mule.runtime.module.deployment.impl.internal.builder.JarFileBuilder) Test(org.junit.Test) File(java.io.File) BOOTSTRAP_PROPERTIES(org.mule.runtime.core.internal.config.bootstrap.ClassLoaderRegistryBootstrapDiscoverer.BOOTSTRAP_PROPERTIES) Authentication(org.mule.runtime.api.security.Authentication) EXPORTED_PACKAGES(org.mule.runtime.deployment.model.api.artifact.ArtifactDescriptorConstants.EXPORTED_PACKAGES) RESOURCE_XML(org.mule.runtime.extension.api.loader.xml.XmlExtensionModelLoader.RESOURCE_XML) MuleArtifactLoaderDescriptorBuilder(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptorBuilder) Product(org.mule.runtime.api.deployment.meta.Product) JUnitProbe(org.mule.tck.probe.JUnitProbe) ApplicationFileBuilder(org.mule.runtime.module.deployment.impl.internal.builder.ApplicationFileBuilder) PolicyNotification(org.mule.runtime.api.notification.PolicyNotification) PollingProber(org.mule.tck.probe.PollingProber) ArrayList(java.util.ArrayList) PolicyParametrization(org.mule.runtime.core.api.policy.PolicyParametrization) URISyntaxException(java.net.URISyntaxException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) PolicyRegistrationException(org.mule.runtime.deployment.model.api.policy.PolicyRegistrationException) SecurityException(org.mule.runtime.api.security.SecurityException) Test(org.junit.Test)

Aggregations

MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)2 AFTER_NEXT (org.mule.runtime.api.notification.PolicyNotification.AFTER_NEXT)2 BEFORE_NEXT (org.mule.runtime.api.notification.PolicyNotification.BEFORE_NEXT)2 File (java.io.File)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.singletonList (java.util.Collections.singletonList)1 Collections.singletonMap (java.util.Collections.singletonMap)1 List (java.util.List)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Inject (javax.inject.Inject)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1 Matchers.hasItems (org.hamcrest.Matchers.hasItems)1 Matchers.hasSize (org.hamcrest.Matchers.hasSize)1