Search in sources :

Example 1 with LookupStrategy

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;
}
Also used : HashMap(java.util.HashMap) LookupStrategy(org.mule.runtime.module.artifact.api.classloader.LookupStrategy) MuleModule(org.mule.runtime.container.api.MuleModule)

Example 2 with LookupStrategy

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)));
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) ArtifactPluginDescriptor(org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor) Map(java.util.Map) DelegateOnlyLookupStrategy(org.mule.runtime.module.artifact.api.classloader.DelegateOnlyLookupStrategy) ContainerOnlyLookupStrategy(org.mule.runtime.container.internal.ContainerOnlyLookupStrategy) LookupStrategy(org.mule.runtime.module.artifact.api.classloader.LookupStrategy) MuleModule(org.mule.runtime.container.api.MuleModule) Test(org.junit.Test)

Example 3 with LookupStrategy

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;
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) HashMap(java.util.HashMap) ArtifactPluginDescriptor(org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) RegionClassLoader(org.mule.runtime.module.artifact.api.classloader.RegionClassLoader) DefaultArtifactClassLoaderFilter(org.mule.runtime.module.artifact.api.classloader.DefaultArtifactClassLoaderFilter) ArtifactClassLoaderFilter(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoaderFilter) LookupStrategy(org.mule.runtime.module.artifact.api.classloader.LookupStrategy) ChildFirstLookupStrategy(org.mule.runtime.module.artifact.api.classloader.ChildFirstLookupStrategy)

Example 4 with LookupStrategy

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);
    }
}
Also used : HashMap(java.util.HashMap) ContainerOnlyLookupStrategy(org.mule.runtime.container.internal.ContainerOnlyLookupStrategy) LookupStrategy(org.mule.runtime.module.artifact.api.classloader.LookupStrategy) ChildFirstLookupStrategy(org.mule.runtime.module.artifact.api.classloader.ChildFirstLookupStrategy) ContainerOnlyLookupStrategy(org.mule.runtime.container.internal.ContainerOnlyLookupStrategy) MuleModule(org.mule.runtime.container.api.MuleModule)

Example 5 with LookupStrategy

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());
}
Also used : TEST_RUNNER_ARTIFACT_ID(org.mule.test.runner.RunnerConfiguration.TEST_RUNNER_ARTIFACT_ID) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) PARENT_FIRST(org.mule.runtime.module.artifact.api.classloader.ParentFirstLookupStrategy.PARENT_FIRST) MuleModule(org.mule.runtime.container.api.MuleModule) DefaultArtifactClassLoaderFilter(org.mule.runtime.module.artifact.api.classloader.DefaultArtifactClassLoaderFilter) FileUtils.toFile(org.apache.commons.io.FileUtils.toFile) ContainerClassLoaderFilterFactory(org.mule.runtime.container.internal.ContainerClassLoaderFilterFactory) FilteringArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.FilteringArtifactClassLoader) ClassLoaderFilterFactory(org.mule.runtime.module.artifact.api.classloader.ClassLoaderFilterFactory) Map(java.util.Map) System.getProperty(java.lang.System.getProperty) JarExplorer(org.mule.runtime.module.artifact.internal.util.JarExplorer) URI(java.net.URI) MULE_LOG_VERBOSE_CLASSLOADING(org.mule.runtime.core.api.config.MuleProperties.MULE_LOG_VERBOSE_CLASSLOADING) Collectors.toSet(java.util.stream.Collectors.toSet) ArtifactClassLoaderHolder(org.mule.test.runner.api.ArtifactClassLoaderHolder) ClassLoaderFilter(org.mule.runtime.module.artifact.api.classloader.ClassLoaderFilter) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) Collections.emptyList(java.util.Collections.emptyList) RegionClassLoader(org.mule.runtime.module.artifact.api.classloader.RegionClassLoader) Collection(java.util.Collection) Set(java.util.Set) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) LookupStrategy(org.mule.runtime.module.artifact.api.classloader.LookupStrategy) DefaultModuleRepository(org.mule.runtime.container.internal.DefaultModuleRepository) Collectors.joining(java.util.stream.Collectors.joining) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) JarInfo(org.mule.runtime.module.artifact.internal.util.JarInfo) ChildFirstLookupStrategy(org.mule.runtime.module.artifact.api.classloader.ChildFirstLookupStrategy) ModuleRepository(org.mule.runtime.container.api.ModuleRepository) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) FileJarExplorer(org.mule.runtime.module.artifact.internal.util.FileJarExplorer) DefaultNativeLibraryFinderFactory(org.mule.runtime.deployment.model.internal.nativelib.DefaultNativeLibraryFinderFactory) ArtifactUrlClassification(org.mule.test.runner.api.ArtifactUrlClassification) ContainerClassLoaderFactory(org.mule.runtime.container.internal.ContainerClassLoaderFactory) HashMap(java.util.HashMap) MuleClassLoaderLookupPolicy(org.mule.runtime.container.internal.MuleClassLoaderLookupPolicy) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) PluginUrlClassification(org.mule.test.runner.api.PluginUrlClassification) MuleArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader) ContainerOnlyLookupStrategy(org.mule.runtime.container.internal.ContainerOnlyLookupStrategy) ArtifactDescriptor(org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptor) Collections.emptyMap(java.util.Collections.emptyMap) SYSTEM_PACKAGES(org.mule.runtime.container.internal.ContainerClassLoaderFactory.SYSTEM_PACKAGES) ArtifactClassLoaderFilterFactory(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoaderFilterFactory) Logger(org.slf4j.Logger) Collections.emptySet(java.util.Collections.emptySet) MalformedURLException(java.net.MalformedURLException) Boolean.valueOf(java.lang.Boolean.valueOf) File(java.io.File) DefaultRegionPluginClassLoadersFactory.getArtifactPluginId(org.mule.runtime.deployment.model.internal.DefaultRegionPluginClassLoadersFactory.getArtifactPluginId) Collectors.toList(java.util.stream.Collectors.toList) MuleApplicationClassLoader(org.mule.runtime.deployment.model.internal.application.MuleApplicationClassLoader) ArtifactsUrlClassification(org.mule.test.runner.api.ArtifactsUrlClassification) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) ArtifactClassLoaderFilter(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoaderFilter) JarInfo(org.mule.runtime.module.artifact.internal.util.JarInfo) ArtifactDescriptor(org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptor) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) MuleClassLoaderLookupPolicy(org.mule.runtime.container.internal.MuleClassLoaderLookupPolicy) FilteringArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.FilteringArtifactClassLoader) MuleArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader) DefaultArtifactClassLoaderFilter(org.mule.runtime.module.artifact.api.classloader.DefaultArtifactClassLoaderFilter) ArtifactClassLoaderFilter(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoaderFilter) PluginUrlClassification(org.mule.test.runner.api.PluginUrlClassification) URL(java.net.URL)

Aggregations

LookupStrategy (org.mule.runtime.module.artifact.api.classloader.LookupStrategy)12 HashMap (java.util.HashMap)9 ArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader)7 ContainerOnlyLookupStrategy (org.mule.runtime.container.internal.ContainerOnlyLookupStrategy)6 ArtifactPluginDescriptor (org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor)6 Map (java.util.Map)5 MuleModule (org.mule.runtime.container.api.MuleModule)5 ClassLoaderLookupPolicy (org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy)5 ChildFirstLookupStrategy (org.mule.runtime.module.artifact.api.classloader.ChildFirstLookupStrategy)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ArtifactClassLoaderFilter (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoaderFilter)3 DefaultArtifactClassLoaderFilter (org.mule.runtime.module.artifact.api.classloader.DefaultArtifactClassLoaderFilter)3 DelegateOnlyLookupStrategy (org.mule.runtime.module.artifact.api.classloader.DelegateOnlyLookupStrategy)3 RegionClassLoader (org.mule.runtime.module.artifact.api.classloader.RegionClassLoader)3 PluginUrlClassification (org.mule.test.runner.api.PluginUrlClassification)3 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 File (java.io.File)2 URL (java.net.URL)2 Optional (java.util.Optional)2