File Source: PluginComponent.java

     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.components;
    24  
    25  import com.dmdirc.config.ConfigManager;
    26  import com.dmdirc.config.IdentityManager;
    27  import com.dmdirc.plugins.PluginInfo;
    28  import com.dmdirc.updater.UpdateChecker;
    29  import com.dmdirc.updater.UpdateComponent;
    30  import com.dmdirc.updater.Version;
    31  
    32  import java.io.File;
    33  
    34  /**
    35   * An update component for plugins.
    36   * 
    37   * @author chris
    38   */
    39  public class PluginComponent implements UpdateComponent {
    40      
    41      /** The plugin this component is for. */
    42      private final PluginInfo plugin;
    43      
    44      /** The config to use. */
             /* 
    P/P       *  Method: com.dmdirc.updater.components.PluginComponent__static_init
              * 
              *  Postconditions:
              *    init'ed(config)
              */
    45      private static final ConfigManager config = IdentityManager.getGlobalConfig();
    46  
    47      /**
    48       * Creates a new PluginComponent for the specified plugin, to enable it to
    49       * be updated automatically.
    50       * 
    51       * @param plugin The plugin to be added to the updater
    52       */
             /* 
    P/P       *  Method: void com.dmdirc.updater.components.PluginComponent(PluginInfo)
              * 
              *  Preconditions:
              *    plugin != null
              * 
              *  Presumptions:
              *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@45 != null
              *    com.dmdirc.plugins.PluginInfo:getVersion(...)@56 != null
              * 
              *  Postconditions:
              *    this.plugin == plugin
              *    this.plugin != null
              * 
              *  Test Vectors:
              *    com.dmdirc.config.ConfigManager:hasOptionInt(...)@56: {0}, {1}
              *    com.dmdirc.plugins.PluginInfo:getAddonID(...)@56: {-231..0}, {1..232-1}
              */
    53      public PluginComponent(final PluginInfo plugin) {
    54          this.plugin = plugin;
    55          
    56          if ((plugin.getAddonID() > 0 && plugin.getVersion().isValid())
    57                  || (config.hasOptionInt("plugin-addonid", plugin.getName()))) {
    58              UpdateChecker.removeComponent(getName());
    59              UpdateChecker.registerComponent(this);
    60          }
    61      }
    62  
    63      /** {@inheritDoc} */
    64      @Override
    65      public String getName() {
                 /* 
    P/P           *  Method: String getName()
                  * 
                  *  Preconditions:
                  *    this.plugin != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@45 != null
                  * 
                  *  Postconditions:
                  *    java.lang.StringBuilder:toString(...)._tainted == 0
                  *    return_value in Addr_Set{&java.lang.StringBuilder:toString(...),&java.lang.StringBuilder:toString(...)}
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.plugins.PluginInfo:getAddonID(...)@66: {-231..0}, {1..232-1}
                  */
    66          if (plugin.getAddonID() > 0) {
    67              return "addon-" + plugin.getAddonID();
    68          } else {
    69              return "addon-" + config.getOption("plugin-addonid", plugin.getName());
    70          }
    71      }
    72  
    73      /** {@inheritDoc} */
    74      @Override    
    75      public String getFriendlyName() {
                 /* 
    P/P           *  Method: String getFriendlyName()
                  * 
                  *  Preconditions:
                  *    this.plugin != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    76          return plugin.getNiceName();
    77      }
    78  
    79      /** {@inheritDoc} */
    80      @Override
    81      public String getFriendlyVersion() {
                 /* 
    P/P           *  Method: String getFriendlyVersion()
                  * 
                  *  Preconditions:
                  *    this.plugin != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    82          return plugin.getFriendlyVersion();
    83      }
    84  
    85      /** {@inheritDoc} */
    86      @Override    
    87      public Version getVersion() {
                 /* 
    P/P           *  Method: Version getVersion()
                  * 
                  *  Preconditions:
                  *    this.plugin != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    88          return plugin.getVersion();
    89      }
    90  
    91      /** {@inheritDoc} */
    92      @Override    
    93      public boolean doInstall(final String path) throws Throwable {
                 /* 
    P/P           *  Method: bool doInstall(String)
                  * 
                  *  Preconditions:
                  *    this.plugin != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.plugins.PluginInfo:isLoaded(...)@100: {1}, {0}
                  *    com.dmdirc.plugins.PluginInfo:isLoaded(...)@114: {0}, {1}
                  *    com.dmdirc.plugins.PluginInfo:isLoaded(...)@96: {1}, {0}
                  *    com.dmdirc.plugins.PluginInfo:isUnloadable(...)@100: {1}, {0}
                  *    com.dmdirc.plugins.PluginInfo:isUnloadable(...)@96: {1}, {0}
                  *    java.io.File:exists(...)@104: {0}, {1}
                  *    java.io.File:exists(...)@96: {0}, {1}
                  *    java.io.File:renameTo(...)@100: {1}, {0}
                  */
    94          final File target = new File(plugin.getFullFilename());
    95          
    96          if ((plugin.isUnloadable() || !plugin.isLoaded()) && target.exists()) {
    97              target.delete();
    98          }
    99          
   100          if ((!plugin.isUnloadable() && plugin.isLoaded()) || !new File(path).renameTo(target)) {
   101              // Windows rocks!
   102              final File newTarget = new File(plugin.getFullFilename() + ".update");
   103              
   104              if (newTarget.exists()) {
   105                  newTarget.delete();
   106              }
   107              
   108              new File(path).renameTo(newTarget);
   109              return true;
   110          }
   111          
   112          plugin.pluginUpdated();
   113          
   114          if (plugin.isLoaded()) {
   115              plugin.unloadPlugin();
   116              plugin.loadPlugin();
   117          }
   118          
   119          return false;
   120      }
   121  
   122  }








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