1 7 22 package com.memoire.vainstall; 23 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.io.FileReader ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.LineNumberReader ; 30 import java.util.Arrays ; 31 import java.util.Vector ; 32 33 public abstract class AbstractInstall { 34 public static final int CANCEL = 0x01; 35 36 public static final int BACK = 0x02; 37 38 public static final int NEXT = 0x04; 39 40 public static final int FINISH = 0x08; 41 42 public static final boolean IS_WIN = System.getProperty("os.name") 43 .startsWith("Win"); 44 45 public static final boolean IS_MAC_OS_X = System.getProperty("os.name") 47 .startsWith("Mac OS X"); 48 49 public static final boolean IS_MAC = System.getProperty("os.name") 50 .startsWith("Mac") 51 && !IS_MAC_OS_X; 52 53 public static final boolean IS_UNIX = !IS_WIN && !IS_MAC || IS_MAC_OS_X; 54 55 public static final boolean IS_ROOT = (IS_UNIX && "root".equals(System 56 .getProperty("user.name"))) 57 || (IS_WIN && new File ("C:\\").canWrite()); 58 59 protected VAStep step_; 60 61 protected boolean dirty_; 62 63 66 protected VAStepFactory ui_; 67 68 71 protected VAStats stats_; 72 73 protected static final int LANGUAGE = 0; 74 75 protected static final int START = 1; 76 77 protected static final int WELCOME = 2; 78 79 protected static final int LICENSE = 3; 80 81 protected static final int README = 4; 82 83 protected static final int LICENSE_KEY = 5; 84 85 protected static final int UPGRADE = 6; 86 87 protected static final int DIRECTORY = 7; 88 89 protected static final int INSTALL = 8; 90 91 protected static final int SHORTCUTS = 9; 92 93 protected static final int END = 10; 94 95 protected int state_; 96 97 protected int actionEnabled_; 98 99 protected File sharedDir_; 100 101 protected File classloaderTempDir_; 102 103 protected UpgradeInfo uInfo_; 104 105 108 protected String language; 109 110 public AbstractInstall() { 111 super(); 112 if (IS_WIN) { 113 extractJniDll("/JNIWinShortcut.dll",true); 114 extractJniDll("/ICE_JNIRegistry.dll",false); 115 } 116 actionEnabled_ = 0; 117 stats_ = new VAStats(); 118 sharedDir_ = null; 119 uInfo_ = null; 120 dirty_ = false; 121 state_ = START; 122 } 123 124 134 public void deleteDirRecursive(File dir) { 135 if (dir == null || dir.exists() == false) { 137 return; 138 } 139 File [] filelist = dir.listFiles(); 141 for (int i = 0; i < filelist.length; i++) { 142 boolean isdir = filelist[i].isDirectory(); 143 if (isdir == true) { 144 deleteDirRecursive(filelist[i]); 146 } else { 147 if (filelist[i].delete() == false) { 149 stats_.addFile(filelist[i], VAStats.FAILURE); 150 filelist[i].deleteOnExit(); 151 VAGlobals.printDebug("F " + filelist[i] + " not deleted"); 152 } } } if (dir.delete() == false) { 157 dir.deleteOnExit(); 158 stats_.addDirectory(dir, VAStats.FAILURE); 159 VAGlobals.printDebug("D " + dir + " not deleted"); 160 } } 162 163 public boolean cleanShortcuts(File shortcutLogDir) throws IOException { 164 boolean value; 165 File logFile; 166 Vector directories; 167 logFile = new File (shortcutLogDir, "shortcuts.vai"); 168 value = false; 169 if (logFile.exists()) { 170 value = true; 171 directories = deleteFiles(logFile); 172 deleteDirectories(directories); 173 } 174 return value; 175 } 176 177 protected Vector deleteFiles(File log) throws IOException { 178 Vector dirs = new Vector (); 179 LineNumberReader logReader = new LineNumberReader (new FileReader (log)); 180 String line = logReader.readLine(); 181 while (line != null) { 182 File del = new File (line); 183 if (del.isDirectory()) { 184 185 dirs.add(del); 186 } else if (del.delete()) { 187 stats_.addFile(del, VAStats.SUCCESS); 188 } else { 189 del.deleteOnExit(); 190 stats_.addFile(del, VAStats.FAILURE); 191 VAGlobals.printDebug("F " + del + " not deleted"); 192 } 193 line = logReader.readLine(); 194 } 195 logReader.close(); 196 return dirs; 197 } 198 199 protected void deleteDirectories(Vector directories) throws IOException { 200 Object [] dirs = directories.toArray(); 201 Arrays.sort(dirs); 202 for (int i = dirs.length - 1; i >= 0; i--) { 203 File del = (File ) dirs[i]; 204 if (del.delete()) { 205 stats_.addDirectory(del, VAStats.SUCCESS); 206 } else { 207 del.deleteOnExit(); 208 stats_.addDirectory(del, VAStats.FAILURE); 209 VAGlobals.printDebug("D " + del + " not deleted"); 210 } 211 } 212 } 213 214 private void restoreLastVersion(File sharedDir, UpgradeInfo uInfo) { 215 File destPath = new File (sharedDir, "vai_" + VAGlobals.APP_NAME + "_" 216 + VAGlobals.APP_VERSION); 217 if (destPath.exists()) 219 deleteDirRecursive(destPath); 220 if ((uInfo == null) || (!uInfo.upgrade)) 221 return; 222 File lastVerPath = new File (sharedDir, "vai_" + VAGlobals.APP_NAME 223 + "_" + uInfo.lastVersion()); 224 if (destPath.equals(lastVerPath)) { 226 lastVerPath = new File (destPath.getAbsolutePath() + ".bak"); 227 lastVerPath.renameTo(destPath); 228 } 229 } 230 231 public void setActionEnabled(int a) { 232 actionEnabled_ = a; 233 ui_.setActionEnabled(a); 234 } 235 236 public void cancel() { 237 cleanInstall(sharedDir_, uInfo_); 238 quit(); 239 } 240 241 protected void cleanInstall(File sharedDir, UpgradeInfo uInfo) { 242 if ((!dirty_) || (sharedDir == null) || (!sharedDir.exists())) { 243 VAGlobals.printDebug("No cleaning needed"); 244 return; 245 } 246 File uninstDir = new File (sharedDir.getAbsolutePath() + File.separator 247 + "vai_" + VAGlobals.APP_NAME + "_" + VAGlobals.APP_VERSION); 248 if (!uninstDir.exists()) { 249 VAGlobals.printDebug("No cleaning needed"); 250 return; 251 } 252 VAGlobals.printDebug("Deleting uninstall files..."); 253 if ((uInfo != null) && (uInfo.upgrade)) { 254 restoreLastVersion(sharedDir, uInfo); 255 } else { 256 deleteDirRecursive(uninstDir); 258 } 259 VAGlobals.printDebug("All cleaned OK"); 260 } 261 262 protected void checkUpgrade(File sharedDir, UpgradeInfo uInfo) { 263 VAUpgradeStep step = (VAUpgradeStep) step_; 264 step.status(VAGlobals.i18n("Setup_LookPreviousVersions")); 265 ui_.uiSleep(2000); 266 checkVersions(sharedDir, uInfo); 267 if (uInfo.upgrade) { 268 VAGlobals.printDebug("Previous version found : " 269 + uInfo.lastVersion()); 270 step.status(VAGlobals.i18n("Setup_PreviousVersionFound") + " " 271 + uInfo.lastVersion()); 272 step.version(uInfo.lastVersion()); 273 step.directory(uInfo.lastPath().getAbsolutePath()); 274 } else { 275 VAGlobals.printDebug("No previous version found"); 276 step.status(VAGlobals.i18n("Setup_NoPreviousVersionFound")); 277 step.version(VAGlobals.i18n("Setup_None")); 278 step.directory(VAGlobals.i18n("Setup_None")); 279 } 280 } 281 282 private void checkVersions(File sharedDir, UpgradeInfo uInfo) { 283 String [] ls = sharedDir.list(new SetupFileFilter("vai_" 284 + VAGlobals.APP_NAME + "_", SetupFileFilter.STARTS_WITH, 285 SetupFileFilter.FILTER)); 286 if ((ls != null) && (ls.length > 0)) { 287 Arrays.sort(ls); 288 uInfo.versions = new String [ls.length]; 289 uInfo.paths = new File [ls.length]; 290 for (int i = 0; i < ls.length; i++) { 291 uInfo.versions[i] = ls[i].substring(ls[i].lastIndexOf('_') + 1); 292 try { 293 LineNumberReader log = new LineNumberReader (new FileReader ( 294 new File (new File (sharedDir, ls[i]), 295 "uninstall.vai"))); 296 String f = log.readLine(); 297 if (f != null) 298 uInfo.paths[i] = new File (f); 299 log.close(); 300 } catch (IOException ex) { 301 System.err.println(ex.getMessage()); 302 uInfo.paths[i] = null; 303 } 304 } 305 } 306 uInfo.upgrade = (uInfo.lastVersion() != null) 307 && (uInfo.lastPath() != null); 308 } 309 310 316 private void extractJniDll(String entryName, boolean isWinShortcut) { 317 try { 318 InputStream dll = getClass().getResourceAsStream(entryName); 319 File dllFile = new File (System.getProperty("java.io.tmpdir") 320 + File.separator 321 + entryName.substring(entryName.lastIndexOf('/') + 1)); 322 VAGlobals.printDebug("Extracting library " + dllFile); 323 System.out.println(dllFile.getAbsolutePath()); 324 FileOutputStream dllout = new FileOutputStream (dllFile); 325 byte[] data = new byte[512]; 326 int read = dll.read(data, 0, data.length); 327 while (read > 0) { 328 dllout.write(data, 0, read); 329 read = dll.read(data, 0, data.length); 330 } 331 dll.close(); 332 dllout.close(); 333 VAGlobals.printDebug(" library extracted"); 334 if (isWinShortcut) { 335 JNIWindowsShortcut.LIB_NAME = dllFile.getAbsolutePath();; 336 } else 337 VAGlobals.JNI_DLL_FILE = dllFile.getAbsolutePath(); 338 dllFile.deleteOnExit(); 339 } catch (IOException e) { 340 exitOnError(e); 341 } 342 } 343 344 protected void exitOnError(Throwable e) { 345 if (ui_ != null) 346 ui_.showFatalError(e); 347 else 348 e.printStackTrace(); 349 deleteTmpFile(); 350 System.exit(1); 351 } 352 353 private void deleteTmpFile(){ 354 if (classloaderTempDir_ != null) { 355 VAGlobals.printDebug("Temporary directory deleted: " 356 + classloaderTempDir_.getAbsolutePath()); 357 deleteDirRecursive(classloaderTempDir_); 358 } 359 } 360 361 protected void quit() { 362 deleteTmpFile(); 366 ui_ = null; 367 VAGlobals.printDebug("Exiting"); 368 System.exit(0); 369 } 370 371 376 public abstract void nextStep(); 377 378 383 public abstract void previousStep(); 384 385 391 public abstract void redoStep(); 392 }
| Popular Tags
|