GH-204 consistent common classloader creation

This commit is contained in:
Marius Bogoevici
2015-11-18 14:46:03 -05:00
committed by Mark Fisher
parent 522881e879
commit 2d245c0424
2 changed files with 20 additions and 1 deletions

View File

@@ -26,6 +26,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.boot.loader.Launcher;
import org.springframework.boot.loader.archive.Archive;
import org.springframework.cloud.stream.module.utils.ClassloaderUtils;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
@@ -94,7 +95,7 @@ public class MultiArchiveLauncher extends Launcher {
@Override
protected ClassLoader createClassLoader(URL[] urls) throws Exception {
return new URLClassLoader(urls);
return ClassloaderUtils.createModuleClassloader(urls);
}
}

View File

@@ -19,13 +19,19 @@ package org.springframework.cloud.stream.module.utils;
import java.net.URL;
import java.net.URLClassLoader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.loader.LaunchedURLClassLoader;
import org.springframework.util.StringUtils;
/**
* @author Marius Bogoevici
*/
public class ClassloaderUtils {
private static final Log log = LogFactory.getLog(ClassloaderUtils.class);
/**
* Creates a ClassLoader for the launched modules by merging the URLs supplied as argument with the URLs that
* make up the additional classpath of the launched JVM (retrieved from the application classloader), and
@@ -36,16 +42,28 @@ public class ClassloaderUtils {
*/
public static ClassLoader createModuleClassloader(URL[] urls) {
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
if (log.isDebugEnabled()) {
log.debug("systemClassLoader is " + systemClassLoader);
}
if (systemClassLoader instanceof URLClassLoader) {
// add the URLs of the application classloader to the created classloader
// to compensate for LaunchedURLClassLoader not delegating to parent to retrieve resources
@SuppressWarnings("resource")
URLClassLoader systemUrlClassLoader = (URLClassLoader) systemClassLoader;
URL[] mergedUrls = new URL[urls.length + systemUrlClassLoader.getURLs().length];
if (log.isDebugEnabled()) {
log.debug("Original URLs: " +
StringUtils.arrayToCommaDelimitedString(urls));
log.debug("Java Classpath URLs: " +
StringUtils.arrayToCommaDelimitedString(systemUrlClassLoader.getURLs()));
}
System.arraycopy(urls, 0, mergedUrls, 0, urls.length);
System.arraycopy(systemUrlClassLoader.getURLs(), 0, mergedUrls, urls.length,
systemUrlClassLoader.getURLs().length);
// add the extension classloader as parent to the created context, if accessible
if (log.isDebugEnabled()) {
log.debug("Classloader URLs: " + StringUtils.arrayToCommaDelimitedString(mergedUrls));
}
return new LaunchedURLClassLoader(mergedUrls, systemUrlClassLoader.getParent());
}
return new LaunchedURLClassLoader(urls, systemClassLoader);