public void loadDriver(WriteableRepository repo,
DriverInfo driver,
FileWriteMonitor monitor) throws IOException {
int urlIndex = 0;
if (driver.urls.length > 1) {
if (random == null) {
random = new Random();
}
urlIndex = random.nextInt(driver.urls.length);
}
URL url = driver.urls[urlIndex];
InputStream in;
String uri = driver.getRepositoryURI();
if (driver.unzipPath != null) {
byte[] buf = new byte[1024];
int size;
int total = 0;
int threshold = 10240;
URLConnection uc = url.openConnection();
int filesize = uc.getContentLength();
InputStream net = uc.getInputStream();
JarFile jar = null;
File download = null;
try {
download = File.createTempFile("geronimo-driver-download", ".zip");
OutputStream out = new BufferedOutputStream(new FileOutputStream(download));
if (monitor != null) {
monitor.writeStarted("Download driver archive to " + download, filesize);
}
try {
while ((size = net.read(buf)) > -1) {
out.write(buf, 0, size);
if (monitor != null) {
total += size;
if (total > threshold) {
monitor.writeProgress(total);
threshold += 10240;
}
}
}
out.flush();
out.close();
} finally {
if (monitor != null) {
monitor.writeComplete(total);
}
}
jar = new JarFile(download);
JarEntry entry = jar.getJarEntry(driver.unzipPath);
if (entry == null) {
log.error("Cannot extract driver JAR " + driver.unzipPath + " from download file " + url);
} else {
in = jar.getInputStream(entry);
repo.copyToRepository(in, (int)entry.getSize(), Artifact.create(uri), monitor);
}
} finally {
if (jar != null) try {
jar.close();
} catch (IOException e) {
log.error("Unable to close JAR file", e);
}
if (download != null) {
download.delete();
}
}
} else {
URLConnection con = url.openConnection();
in = con.getInputStream();
repo.copyToRepository(in, con.getContentLength(), Artifact.create(uri), monitor);
}
}
Downloads a driver and loads it into the local repository. |