Search in sources :

Example 31 with ClassLoaderLookupPolicy

use of org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy in project mule by mulesoft.

the class PluginLookPolicyFactory method createLookupPolicy.

/**
 * Creates a {@link ClassLoaderLookupPolicy} for plugins considering their dependencies.
 *
 * @param pluginClassification the {@link PluginUrlClassification} to creates its {@link ClassLoaderLookupPolicy}
 * @param pluginClassifications whole list of {@link PluginUrlClassification} for the current context
 * @param parentLookupPolicies the {@link ClassLoaderLookupPolicy} for the parent {@link ClassLoader}
 * @param classLoaders
 * @return {@link ClassLoaderLookupPolicy} for the plugin
 */
public ClassLoaderLookupPolicy createLookupPolicy(PluginUrlClassification pluginClassification, List<PluginUrlClassification> pluginClassifications, ClassLoaderLookupPolicy parentLookupPolicies, List<ArtifactClassLoader> classLoaders) {
    Map<String, LookupStrategy> pluginsLookupPolicies = new HashMap<>();
    for (PluginUrlClassification dependencyPluginClassification : pluginClassifications) {
        if (dependencyPluginClassification.getArtifactId().equals(pluginClassification.getArtifactId())) {
            continue;
        }
        if (pluginClassification.getPluginDependencies().contains(dependencyPluginClassification.getName())) {
            LookupStrategy lookUpPolicyStrategy = PARENT_FIRST;
            for (String exportedPackage : dependencyPluginClassification.getExportedPackages()) {
                pluginsLookupPolicies.put(exportedPackage, lookUpPolicyStrategy);
            }
            if (isPrivilegedPluginDependency(pluginClassification, dependencyPluginClassification)) {
                Optional<ArtifactClassLoader> pluginClassLoader = classLoaders.stream().filter(c -> c.getArtifactId().contains(dependencyPluginClassification.getName())).findFirst();
                if (!pluginClassLoader.isPresent()) {
                    throw new IllegalStateException("Cannot find classloader for plugin: " + dependencyPluginClassification.getArtifactId());
                }
                lookUpPolicyStrategy = new DelegateOnlyLookupStrategy(pluginClassLoader.get().getClassLoader());
                for (String exportedPackage : dependencyPluginClassification.getPrivilegedExportedPackages()) {
                    pluginsLookupPolicies.put(exportedPackage, lookUpPolicyStrategy);
                }
            }
        }
    }
    return parentLookupPolicies.extend(pluginsLookupPolicies);
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) PluginUrlClassification(org.mule.test.runner.api.PluginUrlClassification) List(java.util.List) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) DelegateOnlyLookupStrategy(org.mule.runtime.module.artifact.api.classloader.DelegateOnlyLookupStrategy) Map(java.util.Map) Optional(java.util.Optional) LookupStrategy(org.mule.runtime.module.artifact.api.classloader.LookupStrategy) HashMap(java.util.HashMap) PARENT_FIRST(org.mule.runtime.module.artifact.api.classloader.ParentFirstLookupStrategy.PARENT_FIRST) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) HashMap(java.util.HashMap) DelegateOnlyLookupStrategy(org.mule.runtime.module.artifact.api.classloader.DelegateOnlyLookupStrategy) PluginUrlClassification(org.mule.test.runner.api.PluginUrlClassification) DelegateOnlyLookupStrategy(org.mule.runtime.module.artifact.api.classloader.DelegateOnlyLookupStrategy) LookupStrategy(org.mule.runtime.module.artifact.api.classloader.LookupStrategy)

Example 32 with ClassLoaderLookupPolicy

use of org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy in project mule by mulesoft.

the class PluginLookPolicyFactoryTestCase method lookupPoliciesForPluginThatDoesNotDeclareDependency.

@Test
public void lookupPoliciesForPluginThatDoesNotDeclareDependency() {
    PluginUrlClassification barPluginClassification = new PluginUrlClassification(BAR_PLUGIN_ID, emptyList(), emptyList(), emptyList());
    List<PluginUrlClassification> pluginClassifications = newArrayList(barPluginClassification, fooPluginClassification);
    ClassLoaderLookupPolicy parentLookupPolicies = getParentClassLoaderLookupPolicy();
    ClassLoaderLookupPolicy pluginPolicy = factory.createLookupPolicy(barPluginClassification, pluginClassifications, parentLookupPolicies, null);
    assertThat(pluginPolicy.getClassLookupStrategy(FOO_PACKAGE), is(nullValue()));
}
Also used : ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) PluginUrlClassification(org.mule.test.runner.api.PluginUrlClassification) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Aggregations

ClassLoaderLookupPolicy (org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy)32 Test (org.junit.Test)19 SmallTest (org.mule.tck.size.SmallTest)16 ArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader)12 URLClassLoader (java.net.URLClassLoader)8 FineGrainedControlClassLoader (org.mule.runtime.module.artifact.api.classloader.FineGrainedControlClassLoader)8 URL (java.net.URL)6 HashMap (java.util.HashMap)5 ArtifactPluginDescriptor (org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor)5 LookupStrategy (org.mule.runtime.module.artifact.api.classloader.LookupStrategy)5 PARENT_FIRST (org.mule.runtime.module.artifact.api.classloader.ParentFirstLookupStrategy.PARENT_FIRST)5 RegionClassLoader (org.mule.runtime.module.artifact.api.classloader.RegionClassLoader)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 MuleModule (org.mule.runtime.container.api.MuleModule)4 ArtifactClassLoaderFilter (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoaderFilter)4 ArtifactDescriptor (org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptor)4 TestClassLoader (org.mule.tck.classlaoder.TestClassLoader)4 System.lineSeparator (java.lang.System.lineSeparator)3