jasenautoupdater.java
来自「spam source codejasen-0.9jASEN - java An」· Java 代码 · 共 664 行 · 第 1/2 页
JAVA
664 行
tmpFiles = new Vector();
}
// Create a temp file so we don't overwrite
tmpFile = new File(outFile.getAbsolutePath() + ".tmp");
tmpFiles.add(tmpFile);
// Copy original to the temp
FileUtils.copy(outFile, tmpFile);
// Save the path of the original for rollback
oldFiles.add(outFile.getAbsolutePath());
}
zin = zip.getInputStream(zip.getEntry(updateFile.getArchivePath()));
fout = new FileOutputStream(outFile);
IOUtils.pipe(zin, fout, 1024);
}
catch (Exception e) {
// We need to rollback the file replacements
if(oldFiles != null) {
if(fout != null) {
try {
fout.close();
fout = null;
} catch (Exception ignore) {}
}
if(zin != null) {
try {
zin.close();
zin = null;
} catch (Exception ignore) {}
}
String path = null;
for (int j = 0; j < oldFiles.size(); j++) {
try {
path = (String)oldFiles.get(j);
outFile = new File(path);
// Get the tmp file
tmpFile = new File(outFile.getAbsolutePath() + ".tmp");
// If the replacment exists, delete it
if(outFile.exists() && tmpFile.exists()) {
outFile.delete();
}
// now move the tmp file back
if(tmpFile.exists()) {
tmpFile.renameTo(outFile);
}
}
catch (Exception ignore) {
manager.getErrorHandler().handleException(ignore);
}
}
}
// Now throw the error back up
if (e instanceof IOException) {
throw (IOException)e;
}
else {
throw new IOException(e.toString());
}
}
finally {
if(fout != null) {
try {
fout.close();
} catch (Exception ignore) {}
}
if(zin != null) {
try {
zin.close();
} catch (Exception ignore) {}
}
}
}
}
// Now, look for classes to execute and jars to load
if(parcel.getJarName() != null) {
// We have a jar to load!
// Save the jar to the given path
File jar = new File(parcel.getJarPath());
File tmpJar = null;
try {
if(jar.exists()) {
// create a copy until the end
tmpJar = new File(jar.getAbsolutePath() + ".tmp");
FileUtils.copy(jar, tmpJar);
if(tmpFiles == null) tmpFiles = new Vector();
tmpFiles.add(tmpJar);
}
else
{
jar.getParentFile().mkdirs();
}
// Now save the zipped jar to the jar path
zin = zip.getInputStream(zip.getEntry(parcel.getJarName()));
fout = new FileOutputStream(jar);
IOUtils.pipe(zin, fout, 1024);
// Now, we need to dynamically load the jar
URL[] jars = new URL[]{jar.toURL()};
ClassLoader parent = JasenScanner.getInstance().getEngine().getContextClassLoader();
if(parent == null) {
parent = this.getContextClassLoader();
}
URLClassLoader loader = new URLClassLoader(jars, parent);
// Set this as the current classloader for the engine
JasenScanner.getInstance().getEngine().setContextClassLoader(loader);
// Now, if there is a class to execute, execute it...
if(parcel.getClassName() != null && parcel.getClassName().trim().length() > 0) {
AutoUpdateExecutor executor = (AutoUpdateExecutor)loader.loadClass(parcel.getClassName()).newInstance();
executor.execute();
}
}
catch (Exception e) {
// Rollback
if(fout != null) {
try {
fout.close();
fout = null;
} catch (Exception ignore) {}
}
if(zin != null) {
try {
zin.close();
zin = null;
} catch (Exception ignore) {}
}
// Move the tmp jar back
if(tmpJar != null) {
if(jar.exists()) {
jar.delete();
}
tmpJar.renameTo(jar);
}
e.printStackTrace();
// Now throw the error back up
if (e instanceof IOException) {
throw (IOException)e;
}
else {
throw new IOException(e.toString());
}
}
finally {
if(fout != null) {
try {
fout.close();
} catch (Exception ignore) {}
}
if(zin != null) {
try {
zin.close();
} catch (Exception ignore) {}
}
}
// Now, if we had a jar and were told NOT to retain it, then we need to delete it...
if(jar != null && jar.exists()) {
if(parcel.getRetainJar().equalsIgnoreCase("false")) {
jar.delete();
}
}
}
// Now we have copied all the files, delete all the tmp files...
if(tmpFiles != null) {
for (int j = 0; j < tmpFiles.size(); j++) {
((File)tmpFiles.get(j)).delete();
}
}
}
finally {
if(zip != null) {
try {
zip.close();
} catch (IOException ignore) {
manager.getErrorHandler().handleException(ignore);
}
}
}
// Now delete the update file itself
if(update != null && update.exists()) {
if(!update.delete()) {
logger.warn("Failed to delete update file at " + update.getAbsolutePath());
}
}
}
private URL prepareItemURL(URL source, String item) throws MalformedURLException {
URL url = null;
String strUrl = source.toExternalForm();
if(!strUrl.endsWith("/")) {
strUrl += "/";
}
strUrl += item;
url = new URL(strUrl);
return url;
}
private JasenAutoUpdateParcel loadLastParcel() throws IOException, ClassNotFoundException {
File parcelFile = new File(PARCEL_CACHE);
JasenAutoUpdateParcel parcel = null;
if(parcelFile.exists()) {
ObjectInputStream in = null;
try {
in = new ObjectInputStream(new FileInputStream(parcelFile));
if(in != null) {
parcel = (JasenAutoUpdateParcel)in.readObject();
}
}
finally {
if(in != null) {
try {
in.close();
} catch (Exception ignore) {}
}
}
}
return parcel;
}
private void saveCurrentParcel(JasenAutoUpdateParcel parcel) throws IOException {
File parcelFile = new File(PARCEL_CACHE);
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new FileOutputStream(parcelFile));
out.writeObject(parcel);
}
finally {
if(out != null) {
try {
out.close();
} catch (Exception ignore) {}
}
}
}
public synchronized void finish() {
logger.debug("AutoUpdater stopping...");
neverStart = true;
if(running) {
running = false;
}
logger.debug("AutoUpdater stop command completed");
notifyAll();
}
/**
* Forces the auto updater to check for updates immediately
*
*/
synchronized boolean forceUpdate() {
logger.debug("Update has been forced");
if(idle) {
forcedUpdate = true;
notifyAll();
return true;
}
else
{
return false;
}
}
public boolean isRunning() {
return running;
}
public boolean isIdle() {
return idle;
}
public boolean isStopped() {
return stopped;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?