Search in sources :

Example 11 with ClassLoaderLookupPolicy

use of org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy 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 12 with ClassLoaderLookupPolicy

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

the class PluginLookPolicyFactoryTestCase method getParentClassLoaderLookupPolicy.

private ClassLoaderLookupPolicy getParentClassLoaderLookupPolicy() {
    ArgumentCaptor<Map> argumentCaptor = forClass(Map.class);
    ClassLoaderLookupPolicy parentLookupPolicies = mock(ClassLoaderLookupPolicy.class);
    when(parentLookupPolicies.extend(argumentCaptor.capture())).thenAnswer(invocation -> getClassLoaderLookupPolicyByPackage(argumentCaptor.getValue()));
    return parentLookupPolicies;
}
Also used : ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) Map(java.util.Map)

Example 13 with ClassLoaderLookupPolicy

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

the class PluginLookPolicyFactoryTestCase method lookupPoliciesForPluginThatDeclaresDependency.

@Test
public void lookupPoliciesForPluginThatDeclaresDependency() {
    PluginUrlClassification barPluginClassification = new PluginUrlClassification(BAR_PLUGIN_ID, emptyList(), emptyList(), newArrayList(FOO_PLUGIN_ID));
    List<PluginUrlClassification> pluginClassifications = newArrayList(barPluginClassification, fooPluginClassification);
    ClassLoaderLookupPolicy parentLookupPolicies = getParentClassLoaderLookupPolicy();
    ClassLoaderLookupPolicy pluginPolicy = factory.createLookupPolicy(barPluginClassification, pluginClassifications, parentLookupPolicies, null);
    assertThat(pluginPolicy.getClassLookupStrategy(FOO_PACKAGE), sameInstance(PARENT_FIRST));
}
Also used : ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) PluginUrlClassification(org.mule.test.runner.api.PluginUrlClassification) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 14 with ClassLoaderLookupPolicy

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

the class CustomJavaSerializationProtocolTestCase method serializeWithoutDefaultConstructorFromArtifactClassLoader.

@Test
public final void serializeWithoutDefaultConstructorFromArtifactClassLoader() throws Exception {
    final File compiledClasses = new File(temporaryFolder.getRoot(), "compiledClasses");
    compiledClasses.mkdirs();
    final File sourceFile = new File(getClass().getResource("/org/foo/SerializableClass.java").toURI());
    CompilerUtils.SingleClassCompiler compiler = new CompilerUtils.SingleClassCompiler();
    compiler.compile(sourceFile);
    final URL[] urls = new URL[] { compiler.getTargetFolder().toURL() };
    final ClassLoaderLookupPolicy lookupPolicy = mock(ClassLoaderLookupPolicy.class);
    when(lookupPolicy.getClassLookupStrategy(any())).thenReturn(PARENT_FIRST);
    final MuleArtifactClassLoader artifactClassLoader = new MuleArtifactClassLoader(ARTIFACT_ID, new ArtifactDescriptor(ARTIFACT_NAME), urls, getClass().getClassLoader(), lookupPolicy);
    when(classLoaderRepository.getId(artifactClassLoader)).thenReturn(Optional.of(ARTIFACT_ID));
    when(classLoaderRepository.find(ARTIFACT_ID)).thenReturn(Optional.of(artifactClassLoader));
    final Class<?> echoTestClass = artifactClassLoader.loadClass(SERIALIZABLE_CLASS);
    final Object payload = echoTestClass.newInstance();
    setObjectName(payload);
    CoreEvent event = eventBuilder(muleContext).message(of(payload)).build();
    byte[] bytes = serializationProtocol.serialize(event.getMessage());
    InternalMessage message = serializationProtocol.deserialize(bytes);
    Object deserialized = message.getPayload().getValue();
    assertThat(deserialized.getClass().getName(), equalTo(SERIALIZABLE_CLASS));
    assertThat(deserialized.getClass().getClassLoader(), equalTo(artifactClassLoader));
    assertThat(deserialized, equalTo(payload));
    assertThat(getObjectName(deserialized), equalTo(INSTANCE_NAME));
}
Also used : InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) URL(java.net.URL) ArtifactDescriptor(org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptor) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) CompilerUtils(org.mule.tck.util.CompilerUtils) MuleArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader) File(java.io.File) Test(org.junit.Test)

Example 15 with ClassLoaderLookupPolicy

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

the class FineGrainedControlClassLoaderTestCase method usesParentFirstLookup.

@Test
public void usesParentFirstLookup() throws Exception {
    URLClassLoader parent = new URLClassLoader(new URL[] { getParentResource() }, Thread.currentThread().getContextClassLoader());
    final ClassLoaderLookupPolicy lookupPolicy = mock(ClassLoaderLookupPolicy.class);
    when(lookupPolicy.getClassLookupStrategy(TEST_CLASS_NAME)).thenReturn(PARENT_FIRST);
    FineGrainedControlClassLoader ext = new FineGrainedControlClassLoader(new URL[] { getChildFileResource() }, parent, lookupPolicy);
    assertEquals(EXPECTED_PARENT_MESSAGE, invokeTestClassMethod(ext));
}
Also used : FineGrainedControlClassLoader(org.mule.runtime.module.artifact.api.classloader.FineGrainedControlClassLoader) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) URLClassLoader(java.net.URLClassLoader) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Aggregations

ClassLoaderLookupPolicy (org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy)32 Test (org.junit.Test)19 SmallTest (org.mule.tck.size.SmallTest)16 ArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader)12 URLClassLoader (java.net.URLClassLoader)8 FineGrainedControlClassLoader (org.mule.runtime.module.artifact.api.classloader.FineGrainedControlClassLoader)8 URL (java.net.URL)6 HashMap (java.util.HashMap)5 ArtifactPluginDescriptor (org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor)5 LookupStrategy (org.mule.runtime.module.artifact.api.classloader.LookupStrategy)5 PARENT_FIRST (org.mule.runtime.module.artifact.api.classloader.ParentFirstLookupStrategy.PARENT_FIRST)5 RegionClassLoader (org.mule.runtime.module.artifact.api.classloader.RegionClassLoader)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 MuleModule (org.mule.runtime.container.api.MuleModule)4 ArtifactClassLoaderFilter (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoaderFilter)4 ArtifactDescriptor (org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptor)4 TestClassLoader (org.mule.tck.classlaoder.TestClassLoader)4 System.lineSeparator (java.lang.System.lineSeparator)3