Switched from Update to ResourceGrabber, next is cache support...

This commit is contained in:
Travis Burtrum 2011-09-03 18:43:15 -04:00 committed by moparisthebest
parent 3ccf637838
commit 4cff6d2a68
3 changed files with 76 additions and 19 deletions

View File

@ -20,8 +20,6 @@
package org.moparscape.classloader;
import org.moparscape.Update;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
@ -108,7 +106,9 @@ public class CRCClassLoader extends ClassLoader {
// fos.close();
// use Update instead
new Update(backupURL, jarFileLoc, true);
//new Update(backupURL, jarFileLoc, true);
// use ResourceGrabber
org.moparscape.res.ResourceGrabber.getRG().downloadWaitCatch(backupURL, jarFileLoc);
setup(jarFileLoc);

View File

@ -46,13 +46,16 @@ import java.util.List;
* This is for security reasons. The only exception to this rule is it will download java_client.exe for internal
* purposes, but will only run it if the CRC is correct.
* <p/>
* This class is currently NOT thread-safe, so if you try and use it in a multi-threaded environment it will almost
* certainly break in unexpected and bad ways. Synchronize around it if you must.
* This class should be thread-safe.
* <p/>
* Only one instance of this class needs to be instantiated per-VM. Currently this is enforced via the constructors.
*
* @author moparisthebest
*/
public class ResourceGrabber {
private static ResourceGrabber _instance = null;
private final Downloader[] downloaders;
private static final int delay = 500; //milliseconds
@ -79,7 +82,7 @@ public class ResourceGrabber {
"You can specify as many downloads on the command line as you wish.");
return;
}
ResourceGrabber rg = new ResourceGrabber(System.getProperty("user.home") + "/.moparscape/bin/");
ResourceGrabber rg = getResourceGrabber(System.getProperty("user.home") + "/.moparscape/bin/");
int[] uids = new int[args.length / 4];
for (int x = 0; x < uids.length; ++x) {
int argIndex = x * 4;
@ -133,7 +136,7 @@ public class ResourceGrabber {
for(String p : s.split(":"))
System.out.println("part: '"+p.trim()+"'");
if(true) return; */
ResourceGrabber rg = new ResourceGrabber(System.getProperty("user.home") + "/.moparscape/bin/");
ResourceGrabber rg = getResourceGrabber(System.getProperty("user.home") + "/.moparscape/bin/");
System.out.println("before downloads...");
int clientZipUID = -1;
try {
@ -152,7 +155,7 @@ public class ResourceGrabber {
}
public ResourceGrabber(String binDir, String title) throws FileNotFoundException {
private ResourceGrabber(String binDir, String title) throws FileNotFoundException {
if(title != null)
this.title = title;
File f = new File(binDir);
@ -163,10 +166,30 @@ public class ResourceGrabber {
downloaders = new Downloader[]{new BTDownloader(binDir), new URLDownloader()};
}
public ResourceGrabber(String binDir) throws FileNotFoundException {
private ResourceGrabber(String binDir) throws FileNotFoundException {
this(binDir, null);
}
public synchronized static ResourceGrabber getResourceGrabber(String binDir, String title) throws FileNotFoundException{
if(_instance == null)
_instance = new ResourceGrabber(binDir, title);
return _instance;
}
public synchronized static ResourceGrabber getResourceGrabber() throws IllegalStateException{
if(_instance == null)
throw new IllegalStateException("Must call getResourceGrabber method with parameters first.");
return _instance;
}
public static ResourceGrabber getRG() throws IllegalStateException{
return getResourceGrabber();
}
public static ResourceGrabber getResourceGrabber(String binDir) throws FileNotFoundException{
return getResourceGrabber(binDir, null);
}
public boolean wait(int uid) throws Exception {
// -1 is a special value meaning return immediately
// maybe because CRC was already correct and download not needed
@ -236,6 +259,35 @@ public class ResourceGrabber {
return uid;
}
public boolean downloadWait(String url, String savePath) throws Exception {
return this.downloadWait(url, savePath, false, null);
}
public boolean downloadWait(String url, String savePath, boolean extract) throws Exception {
return this.downloadWait(url, savePath, extract, null);
}
public boolean downloadWait(String url, String savePath, boolean extract, ChecksumInfo ci) throws Exception {
return this.wait(this.download(url, savePath, extract, ci));
}
public boolean downloadWaitCatch(String url, String savePath) {
return this.downloadWaitCatch(url, savePath, false, null);
}
public boolean downloadWaitCatch(String url, String savePath, boolean extract) {
return this.downloadWaitCatch(url, savePath, extract, null);
}
public boolean downloadWaitCatch(String url, String savePath, boolean extract, ChecksumInfo ci) {
try{
return this.wait(this.download(url, savePath, extract, ci));
}catch(Exception e){
// ignore, just return false
return false;
}
}
private synchronized int getUID() {
return this.currentUID++;
}

View File

@ -52,13 +52,18 @@ public class URLDownloader extends Downloader {
public void run() {
try {
// first make savedTo dir
new File(savePath).mkdirs();
String sp = savePath;
if (!sp.endsWith("/"))
sp += "/";
String savedTo = sp + url.substring(url.lastIndexOf('/') + 1);
// lets detect how we want to save this, if the path ends in a /, save the file named as-is in that folder
// but if it ends in anything else, save the file to that file exactly
String saveTo;
if (savePath.endsWith("/")) {
// first make savePath dir
new File(savePath).mkdirs();
saveTo = savePath + url.substring(url.lastIndexOf('/') + 1);
} else {
// create all directories except last file
new File(savePath.substring(0, savePath.lastIndexOf('/'))).mkdirs();
saveTo = savePath;
}
URLConnection uc = new URL(url).openConnection();
if (uc instanceof HttpURLConnection) {
String userAgent = System.getProperty("http.agent");
@ -70,14 +75,14 @@ public class URLDownloader extends Downloader {
InputStream in = uc.getInputStream();
if (callback != null) {
callback.starting("Downloading " + url, length, "to " + savedTo + "...");
callback.starting("Downloading " + url, length, "to " + saveTo + "...");
in = new ProgressInputStream(in, callback);
}
writeStream(in, new FileOutputStream(savedTo));
writeStream(in, new FileOutputStream(saveTo));
if (callback != null) {
callback.finished(sp, savedTo);
callback.finished(savePath, saveTo);
callback.stopped();
}
} catch (IOException e) {