Search in sources :

Example 6 with LookupStrategy

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

the class IsolatedClassLoaderFactory method createArtifactClassLoader.

/**
 * Creates a {@link ArtifactClassLoaderHolder} containing the container, plugins and application {@link ArtifactClassLoader}s
 *
 * @param extraBootPackages {@link List} of {@link String}s of extra boot packages to be appended to the container
 *        {@link ClassLoader}
 * @param extraPrivilegedArtifacts {@link List} of {@link String}s of extra privileged artifacts. Each value needs to have the
 *        form groupId:versionId.
 * @param artifactsUrlClassification the {@link ArtifactsUrlClassification} that defines the different {@link URL}s for each
 *        {@link ClassLoader}
 * @return a {@link ArtifactClassLoaderHolder} that would be used to run the test
 */
public ArtifactClassLoaderHolder createArtifactClassLoader(List<String> extraBootPackages, Set<String> extraPrivilegedArtifacts, ArtifactsUrlClassification artifactsUrlClassification) {
    Map<String, LookupStrategy> appExportedLookupStrategies = new HashMap<>();
    JarInfo testJarInfo = getAppSharedPackages(artifactsUrlClassification.getApplicationSharedLibUrls());
    testJarInfo.getPackages().stream().forEach(p -> appExportedLookupStrategies.put(p, PARENT_FIRST));
    ArtifactClassLoader containerClassLoader;
    ClassLoaderLookupPolicy childClassLoaderLookupPolicy;
    RegionClassLoader regionClassLoader;
    final List<ArtifactClassLoader> filteredPluginsArtifactClassLoaders = new ArrayList<>();
    final List<ArtifactClassLoader> pluginsArtifactClassLoaders = new ArrayList<>();
    final List<ArtifactClassLoaderFilter> pluginArtifactClassLoaderFilters = new ArrayList<>();
    List<ArtifactClassLoader> serviceArtifactClassLoaders;
    DefaultModuleRepository moduleRepository = new DefaultModuleRepository(new TestModuleDiscoverer(extraPrivilegedArtifacts, new TestContainerModuleDiscoverer(ContainerClassLoaderFactory.class.getClassLoader())));
    try (final TestContainerClassLoaderFactory testContainerClassLoaderFactory = new TestContainerClassLoaderFactory(extraBootPackages, artifactsUrlClassification.getContainerUrls().toArray(new URL[0]), moduleRepository)) {
        final Map<String, LookupStrategy> pluginsLookupStrategies = new HashMap<>();
        for (PluginUrlClassification pluginUrlClassification : artifactsUrlClassification.getPluginUrlClassifications()) {
            pluginUrlClassification.getExportedPackages().forEach(p -> pluginsLookupStrategies.put(p, PARENT_FIRST));
        }
        containerClassLoader = createContainerArtifactClassLoader(testContainerClassLoaderFactory, artifactsUrlClassification);
        childClassLoaderLookupPolicy = testContainerClassLoaderFactory.getContainerClassLoaderLookupPolicy(containerClassLoader.getClassLoader());
        final ClassLoaderLookupPolicy appLookupPolicy = childClassLoaderLookupPolicy.extend(pluginsLookupStrategies);
        serviceArtifactClassLoaders = createServiceClassLoaders(containerClassLoader.getClassLoader(), childClassLoaderLookupPolicy, artifactsUrlClassification);
        regionClassLoader = new RegionClassLoader("Region", new ArtifactDescriptor("Region"), containerClassLoader.getClassLoader(), childClassLoaderLookupPolicy);
        if (!artifactsUrlClassification.getPluginUrlClassifications().isEmpty()) {
            for (PluginUrlClassification pluginUrlClassification : artifactsUrlClassification.getPluginUrlClassifications()) {
                logClassLoaderUrls("PLUGIN (" + pluginUrlClassification.getName() + ")", pluginUrlClassification.getUrls());
                String artifactId = getArtifactPluginId(regionClassLoader.getArtifactId(), pluginUrlClassification.getName());
                ClassLoaderLookupPolicy pluginLookupPolicy = extendLookupPolicyForPrivilegedAccess(childClassLoaderLookupPolicy, moduleRepository, testContainerClassLoaderFactory, pluginUrlClassification);
                pluginLookupPolicy = pluginLookupPolicy.extend(appExportedLookupStrategies);
                MuleArtifactClassLoader pluginCL = new MuleArtifactClassLoader(artifactId, new ArtifactDescriptor(pluginUrlClassification.getName()), pluginUrlClassification.getUrls().toArray(new URL[0]), regionClassLoader, pluginLookupPolicyGenerator.createLookupPolicy(pluginUrlClassification, artifactsUrlClassification.getPluginUrlClassifications(), pluginLookupPolicy, pluginsArtifactClassLoaders));
                pluginsArtifactClassLoaders.add(pluginCL);
                ArtifactClassLoaderFilter filter = createArtifactClassLoaderFilter(pluginUrlClassification, testJarInfo.getPackages(), childClassLoaderLookupPolicy);
                pluginArtifactClassLoaderFilters.add(filter);
                filteredPluginsArtifactClassLoaders.add(new FilteringArtifactClassLoader(pluginCL, filter, emptyList()));
            }
            createTestRunnerPlugin(artifactsUrlClassification, appExportedLookupStrategies, childClassLoaderLookupPolicy, regionClassLoader, filteredPluginsArtifactClassLoaders, pluginsArtifactClassLoaders, pluginArtifactClassLoaderFilters, moduleRepository, testContainerClassLoaderFactory, testJarInfo.getPackages());
        }
        ArtifactClassLoader appClassLoader = createApplicationArtifactClassLoader(regionClassLoader, appLookupPolicy, artifactsUrlClassification, pluginsArtifactClassLoaders);
        regionClassLoader.addClassLoader(appClassLoader, new DefaultArtifactClassLoaderFilter(testJarInfo.getPackages(), testJarInfo.getResources()));
        for (int i = 0; i < filteredPluginsArtifactClassLoaders.size(); i++) {
            final ArtifactClassLoaderFilter classLoaderFilter = pluginArtifactClassLoaderFilters.get(i);
            regionClassLoader.addClassLoader(filteredPluginsArtifactClassLoaders.get(i), classLoaderFilter);
        }
        return new ArtifactClassLoaderHolder(containerClassLoader, serviceArtifactClassLoaders, pluginsArtifactClassLoaders, appClassLoader);
    }
}
Also used : HashMap(java.util.HashMap) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) MuleClassLoaderLookupPolicy(org.mule.runtime.container.internal.MuleClassLoaderLookupPolicy) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) ArtifactClassLoaderHolder(org.mule.test.runner.api.ArtifactClassLoaderHolder) URL(java.net.URL) ArtifactDescriptor(org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptor) FilteringArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.FilteringArtifactClassLoader) 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) MuleArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader) ContainerClassLoaderFactory(org.mule.runtime.container.internal.ContainerClassLoaderFactory) PluginUrlClassification(org.mule.test.runner.api.PluginUrlClassification) 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) FilteringArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.FilteringArtifactClassLoader) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) MuleArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader) DefaultArtifactClassLoaderFilter(org.mule.runtime.module.artifact.api.classloader.DefaultArtifactClassLoaderFilter) JarInfo(org.mule.runtime.module.artifact.internal.util.JarInfo) DefaultModuleRepository(org.mule.runtime.container.internal.DefaultModuleRepository)

Example 7 with LookupStrategy

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

the class MuleClassLoaderLookupPolicyProviderTestCase method returnsConfiguredLookupStrategy.

@Test
public void returnsConfiguredLookupStrategy() throws Exception {
    MuleClassLoaderLookupPolicy lookupPolicy = new MuleClassLoaderLookupPolicy(singletonMap(JAVA_PACKAGE, CHILD_FIRST), emptySet());
    LookupStrategy lookupStrategy = lookupPolicy.getClassLookupStrategy(Object.class.getName());
    assertThat(lookupStrategy, sameInstance(CHILD_FIRST));
    lookupPolicy = new MuleClassLoaderLookupPolicy(singletonMap(JAVA_PACKAGE_PREFIX, CHILD_FIRST), emptySet());
    lookupStrategy = lookupPolicy.getClassLookupStrategy(Object.class.getName());
    assertThat(lookupStrategy, sameInstance(CHILD_FIRST));
}
Also used : LookupStrategy(org.mule.runtime.module.artifact.api.classloader.LookupStrategy) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 8 with LookupStrategy

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

the class ToolingApplicationClassLoaderBuilder method getParentLookupPolicy.

@Override
protected ClassLoaderLookupPolicy getParentLookupPolicy(ArtifactClassLoader parentClassLoader) {
    if (!usesCustomDomain) {
        return super.getParentLookupPolicy(parentClassLoader);
    }
    Map<String, LookupStrategy> lookupStrategies = new HashMap<>();
    DomainDescriptor descriptor = parentClassLoader.getArtifactDescriptor();
    descriptor.getClassLoaderModel().getExportedPackages().forEach(p -> lookupStrategies.put(p, PARENT_FIRST));
    for (ArtifactPluginDescriptor artifactPluginDescriptor : descriptor.getPlugins()) {
        artifactPluginDescriptor.getClassLoaderModel().getExportedPackages().forEach(p -> lookupStrategies.put(p, PARENT_FIRST));
    }
    return parentClassLoader.getClassLoaderLookupPolicy().extend(lookupStrategies);
}
Also used : HashMap(java.util.HashMap) ArtifactPluginDescriptor(org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor) LookupStrategy(org.mule.runtime.module.artifact.api.classloader.LookupStrategy) DomainDescriptor(org.mule.runtime.deployment.model.api.domain.DomainDescriptor)

Example 9 with LookupStrategy

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

the class DefaultRegionPluginClassLoadersFactoryTestCase method createsPluginWithPrivilegedPluginAccess.

@Test
public void createsPluginWithPrivilegedPluginAccess() throws Exception {
    ClassLoaderModel plugin1ClassLoaderModel = new ClassLoaderModel.ClassLoaderModelBuilder().exportingPrivilegedPackages(singleton(PRIVILEGED_PACKAGE), singleton(PLUGIN_ARTIFACT_ID2)).build();
    plugin1Descriptor.setClassLoaderModel(plugin1ClassLoaderModel);
    BundleDependency pluginDependency = new BundleDependency.Builder().setScope(BundleScope.COMPILE).setDescriptor(PLUGIN1_BUNDLE_DESCRIPTOR).setBundleUri(new File("test").toURI()).build();
    plugin2Descriptor.setClassLoaderModel(new ClassLoaderModel.ClassLoaderModelBuilder().dependingOn(singleton(pluginDependency)).build());
    List<ArtifactPluginDescriptor> artifactPluginDescriptors = new ArrayList<>();
    artifactPluginDescriptors.add(plugin1Descriptor);
    artifactPluginDescriptors.add(plugin2Descriptor);
    ArgumentCaptor<Map> argumentCaptor = forClass(Map.class);
    when(regionOwnerLookupPolicy.extend(argumentCaptor.capture())).thenReturn(pluginLookupPolicy);
    List<ArtifactClassLoader> pluginClassLoaders = factory.createPluginClassLoaders(regionClassLoader, artifactPluginDescriptors, regionOwnerLookupPolicy);
    assertThat(pluginClassLoaders, contains(pluginClassLoader1, pluginClassLoader2));
    assertThat((Map<String, LookupStrategy>) argumentCaptor.getAllValues().get(0), not(hasEntry(equalTo(PRIVILEGED_PACKAGE), instanceOf(DelegateOnlyLookupStrategy.class))));
    assertThat((Map<String, LookupStrategy>) argumentCaptor.getAllValues().get(1), hasEntry(equalTo(PRIVILEGED_PACKAGE), instanceOf(DelegateOnlyLookupStrategy.class)));
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) ArtifactPluginDescriptor(org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor) BundleDependency(org.mule.runtime.module.artifact.api.descriptor.BundleDependency) ArrayList(java.util.ArrayList) DelegateOnlyLookupStrategy(org.mule.runtime.module.artifact.api.classloader.DelegateOnlyLookupStrategy) File(java.io.File) 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) ClassLoaderModel(org.mule.runtime.module.artifact.api.descriptor.ClassLoaderModel) Test(org.junit.Test)

Example 10 with LookupStrategy

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

the class DefaultRegionPluginClassLoadersFactory method createPluginLookupPolicy.

private ClassLoaderLookupPolicy createPluginLookupPolicy(List<ArtifactClassLoader> classLoaders, ArtifactPluginDescriptor descriptor, ClassLoaderLookupPolicy baseLookupPolicy, List<ArtifactPluginDescriptor> artifactPluginDescriptors) {
    Map<String, LookupStrategy> pluginsLookupPolicies = new HashMap<>();
    List<ArtifactPluginDescriptor> pluginDescriptors = getPluginDescriptors(descriptor, artifactPluginDescriptors);
    for (ArtifactPluginDescriptor dependencyPluginDescriptor : pluginDescriptors) {
        if (dependencyPluginDescriptor.getName().equals(descriptor.getName())) {
            continue;
        }
        LookupStrategy lookupStrategy = getClassLoaderLookupStrategy(descriptor, dependencyPluginDescriptor);
        for (String exportedPackage : dependencyPluginDescriptor.getClassLoaderModel().getExportedPackages()) {
            pluginsLookupPolicies.put(exportedPackage, lookupStrategy);
        }
        if (isPrivilegedPluginDependency(descriptor, dependencyPluginDescriptor)) {
            Optional<ArtifactClassLoader> pluginClassLoader = classLoaders.stream().filter(c -> c.getArtifactDescriptor().getBundleDescriptor().getArtifactId().equals(dependencyPluginDescriptor.getBundleDescriptor().getArtifactId())).findFirst();
            if (!pluginClassLoader.isPresent()) {
                throw new IllegalStateException("Cannot find classloader for plugin: " + dependencyPluginDescriptor.getBundleDescriptor().getArtifactId());
            }
            lookupStrategy = new DelegateOnlyLookupStrategy(pluginClassLoader.get().getClassLoader());
            for (String exportedPackage : dependencyPluginDescriptor.getClassLoaderModel().getPrivilegedExportedPackages()) {
                pluginsLookupPolicies.put(exportedPackage, lookupStrategy);
            }
        }
    }
    ContainerOnlyLookupStrategy containerOnlyLookupStrategy = new ContainerOnlyLookupStrategy(this.getClass().getClassLoader());
    for (MuleModule module : moduleRepository.getModules()) {
        if (module.getPrivilegedArtifacts().contains(descriptor.getBundleDescriptor().getGroupId() + ":" + descriptor.getBundleDescriptor().getArtifactId())) {
            for (String packageName : module.getPrivilegedExportedPackages()) {
                pluginsLookupPolicies.put(packageName, containerOnlyLookupStrategy);
            }
        }
    }
    return baseLookupPolicy.extend(pluginsLookupPolicies);
}
Also used : ModuleRepository(org.mule.runtime.container.api.ModuleRepository) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) ArtifactPluginDescriptor(org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor) DelegateOnlyLookupStrategy(org.mule.runtime.module.artifact.api.classloader.DelegateOnlyLookupStrategy) CHILD_ONLY(org.mule.runtime.module.artifact.api.classloader.ChildOnlyLookupStrategy.CHILD_ONLY) BundleDependency(org.mule.runtime.module.artifact.api.descriptor.BundleDependency) Set(java.util.Set) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) 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) MuleModule(org.mule.runtime.container.api.MuleModule) Collectors.toList(java.util.stream.Collectors.toList) MULE_PLUGIN_CLASSIFIER(org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor.MULE_PLUGIN_CLASSIFIER) List(java.util.List) ArtifactClassLoaderFactory(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoaderFactory) Map(java.util.Map) Optional(java.util.Optional) ContainerOnlyLookupStrategy(org.mule.runtime.container.internal.ContainerOnlyLookupStrategy) LinkedList(java.util.LinkedList) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) HashMap(java.util.HashMap) ArtifactPluginDescriptor(org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor) ContainerOnlyLookupStrategy(org.mule.runtime.container.internal.ContainerOnlyLookupStrategy) MuleModule(org.mule.runtime.container.api.MuleModule) DelegateOnlyLookupStrategy(org.mule.runtime.module.artifact.api.classloader.DelegateOnlyLookupStrategy) DelegateOnlyLookupStrategy(org.mule.runtime.module.artifact.api.classloader.DelegateOnlyLookupStrategy) LookupStrategy(org.mule.runtime.module.artifact.api.classloader.LookupStrategy) ContainerOnlyLookupStrategy(org.mule.runtime.container.internal.ContainerOnlyLookupStrategy)

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