use of org.mule.runtime.module.artifact.api.classloader.LookupStrategy in project mule by mulesoft.
the class ContainerClassLoaderFactory method buildClassLoaderLookupStrategy.
/**
* Creates a {@link Map<String, LookupStrategy>} for the packages exported on the container.
*
* @param containerClassLoader class loader containing container's classes. Non null.
* @param modules to be used for collecting the exported packages. Non null
* @return a {@link Map<String, LookupStrategy>} for the packages exported on the container
*/
private Map<String, LookupStrategy> buildClassLoaderLookupStrategy(ClassLoader containerClassLoader, List<MuleModule> modules) {
checkArgument(containerClassLoader != null, "containerClassLoader cannot be null");
checkArgument(modules != null, "modules cannot be null");
ContainerOnlyLookupStrategy containerOnlyLookupStrategy = new ContainerOnlyLookupStrategy(containerClassLoader);
final Map<String, LookupStrategy> result = new HashMap<>();
for (MuleModule muleModule : modules) {
for (String exportedPackage : muleModule.getExportedPackages()) {
// Lets artifacts to extend javax packages
result.put(exportedPackage, exportedPackage.startsWith("javax.") ? PARENT_FIRST : containerOnlyLookupStrategy);
}
}
return result;
}
use of org.mule.runtime.module.artifact.api.classloader.LookupStrategy in project mule by mulesoft.
the class DefaultRegionPluginClassLoadersFactoryTestCase method createsPluginWithPrivilegedContainerAccess.
@Test
public void createsPluginWithPrivilegedContainerAccess() throws Exception {
MuleModule privilegedModule = mock(MuleModule.class);
when(privilegedModule.getPrivilegedArtifacts()).thenReturn(singleton(PLUGIN_ARTIFACT_ID1));
when(privilegedModule.getPrivilegedExportedPackages()).thenReturn(singleton(PRIVILEGED_PACKAGE));
when(moduleRepository.getModules()).thenReturn(singletonList(privilegedModule));
List<ArtifactPluginDescriptor> artifactPluginDescriptors = singletonList(plugin1Descriptor);
ArgumentCaptor<Map> mapArgumentCaptor = forClass(Map.class);
when(regionOwnerLookupPolicy.extend(mapArgumentCaptor.capture())).thenReturn(pluginLookupPolicy);
List<ArtifactClassLoader> pluginClassLoaders = factory.createPluginClassLoaders(regionClassLoader, artifactPluginDescriptors, regionOwnerLookupPolicy);
assertThat(pluginClassLoaders, contains(pluginClassLoader1));
Map<String, LookupStrategy> value = mapArgumentCaptor.getValue();
assertThat(value, hasEntry(equalTo(PRIVILEGED_PACKAGE), instanceOf(ContainerOnlyLookupStrategy.class)));
}
use of org.mule.runtime.module.artifact.api.classloader.LookupStrategy in project mule by mulesoft.
the class AbstractArtifactClassLoaderBuilder method build.
/**
* Creates a new {@code ArtifactClassLoader} using the provided configuration. It will create the proper class loader hierarchy
* and filters the artifact resources and plugins classes and resources are resolve correctly.
*
* @return a {@code ArtifactClassLoader} created from the provided configuration.
* @throws IOException exception cause when it was not possible to access the file provided as dependencies
*/
public ArtifactClassLoader build() throws IOException {
checkState(artifactDescriptor != null, "artifact descriptor cannot be null");
parentClassLoader = getParentClassLoader();
checkState(parentClassLoader != null, "parent class loader cannot be null");
final String artifactId = getArtifactId(artifactDescriptor);
ClassLoaderLookupPolicy parentLookupPolicy = getParentLookupPolicy(parentClassLoader);
RegionClassLoader regionClassLoader = new RegionClassLoader(artifactId, artifactDescriptor, parentClassLoader.getClassLoader(), parentLookupPolicy);
ArtifactClassLoaderFilter artifactClassLoaderFilter = createArtifactClassLoaderFilter(artifactDescriptor.getClassLoaderModel(), parentLookupPolicy);
Map<String, LookupStrategy> appAdditionalLookupStrategy = new HashMap<>();
artifactClassLoaderFilter.getExportedClassPackages().stream().forEach(p -> appAdditionalLookupStrategy.put(p, PARENT_FIRST));
artifactPluginClassLoaders = pluginClassLoadersFactory.createPluginClassLoaders(regionClassLoader, artifactPluginDescriptors, regionClassLoader.getClassLoaderLookupPolicy().extend(appAdditionalLookupStrategy));
final ArtifactClassLoader artifactClassLoader = createArtifactClassLoader(artifactId, regionClassLoader);
regionClassLoader.addClassLoader(artifactClassLoader, artifactClassLoaderFilter);
int artifactPluginIndex = 0;
for (ArtifactPluginDescriptor artifactPluginDescriptor : artifactPluginDescriptors) {
final ArtifactClassLoaderFilter classLoaderFilter = createPluginClassLoaderFilter(artifactPluginDescriptor, artifactDescriptor.getClassLoaderModel().getExportedPackages(), parentLookupPolicy);
regionClassLoader.addClassLoader(artifactPluginClassLoaders.get(artifactPluginIndex), classLoaderFilter);
artifactPluginIndex++;
}
return artifactClassLoader;
}
use of org.mule.runtime.module.artifact.api.classloader.LookupStrategy in project mule by mulesoft.
the class IsolatedClassLoaderFactory method extendLookupPolicyForPrivilegedAccess.
private ClassLoaderLookupPolicy extendLookupPolicyForPrivilegedAccess(ClassLoaderLookupPolicy childClassLoaderLookupPolicy, ModuleRepository moduleRepository, TestContainerClassLoaderFactory testContainerClassLoaderFactory, PluginUrlClassification pluginUrlClassification) {
ContainerOnlyLookupStrategy containerOnlyLookupStrategy = new ContainerOnlyLookupStrategy(testContainerClassLoaderFactory.getContainerClassLoader().getClassLoader());
Map<String, LookupStrategy> privilegedLookupStrategies = new HashMap<>();
for (MuleModule module : moduleRepository.getModules()) {
if (hasPrivilegedApiAccess(pluginUrlClassification, module)) {
for (String packageName : module.getPrivilegedExportedPackages()) {
privilegedLookupStrategies.put(packageName, containerOnlyLookupStrategy);
}
}
}
if (privilegedLookupStrategies.isEmpty()) {
return childClassLoaderLookupPolicy;
} else {
return childClassLoaderLookupPolicy.extend(privilegedLookupStrategies);
}
}
use of org.mule.runtime.module.artifact.api.classloader.LookupStrategy in project mule by mulesoft.
the class IsolatedClassLoaderFactory method createTestRunnerPlugin.
private void createTestRunnerPlugin(ArtifactsUrlClassification artifactsUrlClassification, Map<String, LookupStrategy> appExportedLookupStrategies, ClassLoaderLookupPolicy childClassLoaderLookupPolicy, RegionClassLoader regionClassLoader, List<ArtifactClassLoader> filteredPluginsArtifactClassLoaders, List<ArtifactClassLoader> pluginsArtifactClassLoaders, List<ArtifactClassLoaderFilter> pluginArtifactClassLoaderFilters, DefaultModuleRepository moduleRepository, TestContainerClassLoaderFactory testContainerClassLoaderFactory, Set<String> parentExportedPackages) {
JarInfo testRunnerJarInfo = getTestRunnerJarInfo(artifactsUrlClassification);
String testRunnerArtifactId = getArtifactPluginId(regionClassLoader.getArtifactId(), "test-runner");
List<String> pluginDependencies = artifactsUrlClassification.getPluginUrlClassifications().stream().map(p -> p.getName()).collect(toList());
PluginUrlClassification testRunnerPluginClassification = new PluginUrlClassification(TEST_RUNNER_ARTIFACT_ID + ":", artifactsUrlClassification.getTestRunnerLibUrls(), emptyList(), pluginDependencies, testRunnerJarInfo.getPackages(), testRunnerJarInfo.getResources(), emptySet(), emptySet());
ClassLoaderLookupPolicy pluginLookupPolicy = extendLookupPolicyForPrivilegedAccess(childClassLoaderLookupPolicy, moduleRepository, testContainerClassLoaderFactory, testRunnerPluginClassification);
pluginLookupPolicy = pluginLookupPolicy.extend(appExportedLookupStrategies);
MuleArtifactClassLoader pluginCL = new MuleArtifactClassLoader(testRunnerArtifactId, new ArtifactDescriptor(testRunnerPluginClassification.getName()), testRunnerPluginClassification.getUrls().toArray(new URL[0]), regionClassLoader, pluginLookupPolicyGenerator.createLookupPolicy(testRunnerPluginClassification, artifactsUrlClassification.getPluginUrlClassifications(), pluginLookupPolicy, pluginsArtifactClassLoaders));
pluginsArtifactClassLoaders.add(pluginCL);
ArtifactClassLoaderFilter filter = createArtifactClassLoaderFilter(testRunnerPluginClassification, parentExportedPackages, childClassLoaderLookupPolicy);
pluginArtifactClassLoaderFilters.add(filter);
filteredPluginsArtifactClassLoaders.add(new FilteringArtifactClassLoader(pluginCL, filter, emptyList()));
logClassLoaderUrls("PLUGIN (" + testRunnerPluginClassification.getName() + ")", testRunnerPluginClassification.getUrls());
}
Aggregations