Search in sources :

Example 1 with PolicyTemplateDescriptor

use of org.mule.runtime.deployment.model.api.policy.PolicyTemplateDescriptor 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 PolicyTemplateDescriptor

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

the class PolicyTemplateClassLoaderFactoryTestCase method setUp.

@Before
public void setUp() throws Exception {
    descriptor = new PolicyTemplateDescriptor("testPolicy");
    descriptor.setRootFolder(policyFolder.getRoot());
    parentClassLoader = mock(ArtifactClassLoader.class);
    when(parentClassLoader.getClassLoader()).thenReturn(getClass().getClassLoader());
    when(lookupPolicy.getClassLookupStrategy(anyString())).thenReturn(PARENT_FIRST);
    when(parentClassLoader.getClassLoaderLookupPolicy()).thenReturn(lookupPolicy);
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) MuleDeployableArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleDeployableArtifactClassLoader) MuleArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader) PolicyTemplateDescriptor(org.mule.runtime.deployment.model.api.policy.PolicyTemplateDescriptor) Before(org.junit.Before)

Example 3 with PolicyTemplateDescriptor

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

the class PolicyTemplateDescriptorFactoryTestCase method readsRuntimeLibs.

@Test
public void readsRuntimeLibs() throws Exception {
    MulePolicyModelBuilder mulePolicyModelBuilder = new MulePolicyModelBuilder().setName(POLICY_NAME).setMinMuleVersion("4.0.0").setRequiredProduct(MULE).withBundleDescriptorLoader(createPolicyBundleDescriptorLoader(PROPERTIES_BUNDLE_DESCRIPTOR_LOADER_ID));
    mulePolicyModelBuilder.withClassLoaderModelDescriptorLoader(new MuleArtifactLoaderDescriptor(FILE_SYSTEM_POLICY_MODEL_LOADER_ID, emptyMap()));
    PolicyFileBuilder policyFileBuilder = new PolicyFileBuilder(POLICY_NAME).usingLibrary(echoTestJarFile.getAbsolutePath()).describedBy(mulePolicyModelBuilder.build());
    File tempFolder = createTempFolder();
    unzip(policyFileBuilder.getArtifactFile(), tempFolder);
    PolicyTemplateDescriptorFactory descriptorFactory = new PolicyTemplateDescriptorFactory(artifactPluginDescriptorLoader, descriptorLoaderRepository, ArtifactDescriptorValidatorBuilder.builder());
    PolicyTemplateDescriptor desc = descriptorFactory.create(tempFolder, empty());
    assertThat(desc.getClassLoaderModel().getUrls().length, equalTo(2));
    assertThat(toFile(desc.getClassLoaderModel().getUrls()[0]).getPath(), equalTo(tempFolder.toString()));
    assertThat(toFile(desc.getClassLoaderModel().getUrls()[1]).getPath(), equalTo(new File(tempFolder, LIB_DIR + separator + JAR_FILE_NAME).toString()));
}
Also used : MuleArtifactLoaderDescriptor(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptor) PolicyFileBuilder(org.mule.runtime.module.deployment.impl.internal.builder.PolicyFileBuilder) MulePolicyModelBuilder(org.mule.runtime.api.deployment.meta.MulePolicyModel.MulePolicyModelBuilder) PolicyTemplateDescriptor(org.mule.runtime.deployment.model.api.policy.PolicyTemplateDescriptor) File.createTempFile(java.io.File.createTempFile) FileUtils.toFile(org.apache.commons.io.FileUtils.toFile) File(java.io.File) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 4 with PolicyTemplateDescriptor

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

the class PolicyTemplateDescriptorFactoryTestCase method readsPlugin.

@Test
public void readsPlugin() throws Exception {
    MulePolicyModelBuilder policyModelBuilder = new MulePolicyModelBuilder().setName(POLICY_NAME).setMinMuleVersion("4.0.0").setRequiredProduct(MULE).withBundleDescriptorLoader(createPolicyBundleDescriptorLoader(PROPERTIES_BUNDLE_DESCRIPTOR_LOADER_ID)).withClassLoaderModelDescriptorLoader(new MuleArtifactLoaderDescriptor(MULE_LOADER_ID, emptyMap()));
    ArtifactPluginFileBuilder plugin1 = new ArtifactPluginFileBuilder("plugin1");
    ArtifactPluginFileBuilder plugin2 = new ArtifactPluginFileBuilder("plugin2");
    PolicyFileBuilder policyFileBuilder = new PolicyFileBuilder(POLICY_NAME).describedBy(policyModelBuilder.build()).dependingOn(plugin1).dependingOn(plugin2);
    File tempFolder = createTempFolder();
    unzip(policyFileBuilder.getArtifactFile(), tempFolder);
    final ArtifactPluginDescriptorFactory pluginDescriptorFactory = mock(ArtifactPluginDescriptorFactory.class);
    final PolicyTemplateDescriptorFactory policyTemplateDescriptorFactory = new PolicyTemplateDescriptorFactory(new ArtifactPluginDescriptorLoader(pluginDescriptorFactory), descriptorLoaderRepository, ArtifactDescriptorValidatorBuilder.builder());
    final ArtifactPluginDescriptor expectedPluginDescriptor1 = new ArtifactPluginDescriptor("plugin1");
    final ArtifactPluginDescriptor expectedPluginDescriptor2 = new ArtifactPluginDescriptor("plugin2");
    when(pluginDescriptorFactory.create(any(), any())).thenReturn(expectedPluginDescriptor1).thenReturn(expectedPluginDescriptor2);
    PolicyTemplateDescriptor descriptor = policyTemplateDescriptorFactory.create(tempFolder, empty());
    Set<ArtifactPluginDescriptor> plugins = descriptor.getPlugins();
    assertThat(plugins.size(), equalTo(2));
    assertThat(plugins, hasItem(equalTo(expectedPluginDescriptor1)));
    assertThat(plugins, hasItem(equalTo(expectedPluginDescriptor2)));
}
Also used : MuleArtifactLoaderDescriptor(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptor) ArtifactPluginFileBuilder(org.mule.runtime.module.deployment.impl.internal.builder.ArtifactPluginFileBuilder) ArtifactPluginDescriptor(org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor) PolicyFileBuilder(org.mule.runtime.module.deployment.impl.internal.builder.PolicyFileBuilder) ArtifactPluginDescriptorLoader(org.mule.runtime.module.deployment.impl.internal.plugin.ArtifactPluginDescriptorLoader) MulePolicyModelBuilder(org.mule.runtime.api.deployment.meta.MulePolicyModel.MulePolicyModelBuilder) ArtifactPluginDescriptorFactory(org.mule.runtime.module.deployment.impl.internal.plugin.ArtifactPluginDescriptorFactory) PolicyTemplateDescriptor(org.mule.runtime.deployment.model.api.policy.PolicyTemplateDescriptor) File.createTempFile(java.io.File.createTempFile) FileUtils.toFile(org.apache.commons.io.FileUtils.toFile) File(java.io.File) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 5 with PolicyTemplateDescriptor

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

the class PolicyTemplateDescriptorFactoryTestCase method assignsBundleDescriptor.

@Test
public void assignsBundleDescriptor() throws Exception {
    MulePolicyModelBuilder mulePolicyModelBuilder = new MulePolicyModelBuilder().setName(POLICY_NAME).setMinMuleVersion("4.0.0").setRequiredProduct(MULE).withBundleDescriptorLoader(createPolicyBundleDescriptorLoader(PROPERTIES_BUNDLE_DESCRIPTOR_LOADER_ID)).withClassLoaderModelDescriptorLoader(new MuleArtifactLoaderDescriptor(MULE_LOADER_ID, emptyMap()));
    PolicyFileBuilder policyFileBuilder = new PolicyFileBuilder(POLICY_NAME).usingLibrary(echoTestJarFile.getAbsolutePath()).describedBy(mulePolicyModelBuilder.build());
    File tempFolder = createTempFolder();
    unzip(policyFileBuilder.getArtifactFile(), tempFolder);
    PolicyTemplateDescriptorFactory descriptorFactory = new PolicyTemplateDescriptorFactory(artifactPluginDescriptorLoader, descriptorLoaderRepository, ArtifactDescriptorValidatorBuilder.builder());
    PolicyTemplateDescriptor desc = descriptorFactory.create(tempFolder, empty());
    assertThat(desc.getBundleDescriptor().getArtifactId(), equalTo(POLICY_NAME));
    assertThat(desc.getBundleDescriptor().getGroupId(), equalTo(POLICY_GROUP_ID));
    assertThat(desc.getBundleDescriptor().getClassifier().get(), equalTo(POLICY_CLASSIFIER));
    assertThat(desc.getBundleDescriptor().getType(), equalTo(POLICY_ARTIFACT_TYPE));
    assertThat(desc.getBundleDescriptor().getVersion(), equalTo(POLICY_VERSION));
}
Also used : MuleArtifactLoaderDescriptor(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptor) PolicyFileBuilder(org.mule.runtime.module.deployment.impl.internal.builder.PolicyFileBuilder) MulePolicyModelBuilder(org.mule.runtime.api.deployment.meta.MulePolicyModel.MulePolicyModelBuilder) PolicyTemplateDescriptor(org.mule.runtime.deployment.model.api.policy.PolicyTemplateDescriptor) File.createTempFile(java.io.File.createTempFile) FileUtils.toFile(org.apache.commons.io.FileUtils.toFile) File(java.io.File) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Aggregations

PolicyTemplateDescriptor (org.mule.runtime.deployment.model.api.policy.PolicyTemplateDescriptor)8 File (java.io.File)5 Test (org.junit.Test)4 SmallTest (org.mule.tck.size.SmallTest)4 File.createTempFile (java.io.File.createTempFile)3 FileUtils.toFile (org.apache.commons.io.FileUtils.toFile)3 MuleArtifactLoaderDescriptor (org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptor)3 MulePolicyModelBuilder (org.mule.runtime.api.deployment.meta.MulePolicyModel.MulePolicyModelBuilder)3 PolicyFileBuilder (org.mule.runtime.module.deployment.impl.internal.builder.PolicyFileBuilder)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Optional (java.util.Optional)2 Preconditions.checkArgument (org.mule.runtime.api.util.Preconditions.checkArgument)2 PolicyParametrization (org.mule.runtime.core.api.policy.PolicyParametrization)2 Application (org.mule.runtime.deployment.model.api.application.Application)2 PolicyRegistrationException (org.mule.runtime.deployment.model.api.policy.PolicyRegistrationException)2 ArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader)2 MuleArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader)2 Integer.compare (java.lang.Integer.compare)1