Finishing up and fixing bugs with ResourceGrabber.
This commit is contained in:
parent
a430006739
commit
aaa521c1cb
|
@ -39,15 +39,16 @@ import java.util.zip.GZIPInputStream;
|
|||
* This is a class loader that loads classes from jars on the filesystem, optionally checking the CRC of the classes and
|
||||
* grabbing new updated jars if the CRC doesn't match.
|
||||
* <p/>
|
||||
* todo: make this extend URLClassLoader so resources etc are handled automatically
|
||||
*/
|
||||
public class CRCClassLoader extends URLClassLoader {
|
||||
|
||||
private Map<String, byte[]> classes = new HashMap<String, byte[]>();
|
||||
private long crcVal;
|
||||
private ClassLoader parent = null;
|
||||
private long crcVal = 0;
|
||||
//private ClassLoader parent = null;
|
||||
private ProtectionDomain pd = null;
|
||||
|
||||
private int classesLoaded = 0;
|
||||
|
||||
/**
|
||||
* Reads the jar file and calculates the CRC. Sets up the class.
|
||||
* Responsibility of checking the CRC is the user's
|
||||
|
@ -62,7 +63,7 @@ public class CRCClassLoader extends URLClassLoader {
|
|||
super(new URL[]{new URL("file://"+jarFileLoc)}, parent == null ? null : parent.getClassLoader());
|
||||
setup(jarFileLoc);
|
||||
if (parent != null) {
|
||||
this.parent = parent.getClassLoader();
|
||||
//this.parent = parent.getClassLoader();
|
||||
this.pd = parent.getProtectionDomain();
|
||||
}
|
||||
}
|
||||
|
@ -86,7 +87,8 @@ public class CRCClassLoader extends URLClassLoader {
|
|||
ret = new CRCClassLoader(jarFileLoc, parent);
|
||||
|
||||
// check CRC
|
||||
if (ret.getCRC() == expectedCRC)
|
||||
System.out.println("ret1: "+ret);
|
||||
if (ret.successfullyLoaded(expectedCRC))
|
||||
return ret;
|
||||
} catch (IOException e) {
|
||||
//e.printStackTrace();
|
||||
|
@ -115,7 +117,7 @@ public class CRCClassLoader extends URLClassLoader {
|
|||
// use ResourceGrabber
|
||||
int jarWait = org.moparscape.res.ResourceGrabber.getRG().download(backupURL, jarFileLoc);
|
||||
if (org.moparscape.res.ResourceGrabber.getRG().waitCatch(jarWait)) {
|
||||
jarFileLoc += org.moparscape.res.ResourceGrabber.getRG().firstFileEndsWithIgnoreCase(jarWait, "jar.gz", "jar");
|
||||
jarFileLoc = org.moparscape.res.ResourceGrabber.getRG().firstFileEndsWithIgnoreCase(jarWait, "jar.gz", "jar");
|
||||
org.moparscape.res.ResourceGrabber.getRG().freeResources(jarWait);
|
||||
}
|
||||
System.out.println("new jarFileLoc: "+jarFileLoc);
|
||||
|
@ -123,7 +125,8 @@ public class CRCClassLoader extends URLClassLoader {
|
|||
|
||||
}
|
||||
|
||||
if (ret.getCRC() != expectedCRC) {
|
||||
System.out.println("ret2: "+ret);
|
||||
if (ret.successfullyLoaded(expectedCRC)) {
|
||||
String s = "CRC checksum failed. crc:" + ret.getCRC() + " expected:" + expectedCRC;
|
||||
if (crcMismatchException)
|
||||
throw new IOException(s);
|
||||
|
@ -185,6 +188,7 @@ public class CRCClassLoader extends URLClassLoader {
|
|||
//if (updateCRC) System.out.println("class name: " + className);
|
||||
// save class
|
||||
classes.put(className, baos.toByteArray());
|
||||
++classesLoaded;
|
||||
}
|
||||
}
|
||||
//jf.close();
|
||||
|
@ -198,6 +202,23 @@ public class CRCClassLoader extends URLClassLoader {
|
|||
return crcVal;
|
||||
}
|
||||
|
||||
public boolean successfullyLoaded(long expectedCRC){
|
||||
if(expectedCRC == 0)
|
||||
return classesLoaded > 0;
|
||||
else
|
||||
return crcVal == expectedCRC;
|
||||
//return (expectedCRC == 0 && classesLoaded > 0) || crcVal == expectedCRC;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CRCClassLoader{" +
|
||||
"crcVal=" + crcVal +
|
||||
", classesLoaded=" + classesLoaded +
|
||||
//", successfullyLoaded(0)=" + successfullyLoaded(0) +
|
||||
'}';
|
||||
}
|
||||
|
||||
/**
|
||||
* Is called by the ClassLoader when the requested class
|
||||
* is not found in its cache. The parent is only used when
|
||||
|
|
|
@ -197,6 +197,17 @@ public class ChecksumInfo {
|
|||
return ci.getChecksum();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ChecksumInfo{" +
|
||||
"expectedCRC=" + expectedCRC +
|
||||
", cs=" + cs +
|
||||
", list=" + (list == null ? null : Arrays.asList(list)) +
|
||||
", whitelist=" + whitelist +
|
||||
", checksumCalculated=" + checksumCalculated +
|
||||
'}';
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (args.length < 2) {
|
||||
System.out.println("Usage: ChecksumInfo logFile jarFile...");
|
||||
|
|
|
@ -344,6 +344,9 @@ public class ResourceGrabber {
|
|||
try {
|
||||
File listFile = new File(savePath + fileListFile);
|
||||
if (listFile.exists() && listFile.canRead() && listFile.isFile()) {
|
||||
// if this file exists, and expectedChecksum is 0, just return -1 (already downloaded)
|
||||
if(ci.getExpectedChecksum() == 0)
|
||||
return -1;
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
Downloader.writeStream(new FileInputStream(listFile), baos);
|
||||
whitelist = new String(baos.toByteArray()).split("\n");
|
||||
|
@ -716,14 +719,15 @@ public class ResourceGrabber {
|
|||
|
||||
// write files to a file in the savePath, to be used on later runs to see what to CRC
|
||||
if (files != null) {
|
||||
String[] fileArray = files.toArray(new String[files.size()]);
|
||||
// first strip off savePath from the files
|
||||
for (int x = 0; x < files.size(); ++x)
|
||||
files.set(x, files.get(x).replaceFirst(savePath, ""));
|
||||
for (int x = 0; x < fileArray.length; ++x)
|
||||
fileArray[x] = fileArray[x].replaceFirst(savePath, "");
|
||||
try {
|
||||
FileOutputStream fos = new FileOutputStream(savePath + fileListFile);
|
||||
for (String file : files) {
|
||||
for (String file : fileArray) {
|
||||
file += "\n";
|
||||
System.out.print("file to crc: " + file);
|
||||
//System.out.print("file to crc: " + file);
|
||||
fos.write(file.getBytes());
|
||||
}
|
||||
fos.close();
|
||||
|
@ -731,7 +735,7 @@ public class ResourceGrabber {
|
|||
Debug.debug(e);
|
||||
}
|
||||
if (ci != null)
|
||||
ci.setList(true, files.toArray(new String[files.size()]));
|
||||
ci.setList(true, fileArray);
|
||||
}
|
||||
|
||||
// if we want a list of files, grab one
|
||||
|
@ -746,11 +750,17 @@ public class ResourceGrabber {
|
|||
super.finished(savePath, filesDownloaded);
|
||||
|
||||
|
||||
System.out.println("returning from finished");
|
||||
//System.out.println("returning from finished");
|
||||
// we can at least free this now
|
||||
ci = null;
|
||||
}
|
||||
|
||||
public void error(String msg, Exception e) {
|
||||
Debug.debug("Error: "+msg);
|
||||
Debug.debug(e);
|
||||
super.error(msg, e);
|
||||
}
|
||||
|
||||
public synchronized void freeResources() {
|
||||
ci = null;
|
||||
files = null;
|
||||
|
|
Loading…
Reference in New Issue