Search in sources :

Example 1 with DelegateOnlyLookupStrategy

use of org.mule.runtime.module.artifact.api.classloader.DelegateOnlyLookupStrategy 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)

Example 2 with DelegateOnlyLookupStrategy

use of org.mule.runtime.module.artifact.api.classloader.DelegateOnlyLookupStrategy 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)

Aggregations

HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 ArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader)2 ClassLoaderLookupPolicy (org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy)2 DelegateOnlyLookupStrategy (org.mule.runtime.module.artifact.api.classloader.DelegateOnlyLookupStrategy)2 LookupStrategy (org.mule.runtime.module.artifact.api.classloader.LookupStrategy)2 PARENT_FIRST (org.mule.runtime.module.artifact.api.classloader.ParentFirstLookupStrategy.PARENT_FIRST)2 LinkedList (java.util.LinkedList)1 Set (java.util.Set)1 Collectors.toList (java.util.stream.Collectors.toList)1 StringUtils.isEmpty (org.apache.commons.lang3.StringUtils.isEmpty)1 Preconditions.checkArgument (org.mule.runtime.api.util.Preconditions.checkArgument)1 ModuleRepository (org.mule.runtime.container.api.ModuleRepository)1 MuleModule (org.mule.runtime.container.api.MuleModule)1 ContainerOnlyLookupStrategy (org.mule.runtime.container.internal.ContainerOnlyLookupStrategy)1 ArtifactPluginDescriptor (org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor)1 MULE_PLUGIN_CLASSIFIER (org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor.MULE_PLUGIN_CLASSIFIER)1 ArtifactClassLoaderFactory (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoaderFactory)1