diff --git a/pom.xml b/pom.xml index 82dedb24..734a1b28 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.rarchives.ripme ripme jar - 1.0.2-beta1 + 1.0.2 ripme http://rip.rarchives.com diff --git a/src/main/java/com/rarchives/ripme/App.java b/src/main/java/com/rarchives/ripme/App.java index 914fccb7..ee54e1ab 100644 --- a/src/main/java/com/rarchives/ripme/App.java +++ b/src/main/java/com/rarchives/ripme/App.java @@ -14,6 +14,7 @@ import org.apache.log4j.Logger; import com.rarchives.ripme.ripper.AbstractRipper; import com.rarchives.ripme.ui.MainWindow; +import com.rarchives.ripme.ui.UpdateUtils; import com.rarchives.ripme.utils.Utils; /** @@ -24,7 +25,7 @@ public class App { public static final Logger logger = Logger.getLogger(App.class); public static void main(String[] args) throws MalformedURLException { - logger.debug("Initialized"); + logger.info("Initialized ripme v" + UpdateUtils.getThisJarVersion()); if (args.length > 0) { CommandLine cl = handleArguments(args); diff --git a/src/main/java/com/rarchives/ripme/ui/MainWindow.java b/src/main/java/com/rarchives/ripme/ui/MainWindow.java index 60cbe3dd..5631aa4e 100644 --- a/src/main/java/com/rarchives/ripme/ui/MainWindow.java +++ b/src/main/java/com/rarchives/ripme/ui/MainWindow.java @@ -83,6 +83,7 @@ public class MainWindow implements Runnable, RipStatusHandler { // TODO Configuration components public MainWindow() { + UpdateUtils.moveUpdatedJar(); mainFrame = new JFrame(WINDOW_TITLE); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //mainFrame.setPreferredSize(new Dimension(400, 180)); @@ -196,14 +197,14 @@ public class MainWindow implements Runnable, RipStatusHandler { configurationPanel.setVisible(false); configurationPanel.setPreferredSize(new Dimension(300, 250)); // TODO Configuration components - JLabel configLabel = new JLabel("Version: " + Utils.getConfigInteger("version.major", 0) + "." + Utils.getConfigInteger("version.minor", 0) + "." + Utils.getConfigInteger("version.build", 0)); + JLabel configLabel = new JLabel("Version: " + UpdateUtils.getThisJarVersion()); configurationPanel.add(configLabel); configUpdateButton = new JButton("Check for updates"); configUpdateLabel = new JLabel(""); - gbc.ipady = 0; gbc.gridy = 1; configurationPanel.add(configUpdateButton, gbc); gbc.gridy = 2; + gbc.ipady = 10; configurationPanel.add(configUpdateLabel, gbc); gbc.gridy = 0; pane.add(ripPanel, gbc); diff --git a/src/main/java/com/rarchives/ripme/ui/UpdateUtils.java b/src/main/java/com/rarchives/ripme/ui/UpdateUtils.java index 6fbed020..587c70f5 100644 --- a/src/main/java/com/rarchives/ripme/ui/UpdateUtils.java +++ b/src/main/java/com/rarchives/ripme/ui/UpdateUtils.java @@ -1,10 +1,12 @@ package com.rarchives.ripme.ui; +import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import javax.swing.JLabel; +import org.apache.log4j.Logger; import org.json.JSONArray; import org.json.JSONObject; import org.jsoup.Connection.Response; @@ -13,10 +15,22 @@ import org.jsoup.nodes.Document; public class UpdateUtils { - private static final String DEFAULT_VERSION = "1.0.0"; + private static final Logger logger = Logger.getLogger(UpdateUtils.class); + private static final String DEFAULT_VERSION = "100.0.0"; private static final String updateJsonURL = "http://rarchives.com/ripme.json"; private static final String updateJarURL = "http://rarchives.com/ripme.jar"; + private static final String mainFileName = "ripme.jar"; + private static final String updateFileName = "ripme.jar.update"; + public static String getThisJarVersion() { + String thisVersion = UpdateUtils.class.getPackage().getImplementationVersion(); + if (thisVersion == null) { + // Version is null if we're not running from the JAR + thisVersion = DEFAULT_VERSION; ; // Super-high version number + } + return thisVersion; + } + public static void updateProgram(JLabel configUpdateLabel) { configUpdateLabel.setText("Checking for update...:"); @@ -27,7 +41,7 @@ public class UpdateUtils { .get(); } catch (IOException e) { configUpdateLabel.setText("Error while fetching update: " + e.getMessage()); - e.printStackTrace(); + logger.error("Error while fetching update: ", e); return; } String jsonString = doc.body().html().replaceAll(""", "\""); @@ -42,43 +56,20 @@ public class UpdateUtils { String latestVersion = json.getString("latestVersion"); if (UpdateUtils.isNewerVersion(latestVersion)) { configUpdateLabel.setText("Newer version found!

" + configUpdateLabel.getText() + ""); + logger.info("New version found, downloading..."); try { UpdateUtils.downloadJarAndReplace(updateJarURL); } catch (IOException e) { configUpdateLabel.setText("Error while updating: " + e.getMessage()); - e.printStackTrace(); + logger.error("Error while updating: ", e); return; } } else { configUpdateLabel.setText("Running latest version: " + UpdateUtils.getThisJarVersion()); + logger.info("Running latest version: " + UpdateUtils.getThisJarVersion()); } } - private static void downloadJarAndReplace(String updateJarURL) - throws IOException { - String newFile = "ripme.jar.update"; - Response response; - response = Jsoup.connect(updateJarURL) - .ignoreContentType(true) - .timeout(60000) - .maxBodySize(1024 * 1024 * 100) - .execute(); - FileOutputStream out = new FileOutputStream(newFile); - out.write(response.bodyAsBytes()); - out.close(); - Runtime.getRuntime().exec(new String[] {"java", "-jar", newFile}); - System.exit(0); - } - - private static String getThisJarVersion() { - String thisVersion = UpdateUtils.class.getPackage().getImplementationVersion(); - if (thisVersion == null) { - // Version is null if we're not running from the JAR - thisVersion = DEFAULT_VERSION; ; // Super-high version number - } - return thisVersion; - } - private static boolean isNewerVersion(String latestVersion) { int[] oldVersions = versionStringToInt(getThisJarVersion()); int[] newVersions = versionStringToInt(latestVersion); @@ -88,11 +79,12 @@ public class UpdateUtils { } for (int i = 0; i < oldVersions.length; i++) { - System.err.println("Calculating: " + newVersions[i] + " <> " + oldVersions[i]); if (newVersions[i] > oldVersions[i]) { + logger.debug("oldVersion " + getThisJarVersion() + " < latestVersion" + latestVersion); return true; } else if (newVersions[i] < oldVersions[i]) { + logger.debug("oldVersion " + getThisJarVersion() + " > latestVersion " + latestVersion); return false; } else { @@ -114,4 +106,54 @@ public class UpdateUtils { } return intVersions; } + + private static void downloadJarAndReplace(String updateJarURL) + throws IOException { + Response response; + response = Jsoup.connect(updateJarURL) + .ignoreContentType(true) + .timeout(60000) + .maxBodySize(1024 * 1024 * 100) + .execute(); + FileOutputStream out = new FileOutputStream(updateFileName); + out.write(response.bodyAsBytes()); + out.close(); + Runtime.getRuntime().exec(new String[] {"java", "-jar", updateFileName}); + System.exit(0); + } + + public static void moveUpdatedJar() { + File newFile = new File(updateFileName); + File oldFile = new File(mainFileName); + if (!newFile.exists()) { + // Can't update without .update file + return; + } + if (oldFile.exists()) { + logger.info("Deleting existing .jar file: " + oldFile); + try { + oldFile.delete(); + } catch (Exception e) { + logger.error("Failed to delete old jar file: " + oldFile); + return; + } + } + + boolean success = newFile.renameTo(oldFile); + + if (!success) { + logger.error("Failed to rename file from " + newFile.getAbsolutePath() + " to " + oldFile.getAbsolutePath()); + return; + } + try { + logger.debug("Executing jar " + oldFile.getName()); + Runtime.getRuntime().exec(new String[] {"java", "-jar", oldFile.getName()}); + logger.info("Started new version, quitting old version..."); + System.exit(0); + } catch (IOException e) { + logger.error("Error while executing new jar " + newFile, e); + return; + } + } + }