You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Update.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.updater;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.interfaces.UpdateListener;
  25. import com.dmdirc.logger.ErrorLevel;
  26. import com.dmdirc.logger.Logger;
  27. import com.dmdirc.updater.Update.STATUS;
  28. import com.dmdirc.util.Downloader;
  29. import com.dmdirc.util.WeakList;
  30. import java.util.List;
  31. /**
  32. * Represents a single available update for some component.
  33. *
  34. * @author chris
  35. */
  36. public final class Update {
  37. /**
  38. * An enumeration of possible statuses.
  39. */
  40. public static enum STATUS {
  41. /** The update is waiting to be started. */
  42. PENDING,
  43. /** The update is currently downloading. */
  44. DOWNLOADING,
  45. /** The update is currently installing. */
  46. INSTALLING,
  47. /** The update has been installed correctly. */
  48. INSTALLED,
  49. /** An error was encountered during update. */
  50. ERROR,
  51. /** A restart is needed to complete the update. */
  52. RESTART_NEEDED
  53. }
  54. /** Update component. */
  55. private final String component;
  56. /** Channel name. */
  57. private final String channel;
  58. /** Remote version number. */
  59. private final String versionID;
  60. /** Remote version name. */
  61. private final String versionName;
  62. /** Update url. */
  63. private final String url;
  64. /** A list of registered update listeners. */
  65. private final List<UpdateListener> listeners
  66. = new WeakList<UpdateListener>();
  67. /** Our current status. */
  68. private STATUS status = Update.STATUS.PENDING;
  69. /**
  70. * Creates a new instance of Update, with details from the specified line.
  71. *
  72. * @param updateInfo An update information line from the update server
  73. */
  74. public Update(final String updateInfo) {
  75. // outofdate client STABLE 20071007 0.5.1 file
  76. final String[] parts = updateInfo.split(" ");
  77. if (parts.length == 6) {
  78. component = parts[1];
  79. channel = parts[2];
  80. versionID = parts[3];
  81. versionName = parts[4];
  82. url = parts[5];
  83. } else {
  84. component = null;
  85. channel = null;
  86. versionID = null;
  87. versionName = null;
  88. url = null;
  89. Logger.appError(ErrorLevel.LOW,
  90. "Invalid update line received from server: ",
  91. new UnsupportedOperationException("line: " + updateInfo));
  92. }
  93. }
  94. /**
  95. * Retrieves the component that this update is for.
  96. *
  97. * @return The component of this update
  98. */
  99. public String getComponent() {
  100. return component;
  101. }
  102. /**
  103. * Returns the remote version of the component that's available.
  104. *
  105. * @return The remote version number
  106. */
  107. public String getRemoteVersion() {
  108. return versionName;
  109. }
  110. /**
  111. * Returns the URL where the new update may be downloaded.
  112. *
  113. * @return The URL of the update
  114. */
  115. public String getUrl() {
  116. return url;
  117. }
  118. /**
  119. * Retrieves the status of this update.
  120. *
  121. * @return This update's status
  122. */
  123. public STATUS getStatus() {
  124. return status;
  125. }
  126. /**
  127. * Sets the status of this update, and notifies all listeners of the change.
  128. *
  129. * @param newStatus This update's new status
  130. */
  131. protected void setStatus(final STATUS newStatus) {
  132. status = newStatus;
  133. for (UpdateListener listener : listeners) {
  134. listener.updateStatusChange(this, status);
  135. }
  136. }
  137. /**
  138. * Removes the specified update listener.
  139. *
  140. * @param o The update listener to remove
  141. */
  142. public void removeUpdateListener(Object o) {
  143. listeners.remove(o);
  144. }
  145. /**
  146. * Adds the specified update listener.
  147. *
  148. * @param e The update listener to add
  149. */
  150. public void addUpdateListener(UpdateListener e) {
  151. listeners.add(e);
  152. }
  153. /**
  154. * Makes this update download and install itself.
  155. */
  156. public void doUpdate() {
  157. new Thread(new Runnable() {
  158. /** {@inheritDoc} */
  159. @Override
  160. public void run() {
  161. final String path = Main.getConfigDir() + "update.tmp."
  162. + Math.round(Math.random() * 1000);
  163. setStatus(STATUS.DOWNLOADING);
  164. try {
  165. Downloader.downloadPage(getUrl(), path);
  166. } catch (Throwable ex) {
  167. setStatus(STATUS.ERROR);
  168. Logger.appError(ErrorLevel.MEDIUM,
  169. "Error when updating component " + component, ex);
  170. return;
  171. }
  172. setStatus(STATUS.INSTALLING);
  173. try {
  174. final boolean restart = UpdateChecker.findComponent(getComponent()).doInstall(path);
  175. if (restart) {
  176. setStatus(STATUS.RESTART_NEEDED);
  177. UpdateChecker.removeComponent(getComponent());
  178. } else {
  179. setStatus(STATUS.INSTALLED);
  180. }
  181. } catch (Throwable ex) {
  182. setStatus(STATUS.ERROR);
  183. Logger.appError(ErrorLevel.MEDIUM,
  184. "Error when updating component " + component, ex);
  185. }
  186. }
  187. }, "Update thread").start();
  188. }
  189. }