Search in sources :

Example 6 with ArtifactDescriptor

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

Example 7 with ArtifactDescriptor

use of org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptor in project mule by mulesoft.

the class IsolatedClassLoaderFactory method createServiceClassLoaders.

/**
 * For each service defined in the classification it creates an {@link ArtifactClassLoader} wit the name defined in
 * classification.
 *
 * @param parent the parent class loader to be assigned to the new one created here
 * @param childClassLoaderLookupPolicy look policy to be used
 * @param artifactsUrlClassification the url classifications to get service {@link URL}s
 * @return a list of {@link ArtifactClassLoader} for service class loaders
 */
protected List<ArtifactClassLoader> createServiceClassLoaders(ClassLoader parent, ClassLoaderLookupPolicy childClassLoaderLookupPolicy, ArtifactsUrlClassification artifactsUrlClassification) {
    List<ArtifactClassLoader> servicesArtifactClassLoaders = newArrayList();
    for (ArtifactUrlClassification serviceUrlClassification : artifactsUrlClassification.getServiceUrlClassifications()) {
        logClassLoaderUrls("SERVICE (" + serviceUrlClassification.getArtifactId() + ")", serviceUrlClassification.getUrls());
        MuleArtifactClassLoader artifactClassLoader = new MuleArtifactClassLoader(serviceUrlClassification.getName(), new ArtifactDescriptor(serviceUrlClassification.getName()), serviceUrlClassification.getUrls().toArray(new URL[0]), parent, childClassLoaderLookupPolicy);
        servicesArtifactClassLoaders.add(artifactClassLoader);
    }
    return servicesArtifactClassLoaders;
}
Also used : 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) ArtifactUrlClassification(org.mule.test.runner.api.ArtifactUrlClassification) ArtifactDescriptor(org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptor) MuleArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader) URL(java.net.URL)

Example 8 with ArtifactDescriptor

use of org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptor 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 9 with ArtifactDescriptor

use of org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptor in project mule by mulesoft.

the class TestContainerClassLoaderFactory method createContainerClassLoader.

/**
 * Overrides method due to it has to use the {@link ClassLoader} set to this factory in order to discover modules.
 *
 * @param parentClassLoader parent classLoader. Can be null.
 * @return a non null {@link ArtifactClassLoader} containing container code that can be used as parent classloader for other
 *         mule artifacts.
 */
@Override
public ArtifactClassLoader createContainerClassLoader(final ClassLoader parentClassLoader) {
    final List<MuleModule> muleModules = withContextClassLoader(classLoader, () -> testContainerModuleRepository.getModules());
    MuleClassLoaderLookupPolicy lookupPolicy = new MuleClassLoaderLookupPolicy(Collections.emptyMap(), getBootPackages());
    return createArtifactClassLoader(parentClassLoader, muleModules, lookupPolicy, new ArtifactDescriptor("mule"));
}
Also used : MuleClassLoaderLookupPolicy(org.mule.runtime.container.internal.MuleClassLoaderLookupPolicy) ArtifactDescriptor(org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptor) MuleModule(org.mule.runtime.container.api.MuleModule)

Example 10 with ArtifactDescriptor

use of org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptor in project mule by mulesoft.

the class TrackingArtifactClassLoaderFactoryTestCase method setUp.

@Before
public void setUp() throws Exception {
    artifactClassLoaderManager = mock(ArtifactClassLoaderManager.class);
    delegateFactory = mock(ArtifactClassLoaderFactory.class);
    factory = new TrackingArtifactClassLoaderFactory<>(artifactClassLoaderManager, delegateFactory);
    parent = mock(ArtifactClassLoader.class);
    descriptor = new ArtifactDescriptor(ARTIFACT_NAME);
    lookupPolicy = mock(ClassLoaderLookupPolicy.class);
    classLoader = new MuleArtifactClassLoader(ARTIFACT_NAME, descriptor, new URL[0], getClass().getClassLoader(), lookupPolicy);
    when(lookupPolicy.getClassLookupStrategy(any())).thenReturn(PARENT_FIRST);
    when(delegateFactory.create(ARTIFACT_ID, descriptor, getClass().getClassLoader(), lookupPolicy)).thenReturn(classLoader);
}
Also used : ArtifactDescriptor(org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptor) URL(java.net.URL) Before(org.junit.Before)

Aggregations

ArtifactDescriptor (org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptor)15 URL (java.net.URL)8 ArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader)5 ClassLoaderLookupPolicy (org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy)5 MuleArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader)5 RegionClassLoader (org.mule.runtime.module.artifact.api.classloader.RegionClassLoader)4 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 Before (org.junit.Before)3 MuleModule (org.mule.runtime.container.api.MuleModule)3 MuleClassLoaderLookupPolicy (org.mule.runtime.container.internal.MuleClassLoaderLookupPolicy)3 ArtifactClassLoaderFilter (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoaderFilter)3 FilteringArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.FilteringArtifactClassLoader)3 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 Test (org.junit.Test)2 ContainerClassLoaderFactory (org.mule.runtime.container.internal.ContainerClassLoaderFactory)2 ContainerOnlyLookupStrategy (org.mule.runtime.container.internal.ContainerOnlyLookupStrategy)2 DefaultModuleRepository (org.mule.runtime.container.internal.DefaultModuleRepository)2