Simplify ClassLoader isolation in launch command

No more SBT-inspired ClasspathFilter. It now just tries to find the ClassLoader that loaded coursier, and use the parent as a base to load the launched application.

Allows to remove the -b option of the bootstrap command, and remove BaseCommand (that was holding options common to all commands).
This commit is contained in:
Alexandre Archambault
2016-01-26 18:14:20 +01:00
parent 3e9a37edc3
commit 92e5917af6
6 changed files with 70 additions and 197 deletions
@@ -8,7 +8,6 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.nio.file.Files;
import java.security.CodeSource;
@@ -170,8 +169,6 @@ public class Bootstrap {
String mainClass0 = System.getProperty("bootstrap.mainClass");
String jarDir0 = System.getProperty("bootstrap.jarDir");
boolean prependClasspath = Boolean.parseBoolean(System.getProperty("bootstrap.prependClasspath", "false"));
final File jarDir = new File(jarDir0);
if (jarDir.exists()) {
@@ -267,7 +264,7 @@ public class Bootstrap {
parentClassLoader = new IsolatedClassLoader(contextURLs, parentClassLoader, new String[]{ isolationID });
}
URLClassLoader classLoader = new URLClassLoader(localURLs.toArray(new URL[localURLs.size()]), parentClassLoader);
ClassLoader classLoader = new BootstrapClassLoader(localURLs.toArray(new URL[localURLs.size()]), parentClassLoader);
Class<?> mainClass = null;
Method mainMethod = null;
@@ -288,14 +285,6 @@ public class Bootstrap {
List<String> userArgs0 = new ArrayList<>();
if (prependClasspath) {
for (URL url : localURLs) {
assert url.getProtocol().equals("file");
userArgs0.add("-B");
userArgs0.add(url.getPath());
}
}
for (int i = 0; i < args.length; i++)
userArgs0.add(args[i]);
@@ -0,0 +1,25 @@
package coursier;
import java.net.URL;
import java.net.URLClassLoader;
public class BootstrapClassLoader extends URLClassLoader {
public BootstrapClassLoader(
URL[] urls,
ClassLoader parent
) {
super(urls, parent);
}
/**
* Can be called by reflection by launched applications, to find the "main" `ClassLoader`
* that loaded them, and possibly short-circuit it to load other things for example.
*
* The `launch` command of coursier does that.
*/
public boolean isBootstrapLoader() {
return true;
}
}