|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| package edu.rice.cs.drjava; |
|
38 |
| |
|
39 |
| import edu.rice.cs.plt.concurrent.JVMBuilder; |
|
40 |
| import edu.rice.cs.util.FileOps; |
|
41 |
| |
|
42 |
| import java.io.*; |
|
43 |
| import javax.swing.JOptionPane; |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| public class DrJavaRestart { |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| public static final int ATTEMPTS = 5; |
|
55 |
| public static final int TIME_BETWEEN_ATTEMPTS = 1000; |
|
56 |
| |
|
57 |
| public static edu.rice.cs.util.Log LOG = new edu.rice.cs.util.Log("version.txt",false); |
|
58 |
0
| public static void message(String message) {
|
|
59 |
0
| LOG.log(message);
|
|
60 |
0
| JOptionPane.showMessageDialog(null, message,
|
|
61 |
| "Error Updating DrJava", JOptionPane.ERROR_MESSAGE); |
|
62 |
| |
|
63 |
| } |
|
64 |
0
| public static boolean delete(File f) {
|
|
65 |
0
| for(int i = 0; i < ATTEMPTS; ++i) {
|
|
66 |
0
| if (f.delete()) return true;
|
|
67 |
0
| LOG.log("Failed to delete " + f + ", trying again");
|
|
68 |
0
| try {
|
|
69 |
0
| Thread.sleep(TIME_BETWEEN_ATTEMPTS);
|
|
70 |
| } |
|
71 |
| catch(InterruptedException ie) { } |
|
72 |
| } |
|
73 |
0
| return false;
|
|
74 |
| } |
|
75 |
0
| public static boolean deleteRecursively(File f) {
|
|
76 |
0
| for(int i = 0; i < ATTEMPTS; ++i) {
|
|
77 |
0
| if (edu.rice.cs.plt.io.IOUtil.deleteRecursively(f)) return true;
|
|
78 |
0
| LOG.log("Failed to recursively delete " + f + ", trying again");
|
|
79 |
0
| try {
|
|
80 |
0
| Thread.sleep(TIME_BETWEEN_ATTEMPTS);
|
|
81 |
| } |
|
82 |
| catch(InterruptedException ie) { } |
|
83 |
| } |
|
84 |
0
| return false;
|
|
85 |
| } |
|
86 |
0
| public static boolean rename(File from, File to) {
|
|
87 |
0
| for(int i = 0; i < ATTEMPTS; ++i) {
|
|
88 |
0
| if (from.renameTo(to)) return true;
|
|
89 |
0
| LOG.log("Failed to rename " + from + " to " + to + ", trying again");
|
|
90 |
0
| try {
|
|
91 |
0
| Thread.sleep(TIME_BETWEEN_ATTEMPTS);
|
|
92 |
| } |
|
93 |
| catch(InterruptedException ie) { } |
|
94 |
| } |
|
95 |
0
| return false;
|
|
96 |
| } |
|
97 |
0
| public static void main(final String[] args) {
|
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
0
| File source = new File(args[0]);
|
|
102 |
0
| File dest = new File(args[1]);
|
|
103 |
0
| File exec = dest;
|
|
104 |
| |
|
105 |
0
| try {
|
|
106 |
0
| LOG.log("source: " + source.getAbsolutePath());
|
|
107 |
0
| LOG.log("dest : " + dest.getAbsolutePath());
|
|
108 |
| |
|
109 |
0
| if (dest.exists()) {
|
|
110 |
0
| if (dest.isFile()) {
|
|
111 |
| |
|
112 |
0
| if (delete(dest)) {
|
|
113 |
| |
|
114 |
0
| if (!rename(source,dest)) {
|
|
115 |
| |
|
116 |
0
| exec = source;
|
|
117 |
0
| message("A new version of DrJava was downloaded. However, it could not be\n" +
|
|
118 |
| "installed in the same place as the old DrJava.\n\n" + |
|
119 |
| "The new copy is now installed at:\n" + |
|
120 |
| source.getAbsolutePath() + "\n\n" + |
|
121 |
| "The old copy has been deleted."); |
|
122 |
| } |
|
123 |
| } |
|
124 |
| else { |
|
125 |
| |
|
126 |
0
| exec = source;
|
|
127 |
0
| message("A new version of DrJava was downloaded. However, it could not be\n" +
|
|
128 |
| "installed in the same place as the old DrJava.\n\n" + |
|
129 |
| "The new copy is now installed at:\n" + |
|
130 |
| source.getAbsolutePath() + "\n\n" + |
|
131 |
| "The old copy is still installed at:\n" + |
|
132 |
| dest.getAbsolutePath()); |
|
133 |
| } |
|
134 |
0
| LOG.log("Restarting...");
|
|
135 |
0
| Process p = JVMBuilder.DEFAULT.classPath(exec).start(DrJava.class.getName(), "-new", "-delete-after-restart", args[2]);
|
|
136 |
0
| LOG.log("Done with DrJavaRestart");
|
|
137 |
0
| System.exit(0);
|
|
138 |
| } |
|
139 |
| else { |
|
140 |
| |
|
141 |
| |
|
142 |
0
| File old = FileOps.generateNewFileName(dest);
|
|
143 |
0
| if (rename(dest,old)) {
|
|
144 |
0
| if (rename(source,dest)) {
|
|
145 |
| |
|
146 |
0
| delete(source.getParentFile());
|
|
147 |
0
| if (!deleteRecursively(old)) {
|
|
148 |
0
| message("A new version of DrJava was downloaded. However, the old version" +
|
|
149 |
| "could not be deleted.\n\n" + |
|
150 |
| "The new copy is now installed at:\n" + |
|
151 |
| dest.getAbsolutePath() + "\n\n" + |
|
152 |
| "The old copy is still installed at:\n" + |
|
153 |
| old.getAbsolutePath()); |
|
154 |
| } |
|
155 |
| } |
|
156 |
| else { |
|
157 |
| |
|
158 |
0
| exec = source;
|
|
159 |
| |
|
160 |
0
| if (rename(old,dest)) {
|
|
161 |
| |
|
162 |
0
| message("A new version of DrJava was downloaded. However, it could not be\n" +
|
|
163 |
| "installed in the same place as the old DrJava.\n\n" + |
|
164 |
| "The new copy is now installed at:\n" + |
|
165 |
| source.getAbsolutePath() + "\n\n" + |
|
166 |
| "The old copy is still installed at:\n" + |
|
167 |
| dest.getAbsolutePath()); |
|
168 |
| } |
|
169 |
| else { |
|
170 |
| |
|
171 |
0
| message("A new version of DrJava was downloaded. However, it could not be\n" +
|
|
172 |
| "installed in the same place as the old DrJava.\n\n" + |
|
173 |
| "The new copy is now installed at:\n" + |
|
174 |
| source.getAbsolutePath() + "\n\n" + |
|
175 |
| "The old copy is still installed at:\n" + |
|
176 |
| old.getAbsolutePath()); |
|
177 |
| } |
|
178 |
| } |
|
179 |
| } |
|
180 |
| else { |
|
181 |
| |
|
182 |
0
| exec = source;
|
|
183 |
0
| message("A new version of DrJava was downloaded. However, it could not be\n" +
|
|
184 |
| "installed in the same place as the old DrJava.\n\n" + |
|
185 |
| "The new copy is now installed at:\n" + |
|
186 |
| source.getAbsolutePath() + "\n\n" + |
|
187 |
| "The old copy is still installed at:\n" + |
|
188 |
| dest.getAbsolutePath()); |
|
189 |
| } |
|
190 |
| |
|
191 |
| |
|
192 |
0
| File macOpenFile = new File("/usr/bin/open");
|
|
193 |
0
| LOG.log("Searching for " + macOpenFile);
|
|
194 |
0
| if (!macOpenFile.exists()) {
|
|
195 |
0
| String path = System.getenv("PATH");
|
|
196 |
0
| for(String p: path.split(System.getProperty("path.separator"))) {
|
|
197 |
0
| macOpenFile = new File(p, "tar");
|
|
198 |
0
| LOG.log("Searching for " + macOpenFile);
|
|
199 |
0
| if (macOpenFile.exists()) break;
|
|
200 |
| } |
|
201 |
| } |
|
202 |
0
| if (macOpenFile.exists()) {
|
|
203 |
0
| LOG.log("Restarting using ProcessBuilder...");
|
|
204 |
0
| Process p = new ProcessBuilder()
|
|
205 |
| .command(macOpenFile.getAbsolutePath(), exec.getAbsolutePath()) |
|
206 |
| .redirectErrorStream(true) |
|
207 |
| .start(); |
|
208 |
0
| System.exit(0);
|
|
209 |
| } |
|
210 |
| else { |
|
211 |
0
| LOG.log("Restarting using JVMBuilder...");
|
|
212 |
0
| exec = new File(exec,"Contents/Resources/Java/drjava.jar");
|
|
213 |
0
| Process p = JVMBuilder.DEFAULT.classPath(exec).start(DrJava.class.getName(), "-new", "-delete-after-restart", args[2]);
|
|
214 |
0
| LOG.log("Done with DrJavaRestart");
|
|
215 |
0
| System.exit(0);
|
|
216 |
| } |
|
217 |
| } |
|
218 |
| } |
|
219 |
| } |
|
220 |
| catch(Exception e) { |
|
221 |
0
| message("A new version of DrJava was downloaded. However, there was an error" +
|
|
222 |
| "during installation:\n" + e.getMessage()); |
|
223 |
| } |
|
224 |
| } |
|
225 |
| } |