您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Installer.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2006-2008 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.installer;
  23. import java.io.FileInputStream;
  24. import java.io.FileOutputStream;
  25. import java.io.IOException;
  26. import java.io.File;
  27. import java.io.FilenameFilter;
  28. import java.nio.channels.FileChannel;
  29. /**
  30. * Installs DMDirc
  31. *
  32. * @author Shane Mc Cormack
  33. */
  34. public abstract class Installer extends Thread {
  35. /** Types of shortcut */
  36. public static enum ShortcutType { DESKTOP, MENU, QUICKLAUNCH, UNINSTALLER, PROTOCOL; }
  37. /** Step where things happen. */
  38. protected StepInstall step = null;
  39. /**
  40. * Get the default install location
  41. */
  42. abstract String defaultInstallLocation();
  43. /**
  44. * This is what helps actually perform the installation in run().
  45. * This is a hack to keep the installing and the GUI separate.
  46. *
  47. * @param step The step that called this
  48. */
  49. public final void setInstallStep(final StepInstall step) {
  50. this.step = step;
  51. }
  52. /**
  53. * Create a new Installer
  54. */
  55. public Installer() {
  56. super("Installer-Thread");
  57. }
  58. /**
  59. * This step performs the installation, via the StepInstall step.
  60. */
  61. @Override
  62. public final void run() {
  63. step.performInstall(this);
  64. }
  65. /**
  66. * Is the given file name vaild to copy to the installation directory?
  67. *
  68. * @param filename File to check
  69. * @return true If the file should be copied, else false.
  70. */
  71. public abstract boolean validFile(final String filename);
  72. /**
  73. * Main Setup stuff
  74. *
  75. * @param location Location where app will be installed to.
  76. * @return True if installation passed, else false;
  77. */
  78. public boolean doSetup(final String location) {
  79. // Create the directory
  80. final File directory = new File(location);
  81. if (!directory.exists()) { directory.mkdir(); }
  82. try {
  83. final File dir = new File(".");
  84. final FilenameFilter filter = new FilenameFilter() {
  85. public boolean accept(final File dir, final String name) {
  86. return name.charAt(0) != '.' &&
  87. !name.equalsIgnoreCase("installer.jar") &&
  88. validFile(name);
  89. }
  90. };
  91. final String[] children = dir.list(filter);
  92. if (children != null) {
  93. for (String filename : children) {
  94. step.addText("Copying "+filename);
  95. copyFile(filename, location+File.separator+filename);
  96. }
  97. }
  98. } catch (IOException e) {
  99. step.addText("Error copying files: "+e.getMessage());
  100. return false;
  101. }
  102. step.addText("File Copying Complete.");
  103. return true;
  104. }
  105. /**
  106. * Check if this OS supports a given shortcut Type
  107. *
  108. * @param shortcutType Type of shortcut to check
  109. * @return True if this OS supports a given shortcut Type
  110. */
  111. public boolean supportsShortcut(final ShortcutType shortcutType) {
  112. return false;
  113. }
  114. /**
  115. * Setup shortcut
  116. *
  117. * @param location Location where app will be installed to.
  118. * @param shortcutType Type of shortcut to add.
  119. */
  120. abstract void setupShortcut(final String location, final ShortcutType shortcutType);
  121. /**
  122. * Copy a file from one location to another.
  123. * Based on http://www.exampledepot.com/egs/java.io/CopyFile.html
  124. *
  125. * @param srcFile Original file
  126. * @param dstFile New file
  127. */
  128. protected final void copyFile(final String srcFile, final String dstFile) throws IOException {
  129. if (new File(srcFile).exists()) {
  130. final FileChannel srcChannel = new FileInputStream(srcFile).getChannel();
  131. final FileChannel dstChannel = new FileOutputStream(dstFile).getChannel();
  132. dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
  133. srcChannel.close();
  134. dstChannel.close();
  135. } else {
  136. throw new IOException(srcFile+" does not exist.");
  137. }
  138. }
  139. /**
  140. * Any post-install tasks should be done here.
  141. *
  142. * @param location Location where app was installed to.
  143. */
  144. public void postInstall(final String location) { }
  145. }