Search in sources :

Example 16 with ArtifactClassLoader

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

the class DefaultApplicationFactoryTestCase method createDomain.

private Domain createDomain(String name) {
    final Domain domain = mock(Domain.class);
    final ArtifactClassLoader domainArtifactClassLoader = mock(ArtifactClassLoader.class);
    when(domainArtifactClassLoader.getClassLoader()).thenReturn(mock(ClassLoader.class));
    final ClassLoaderLookupPolicy domainLookupPolicy = mock(ClassLoaderLookupPolicy.class);
    when(domainArtifactClassLoader.getClassLoaderLookupPolicy()).thenReturn(domainLookupPolicy);
    when(domainRepository.getDomain(DOMAIN_ARTIFACT_FILE_NAME)).thenReturn(domain);
    when(domain.getArtifactClassLoader()).thenReturn(domainArtifactClassLoader);
    when(domain.getDescriptor()).thenReturn(new DomainDescriptor(name));
    return domain;
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) MuleApplicationClassLoader(org.mule.runtime.deployment.model.internal.application.MuleApplicationClassLoader) Domain(org.mule.runtime.deployment.model.api.domain.Domain) DomainDescriptor(org.mule.runtime.deployment.model.api.domain.DomainDescriptor)

Example 17 with ArtifactClassLoader

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

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

the class ArtifactAwareContextSelectorTestCase method returnsMuleLoggerContextForInternalArtifactClassLoader.

@Test
public void returnsMuleLoggerContextForInternalArtifactClassLoader() {
    ArtifactClassLoader serviceClassLoader = new MuleArtifactClassLoader("test", new ApplicationDescriptor("test"), new URL[0], this.getClass().getClassLoader(), mock(ClassLoaderLookupPolicy.class));
    LoggerContext systemContext = selector.getContext("", this.getClass().getClassLoader(), true);
    LoggerContext serviceCtx = selector.getContext("", serviceClassLoader.getClassLoader(), true);
    assertThat(serviceCtx, instanceOf(MuleLoggerContext.class));
    assertThat(serviceCtx, sameInstance(systemContext));
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) MuleArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) MuleArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader) LoggerContext(org.apache.logging.log4j.core.LoggerContext) ApplicationDescriptor(org.mule.runtime.deployment.model.api.application.ApplicationDescriptor) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 19 with ArtifactClassLoader

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

the class MuleLoggerContextFactory method build.

/**
 * Builds a new {@link LoggerContext} for the given {@code classLoader} and {@code selector}
 *
 * @param classLoader the classloader of the artifact this logger context is for.
 * @param selector the selector to bew used when building the loggers for the new context.
 * @return
 */
public LoggerContext build(final ClassLoader classLoader, final ArtifactAwareContextSelector selector) {
    NewContextParameters parameters = resolveContextParameters(classLoader);
    if (parameters == null) {
        return getDefaultContext(selector);
    }
    MuleLoggerContext loggerContext = new MuleLoggerContext(parameters.contextName, parameters.loggerConfigFile, classLoader, selector, isStandalone());
    if (classLoader instanceof ArtifactClassLoader) {
        final ArtifactClassLoader artifactClassLoader = (ArtifactClassLoader) classLoader;
        artifactClassLoader.addShutdownListener(() -> selector.destroyLoggersFor(ArtifactAwareContextSelector.resolveLoggerContextClassLoader(classLoader)));
    }
    return loggerContext;
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader)

Example 20 with ArtifactClassLoader

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

the class FileSystemServiceProviderDiscoverer method createServiceProviders.

private List<Pair<ArtifactClassLoader, ServiceProvider>> createServiceProviders(List<ServiceDescriptor> serviceDescriptors, ArtifactClassLoaderFactory<ServiceDescriptor> serviceClassLoaderFactory) throws ServiceResolutionError {
    List<Pair<ArtifactClassLoader, ServiceProvider>> serviceProviders = new LinkedList<>();
    for (ServiceDescriptor serviceDescriptor : serviceDescriptors) {
        final ArtifactClassLoader serviceClassLoader = serviceClassLoaderFactory.create(getServiceArtifactId(serviceDescriptor), serviceDescriptor, apiClassLoader.getClassLoader(), apiClassLoader.getClassLoaderLookupPolicy());
        final ServiceProvider serviceProvider = instantiateServiceProvider(serviceClassLoader.getClassLoader(), serviceDescriptor.getServiceProviderClassName());
        serviceProviders.add(new Pair<>(serviceClassLoader, serviceProvider));
    }
    return serviceProviders;
}
Also used : ArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader) ServiceDescriptor(org.mule.runtime.module.service.internal.artifact.ServiceDescriptor) ServiceProvider(org.mule.runtime.api.service.ServiceProvider) LinkedList(java.util.LinkedList) Pair(org.mule.runtime.api.util.Pair)

Aggregations

ArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader)58 Test (org.junit.Test)30 ArrayList (java.util.ArrayList)17 URL (java.net.URL)12 ClassLoaderLookupPolicy (org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy)12 Pair (org.mule.runtime.api.util.Pair)10 ArtifactPluginDescriptor (org.mule.runtime.deployment.model.api.plugin.ArtifactPluginDescriptor)10 MuleArtifactClassLoader (org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader)10 Service (org.mule.runtime.api.service.Service)8 SmallTest (org.mule.tck.size.SmallTest)8 LookupStrategy (org.mule.runtime.module.artifact.api.classloader.LookupStrategy)7 Map (java.util.Map)6 RegionClassLoader (org.mule.runtime.module.artifact.api.classloader.RegionClassLoader)6 File (java.io.File)5 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)5 List (java.util.List)5 MuleModule (org.mule.runtime.container.api.MuleModule)5 ContainerOnlyLookupStrategy (org.mule.runtime.container.internal.ContainerOnlyLookupStrategy)5 HashSet (java.util.HashSet)4