PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/raptor/src/raptor/updater/UpdateManager.java

http://raptor-chess-interface.googlecode.com/
Java | 376 lines | 312 code | 52 blank | 12 comment | 54 complexity | cf6f192ce7ce142fc1ed1f7f8f37e73d MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, IPL-1.0, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, AGPL-1.0, LGPL-2.1
  1. package raptor.updater;
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. import java.lang.reflect.Method;
  11. import java.net.URL;
  12. import java.net.URLClassLoader;
  13. import java.nio.channels.Channels;
  14. import java.nio.channels.ReadableByteChannel;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import org.eclipse.swt.SWT;
  18. import org.eclipse.swt.graphics.Point;
  19. import org.eclipse.swt.graphics.Rectangle;
  20. import org.eclipse.swt.layout.GridData;
  21. import org.eclipse.swt.layout.GridLayout;
  22. import org.eclipse.swt.widgets.Button;
  23. import org.eclipse.swt.widgets.Display;
  24. import org.eclipse.swt.widgets.Event;
  25. import org.eclipse.swt.widgets.Label;
  26. import org.eclipse.swt.widgets.Listener;
  27. import org.eclipse.swt.widgets.Shell;
  28. public class UpdateManager {
  29. public static final String APP_HOME_DIR = ".raptor/";
  30. public static final File USER_RAPTOR_PREF = new File(
  31. System.getProperty("user.home") + "/" + APP_HOME_DIR
  32. + "/raptor.properties");
  33. private boolean isUpdateOn = false;
  34. private boolean isReadyToUpdate = false;
  35. private static String appVersionStr = ".98u3f7";
  36. protected static boolean isLikelyLinux = false;
  37. protected static boolean isLikelyOSX = false;
  38. protected static boolean isLikelyWindows = false;
  39. protected static boolean isLikelyWindowsVistaOrLater;
  40. /**
  41. * Upgrade-file-list for Windows. We add all the files to move over to this
  42. * list. See {@link #applyWindows()}.
  43. */
  44. private static List<String> windowsUpgradeCommands = null;
  45. private static final String updActionsUrl = "http://dl.dropbox.com/u/46373738/updActions";
  46. static {
  47. String osName = System.getProperty("os.name");
  48. if (osName.startsWith("Mac OS")) {
  49. isLikelyOSX = true;
  50. } else if (osName.startsWith("Windows")) {
  51. isLikelyWindows = true;
  52. isLikelyWindowsVistaOrLater = osName.contains("Vista")
  53. || osName.contains("7") || osName.contains("8");
  54. } else {
  55. isLikelyLinux = true;
  56. }
  57. }
  58. private Label infoLabel1;
  59. private Label infoLabel2;
  60. Thread upgradeThread;
  61. private Shell shell;
  62. public static void center(Shell shell) {
  63. Rectangle bds = shell.getDisplay().getBounds();
  64. Point p = shell.getSize();
  65. int nLeft = (bds.width - p.x) / 2;
  66. int nTop = (bds.height - p.y) / 2;
  67. shell.setBounds(nLeft, nTop, p.x, p.y);
  68. }
  69. public static void invokeMain(String args[]) {
  70. File f = new File("Raptor.jar");
  71. try {
  72. URLClassLoader u = new URLClassLoader(
  73. new URL[] { f.toURI().toURL() });
  74. Class<?> c = u.loadClass("raptor.Raptor");
  75. Method m = c.getMethod("main", new Class[] { args.getClass() });
  76. m.setAccessible(true);
  77. m.invoke(null, new Object[] { args });
  78. } catch (Exception ex) {
  79. ex.printStackTrace();
  80. }
  81. }
  82. public void open() {
  83. Display display = new Display();
  84. shell = new Shell(display, SWT.DIALOG_TRIM | SWT.MIN);
  85. shell.setText("Raptor upgrade");
  86. shell.addListener(SWT.Close, new Listener() {
  87. public void handleEvent(Event event) {
  88. upgradeThread.interrupt();
  89. }
  90. });
  91. createGuiContents(shell);
  92. shell.pack();
  93. center(shell);
  94. shell.open();
  95. while (!shell.isDisposed()) {
  96. if (!display.readAndDispatch()) {
  97. display.sleep();
  98. }
  99. }
  100. display.close();
  101. }
  102. void createGuiContents(final Shell shell) {
  103. GridLayout gridLayout = new GridLayout();
  104. gridLayout.numColumns = 1;
  105. gridLayout.marginWidth = 9;
  106. shell.setLayout(gridLayout);
  107. Label label = new Label(shell, SWT.NULL);
  108. label.setText("Update initiated... ");
  109. infoLabel1 = new Label(shell, SWT.NULL);
  110. infoLabel1
  111. .setText(" ");
  112. infoLabel2 = new Label(shell, SWT.NULL);
  113. infoLabel2
  114. .setText(" ");
  115. Button bt = new Button(shell, SWT.PUSH);
  116. bt.setText("Cancel");
  117. bt.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
  118. bt.addListener(SWT.Selection, new Listener() {
  119. public void handleEvent(Event event) {
  120. upgradeThread.interrupt();
  121. shell.close();
  122. }
  123. });
  124. }
  125. private static void transferFile(String url, String toFilename) throws IOException {
  126. URL fileUrl = new URL(url);
  127. ReadableByteChannel rbc2 = Channels.newChannel(fileUrl
  128. .openStream());
  129. FileOutputStream fos2 = new FileOutputStream(toFilename);
  130. fos2.getChannel().transferFrom(rbc2, 0, 1 << 24);
  131. fos2.close();
  132. }
  133. public void upgrade() {
  134. if (!infoLabel1.isDisposed()) {
  135. Display.getDefault().syncExec(new Runnable() {
  136. @Override
  137. public void run() {
  138. infoLabel1.setText("Initializing the remote connection...");
  139. }
  140. });
  141. }
  142. boolean upgradeMade = false;
  143. try {
  144. boolean forVersionSet = false;
  145. URL google = new URL(updActionsUrl);
  146. BufferedReader bin = new BufferedReader(new InputStreamReader(
  147. google.openStream()), 1024);
  148. String currentLine;
  149. while ((currentLine = bin.readLine()) != null) {
  150. if (currentLine.startsWith("for-version:") && forVersionSet)
  151. break;
  152. else if (currentLine.startsWith("for-version:")
  153. && currentLine.substring(13).equals(appVersionStr)) {
  154. forVersionSet = true;
  155. } else if (forVersionSet && currentLine.startsWith("file:")) {
  156. if (!infoLabel2.isDisposed()) {
  157. Display.getDefault().asyncExec(new Runnable() {
  158. @Override
  159. public void run() {
  160. infoLabel2
  161. .setText("Downloading upgrade files...");
  162. }
  163. });
  164. }
  165. String data[] = currentLine.substring(6).split(" ");
  166. String tempFilename = System.getProperty("java.io.tmpdir")
  167. + System.getProperty("file.separator")
  168. + Math.abs(data[1].hashCode());
  169. transferFile(data[0], tempFilename);
  170. upgradeMade = true;
  171. if (isLikelyOSX)
  172. applyOSX(tempFilename, data[1]);
  173. else if (isLikelyWindows)
  174. addWindows(tempFilename, data[1]);
  175. else if (isLikelyLinux)
  176. applyLinux(tempFilename, data[1]);
  177. }
  178. }
  179. bin.close();
  180. // most likely some error. Upgrade Raptor.jar anyway.
  181. if (!upgradeMade) {
  182. String tempFilename = System.getProperty("java.io.tmpdir")
  183. + System.getProperty("file.separator")
  184. + Math.abs("Raptor.jar".hashCode());
  185. transferFile("http://raptor-chess-interface.googlecode.com/files/Raptor.jar", tempFilename);
  186. if (isLikelyOSX)
  187. applyOSX(tempFilename, "Raptor.jar");
  188. else if (isLikelyWindows)
  189. addWindows(tempFilename, "Raptor.jar");
  190. else if (isLikelyLinux)
  191. applyLinux(tempFilename, "Raptor.jar");
  192. }
  193. if (isLikelyWindows) {
  194. applyWindows();
  195. }
  196. } catch (IOException ex) {
  197. ex.printStackTrace();
  198. }
  199. if (!shell.isDisposed()) {
  200. Display.getDefault().syncExec(new Runnable() {
  201. @Override
  202. public void run() {
  203. shell.close();
  204. }
  205. });
  206. }
  207. }
  208. private static void applyLinux(String tempFilename, String dest) {
  209. Process p;
  210. try {
  211. p = Runtime.getRuntime().exec(
  212. new String[] {"gksudo", "mv", tempFilename, dest});
  213. if (p.waitFor() != 0) {
  214. p = Runtime.getRuntime().exec(
  215. new String[] { "kdesudo", "mv", tempFilename, dest });
  216. p.waitFor();
  217. }
  218. } catch (Exception e) {
  219. e.printStackTrace();
  220. }
  221. }
  222. private static void addWindows(String tempFilename, String dest) {
  223. if (windowsUpgradeCommands == null) {
  224. windowsUpgradeCommands = new ArrayList<String>();
  225. }
  226. windowsUpgradeCommands.add("move /y \"" + tempFilename + "\" \"" + dest
  227. + "\"");
  228. }
  229. /**
  230. * Write the list of move commands to a batch file, then execute it with
  231. * elevated privileges. This way, we only have to elevate once.
  232. */
  233. private static void applyWindows() {
  234. if (windowsUpgradeCommands != null) {
  235. try {
  236. File tmp = File.createTempFile("rap-upd", ".bat");
  237. BufferedWriter fout = new BufferedWriter(new FileWriter(tmp));
  238. String cd = System.getProperty("user.dir");
  239. fout.write("@echo off\r\n");
  240. fout.write(cd.substring(0, 2) + "\r\ncd " + cd.substring(2)
  241. + "\r\n");
  242. for (String cmd : windowsUpgradeCommands) {
  243. fout.write(cmd + "\r\n");
  244. }
  245. fout.close();
  246. Process p;
  247. if (isLikelyWindowsVistaOrLater) {
  248. p = Runtime.getRuntime().exec(
  249. new String[] { "elevate", "-wait", "cmd", "/c",
  250. tmp.getAbsolutePath() });
  251. } else {
  252. p = Runtime.getRuntime().exec(new String[] { "cmd", "/c",
  253. tmp.getAbsolutePath() });
  254. }
  255. p.waitFor();
  256. tmp.delete();
  257. } catch (Exception e) {
  258. e.printStackTrace();
  259. }
  260. }
  261. }
  262. private static void applyOSX(String tempFilename, String dest) {
  263. Process p;
  264. try {
  265. p = Runtime.getRuntime().exec(
  266. new String[] { "mv", tempFilename, dest });
  267. p.waitFor();
  268. } catch (Exception e) {
  269. e.printStackTrace();
  270. }
  271. }
  272. void checkPrefs() {
  273. try {
  274. BufferedReader reader = new BufferedReader(new FileReader(
  275. USER_RAPTOR_PREF));
  276. String currentLine;
  277. while ((currentLine = reader.readLine()) != null) {
  278. if (currentLine.startsWith("app-update")) {
  279. isUpdateOn = Boolean
  280. .parseBoolean(currentLine.substring(11));
  281. if (!isUpdateOn) {
  282. reader.close();
  283. return;
  284. }
  285. } else if (currentLine.startsWith("app-version")) {
  286. appVersionStr = currentLine.substring(12);
  287. } else if (currentLine.startsWith("ready-to-update")) {
  288. isReadyToUpdate = Boolean.parseBoolean(currentLine
  289. .substring(16));
  290. }
  291. }
  292. reader.close();
  293. } catch (IOException e) {
  294. e.printStackTrace();
  295. }
  296. }
  297. /**
  298. * @param args
  299. */
  300. public static void main(String[] args) {
  301. if (!isLikelyOSX) {
  302. final UpdateManager manager = new UpdateManager();
  303. manager.checkPrefs();
  304. if (manager.isUpdateOn && manager.isReadyToUpdate) {
  305. manager.upgradeThread = new Thread(new Runnable() {
  306. @Override
  307. public void run() {
  308. boolean threadDisposed = false;
  309. try {
  310. Thread.sleep(600);
  311. } catch (InterruptedException e) {
  312. threadDisposed = true;
  313. }
  314. if (!threadDisposed)
  315. manager.upgrade();
  316. }
  317. });
  318. manager.upgradeThread.start();
  319. manager.open();
  320. }
  321. }
  322. invokeMain(args);
  323. }
  324. }