20 package net.sf.gridarta.updater;
22 import java.awt.Component;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InterruptedIOException;
28 import java.io.OutputStream;
29 import java.net.Proxy;
31 import java.net.URLConnection;
32 import java.net.UnknownHostException;
33 import java.util.MissingResourceException;
34 import java.util.PropertyResourceBundle;
35 import java.util.ResourceBundle;
36 import java.util.prefs.Preferences;
37 import javax.swing.JOptionPane;
38 import javax.swing.ProgressMonitor;
39 import javax.swing.ProgressMonitorInputStream;
44 import net.
sf.japi.swing.action.ActionBuilder;
45 import net.
sf.japi.swing.action.ActionBuilderFactory;
46 import org.apache.log4j.Category;
47 import org.apache.log4j.Logger;
48 import org.jetbrains.annotations.NotNull;
49 import org.jetbrains.annotations.Nullable;
58 public class Updater implements Runnable {
64 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
70 private static final Category
LOG = Logger.getLogger(
Updater.class);
126 if (propUrl ==
null) {
131 final ResourceBundle updateBundle =
new PropertyResourceBundle(pin);
132 final String downloadUrl = updateBundle.getString(
"update.url");
133 if (downloadUrl ==
null) {
140 active =
new VersionInfo(ResourceBundle.getBundle(
"build"),
"build");
141 }
catch (
final MissingResourceException e) {
145 if (active ==
null || update.
isNewerThan(active)) {
153 }
catch (
final UnknownHostException e) {
155 }
catch (
final IOException e) {
174 return ACTION_BUILDER.showConfirmDialog(
parentComponent, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
"updateAvailable",
updateFileName, active ==
null ?
"?" : active.version, update.version, active ==
null ?
"?" : active.developer, update.developer, active ==
null ?
"?" : active.timestamp, update.timestamp, propUrl, downloadUrl) == JOptionPane.YES_OPTION;
185 ACTION_BUILDER.showMessageDialog(
parentComponent,
"updateUnavailable", active ==
null ?
"?" : active.version, update.version, active ==
null ?
"?" : active.developer, update.developer, active ==
null ?
"?" : active.timestamp, update.timestamp, propUrl, downloadUrl);
201 try (OutputStream out =
new FileOutputStream(download)) {
202 final byte[] buf =
new byte[
BUF_SIZE];
204 final int bytesRead = in.read(buf);
205 if (bytesRead == -1) {
208 out.write(buf, 0, bytesRead);
212 if ( !orig.renameTo(backup)) {
214 }
else if (!download.renameTo(orig)) {
215 backup.renameTo(orig);
221 }
catch (
final InterruptedIOException e) {
223 }
catch (
final Exception e) {
224 LOG.warn(
"updateError", e);
237 private InputStream
openStream(@NotNull
final String url)
throws IOException {
239 final URLConnection con =
new URL(url).openConnection(proxy);
241 final ProgressMonitor monitor = stream.getProgressMonitor();
242 monitor.setMaximum(con.getContentLength());
244 monitor.setMillisToDecideToPopup(10);
245 monitor.setMillisToPopup(10);
283 this(
"unavailable",
"unavailable",
"unavailable");
306 private VersionInfo(@NotNull
final ResourceBundle bundle, @NotNull
final String prefix) {
307 this(bundle.getString(prefix +
".number"), bundle.getString(prefix +
".tstamp"), bundle.getString(prefix +
".developer"));