1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
package test;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.lang.Exception;
import java.lang.System;
import java.net.URL;
import java.util.zip.GZIPInputStream;
import javafx.lang.FX;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.helpers.DefaultHandler;
import test.IOUtilities;
/**
* @author fabriceb
*/
// Define proxy settings.
System.setProperty("proxySet", "true");
// Proxy host.
System.setProperty("proxyHost", "host");
// Proxy port.
System.setProperty("proxyPort", "port");
try {
// Gets JNLP cache file.
def url = new URL("http://dl.javafx.com/javafx-cache.jnlp");
def connection = url.openConnection();
println("Type: {connection.getContentType()}");
println("Lenght: {connection.getContentLength()}");
println("Encoding: {connection.getContentEncoding()}");
// Stream is compressed with GZip.
def gzip = new GZIPInputStream(url.openStream());
try {
// Initialize SAX.
def saxFactory = SAXParserFactory.newInstance();
var codebase;
// Define handler to be used during XML parsing.
def handler = DefaultHandler {
public override function startElement(uri, localName, qName, attributes) {
// Extract base site URL (should be http://dl.javafx.com/).
if ("jnlp".equals(qName)) {
codebase = attributes.getValue("codebase");
}
// Extract library files.
else if ("jar".equals(qName) or "nativelib".equals(qName)) {
def href = attributes.getValue("href");
if (href != null) {
def libURL = "{codebase}{href}";
downloadLib(libURL, href);
}
}
}
}
// Start SAX parser on the GZipped stream with custom handler.
saxFactory.newSAXParser().parse(gzip, handler);
}
finally {
gzip.close();
}
} catch (e:Exception) {
e.printStackTrace();
}
/**
* Download the file at the provided URL.
* @param libname Remote file.
* @param filename Local file.
* @exception IOException In case of I/O error.
*/
function downloadLib(libname:String, filename:String) {
def libURL = new URL(libname);
def file = new File("{FX.getProperty("javafx.user.dir")}/{filename}");
println("Downloading {libURL} into {file.getAbsolutePath()}");
try {
// Open connection to lib.
def libConnection = libURL.openConnection();
def libLenght = libConnection.getContentLength();
if (file.exists()) {
def fileLength = file.length();
// Check if already downloaded.
if (libLenght == fileLength) {
println("File already downloaded, skipping.");
return;
}
// Partial download, overwrite.
else {
println("File already downloaded, overriding.");
}
}
def remoteIn = new BufferedInputStream(libConnection.getInputStream());
try {
def out = new FileOutputStream(file);
try {
IOUtilities.copyStream(remoteIn, out, 4 * 4096);
out.flush();
}
finally {
out.close();
}
}
finally {
remoteIn.close();
}
} catch (fne:FileNotFoundException) {
println("Lib {libURL} missing on web server!");
}
} |
Partager