1 package com.tedpearson.util.update;
5 import java.util.prefs.*;
6 import java.awt.Component;
7 import javax.swing.JOptionPane;
12 show update window with progress bar when updating (optionally)
15 public class Updater {
16 private int version = -1;
17 private URL downloadURL;
18 private static Updater updater = new Updater();
20 public static Updater getUpdater() {
24 public boolean checkForUpdate(
25 Component parentComponent,
28 return checkForUpdate(parentComponent, updateUrl, "this program", currentVersion, true);
33 * Just check to see if there is an update.
35 public boolean checkForUpdate(
36 Component parentComponent,
41 String node = "com/tedpearson/update";
42 Preferences prefs = Preferences.userRoot().node(node);
43 String update = prefs.get(updateUrl,"");
44 if(update.equals("Never")) {
47 if(!update.equals("Yes")) {
51 // first, confirm user wants to allow updates
52 int option = JOptionPane.showOptionDialog(parentComponent,
53 "Do you want to let " + program + " check for updates when it opens?",
55 0, JOptionPane.QUESTION_MESSAGE,
56 null, new String[] {"Yes","This Time","Not Now","Never"},"Yes");
61 prefs.put(updateUrl,"Never");
64 prefs.put(updateUrl,"Yes");
68 // query the server to check the version number
69 accessVersion(parentComponent, updateUrl);
70 // System.out.println(version+","+currentVersion);
71 if(version > currentVersion) {
78 public void checkAndUpdate(
79 Component parentComponent,
85 if(checkForUpdate(parentComponent, updateUrl, program, currentVersion, promptUser)) {
86 updateJar(parentComponent, updateUrl, currentVersion, promptUser, jar);
90 public void checkAndUpdate(
91 Component parentComponent,
94 if(checkForUpdate(parentComponent, updateUrl, currentVersion)) {
95 updateJar(parentComponent, updateUrl, currentVersion);
99 public void updateJar(
100 Component parentComponent,
102 int currentVersion) {
103 updateJar(parentComponent, updateUrl, currentVersion, true, null);
106 public void updateJar(
107 Component parentComponent,
113 accessVersion(parentComponent, updateUrl);
114 // download new version
115 File temp = File.createTempFile("com.tedpearson.updater.download",null);
117 URLConnection conn = downloadURL.openConnection();
118 ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(
120 "Downloading program update",
121 conn.getInputStream()
123 ProgressMonitor pm = pmis.getProgressMonitor();
124 pm.setMillisToDecideToPopup(0);
125 pm.setMillisToPopup(0);
127 pm.setMillisToDecideToPopup(Integer.MAX_VALUE);
128 pm.setMillisToPopup(Integer.MAX_VALUE);
130 pm.setMaximum(conn.getHeaderFieldInt("Content-Length",0));
131 copyFile(pmis, new FileOutputStream(temp));
132 // replace old with new.
134 jar = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
136 jar = jar.replaceAll("%20"," ");
137 copyFile(new FileInputStream(temp), new FileOutputStream(new File(jar)));
140 // launch new version and quit this one.
141 ProcessBuilder pb = new ProcessBuilder("java","-jar",jar);
145 } catch(Exception e) {
150 JOptionPane.showMessageDialog(parentComponent, "There was an error while trying to update.",
151 "Error", JOptionPane.ERROR_MESSAGE);
156 private void accessVersion(Component parentComponent, String updateUrl) {
157 if(version != -1) return;
159 URL url = new URL(updateUrl);
160 BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
161 version = Integer.parseInt(br.readLine());
162 downloadURL = new URL(br.readLine());
163 } catch(Exception e) {
167 JOptionPane.showMessageDialog(parentComponent, "There was an error while trying to update.",
168 "Error", JOptionPane.ERROR_MESSAGE);
173 * Utility method to copy a file. I can't believe java doesn't have anything
174 * built-in to do something this simple with less code. Other than channels, that is.
176 * @param in stream to read file from
177 * @param out stream to write file to
179 public static void copyFile(InputStream in, OutputStream out) throws IOException {
180 byte[] buf = new byte[1024];
182 while ((len = in.read(buf)) > 0) {
183 out.write(buf, 0, len);