Search in sources :

Example 56 with ArtifactClassLoader

use of org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader 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 57 with ArtifactClassLoader

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

the class TestBootstrapRegistryConfigurationBuilderTestCase method createBootstrapServiceDiscovererContextBuilder.

private TestBootstrapServiceDiscovererConfigurationBuilder createBootstrapServiceDiscovererContextBuilder() {
    try {
        final ClassLoaderFilter filter = mock(ClassLoaderFilter.class);
        when(filter.exportsClass(anyString())).thenReturn(true);
        final ArtifactClassLoader pluginClassLoader1 = mock(ArtifactClassLoader.class);
        when(pluginClassLoader1.getClassLoader()).thenReturn(this.getClass().getClassLoader());
        final List<ArtifactClassLoader> artifactClassLoaders = new ArrayList<>();
        artifactClassLoaders.add(new FilteringArtifactClassLoader(pluginClassLoader1, filter, Collections.emptyList()));
        final List<URL> urls = new ArrayList<>();
        urls.add(this.getClass().getResource("/plugin1-bootstrap.properties"));
        urls.add(this.getClass().getResource("/plugin2-bootstrap.properties"));
        when(pluginClassLoader1.findResources(BOOTSTRAP_PROPERTIES)).thenReturn(new EnumerationAdapter<>(urls));
        final ArtifactClassLoader appClassLoader = mock(ArtifactClassLoader.class);
        when(appClassLoader.getClassLoader()).thenReturn(this.getClass().getClassLoader());
        when(appClassLoader.findResources(BOOTSTRAP_PROPERTIES)).thenReturn(new EnumerationAdapter<>(emptyList()));
        final ClassLoader executionClassLoader = new FilteringArtifactClassLoader(appClassLoader, filter, Collections.emptyList());
        return new TestBootstrapServiceDiscovererConfigurationBuilder(getClass().getClassLoader(), executionClassLoader, artifactClassLoaders);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) FilteringArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.FilteringArtifactClassLoader) ClassLoaderFilter(org.mule.runtime.module.artifact.api.classloader.ClassLoaderFilter) ArrayList(java.util.ArrayList) FilteringArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.FilteringArtifactClassLoader) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) FilteringArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.FilteringArtifactClassLoader) IOException(java.io.IOException) URL(java.net.URL)

Example 58 with ArtifactClassLoader

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

the class ClasspathMuleCoreExtensionDiscovererTestCase method setsContainerClassLoaderOnDiscoveredExtensions.

@Test
public void setsContainerClassLoaderOnDiscoveredExtensions() throws Exception {
    final ArtifactClassLoader artifactClassLoader = mock(ArtifactClassLoader.class);
    final ClassLoader classLoader = mock(ClassLoader.class);
    final URL resource = getClass().getClassLoader().getResource("test-core-extension.properties");
    when(classLoader.getResources(CORE_EXTENSION_RESOURCE_NAME)).thenReturn(new EnumerationAdapter<URL>(Collections.singleton(resource)));
    when(artifactClassLoader.getClassLoader()).thenReturn(classLoader);
    final ClasspathMuleCoreExtensionDiscoverer discoverer = new ClasspathMuleCoreExtensionDiscoverer(artifactClassLoader);
    // Uses context classloader to force discovering of the test properties
    final List<MuleCoreExtension> discover = withContextClassLoader(artifactClassLoader.getClassLoader(), () -> {
        try {
            return discoverer.discover();
        } catch (DefaultMuleException e) {
            throw new IllegalStateException(e);
        }
    });
    assertThat(discover.size(), equalTo(1));
    assertThat(discover.get(0), instanceOf(TestCoreExtension.class));
    assertThat(((TestCoreExtension) discover.get(0)).containerClassLoader, is(artifactClassLoader));
}
Also used : DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) ClassUtils.withContextClassLoader(org.mule.runtime.core.api.util.ClassUtils.withContextClassLoader) MuleCoreExtension(org.mule.runtime.container.api.MuleCoreExtension) URL(java.net.URL) Test(org.junit.Test)

Aggregations

ArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader)58 Test (org.junit.Test)30 ArrayList (java.util.ArrayList)17 URL (java.net.URL)12 ClassLoaderLookupPolicy (org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy)12 Pair (org.mule.runtime.api.util.Pair)10 ArtifactPluginDescriptor (org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor)10 MuleArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader)10 Service (org.mule.runtime.api.service.Service)8 SmallTest (org.mule.tck.size.SmallTest)8 LookupStrategy (org.mule.runtime.module.artifact.api.classloader.LookupStrategy)7 Map (java.util.Map)6 RegionClassLoader (org.mule.runtime.module.artifact.api.classloader.RegionClassLoader)6 File (java.io.File)5 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)5 List (java.util.List)5 MuleModule (org.mule.runtime.container.api.MuleModule)5 ContainerOnlyLookupStrategy (org.mule.runtime.container.internal.ContainerOnlyLookupStrategy)5 HashSet (java.util.HashSet)4