Skip to content

Commit e83195b

Browse files
committed
Implemented Auto Updating functionality.
1 parent 4306269 commit e83195b

1 file changed

Lines changed: 110 additions & 1 deletion

File tree

src/de/littlerolf/sav/SortAlgorithmVisualizer.java

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
package de.littlerolf.sav;
22

3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.net.MalformedURLException;
8+
import java.net.URL;
9+
import java.net.URLClassLoader;
10+
import java.net.URLConnection;
11+
import java.nio.channels.Channels;
12+
import java.nio.channels.ReadableByteChannel;
13+
import java.util.jar.Manifest;
14+
315
import javax.swing.JFileChooser;
416
import javax.swing.JFrame;
517
import javax.swing.JOptionPane;
@@ -9,8 +21,105 @@
921
import de.littlerolf.sav.gui.SAVFrame;
1022

1123
public class SortAlgorithmVisualizer {
24+
private static final String SERVER_URL = "http://littlerolf.github.io/SortAlgorithmVisualizer/";
25+
1226
public static void main(String[] args) {
13-
startLocal();
27+
if (isRemoteNewer()) {
28+
startJar(downloadRemoteJar().getAbsolutePath());
29+
} else
30+
startLocal();
31+
}
32+
33+
private static int getLocalVersion() {
34+
URLClassLoader cl = (URLClassLoader) SortAlgorithmVisualizer.class
35+
.getClassLoader();
36+
try {
37+
URL url = cl.findResource("META-INF/MANIFEST.MF");
38+
Manifest manifest = new Manifest(url.openStream());
39+
int version = Integer.valueOf(manifest.getMainAttributes()
40+
.getValue("Build-Number"));
41+
System.out.println("Local version is " + version + ".");
42+
return version;
43+
44+
} catch (IOException e) {
45+
e.printStackTrace();
46+
} catch (NullPointerException e) {
47+
}
48+
return -1;
49+
}
50+
51+
private static int getRemoteVersion() {
52+
URL url = null;
53+
try {
54+
url = new URL(SERVER_URL + "version.txt");
55+
} catch (MalformedURLException e2) {
56+
e2.printStackTrace();
57+
}
58+
try {
59+
URLConnection urlConnection = url.openConnection();
60+
urlConnection.setAllowUserInteraction(false);
61+
62+
InputStream urlStream = url.openStream();
63+
byte buffer[] = new byte[1000];
64+
int numRead = urlStream.read(buffer);
65+
String content = new String(buffer, 0, numRead);
66+
67+
while ((numRead != -1) && (content.length() < 1024)) {
68+
numRead = urlStream.read(buffer);
69+
if (numRead != -1) {
70+
String newContent = new String(buffer, 0, numRead);
71+
content += newContent;
72+
}
73+
}
74+
content = content.trim();
75+
System.out.println("Remote version is " + content + ".");
76+
77+
return Integer.valueOf(content);
78+
} catch (IOException e) {
79+
e.printStackTrace();
80+
} catch (IndexOutOfBoundsException e1) {
81+
e1.printStackTrace();
82+
}
83+
84+
return -1;
85+
}
86+
87+
private static File downloadRemoteJar() {
88+
System.out.println("Downloading remote jar.");
89+
File f = new File(System.getProperty("user.home") + File.separator
90+
+ ".sav" + File.separator + "SAV.jar");
91+
f.getParentFile().mkdirs();
92+
URL website;
93+
try {
94+
website = new URL(SERVER_URL + "jar/SAV.jar");
95+
96+
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
97+
FileOutputStream fos = new FileOutputStream(f);
98+
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
99+
fos.close();
100+
return f;
101+
102+
} catch (MalformedURLException e) {
103+
e.printStackTrace();
104+
} catch (IOException e) {
105+
e.printStackTrace();
106+
}
107+
return null;
108+
}
109+
110+
private static boolean isRemoteNewer() {
111+
return getRemoteVersion() > getLocalVersion();
112+
}
113+
114+
private static void startJar(String path) {
115+
System.out.println("Starting jar " + path + ".");
116+
ProcessBuilder pb = new ProcessBuilder("java", "-classpath", path,
117+
SortAlgorithmVisualizer.class.getName());
118+
try {
119+
pb.start();
120+
} catch (IOException e) {
121+
e.printStackTrace();
122+
}
14123
}
15124

16125
private static void startLocal() {

0 commit comments

Comments
 (0)