Search in sources :

Example 6 with PolicyTemplateDescriptor

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

the class TestPolicyManager method addPolicy.

/**
 * Adds a parameterized policy
 *
 * @param appName application where the policy must be applied. Non empty.
 * @param policyTemplateName template that must be used to instantiate the parametrized policy. Non empty.
 * @param policyParametrization parametrization to instantiate the policy. Non null.
 */
public void addPolicy(String appName, String policyTemplateName, PolicyParametrization policyParametrization) throws PolicyRegistrationException {
    checkArgument(!isEmpty(appName), "appName cannot be empty");
    checkArgument(!isEmpty(policyTemplateName), "policyTemplateName cannot be empty");
    checkArgument(policyParametrization != null, "policyParametrization cannot be ull");
    Optional<PolicyTemplateDescriptor> policyTemplateDescriptor = policyTemplateDescriptors.stream().filter(template -> template.getName().equals(policyTemplateName)).findFirst();
    if (!policyTemplateDescriptor.isPresent()) {
        throw new IllegalStateException("Cannot find policy template descriptor with name: " + policyTemplateName);
    }
    Application application = deploymentService.findApplication(appName);
    ApplicationPolicyManager policyManager = application.getPolicyManager();
    policyManager.addPolicy(policyTemplateDescriptor.get(), policyParametrization);
}
Also used : DeploymentService(org.mule.runtime.module.deployment.api.DeploymentService) Optional.empty(java.util.Optional.empty) PolicyTemplateDescriptorFactory(org.mule.runtime.module.deployment.impl.internal.policy.PolicyTemplateDescriptorFactory) ApplicationPolicyManager(org.mule.runtime.deployment.model.api.application.ApplicationPolicyManager) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) PolicyParametrization(org.mule.runtime.core.api.policy.PolicyParametrization) IOException(java.io.IOException) PolicyRegistrationException(org.mule.runtime.deployment.model.api.policy.PolicyRegistrationException) File(java.io.File) ArrayList(java.util.ArrayList) FilenameUtils.getBaseName(org.apache.commons.io.FilenameUtils.getBaseName) PolicyTemplateDescriptor(org.mule.runtime.deployment.model.api.policy.PolicyTemplateDescriptor) List(java.util.List) FileUtils.unzip(org.mule.runtime.core.api.util.FileUtils.unzip) MuleFoldersUtil.getExecutionFolder(org.mule.runtime.container.api.MuleFoldersUtil.getExecutionFolder) Optional(java.util.Optional) DeploymentListener(org.mule.runtime.module.deployment.api.DeploymentListener) Application(org.mule.runtime.deployment.model.api.application.Application) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) ApplicationPolicyManager(org.mule.runtime.deployment.model.api.application.ApplicationPolicyManager) PolicyTemplateDescriptor(org.mule.runtime.deployment.model.api.policy.PolicyTemplateDescriptor) Application(org.mule.runtime.deployment.model.api.application.Application)

Example 7 with PolicyTemplateDescriptor

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

the class TestPolicyManager method registerPolicyTemplate.

/**
 * Registers a policy template that can be later used to register a parameterized policy
 *
 * @param policyTemplate policy artifact file. Non null.
 */
public void registerPolicyTemplate(File policyTemplate) {
    final File tempFolder = new File(getPoliciesTempFolder(), getBaseName(policyTemplate.getName()));
    try {
        unzip(policyTemplate, tempFolder);
    } catch (IOException e) {
        throw new IllegalStateException("Error processing policy ZIP file: " + policyTemplate, e);
    }
    final PolicyTemplateDescriptor policyTemplateDescriptor = policyTemplateDescriptorFactory.create(tempFolder, empty());
    policyTemplateDescriptors.add(policyTemplateDescriptor);
}
Also used : IOException(java.io.IOException) PolicyTemplateDescriptor(org.mule.runtime.deployment.model.api.policy.PolicyTemplateDescriptor) File(java.io.File)

Example 8 with PolicyTemplateDescriptor

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

the class ArtifactAwareContextSelectorTestCase method returnsParentContextForPolicyClassloader.

@Test
public void returnsParentContextForPolicyClassloader() throws MalformedURLException {
    ClassLoader childClassLoader = new URLClassLoader(new URL[0], regionClassLoader);
    PolicyTemplateDescriptor policyTemplateDescriptor = new PolicyTemplateDescriptor(POLICY_TEMPLATE_NAME, empty());
    RegionClassLoader policyRegionClassLoader = spy(getPolicyRegionClassLoader(policyTemplateDescriptor));
    MuleArtifactClassLoader policyClassLoader = getPolicyArtifactClassLoader(policyTemplateDescriptor, policyRegionClassLoader);
    LoggerContext appCtx = selector.getContext("", childClassLoader, true);
    LoggerContext policyCtx = selector.getContext("", policyClassLoader, true);
    assertThat(policyCtx, sameInstance(appCtx));
}
Also used : URLClassLoader(java.net.URLClassLoader) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) URLClassLoader(java.net.URLClassLoader) MuleArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader) RegionClassLoader(org.mule.runtime.module.artifact.api.classloader.RegionClassLoader) RegionClassLoader(org.mule.runtime.module.artifact.api.classloader.RegionClassLoader) MuleArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader) PolicyTemplateDescriptor(org.mule.runtime.deployment.model.api.policy.PolicyTemplateDescriptor) LoggerContext(org.apache.logging.log4j.core.LoggerContext) 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