Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #991 from coursier/topic/bootstrap-library
Move bootstrap generation to separate library
- Loading branch information
Showing
23 changed files
with
633 additions
and
421 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...-launcher/src/main/java/coursier/bootstrap/launcher/BootstrapURLStreamHandlerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package coursier.bootstrap.launcher; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.net.URLStreamHandler; | ||
import java.net.URLStreamHandlerFactory; | ||
import java.util.Random; | ||
|
||
// JARs from JARs can't be used directly, see: | ||
// http://stackoverflow.com/questions/183292/classpath-including-jar-within-a-jar/2326775#2326775 | ||
// Loading them via a custom protocol, inspired by: | ||
// http://stackoverflow.com/questions/26363573/registering-and-using-a-custom-java-net-url-protocol/26409796#26409796 | ||
public class BootstrapURLStreamHandlerFactory implements URLStreamHandlerFactory { | ||
private final String bootstrapProtocol; | ||
private final String basePath; | ||
private final ClassLoader loader; | ||
private boolean registered = false; | ||
|
||
public BootstrapURLStreamHandlerFactory(String bootstrapProtocol, String basePath, ClassLoader loader) { | ||
this.bootstrapProtocol = bootstrapProtocol; | ||
this.basePath = basePath; | ||
this.loader = loader; | ||
} | ||
|
||
public BootstrapURLStreamHandlerFactory(String basePath, ClassLoader loader) { | ||
Random rng = new Random(); | ||
this.bootstrapProtocol = "bootstrap" + rng.nextLong(); | ||
this.basePath = basePath; | ||
this.loader = loader; | ||
} | ||
|
||
public URLStreamHandler createURLStreamHandler(String protocol) { | ||
return bootstrapProtocol.equals(protocol) ? new URLStreamHandler() { | ||
protected URLConnection openConnection(URL url) throws IOException { | ||
String path = url.getPath(); | ||
URL resURL = loader.getResource(basePath + path); | ||
if (resURL == null) | ||
throw new FileNotFoundException("Resource " + basePath + path); | ||
return resURL.openConnection(); | ||
} | ||
} : null; | ||
} | ||
|
||
public String getProtocol() { | ||
registerIfNeeded(); | ||
return bootstrapProtocol; | ||
} | ||
|
||
public void register() { | ||
URL.setURLStreamHandlerFactory(this); | ||
registered = true; | ||
} | ||
public void registerIfNeeded() { | ||
if (!registered) | ||
register(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ursier/bootstrap/IsolatedClassLoader.java → ...otstrap/launcher/IsolatedClassLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
scala_library( | ||
name = "bootstrap", | ||
dependencies = [ | ||
], | ||
sources = rglobs("*.scala"), | ||
) |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.