123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- /*
- * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.updater;
-
- import com.dmdirc.Main;
- import com.dmdirc.interfaces.UpdateListener;
- import com.dmdirc.logger.ErrorLevel;
- import com.dmdirc.logger.Logger;
- import com.dmdirc.updater.Update.STATUS;
- import com.dmdirc.util.Downloader;
- import com.dmdirc.util.WeakList;
-
- import java.util.List;
-
- /**
- * Represents a single available update for some component.
- *
- * @author chris
- */
- public final class Update {
-
- /**
- * An enumeration of possible statuses.
- */
- public static enum STATUS {
- /** The update is waiting to be started. */
- PENDING,
- /** The update is currently downloading. */
- DOWNLOADING,
- /** The update is currently installing. */
- INSTALLING,
- /** The update has been installed correctly. */
- INSTALLED,
- /** An error was encountered during update. */
- ERROR,
- /** A restart is needed to complete the update. */
- RESTART_NEEDED
- }
-
- /** Update component. */
- private final String component;
- /** Channel name. */
- private final String channel;
- /** Remote version number. */
- private final String versionID;
- /** Remote version name. */
- private final String versionName;
- /** Update url. */
- private final String url;
-
- /** A list of registered update listeners. */
- private final List<UpdateListener> listeners
- = new WeakList<UpdateListener>();
-
- /** Our current status. */
- private STATUS status = Update.STATUS.PENDING;
-
- /**
- * Creates a new instance of Update, with details from the specified line.
- *
- * @param updateInfo An update information line from the update server
- */
- public Update(final String updateInfo) {
- // outofdate client STABLE 20071007 0.5.1 file
- final String[] parts = updateInfo.split(" ");
-
- if (parts.length == 6) {
- component = parts[1];
- channel = parts[2];
- versionID = parts[3];
- versionName = parts[4];
- url = parts[5];
- } else {
- component = null;
- channel = null;
- versionID = null;
- versionName = null;
- url = null;
-
- Logger.appError(ErrorLevel.LOW,
- "Invalid update line received from server: ",
- new UnsupportedOperationException("line: " + updateInfo));
- }
- }
-
- /**
- * Retrieves the component that this update is for.
- *
- * @return The component of this update
- */
- public String getComponent() {
- return component;
- }
-
- /**
- * Returns the remote version of the component that's available.
- *
- * @return The remote version number
- */
- public String getRemoteVersion() {
- return versionName;
- }
-
- /**
- * Returns the URL where the new update may be downloaded.
- *
- * @return The URL of the update
- */
- public String getUrl() {
- return url;
- }
-
- /**
- * Retrieves the status of this update.
- *
- * @return This update's status
- */
- public STATUS getStatus() {
- return status;
- }
-
- /**
- * Sets the status of this update, and notifies all listeners of the change.
- *
- * @param newStatus This update's new status
- */
- protected void setStatus(final STATUS newStatus) {
- status = newStatus;
-
- for (UpdateListener listener : listeners) {
- listener.updateStatusChange(this, status);
- }
- }
-
- /**
- * Removes the specified update listener.
- *
- * @param o The update listener to remove
- */
- public void removeUpdateListener(Object o) {
- listeners.remove(o);
- }
-
- /**
- * Adds the specified update listener.
- *
- * @param e The update listener to add
- */
- public void addUpdateListener(UpdateListener e) {
- listeners.add(e);
- }
-
- /**
- * Makes this update download and install itself.
- */
- public void doUpdate() {
- new Thread(new Runnable() {
-
- /** {@inheritDoc} */
- @Override
- public void run() {
- final String path = Main.getConfigDir() + "update.tmp."
- + Math.round(Math.random() * 1000);
-
- setStatus(STATUS.DOWNLOADING);
-
- try {
- Downloader.downloadPage(getUrl(), path);
- } catch (Throwable ex) {
- setStatus(STATUS.ERROR);
-
- Logger.appError(ErrorLevel.MEDIUM,
- "Error when updating component " + component, ex);
-
- return;
- }
-
- setStatus(STATUS.INSTALLING);
-
- try {
- final boolean restart = UpdateChecker.findComponent(getComponent()).doInstall(path);
-
- if (restart) {
- setStatus(STATUS.RESTART_NEEDED);
- UpdateChecker.removeComponent(getComponent());
- } else {
- setStatus(STATUS.INSTALLED);
- }
- } catch (Throwable ex) {
- setStatus(STATUS.ERROR);
- Logger.appError(ErrorLevel.MEDIUM,
- "Error when updating component " + component, ex);
- }
- }
-
- }, "Update thread").start();
- }
-
- }
|