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);
}
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);
}
}
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));
}
Aggregations