Skip to content

Commit

Permalink
ConfigureProxy task to set proxy before downloading Gradle
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaroslav Tulach committed Jan 25, 2019
1 parent dc92644 commit f2c5275
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 64 deletions.
11 changes: 10 additions & 1 deletion groovy/gradle/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<description>Builds, tests, and runs the project org.netbeans.modules.gradle</description>
<import file="../../nbbuild/templates/projectized.xml"/>

<taskdef name="configureproxy" classname="org.netbeans.nbbuild.extlibs.ConfigureProxy" classpath="${nbantext.jar}"/>

<property name="test-unit-sys-prop.test.data.dir" location="test/data"/>
<property name="tooling" value="netbeans-gradle-tooling"/>
<available property="have.gradle.wrapper" file="${tooling}/gradle/wrapper/gradle-wrapper.jar"/>
Expand All @@ -36,9 +38,16 @@
<copy file="external/gradle-wrapper-4.10.2.jar" tofile="${tooling}/gradle/wrapper/gradle-wrapper.jar"/>
</target>

<target name="build-tooling-lib" depends="-copy-gradle-wrapper,-uptodate-tooling" unless="tooling.uptodate">
<target name="build-tooling-lib" depends="-download.release.files,-copy-gradle-wrapper,-uptodate-tooling" unless="tooling.uptodate">
<configureproxy connectTo="http://netbeans.apache.org" hostProperty="proxyHost" portProperty="proxyPort"/>
<condition property="gradle.proxy.args" value="-Dhttp.proxyHost=${proxyHost} -Dhttp.proxyPort=${proxyPort} -Dhttps.proxyHost=${proxyHost} -Dhttps.proxyPort=${proxyPort}">
<isset property="proxyHost" />
</condition>
<property name="gradle.proxy.args" value=""/>

<java fork="true" dir="${tooling}" classpath="${tooling}/gradle/wrapper/gradle-wrapper.jar" classname="org.gradle.wrapper.GradleWrapperMain" failonerror="true">
<sysproperty key="org.gradle.appname" value="Gradle"/>
<arg line="${gradle.proxy.args}"/>
<arg line="clean build -x check"/>
</java>
<copy file="${tooling}/build/libs/${tooling}.jar" todir="build/tooling" overwrite="true"/>
Expand Down
140 changes: 140 additions & 0 deletions nbbuild/antsrc/org/netbeans/nbbuild/extlibs/ConfigureProxy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.nbbuild.extlibs;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;

public final class ConfigureProxy extends Task {
private URL connectTo;
private String hostProperty = "http.proxyHost";
private String portProperty = "http.proxyPort";

public void setConnectTo(String uri) throws MalformedURLException {
connectTo = new URL(uri);
}

public void setHostProperty(String host) {
hostProperty = host;
}

public void setPortProperty(String port) {
portProperty = port;
}

@Override
public void execute() throws BuildException {
try {
URI[] connectedVia = { null };
URLConnection connect = openConnection(this, connectTo, connectedVia);
if (connect == null) {
throw new BuildException("Cannot connect to " + connectedVia);
}

if (connectedVia[0] != null) {
final String host = connectedVia[0].getHost();
log(String.format("Setting %s to %s", hostProperty, host), Project.MSG_INFO);
getProject().setUserProperty(hostProperty, host);
final int port = connectedVia[0].getPort();
log(String.format("Setting %s to %d", portProperty, port), Project.MSG_INFO);
getProject().setUserProperty(portProperty, "" + port);
}
} catch (IOException ex) {
throw new BuildException(ex);
}
}

static URLConnection openConnection(Task task, final URL url, URI[] connectedVia) throws IOException {
final URLConnection[] conn = { null };
final CountDownLatch connected = new CountDownLatch(1);
ExecutorService connectors = Executors.newFixedThreadPool(3);
connectors.submit(() -> {
String httpProxy = System.getenv("http_proxy");
if (httpProxy != null) {
try {
URI uri = new URI(httpProxy);
InetSocketAddress address = InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort());
Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
URLConnection test = url.openConnection(proxy);
test.connect();
conn[0] = test;
connected.countDown();
if (connectedVia != null) {
connectedVia[0] = uri;
}
} catch (IOException | URISyntaxException ex) {
task.log(ex, Project.MSG_ERR);
}
}
});
connectors.submit(() -> {
String httpProxy = System.getenv("https_proxy");
if (httpProxy != null) {
try {
URI uri = new URI(httpProxy);
InetSocketAddress address = InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort());
Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
URLConnection test = url.openConnection(proxy);
test.connect();
conn[0] = test;
connected.countDown();
if (connectedVia != null) {
connectedVia[0] = uri;
}
} catch (IOException | URISyntaxException ex) {
task.log(ex, Project.MSG_ERR);
}
}
});
connectors.submit(() -> {
try {
URLConnection test = url.openConnection();
test.connect();
conn[0] = test;
connected.countDown();
} catch (IOException ex) {
task.log(ex, Project.MSG_ERR);
}
});
try {
connected.await(5, TimeUnit.SECONDS);
} catch (InterruptedException ex) {
}
if (conn[0] == null) {
throw new IOException("Cannot connect to " + url);
}
return conn[0];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private byte[] legacyDownload(String cacheName) throws IOException {

private byte[] downloadFromServer(URL url) throws IOException {
log("Downloading: " + url);
URLConnection conn = openConnection(url);
URLConnection conn = ConfigureProxy.openConnection(this, url, null);
int code = HttpURLConnection.HTTP_OK;
if (conn instanceof HttpURLConnection) {
code = ((HttpURLConnection) conn).getResponseCode();
Expand All @@ -293,68 +293,6 @@ interface Downloader {
public byte[] download() throws IOException;
}

private URLConnection openConnection(final URL url) throws IOException {
final URLConnection[] conn = { null };
final CountDownLatch connected = new CountDownLatch(1);
ExecutorService connectors = Executors.newFixedThreadPool(3);
connectors.submit(new Runnable() {
public void run() {
String httpProxy = System.getenv("http_proxy");
if (httpProxy != null) {
try {
URI uri = new URI(httpProxy);
InetSocketAddress address = InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort());
Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
URLConnection test = url.openConnection(proxy);
test.connect();
conn[0] = test;
connected.countDown();
} catch (IOException | URISyntaxException ex) {
log(ex, Project.MSG_ERR);
}
}
}
});
connectors.submit(new Runnable() {
public void run() {
String httpProxy = System.getenv("https_proxy");
if (httpProxy != null) {
try {
URI uri = new URI(httpProxy);
InetSocketAddress address = InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort());
Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
URLConnection test = url.openConnection(proxy);
test.connect();
conn[0] = test;
connected.countDown();
} catch (IOException | URISyntaxException ex) {
log(ex, Project.MSG_ERR);
}
}
}
});
connectors.submit(new Runnable() {
public void run() {
try {
URLConnection test = url.openConnection();
test.connect();
conn[0] = test;
connected.countDown();
} catch (IOException ex) {
log(ex, Project.MSG_ERR);
}
}
});
try {
connected.await(5, TimeUnit.SECONDS);
} catch (InterruptedException ex) {
}
if (conn[0] == null) {
throw new IOException("Cannot connect to " + url);
}
return conn[0];
}

private String hash(File f) {
try {
try (FileInputStream is = new FileInputStream(f)) {
Expand Down

0 comments on commit f2c5275

Please sign in to comment.