File Source: Installer.java

         /* 
    P/P   *  Method: com.dmdirc.installer.Installer__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.installer;
    24  
    25  import com.dmdirc.installer.cliparser.CLIParser;
    26  
    27  import java.io.File;
    28  import java.io.FileInputStream;
    29  import java.io.FileOutputStream;
    30  import java.io.FilenameFilter;
    31  import java.io.IOException;
    32  import java.nio.channels.FileChannel;
    33  
    34  /**
    35   * Installs DMDirc.
    36   */
    37  public abstract class Installer extends Thread {
    38  
    39      /** Types of shortcut. */
             /* 
    P/P       *  Method: void com.dmdirc.installer.Installer$ShortcutType(String, int)
              */
    40      public static enum ShortcutType {
    41  
    42          /** Desktop shortcut. */
                 /* 
    P/P           *  Method: com.dmdirc.installer.Installer$ShortcutType__static_init
                  * 
                  *  Postconditions:
                  *    $VALUES == &new Installer$ShortcutType[](Installer$ShortcutType__static_init#6)
                  *    DESKTOP == &new Installer$ShortcutType(Installer$ShortcutType__static_init#1)
                  *    $VALUES[0] == &new Installer$ShortcutType(Installer$ShortcutType__static_init#1)
                  *    MENU == &new Installer$ShortcutType(Installer$ShortcutType__static_init#2)
                  *    $VALUES[1] == &new Installer$ShortcutType(Installer$ShortcutType__static_init#2)
                  *    PROTOCOL == &new Installer$ShortcutType(Installer$ShortcutType__static_init#5)
                  *    $VALUES[4] == &new Installer$ShortcutType(Installer$ShortcutType__static_init#5)
                  *    QUICKLAUNCH == &new Installer$ShortcutType(Installer$ShortcutType__static_init#3)
                  *    $VALUES[2] == &new Installer$ShortcutType(Installer$ShortcutType__static_init#3)
                  *    UNINSTALLER == &new Installer$ShortcutType(Installer$ShortcutType__static_init#4)
                  *    ...
                  */
    43          DESKTOP,
    44          /** Menu (start/k/etc) shortcut. */
    45          MENU,
    46          /** Quick launch shortcut. */
    47          QUICKLAUNCH,
    48          /** The actual uninstaller (not a shortcut, as far as I can tell). */
    49          UNINSTALLER,
    50          /** Associate DMDirc with the irc:// protocol (not a shortcut). */
    51          PROTOCOL;
    52      }
    53  
    54      /** Step where the installation output should be. */
    55      protected TextStep step;
    56  
    57      /**
    58       * Create a new Installer.
    59       */
    60      public Installer() {
                 /* 
    P/P           *  Method: void com.dmdirc.installer.Installer()
                  */
    61          super("Installer-Thread");
    62      }
    63  
    64      /**
    65       * Get the default install location.
    66       *
    67       * @return The default install location
    68       */
    69      public abstract String defaultInstallLocation();
    70  
    71      /**
    72       * This is what helps actually perform the installation in run().
    73       * This is a hack to keep the installing and the GUI separate.
    74       *
    75       * @param step The step that called this
    76       */
    77      public final void setInstallStep(final TextStep step) {
                 /* 
    P/P           *  Method: void setInstallStep(TextStep)
                  * 
                  *  Postconditions:
                  *    this.step == step
                  *    init'ed(this.step)
                  */
    78          this.step = step;
    79      }
    80  
    81      /** {@inheritDoc} */
    82      @Override
    83      public final void run() {
                 /* 
    P/P           *  Method: void run()
                  * 
                  *  Preconditions:
                  *    this.step != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.installer.Main:getInstaller(...)@100 != null
                  *    com.dmdirc.installer.Main:getInstaller(...)@111 != null
                  *    com.dmdirc.installer.Main:getInstaller(...)@120 != null
                  *    com.dmdirc.installer.Main:getInstaller(...)@129 != null
                  *    com.dmdirc.installer.Main:getInstaller(...)@134 != null
                  *    ...
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.installer.cliparser.CLIParser:getParamNumber(...)@86: {0}, {-231..-1, 1..232-1}
                  */
    84          step.setText("Beginning Install..\n");
    85  
    86          final boolean isUnattended = (CLIParser.getCLIParser().getParamNumber(
    87                  "-unattended") != 0);
    88          final Settings settings = (isUnattended) ? new DefaultSettings() : ((Settings) Main.getWizardFrame().
    89                  getStep(1));
    90  
    91          final String location = settings.getInstallLocation();
    92          step.addText("Installing files to: " + location);
    93          if (!doSetup(location)) {
    94              step.addText("");
    95              step.addText("Installation failed\n");
    96              Main.getWizardFrame().enableNextStep(true);
    97              return;
    98          }
    99  
   100          if (Main.getInstaller().supportsShortcut(ShortcutType.MENU)) {
   101              if (settings.getShortcutMenuState()) {
   102                  step.addText("Setting up " + Main.getInstaller().getMenuName() +
   103                               " shortcut");
   104                  setupShortcut(location, ShortcutType.MENU);
   105              } else {
   106                  step.addText("Not setting up " +
   107                               Main.getInstaller().getMenuName() + " shortcut");
   108              }
   109          }
   110  
   111          if (Main.getInstaller().supportsShortcut(ShortcutType.DESKTOP)) {
   112              if (settings.getShortcutDesktopState()) {
   113                  step.addText("Setting up Desktop shortcut");
   114                  setupShortcut(location, ShortcutType.DESKTOP);
   115              } else {
   116                  step.addText("Not setting up Desktop shortcut");
   117              }
   118          }
   119  
   120          if (Main.getInstaller().supportsShortcut(ShortcutType.QUICKLAUNCH)) {
   121              if (settings.getShortcutQuickState()) {
   122                  step.addText("Setting up Quick Launch shortcut");
   123                  setupShortcut(location, ShortcutType.QUICKLAUNCH);
   124              } else {
   125                  step.addText("Not setting up Quick Launch shortcut");
   126              }
   127          }
   128  
   129          if (Main.getInstaller().supportsShortcut(ShortcutType.UNINSTALLER)) {
   130              step.addText("Creating uninstaller");
   131              setupShortcut(location, ShortcutType.UNINSTALLER);
   132          }
   133  
   134          if (Main.getInstaller().supportsShortcut(ShortcutType.PROTOCOL)) {
   135              if (settings.getShortcutProtocolState()) {
   136                  step.addText("Setting up irc:// handler");
   137                  setupShortcut(location, ShortcutType.PROTOCOL);
   138              } else {
   139                  step.addText("Not setting up irc:// handler");
   140              }
   141          }
   142  
   143          postInstall(location);
   144  
   145          step.addText("");
   146          step.addText("Installation finished\n");
   147          Main.getWizardFrame().enableNextStep(true);
   148      }
   149  
   150      /**
   151       * Is the given file name vaild to copy to the installation directory?
   152       *
   153       * @param filename File to check
   154       * @return true If the file should be copied, else false.
   155       */
   156      public abstract boolean validFile(final String filename);
   157  
   158      /**
   159       * Main Setup stuff.
   160       *
   161       * @param location Location where app will be installed to.
   162       * @return True if installation passed, else false
   163       */
   164      public boolean doSetup(final String location) {
   165          // Create the directory
                 /* 
    P/P           *  Method: bool doSetup(String)
                  * 
                  *  Preconditions:
                  *    this.step != null
                  * 
                  *  Presumptions:
                  *    arr$.length@181 <= 232-1
                  *    init'ed(java.io.File.separator)
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    java.io.File:exists(...)@167: {1}, {0}
                  *    java.io.File:list(...)@181: Addr_Set{null}, Inverse{null}
                  */
   166          final File directory = new File(location);
   167          if (!directory.exists()) {
   168              directory.mkdir();
   169          }
   170  
   171          try {
   172              final File dir = new File(".");
                     /* 
    P/P               *  Method: void com.dmdirc.installer.Installer$1(Installer)
                      */
   173              final FilenameFilter filter = new FilenameFilter() {
   174  
   175                  /** {@inheritDoc} */
   176                  @Override
   177                  public boolean accept(final File dir, final String name) {
                             /* 
    P/P                       *  Method: bool accept(File, String)
                              * 
                              *  Preconditions:
                              *    name != null
                              * 
                              *  Postconditions:
                              *    init'ed(return_value)
                              */
   178                      return name.charAt(0) != '.' && validFile(name);
   179                  }
   180              };
   181              final String[] children = dir.list(filter);
   182              if (children != null) {
   183                  for (String filename : children) {
   184                      step.addText("Copying " + filename);
   185                      copyFile(filename, location + File.separator + filename);
   186                  }
   187              }
   188          } catch (IOException e) {
   189              step.addText("Error copying files: " + e.getMessage());
   190              return false;
   191          }
   192          step.addText("File Copying Complete.");
   193          return true;
   194      }
   195  
   196      /**
   197       * Check if this OS supports a given shortcut Type.
   198       *
   199       * @param shortcutType Type of shortcut to check
   200       * @return True if this OS supports a given shortcut Type
   201       */
   202      public boolean supportsShortcut(final ShortcutType shortcutType) {
                 /* 
    P/P           *  Method: bool supportsShortcut(Installer$ShortcutType)
                  * 
                  *  Postconditions:
                  *    return_value == 0
                  */
   203          return false;
   204      }
   205  
   206      /**
   207       * Check what name to show for the menu shortcut
   208       *
   209       * @return Name for menu shortcutType
   210       */
   211      public String getMenuName() {
                 /* 
    P/P           *  Method: String getMenuName()
                  * 
                  *  Postconditions:
                  *    return_value == &amp;"menu"
                  */
   212          return "menu";
   213      }
   214  
   215      /**
   216       * Setup shortcut.
   217       *
   218       * @param location Location where app will be installed to.
   219       * @param shortcutType Type of shortcut to add.
   220       */
   221      protected abstract void setupShortcut(final String location,
   222                                            final ShortcutType shortcutType);
   223  
   224      /**
   225       * Copy a file from one location to another.
   226       * Based on http://www.exampledepot.com/egs/java.io/CopyFile.html
   227       *
   228       * @param srcFile Original file
   229       * @param dstFile New file
   230       * @throws java.io.IOException If an exception occurs while copying
   231       */
   232      protected final void copyFile(final String srcFile, final String dstFile)
   233              throws IOException {
                 /* 
    P/P           *  Method: void copyFile(String, String)
                  * 
                  *  Presumptions:
                  *    java.io.File:exists(...)@234 == 1
                  *    java.io.FileInputStream:getChannel(...)@235 != null
                  *    java.io.FileOutputStream:getChannel(...)@237 != null
                  */
   234          if (new File(srcFile).exists()) {
   235              final FileChannel srcChannel = new FileInputStream(srcFile).
   236                      getChannel();
   237              final FileChannel dstChannel = new FileOutputStream(dstFile).
   238                      getChannel();
   239  
   240              dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
   241  
   242              srcChannel.close();
   243              dstChannel.close();
   244          } else {
   245              throw new IOException(srcFile + " does not exist.");
   246          }
   247      }
   248  
   249      /**
   250       * Any post-install tasks should be done here.
   251       *
   252       * @param location Location where app was installed to.
   253       */
   254      public void postInstall(final String location) {
   255          // Nothing to do by default, installers may override
             /* 
    P/P       *  Method: void postInstall(String)
              */
   256      }
   257  }








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