File Source: Update.java

         /* 
    P/P   *  Method: com.dmdirc.updater.Update__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 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  
    23  package com.dmdirc.updater;
    24  
    25  import com.dmdirc.Main;
    26  import com.dmdirc.logger.ErrorLevel;
    27  import com.dmdirc.logger.Logger;
    28  import com.dmdirc.util.DownloadListener;
    29  import com.dmdirc.util.Downloader;
    30  import com.dmdirc.util.WeakList;
    31  
    32  import java.io.IOException;
    33  import java.util.List;
    34  
    35  /**
    36   * Represents a single available update for some component.
    37   *
    38   * @author chris
    39   */
         /* 
    P/P   *  Method: UpdateComponent access$000(Update)
          * 
          *  Preconditions:
          *    x0 != null
          * 
          *  Postconditions:
          *    return_value == x0.component
          *    init'ed(return_value)
          */
    40  public final class Update implements DownloadListener {
    41  
    42      /** Update component. */
    43      private final UpdateComponent component;
    44      /** Remote version name. */
    45      private final String versionName;
    46      /** Update url. */
    47      private final String url;
    48      /** The progress of the current stage. */
    49      private float progress;
    50  
    51      /** A list of registered update listeners. */
    52      private final List<UpdateListener> listeners
    53              = new WeakList<UpdateListener>();
    54  
    55      /** Our current status. */
    56      private UpdateStatus status = UpdateStatus.PENDING;
    57  
    58      /**
    59       * Creates a new instance of Update, with details from the specified line.
    60       *
    61       * @param updateInfo An update information line from the update server
    62       */
             /* 
    P/P       *  Method: void com.dmdirc.updater.Update(String)
              * 
              *  Preconditions:
              *    updateInfo != null
              * 
              *  Presumptions:
              *    init'ed(com.dmdirc.logger.ErrorLevel.LOW)
              * 
              *  Postconditions:
              *    this.component == null
              *    this.listeners == &amp;new WeakList(Update#1)
              *    this.status == &amp;com.dmdirc.updater.UpdateStatus__static_init.new UpdateStatus(UpdateStatus__static_init#1)
              *    this.url == null
              *    this.versionName == null
              *    new ArrayList(WeakList#1) num objects == 1
              *    new WeakList(Update#1) num objects == 1
              *    this.listeners.list == &amp;new ArrayList(WeakList#1)
              */
    63      public Update(final String updateInfo) {
    64          // outofdate client STABLE 20071007 0.5.1 file
    65          final String[] parts = updateInfo.split(" ");
    66  
    67          if (parts.length == 6) {
    68              component = UpdateChecker.findComponent(parts[1]);
    69              versionName = parts[4];
    70              url = parts[5];
    71          } else {
    72              component = null;
    73              versionName = null;
    74              url = null;
    75  
    76              Logger.appError(ErrorLevel.LOW,
    77                      "Invalid update line received from server: ",
    78                      new UnsupportedOperationException("line: " + updateInfo));
    79          }
    80      }
    81  
    82      /**
    83       * Retrieves the component that this update is for.
    84       *
    85       * @return The component of this update
    86       */
    87      public UpdateComponent getComponent() {
                 /* 
    P/P           *  Method: UpdateComponent getComponent()
                  * 
                  *  Postconditions:
                  *    return_value == this.component
                  *    init'ed(return_value)
                  */
    88          return component;
    89      }
    90  
    91      /**
    92       * Returns the remote version of the component that's available.
    93       *
    94       * @return The remote version number
    95       */
    96      public String getRemoteVersion() {
                 /* 
    P/P           *  Method: String getRemoteVersion()
                  * 
                  *  Postconditions:
                  *    return_value == this.versionName
                  *    init'ed(return_value)
                  */
    97          return versionName;
    98      }
    99  
   100      /**
   101       * Returns the URL where the new update may be downloaded.
   102       *
   103       * @return The URL of the update
   104       */
   105      public String getUrl() {
                 /* 
    P/P           *  Method: String getUrl()
                  * 
                  *  Postconditions:
                  *    return_value == this.url
                  *    init'ed(return_value)
                  */
   106          return url;
   107      }
   108  
   109      /**
   110       * Retrieves the status of this update.
   111       *
   112       * @return This update's status
   113       */
   114      public UpdateStatus getStatus() {
                 /* 
    P/P           *  Method: UpdateStatus getStatus()
                  * 
                  *  Preconditions:
                  *    init'ed(this.status)
                  * 
                  *  Postconditions:
                  *    return_value == this.status
                  *    init'ed(return_value)
                  */
   115          return status;
   116      }
   117  
   118      /**
   119       * Sets the status of this update, and notifies all listeners of the change.
   120       *
   121       * @param newStatus This update's new status
   122       */
   123      protected void setStatus(final UpdateStatus newStatus) {
                 /* 
    P/P           *  Method: void setStatus(UpdateStatus)
                  * 
                  *  Preconditions:
                  *    this.listeners != null
                  *    (soft) com.dmdirc.util.ListenerList.new MapList(ListenerList#1).map != null
                  *    (soft) init'ed(com/dmdirc/updater/UpdateChecker.status)
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@127 != null
                  * 
                  *  Postconditions:
                  *    init'ed(com/dmdirc/updater/UpdateChecker.status)
                  *    this.progress == +0
                  *    init'ed(this.status)
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@127: {0}, {1}
                  */
   124          status = newStatus;
   125          progress = 0;
   126  
   127          for (UpdateListener listener : listeners) {
   128              listener.updateStatusChange(this, status);
   129          }
   130      }
   131  
   132      /**
   133       * Removes the specified update listener.
   134       *
   135       * @param o The update listener to remove
   136       */
   137      public void removeUpdateListener(final Object o) {
                 /* 
    P/P           *  Method: void removeUpdateListener(Object)
                  * 
                  *  Preconditions:
                  *    this.listeners != null
                  */
   138          listeners.remove(o);
   139      }
   140  
   141      /**
   142       * Adds the specified update listener.
   143       *
   144       * @param e The update listener to add
   145       */
   146      public void addUpdateListener(final UpdateListener e) {
                 /* 
    P/P           *  Method: void addUpdateListener(UpdateListener)
                  * 
                  *  Preconditions:
                  *    this.listeners != null
                  */
   147          listeners.add(e);
   148      }
   149  
   150      /**
   151       * Makes this update download and install itself.
   152       */
   153      public void doUpdate() {
                 /* 
    P/P           *  Method: void doUpdate()
                  */
   154          new Thread(new Runnable() {
   155  
   156              /** {@inheritDoc} */
   157              @Override
   158              public void run() {
                         /* 
    P/P                   *  Method: void run()
                          * 
                          *  Preconditions:
                          *    this.component != null
                          *    this.listeners != null
                          *    (soft) com.dmdirc.util.ListenerList.new MapList(ListenerList#1).map != null
                          *    (soft) init'ed(com/dmdirc/updater/UpdateChecker.status)
                          * 
                          *  Presumptions:
                          *    init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
                          *    getName(...)@182 != null
                          * 
                          *  Postconditions:
                          *    init'ed(com/dmdirc/updater/UpdateChecker.status)
                          * 
                          *  Test Vectors:
                          *    doInstall(...)@178: {0}, {1}
                          */
   159                  final String path = Main.getConfigDir() + "update.tmp."
   160                          + Math.round(Math.random() * 1000);
   161  
   162                  setStatus(UpdateStatus.DOWNLOADING);
   163  
   164                  try {
   165                      Downloader.downloadPage(getUrl(), path, Update.this);
   166                  } catch (IOException ex) {
   167                      setStatus(UpdateStatus.ERROR);
   168  
   169                      Logger.userError(ErrorLevel.MEDIUM, "Error when updating component "
   170                              + component.getName(), ex);
   171  
   172                      return;
   173                  }
   174  
   175                  setStatus(UpdateStatus.INSTALLING);
   176  
   177                  try {
   178                      final boolean restart = getComponent().doInstall(path);
   179  
   180                      if (restart) {
   181                          setStatus(UpdateStatus.RESTART_NEEDED);
   182                          UpdateChecker.removeComponent(getComponent().getName());
   183                      } else {
   184                          setStatus(UpdateStatus.INSTALLED);
   185                      }
   186                  } catch (IOException ex) {
   187                      setStatus(UpdateStatus.ERROR);
   188                      Logger.userError(ErrorLevel.MEDIUM,
   189                              "I/O error when updating component " + component.getName(), ex);
   190                  } catch (Throwable ex) {
   191                      setStatus(UpdateStatus.ERROR);
   192                      Logger.appError(ErrorLevel.MEDIUM,
   193                              "Error when updating component " + component.getName(), ex);
   194                  }
   195              }
   196  
   197          }, "Update thread").start();
   198      }
   199  
   200      /** {@inheritDoc} */
   201      @Override
   202      public void downloadProgress(final float percent) {
                 /* 
    P/P           *  Method: void downloadProgress(float)
                  * 
                  *  Preconditions:
                  *    this.listeners != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@205 != null
                  * 
                  *  Postconditions:
                  *    this.progress == percent
                  *    init'ed(this.progress)
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@205: {0}, {1}
                  */
   203          progress = percent;
   204          
   205          for (UpdateListener listener : listeners) {
   206              listener.updateProgressChange(this, percent);
   207          }
   208      }
   209  
   210      /**
   211       * Retrieves the current progress of the current state of this update.
   212       * 
   213       * @return The percentage of the current stage that has been completed
   214       */
   215      public float getProgress() {
                 /* 
    P/P           *  Method: float getProgress()
                  * 
                  *  Preconditions:
                  *    init'ed(this.progress)
                  * 
                  *  Postconditions:
                  *    return_value == this.progress
                  *    init'ed(return_value)
                  */
   216          return progress;
   217      }
   218  
   219      /** {@inheritDoc} */
   220      @Override
   221      public void setIndeterminate(boolean indeterminate) {
   222          //TODO
             /* 
    P/P       *  Method: void setIndeterminate(bool)
              */
   223      }
   224      
   225  }








SofCheck Inspector Build Version : 2.17854
Update.java 2009-Jun-25 01:54:24
Update.class 2009-Sep-02 17:04:12
Update$1.class 2009-Sep-02 17:04:12