Search in sources :

Example 1 with PolicyTemplate

use of org.mule.runtime.deployment.model.api.policy.PolicyTemplate in project mule by mulesoft.

the class MuleApplicationPolicyProvider method addPolicy.

@Override
public synchronized void addPolicy(PolicyTemplateDescriptor policyTemplateDescriptor, PolicyParametrization parametrization) throws PolicyRegistrationException {
    try {
        checkArgument(application != null, "application was not configured on the policy provider");
        Optional<RegisteredPolicyInstanceProvider> registeredPolicyInstanceProvider = registeredPolicyInstanceProviders.stream().filter(p -> p.getPolicyId().equals(parametrization.getId())).findFirst();
        if (registeredPolicyInstanceProvider.isPresent()) {
            throw new IllegalArgumentException(createPolicyAlreadyRegisteredError(parametrization.getId()));
        }
        Optional<RegisteredPolicyTemplate> registeredPolicyTemplate = registeredPolicyTemplates.stream().filter(p -> p.policyTemplate.getDescriptor().getBundleDescriptor().getArtifactId().equals(policyTemplateDescriptor.getBundleDescriptor().getArtifactId())).findAny();
        if (!registeredPolicyTemplate.isPresent()) {
            PolicyTemplate policyTemplate = policyTemplateFactory.createArtifact(application, policyTemplateDescriptor);
            registeredPolicyTemplate = of(new RegisteredPolicyTemplate(policyTemplate));
            registeredPolicyTemplates.add(registeredPolicyTemplate.get());
        }
        ApplicationPolicyInstance applicationPolicyInstance = policyInstanceProviderFactory.create(application, registeredPolicyTemplate.get().policyTemplate, parametrization);
        applicationPolicyInstance.initialise();
        registeredPolicyInstanceProviders.add(new RegisteredPolicyInstanceProvider(applicationPolicyInstance, parametrization.getId()));
        registeredPolicyInstanceProviders.sort(null);
        registeredPolicyTemplate.get().count++;
    } catch (Exception e) {
        throw new PolicyRegistrationException(createPolicyRegistrationError(parametrization.getId()), e);
    }
}
Also used : Disposable(org.mule.runtime.api.lifecycle.Disposable) PolicyTemplate(org.mule.runtime.deployment.model.api.policy.PolicyTemplate) Optional.of(java.util.Optional.of) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) PolicyParametrization(org.mule.runtime.core.api.policy.PolicyParametrization) PolicyRegistrationException(org.mule.runtime.deployment.model.api.policy.PolicyRegistrationException) PolicyInstanceProviderFactory(org.mule.runtime.module.deployment.impl.internal.policy.PolicyInstanceProviderFactory) String.format(java.lang.String.format) ArrayList(java.util.ArrayList) PolicyProvider(org.mule.runtime.core.api.policy.PolicyProvider) PolicyTemplateDescriptor(org.mule.runtime.deployment.model.api.policy.PolicyTemplateDescriptor) List(java.util.List) Integer.compare(java.lang.Integer.compare) ApplicationPolicyInstance(org.mule.runtime.module.deployment.impl.internal.policy.ApplicationPolicyInstance) Policy(org.mule.runtime.core.api.policy.Policy) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters) Optional(java.util.Optional) Application(org.mule.runtime.deployment.model.api.application.Application) LinkedList(java.util.LinkedList) PolicyTemplateFactory(org.mule.runtime.module.deployment.impl.internal.policy.PolicyTemplateFactory) PolicyRegistrationException(org.mule.runtime.deployment.model.api.policy.PolicyRegistrationException) ApplicationPolicyInstance(org.mule.runtime.module.deployment.impl.internal.policy.ApplicationPolicyInstance) PolicyTemplate(org.mule.runtime.deployment.model.api.policy.PolicyTemplate) PolicyRegistrationException(org.mule.runtime.deployment.model.api.policy.PolicyRegistrationException)

Example 2 with PolicyTemplate

use of org.mule.runtime.deployment.model.api.policy.PolicyTemplate in project mule by mulesoft.

the class DefaultApplicationPolicyInstanceTestCase method correctArtifactTypeForPolicies.

@Test
@Issue("MULE-14289")
@Ignore("MULE-14289: The discovered ArtifactConfigurationProcessor is not compatible with the provided mocks.")
public void correctArtifactTypeForPolicies() throws InitialisationException {
    MuleContextListener muleContextListener = mock(MuleContextListener.class);
    ArgumentCaptor<MuleContext> muleContextCaptor = ArgumentCaptor.forClass(MuleContext.class);
    PolicyTemplate policyTemplate = mock(PolicyTemplate.class, RETURNS_DEEP_STUBS);
    when(policyTemplate.getArtifactClassLoader().getClassLoader()).thenReturn(this.getClass().getClassLoader());
    Application application = mock(Application.class, RETURNS_DEEP_STUBS);
    Registry registry = application.getRegistry();
    doReturn(of(mockContextWithServices())).when(registry).lookupByType(MuleContext.class);
    doReturn(of(mock(ExtensionManager.class))).when(registry).lookupByName(OBJECT_EXTENSION_MANAGER);
    PolicyParametrization parameterization = mock(PolicyParametrization.class, RETURNS_DEEP_STUBS);
    when(parameterization.getId()).thenReturn("policyId");
    DefaultApplicationPolicyInstance applicationPolicyInstance = new DefaultApplicationPolicyInstance(application, policyTemplate, parameterization, mock(ServiceRepository.class), mock(ClassLoaderRepository.class), emptyList(), mock(ExtensionModelLoaderRepository.class), muleContextListener);
    applicationPolicyInstance.initialise();
    verify(muleContextListener).onCreation(muleContextCaptor.capture());
    assertThat(muleContextCaptor.getValue().getArtifactType(), is(POLICY));
}
Also used : MuleContext(org.mule.runtime.core.api.MuleContext) ClassLoaderRepository(org.mule.runtime.module.artifact.api.classloader.ClassLoaderRepository) ExtensionModelLoaderRepository(org.mule.runtime.module.extension.internal.loader.ExtensionModelLoaderRepository) MuleContextListener(org.mule.runtime.core.api.context.notification.MuleContextListener) Registry(org.mule.runtime.api.artifact.Registry) ServiceRepository(org.mule.runtime.api.service.ServiceRepository) Application(org.mule.runtime.deployment.model.api.application.Application) PolicyTemplate(org.mule.runtime.deployment.model.api.policy.PolicyTemplate) PolicyParametrization(org.mule.runtime.core.api.policy.PolicyParametrization) Ignore(org.junit.Ignore) Issue(io.qameta.allure.Issue) Test(org.junit.Test)

Example 3 with PolicyTemplate

use of org.mule.runtime.deployment.model.api.policy.PolicyTemplate in project mule by mulesoft.

the class DefaultPolicyTemplateFactoryTestCase method createsPolicyTemplate.

@Test
public void createsPolicyTemplate() throws Exception {
    RegionClassLoader regionClassLoader = createRegionClassLoader();
    PolicyTemplateClassLoaderBuilder policyTemplateClassLoaderBuilder = createPolicyTemplateClassLoaderBuilder(regionClassLoader);
    MuleDeployableArtifactClassLoader policyClassLoader = mock(MuleDeployableArtifactClassLoader.class);
    when(policyClassLoader.getArtifactId()).thenReturn(POLICY_ID);
    when(policyTemplateClassLoaderBuilder.build()).thenReturn(policyClassLoader);
    when(classLoaderBuilderFactory.createArtifactClassLoaderBuilder()).thenReturn(policyTemplateClassLoaderBuilder);
    PolicyTemplate policyTemplate = policyTemplateFactory.createArtifact(createApplication(regionClassLoader), descriptor);
    assertThat(policyTemplate.getArtifactClassLoader(), is(policyClassLoader));
    assertThat(policyTemplate.getDescriptor(), is(descriptor));
    assertThat(policyTemplate.getArtifactId(), is(POLICY_ID));
    assertThat(regionClassLoader.getArtifactPluginClassLoaders().size(), equalTo(1));
}
Also used : MuleDeployableArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleDeployableArtifactClassLoader) PolicyTemplateClassLoaderBuilder(org.mule.runtime.deployment.model.internal.policy.PolicyTemplateClassLoaderBuilder) RegionClassLoader(org.mule.runtime.module.artifact.api.classloader.RegionClassLoader) PolicyTemplate(org.mule.runtime.deployment.model.api.policy.PolicyTemplate) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Aggregations

PolicyTemplate (org.mule.runtime.deployment.model.api.policy.PolicyTemplate)3 Test (org.junit.Test)2 PolicyParametrization (org.mule.runtime.core.api.policy.PolicyParametrization)2 Application (org.mule.runtime.deployment.model.api.application.Application)2 Issue (io.qameta.allure.Issue)1 Integer.compare (java.lang.Integer.compare)1 String.format (java.lang.String.format)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Optional (java.util.Optional)1 Optional.of (java.util.Optional.of)1 Ignore (org.junit.Ignore)1 Registry (org.mule.runtime.api.artifact.Registry)1 Disposable (org.mule.runtime.api.lifecycle.Disposable)1 ServiceRepository (org.mule.runtime.api.service.ServiceRepository)1 Preconditions.checkArgument (org.mule.runtime.api.util.Preconditions.checkArgument)1 MuleContext (org.mule.runtime.core.api.MuleContext)1 MuleContextListener (org.mule.runtime.core.api.context.notification.MuleContextListener)1 Policy (org.mule.runtime.core.api.policy.Policy)1