use of org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader 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;
}
use of org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader in project mule by mulesoft.
the class IsolatedClassLoaderFactory method createContainerArtifactClassLoader.
/**
* Creates an {@link ArtifactClassLoader} for the container. The difference between a mule container {@link ArtifactClassLoader}
* in standalone mode and this one is that it has to be aware that the parent class loader has all the URLs loaded in launcher
* app class loader so it has to create a particular look policy to resolve classes as CHILD_FIRST.
* <p/>
* In order to do that a {@link FilteringArtifactClassLoader} resolve is created with and empty look policy (meaning that
* CHILD_FIRST strategy will be used) for the {@link URL}s that are going to be exposed from the container class loader. This
* would be the parent class loader for the container so instead of going directly the launcher application class loader that
* has access to the whole classpath this filtering class loader will resolve only the classes for the {@link URL}s defined to
* be in the container.
*
* @param testContainerClassLoaderFactory {@link TestContainerClassLoaderFactory} that has the logic to create a container class
* loader
* @param artifactsUrlClassification the classifications to get plugins {@link URL}s
* @return an {@link ArtifactClassLoader} for the container
*/
protected ArtifactClassLoader createContainerArtifactClassLoader(TestContainerClassLoaderFactory testContainerClassLoaderFactory, ArtifactsUrlClassification artifactsUrlClassification) {
MuleArtifactClassLoader launcherArtifact = createLauncherArtifactClassLoader();
final List<MuleModule> muleModules = emptyList();
ClassLoaderFilter filteredClassLoaderLauncher = new ContainerClassLoaderFilterFactory().create(testContainerClassLoaderFactory.getBootPackages(), muleModules);
logClassLoaderUrls("CONTAINER", artifactsUrlClassification.getContainerUrls());
ArtifactClassLoader containerClassLoader = testContainerClassLoaderFactory.createContainerClassLoader(new FilteringArtifactClassLoader(launcherArtifact, filteredClassLoaderLauncher, emptyList()));
return containerClassLoader;
}
use of org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader 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);
}
}
use of org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader in project mule by mulesoft.
the class IsolatedServiceProviderDiscoverer method discover.
@Override
public List<Pair<ArtifactClassLoader, ServiceProvider>> discover() throws ServiceResolutionError {
List<Pair<ArtifactClassLoader, ServiceProvider>> serviceProviders = new LinkedList<>();
for (Object serviceArtifactClassLoader : serviceArtifactClassLoaders) {
try {
final ServiceProvider serviceProvider;
String artifactName = (String) serviceArtifactClassLoader.getClass().getMethod("getArtifactId").invoke(serviceArtifactClassLoader);
ClassLoader classLoader = (ClassLoader) serviceArtifactClassLoader.getClass().getMethod("getClassLoader").invoke(serviceArtifactClassLoader);
serviceProvider = instantiateServiceProvider(classLoader, artifactName);
// TODO MULE-12254 - Remove null which is needed in order to avoid class cast exceptions
serviceProviders.add(new Pair(null, serviceProvider));
} catch (Exception e) {
throw new IllegalStateException("Couldn't discover service from class loader: " + serviceArtifactClassLoader, e);
}
}
return serviceProviders;
}
use of org.mule.runtime.module.artifact.api.classloader.ArtifactClassLoader in project mule by mulesoft.
the class ContainerClassLoaderFactoryTestCase method createsClassLoaderLookupPolicy.
@Test
public void createsClassLoaderLookupPolicy() throws Exception {
final ModuleRepository moduleRepository = mock(ModuleRepository.class);
final ContainerClassLoaderFactory factory = new ContainerClassLoaderFactory(moduleRepository);
final List<MuleModule> modules = new ArrayList<>();
modules.add(new TestModuleBuilder("module1").exportingPackages("org.foo1", "org.foo1.bar").build());
modules.add(new TestModuleBuilder("module2").exportingPackages("org.foo2").build());
when(moduleRepository.getModules()).thenReturn(modules);
final ArtifactClassLoader containerClassLoader = factory.createContainerClassLoader(this.getClass().getClassLoader());
final ClassLoaderLookupPolicy classLoaderLookupPolicy = containerClassLoader.getClassLoaderLookupPolicy();
assertThat(classLoaderLookupPolicy.getClassLookupStrategy("org.foo1.Foo"), instanceOf(ContainerOnlyLookupStrategy.class));
assertThat(classLoaderLookupPolicy.getClassLookupStrategy("org.foo1.bar.Bar"), instanceOf(ContainerOnlyLookupStrategy.class));
assertThat(classLoaderLookupPolicy.getClassLookupStrategy("org.foo2.Fo"), instanceOf(ContainerOnlyLookupStrategy.class));
assertThat(classLoaderLookupPolicy.getClassLookupStrategy("org.foo2.bar.Bar"), sameInstance(CHILD_FIRST));
}
Aggregations