diff options
author | Benjamin Frank <ben+github@pipsfrank.de> | 2016-07-05 21:40:33 +0200 |
---|---|---|
committer | Benjamin Frank <ben+github@pipsfrank.de> | 2016-07-06 11:06:28 +0200 |
commit | c8733351614bed77bd72708fdb12b36268cb2a28 (patch) | |
tree | 60e069857eefee238406202a1a0ba4e690017025 /nailgun_launcher | |
parent | 11342ecc8c6bd92e73b8ad9a2791f8d9462043a6 (diff) | |
download | cbt-c8733351614bed77bd72708fdb12b36268cb2a28.tar.gz cbt-c8733351614bed77bd72708fdb12b36268cb2a28.tar.bz2 cbt-c8733351614bed77bd72708fdb12b36268cb2a28.zip |
Support proxy settings in CBT.
Proxy settings can be supplied either via Java system-properties or via
environment variables (http_proxy/https_proxy/no_proxy).
Java system-properties take precedence over env vars. Evaluation of
proxy settings happens only during nailgun startup for now.
Diffstat (limited to 'nailgun_launcher')
-rw-r--r-- | nailgun_launcher/NailgunLauncher.java | 1 | ||||
-rw-r--r-- | nailgun_launcher/Stage0Lib.java | 48 |
2 files changed, 46 insertions, 3 deletions
diff --git a/nailgun_launcher/NailgunLauncher.java b/nailgun_launcher/NailgunLauncher.java index c909c12..159041f 100644 --- a/nailgun_launcher/NailgunLauncher.java +++ b/nailgun_launcher/NailgunLauncher.java @@ -54,6 +54,7 @@ public class NailgunLauncher{ return; } + installProxySettings(); String[] diff = args[0].split("\\."); long start = _start - (Long.parseLong(diff[0]) * 1000L) - Long.parseLong(diff[1]); diff --git a/nailgun_launcher/Stage0Lib.java b/nailgun_launcher/Stage0Lib.java index 9c13680..5f8c5c7 100644 --- a/nailgun_launcher/Stage0Lib.java +++ b/nailgun_launcher/Stage0Lib.java @@ -67,7 +67,7 @@ public class Stage0Lib{ if(changed){ List<String> zincArgs = new ArrayList<String>( Arrays.asList( - new String[]{ + new String[]{ "-scala-compiler", earlyDeps.scalaCompiler_2_11_8_File, "-scala-library", earlyDeps.scalaLibrary_2_11_8_File, "-scala-extra", earlyDeps.scalaReflect_2_11_8_File, @@ -113,13 +113,55 @@ public class Stage0Lib{ new URL[]{ new URL("file:"+file) }, parent ); } + + private static String getVarFromEnv(String envKey) { + String value = System.getenv(envKey); + if(value==null || value.isEmpty()) { + value = System.getenv(envKey.toUpperCase()); + } + return value; + } + + private static void setProxyfromPropOrEnv(String envKey, String propKeyH, String propKeyP) { + String proxyHost = System.getProperty(propKeyH); + String proxyPort = System.getProperty(propKeyP); + if((proxyHost==null || proxyHost.isEmpty()) && (proxyPort==null || proxyPort.isEmpty())) { + String envVar = getVarFromEnv(envKey); + if(envVar != null && !envVar.isEmpty()) { + String[] proxy = envVar.replaceFirst("^https?://", "").split(":", 2); + System.setProperty(propKeyH, proxy[0]); + System.setProperty(propKeyP, proxy[1]); + } + } + } + + public static void installProxySettings() throws URISyntaxException { + setProxyfromPropOrEnv("http_proxy", "http.proxyHost", "http.proxyPort"); + setProxyfromPropOrEnv("https_proxy", "https.proxyHost", "https.proxyPort"); + String nonHosts = System.getProperty("http.nonProxyHosts"); + if(nonHosts==null || nonHosts.isEmpty()) { + String envVar = getVarFromEnv("no_proxy"); + if(envVar != null && !envVar.isEmpty()) { + System.setProperty("http.nonProxyHosts", envVar.replaceAll(",","|")); + } + } + } + + private static final ProxySelector ps = ProxySelector.getDefault(); + + public static HttpURLConnection openConnectionConsideringProxy(URL urlString) + throws IOException, URISyntaxException { + java.net.Proxy proxy = ps.select(urlString.toURI()).get(0); + return (HttpURLConnection) urlString.openConnection(proxy); + } + public static void download(URL urlString, Path target, String sha1) throws Exception { final Path unverified = Paths.get(target+".unverified"); if(!Files.exists(target)) { new File(target.toString()).getParentFile().mkdirs(); System.err.println("downloading " + urlString); System.err.println("to " + target); - final InputStream stream = urlString.openStream(); + final InputStream stream = openConnectionConsideringProxy(urlString).getInputStream(); Files.copy(stream, unverified, StandardCopyOption.REPLACE_EXISTING); stream.close(); final String checksum = sha1(Files.readAllBytes(unverified)); @@ -151,4 +193,4 @@ public class Stage0Lib{ copy[array.length] = item; return copy; } -}
\ No newline at end of file +} |