0001: /**
0002: * $RCSfile: Setup.java,v $
0003: * @creation 26/01/00
0004: * @modification $Date: 2005/05/14 10:14:16 $
0005: */package com.memoire.vainstall;
0006:
0007: import java.awt.Color;
0008: import java.io.ByteArrayInputStream;
0009: import java.io.ByteArrayOutputStream;
0010: import java.io.File;
0011: import java.io.FileInputStream;
0012: import java.io.FileNotFoundException;
0013: import java.io.FileOutputStream;
0014: import java.io.FileReader;
0015: import java.io.FileWriter;
0016: import java.io.FilenameFilter;
0017: import java.io.IOException;
0018: import java.io.InputStream;
0019: import java.io.LineNumberReader;
0020: import java.io.ObjectInputStream;
0021: import java.io.PrintWriter;
0022: import java.lang.reflect.InvocationTargetException;
0023: import java.lang.reflect.Method;
0024: import java.util.ArrayList;
0025: import java.util.Arrays;
0026: import java.util.Iterator;
0027: import java.util.List;
0028: import java.util.Properties;
0029: import java.util.Set;
0030: import java.util.StringTokenizer;
0031: import java.util.Vector;
0032: import java.util.jar.JarInputStream;
0033: import java.util.jar.JarOutputStream;
0034: import java.util.zip.CRC32;
0035: import java.util.zip.GZIPInputStream;
0036: import java.util.zip.ZipEntry;
0037: import java.util.zip.ZipInputStream;
0038:
0039: import com.ice.jni.registry.RegStringValue;
0040: import com.ice.jni.registry.Registry;
0041: import com.ice.jni.registry.RegistryKey;
0042:
0043: /**
0044: * @version $Id: Setup.java,v 1.36 2005/05/14 10:14:16 deniger Exp $
0045: * @author Axel von Arnim
0046: */
0047: /*
0048: * Strategy for handling installation data for upgrades: the goal is to preserve
0049: * old data until the install is completed successfully. If a directory of
0050: * install data is found for the previous version, and that directory is not the
0051: * same as the name of the directory for the new version. A new directory will
0052: * be created for the install data from the upgrade. Upon successful completion,
0053: * the old directory will be deleted, and upon failure, the new directory will
0054: * be deleted. If the name of the old directory (version) is the same as the new
0055: * directory, the current directory is renamed, and a new directory is created.
0056: * The disposition of the directories will be the same as if they began with
0057: * different names.
0058: */
0059: public class Setup extends AbstractInstall {
0060: public static final String JAVA_HOME = System
0061: .getProperty("java.home");
0062:
0063: public String JAVA_EXE;
0064:
0065: private String JAVAW_EXE;
0066:
0067: private String JAVA_EXE_QUOTE;
0068:
0069: private File fileWithArchive_;
0070:
0071: private String installClassName_;
0072:
0073: private long installClassOffset_;
0074:
0075: private long installClassSize_;
0076:
0077: private long jarOffset_;
0078:
0079: private LicenseKeySupport licenseKeySupport;
0080:
0081: private boolean choosedLanguage;
0082:
0083: private VAShortcutEntry[] launchparms;
0084:
0085: // an array of String arrays, one
0086: // for each launch script.
0087: // Within each string array
0088: // 0 - name of launch script (no suff)
0089: // 1 - name of java binary
0090: // 2 - arguments to java
0091: // 3 - classpath
0092: // 4 - class to be run
0093: // 5 - arguments to class
0094: private String customPrePostClassName_;
0095:
0096: /**
0097: * The original language
0098: */
0099: private String originalLanguage;
0100:
0101: public Setup(String uiMode, String uiBluescreen,
0102: String uiBluescreenColor, String appName,
0103: String appVersion, String destPath, String linkSectionName,
0104: String linkSectionIcon, String linkEntryName,
0105: String linkEntryIcon, Boolean createUninstallShortcut,
0106: String licenseKeySupportName, File fileWithArchive,
0107: String installClassName, Long installClassOffset,
0108: Long installClassSize, Long jarOffset,
0109: File classloaderTempDir) {
0110: // this();
0111: super ();
0112: classloaderTempDir_ = classloaderTempDir;
0113: try {
0114: licenseKeySupport = (LicenseKeySupport) getClass()
0115: .getClassLoader().loadClass(licenseKeySupportName)
0116: .newInstance();
0117: } catch (Exception ex) {
0118: // what should be done here?
0119: throw new IllegalArgumentException(
0120: "cannot load license key support: " + ex.toString());
0121: }
0122: uInfo_ = new UpgradeInfo();
0123: VAGlobals.UI_MODE = System.getProperty("uimode", uiMode);
0124: if ("no".equals(uiBluescreen))
0125: VAGlobals.UI_BLUESCREEN = false;
0126: else
0127: VAGlobals.UI_BLUESCREEN = true;
0128: String bsVar = System.getProperty("bluescreen");
0129: if (bsVar != null) {
0130: if ("no".equalsIgnoreCase(bsVar))
0131: VAGlobals.UI_BLUESCREEN = false;
0132: else
0133: VAGlobals.UI_BLUESCREEN = true;
0134: }
0135: if ((uiBluescreenColor == null)
0136: || ("".equals(uiBluescreenColor))
0137: || ("null".equals(uiBluescreenColor)))
0138: VAGlobals.UI_BLUESCREEN_COLOR = null;
0139: else
0140: VAGlobals.UI_BLUESCREEN_COLOR = new Color(Integer.parseInt(
0141: uiBluescreenColor, 16));
0142: // image is forced to this location now
0143: VAGlobals.IMAGE = "com/memoire/vainstall/resources/banner.gif";
0144: VAGlobals.APP_NAME = appName;
0145: VAGlobals.APP_VERSION = appVersion;
0146: try {
0147: VAGlobals.DEST_PATH = expandDirectory(destPath, false,
0148: uInfo_);
0149: } catch (IOException e) {
0150: VAGlobals.printDebug(e.toString());
0151: //System.out.println("Cannot determine path, trying your home
0152: // directory instead.., received exception " + e);
0153: }
0154: // HACK CVDL since the above exception is not handled and we don't know
0155: // if the environment variable is set, the path will default to the home
0156: // directory"
0157: if (VAGlobals.DEST_PATH == null) {
0158: try {
0159: VAGlobals.DEST_PATH = expandDirectory("[HOME]"
0160: + getWithoutMacro(destPath), false, uInfo_);
0161: } catch (IOException e) {
0162: VAGlobals.printDebug(e.toString());
0163: }
0164: //no problem...
0165: if (VAGlobals.DEST_PATH == null)
0166: VAGlobals.DEST_PATH = System.getProperty("user.dir"
0167: + getWithoutMacro(destPath));
0168: }
0169: VAGlobals.LINK_SECTION_NAME = linkSectionName;
0170: VAGlobals.LINK_SECTION_ICON = linkSectionIcon;
0171: VAGlobals.LINK_ENTRY_NAME = linkEntryName;
0172: VAGlobals.LINK_ENTRY_ICON = linkEntryIcon;
0173: VAGlobals.CREATE_UNINSTALL_SHORTCUT = createUninstallShortcut
0174: .booleanValue();
0175: if (uInfo_.forceUpgrade)
0176: VAGlobals.OPERATION = VAGlobals.UPDATE;
0177: else
0178: VAGlobals.OPERATION = VAGlobals.INSTALL;
0179: fileWithArchive_ = fileWithArchive;
0180: installClassName_ = installClassName;
0181: installClassOffset_ = installClassOffset.longValue();
0182: installClassSize_ = installClassSize.longValue();
0183: jarOffset_ = jarOffset.longValue();
0184: // get vainstall.properties
0185: InputStream pin = getClass().getResourceAsStream(
0186: "resources/vainstall.properties");
0187: Properties prop = new Properties();
0188: try {
0189: prop.load(pin);
0190: } catch (IOException exc) {
0191: // ignore at the moment
0192: }
0193: language = prop.getProperty("vainstall.destination.language");
0194: if (language != null) {
0195: if (language.toLowerCase().indexOf("choose") != -1) {
0196: choosedLanguage = true;
0197: state_ = LANGUAGE;
0198: }
0199: VAGlobals.setLanguage(language);
0200: } // endif
0201: originalLanguage = VAGlobals.getCurrentLanguage();
0202: customPrePostClassName_ = prop
0203: .getProperty("vainstall.install.customprepost.className");
0204: //set the look and feel
0205: VAStepFactory.setLnf(VAGlobals.UI_MODE, prop, getClass(),
0206: classloaderTempDir);
0207: //the user interface
0208: ui_ = VAStepFactory.createUI(VAGlobals.UI_MODE, this );
0209: VAGlobals.printDebug("UI created");
0210: if (!fileWithArchive.exists()) {
0211: ui_.showError(new Exception(VAGlobals
0212: .i18n("Setup_ArchiveNotFound")
0213: + fileWithArchive.getAbsolutePath()));
0214: quit();
0215: }
0216: //the the jvm version
0217: testVersion(prop);
0218: if ("true".equals(prop.getProperty(
0219: "vainstall.script.java.fullpath", null))) {
0220: VAGlobals.USE_FULL_JAVA_PATH = true;
0221: if (VAGlobals.DEBUG) {
0222: VAGlobals.printDebug("Full java path always used");
0223: }
0224: } else if (VAGlobals.DEBUG)
0225: VAGlobals.printDebug("Full java path used only if needed");
0226: if ("true".equals(prop.getProperty(
0227: "vainstall.shortcut.in.installdir", null))) {
0228: VAGlobals.SHORTCUTS_IN_INSTALLDIR = true;
0229: if (VAGlobals.DEBUG) {
0230: VAGlobals
0231: .printDebug("Shortcuts will be created in install dir");
0232: }
0233: }
0234: JAVA_EXE = getJavaExe(".exe");
0235:
0236: JAVAW_EXE = getJavaExe("w.exe");
0237:
0238: JAVA_EXE_QUOTE = JAVA_EXE.indexOf(" ") > -1 ? "\"" : "";
0239: nextStep();
0240: ui_.activateUI();
0241: }
0242:
0243: public void testVersion(final Properties prop) {
0244: // we test the java version
0245: boolean wrongVersion = false;
0246: String minVersion = prop.getProperty(
0247: "vainstall.java.version.min", null);
0248: String maxVersion = prop.getProperty(
0249: "vainstall.java.version.max", null);
0250: String vendor = prop.getProperty("vainstall.java.vendor", null);
0251: if (minVersion != null || maxVersion != null) {
0252: String version = System.getProperty("java.version");
0253: if (minVersion != null && minVersion.compareTo(version) > 0) {
0254: wrongVersion = true;
0255: }
0256: if (maxVersion != null && maxVersion.compareTo(version) < 0) {
0257: wrongVersion = true;
0258: }
0259: }
0260: //if the version is correct and a spec vendor is required
0261: if (!wrongVersion && vendor != null) {
0262: String currentVendor = System.getProperty("java.vendor",
0263: null);
0264: boolean found = false;
0265: if (currentVendor != null) {
0266: currentVendor = currentVendor.toLowerCase();
0267: System.err.println("current vendor " + currentVendor);
0268: StringTokenizer tk = new StringTokenizer(vendor
0269: .toLowerCase(), ",");
0270: while (!found && tk.hasMoreTokens()) {
0271: if (currentVendor.indexOf(tk.nextToken().trim()) != -1)
0272: found = true;
0273: }
0274: }
0275: wrongVersion = !found;
0276: }
0277: if (wrongVersion) {
0278: String minMax = "";
0279: if (minVersion != null) {
0280: minMax = VAGlobals.i18n("Setup_MinJavaVersion") + ": "
0281: + minVersion + "\n";
0282: }
0283: if (maxVersion != null) {
0284: minMax = minMax
0285: + VAGlobals.i18n("Setup_MaxJavaVersion") + ": "
0286: + maxVersion + "\n";
0287: }
0288: if (vendor != null) {
0289: minMax = minMax + VAGlobals.i18n("Setup_JavaVendor")
0290: + ": " + vendor + "\n";
0291: }
0292: String error = VAGlobals
0293: .i18n("Setup_JavaVersionNotSuitable")
0294: + "\n\n"
0295: + minMax
0296: + "\n"
0297: + VAGlobals.i18n("Setup_JavaUsedVersion")
0298: + ": "
0299: + System.getProperty("java.version")
0300: + " ("
0301: + System.getProperty("java.vendor")
0302: + ")"
0303: + "\n\n"
0304: + VAGlobals.i18n("Setup_JavaDownload")
0305: + ":\n"
0306: + prop.getProperty("vainstall.java.download.url",
0307: "http://www.java.com/download/manual.jsp");
0308: if (VAGlobals.DEBUG) {
0309: System.err.println("check version failed ...");
0310: }
0311: ui_.showError(new Exception(error));
0312: quit();
0313: }
0314: }
0315:
0316: public void nextStep() {
0317: switch (state_) {
0318: case LANGUAGE: {
0319: VAGlobals.printDebug("LANGUAGE");
0320: state_ = START;
0321: setActionEnabled(NEXT | CANCEL);
0322: step_ = ui_.createSetupLanguageStep();
0323: break;
0324: }
0325: case START: {
0326: VAGlobals.printDebug("WELCOME");
0327: if (step_ instanceof VALanguageStep) {
0328: language = ((VALanguageStep) step_).getLanguage();
0329: VAGlobals.setLanguage(language);
0330: }
0331: state_ = WELCOME;
0332: if (choosedLanguage == false) {
0333: setActionEnabled(NEXT | CANCEL);
0334: } else {
0335: setActionEnabled(BACK | NEXT | CANCEL);
0336: }
0337: step_ = ui_.createWelcomeStep();
0338: break;
0339: }
0340: case WELCOME: {
0341: VAGlobals.printDebug("LICENSE");
0342: state_ = LICENSE;
0343: setActionEnabled(BACK | NEXT | CANCEL);
0344: step_ = ui_.createLicenseStep();
0345: ((VALicenseStep) step_).setText(getClass()
0346: .getResourceAsStream("license.txt"));
0347: break;
0348: }
0349: case LICENSE: {
0350: if (!((VALicenseStep) step_).isLicenseAccepted()) {
0351: ui_.showError(new Exception(VAGlobals
0352: .i18n("Setup_AcceptLicense")));
0353: } else {
0354: VAGlobals.printDebug("README");
0355: state_ = README;
0356: setActionEnabled(BACK | NEXT | CANCEL);
0357: step_ = ui_.createReadmeStep();
0358: ((VAReadmeStep) step_).setText(getClass()
0359: .getResourceAsStream("readme.txt"));
0360: }
0361: break;
0362: }
0363: case README: {
0364: if (licenseKeySupport.needsLicenseKey()) {
0365: VAGlobals.printDebug("LICENSE_KEY");
0366: state_ = LICENSE_KEY;
0367: setActionEnabled(0);
0368: step_ = ui_.createLicenseKeyStep();
0369: ((VALicenseKeyStep) step_)
0370: .setLicenseKeySupport(licenseKeySupport);
0371: setActionEnabled(BACK | NEXT | CANCEL);
0372: } else {
0373: nextStepUpgrade();
0374: }
0375: break;
0376: }
0377: case LICENSE_KEY: {
0378: if (((VALicenseKeyStep) step_)
0379: .getGetFields(licenseKeySupport)) {
0380: nextStepUpgrade();
0381: } else {
0382: ui_.showError(new Exception("invalid license key" /*
0383: * i18n fix
0384: * needed
0385: */
0386: ));
0387: }
0388: break;
0389: }
0390: case UPGRADE: {
0391: boolean uConfirm = true;
0392: if (uInfo_.forceUpgrade
0393: || VAGlobals.APP_VERSION.equals(uInfo_
0394: .lastVersion()))
0395: uConfirm = true;
0396: else {
0397: VAUpgradeStep ustep = (VAUpgradeStep) step_;
0398: uConfirm = ustep.isConfirmUpgrade();
0399: }
0400: uInfo_.upgrade = uInfo_.upgrade && uConfirm;
0401: if (uInfo_.upgrade) {
0402: if (!uInfo_.lastPath().exists()) {
0403: exitOnError(new Exception(
0404: uInfo_.lastPath()
0405: + VAGlobals
0406: .i18n("Setup_DirectoryNotAccessible")
0407: + "\n"
0408: + VAGlobals
0409: .i18n("Setup_ReinstallFirst")));
0410: }
0411: if (uInfo_.module
0412: && !VAGlobals.APP_VERSION.equals(uInfo_
0413: .lastVersion())) {
0414: exitOnError(new Exception(VAGlobals
0415: .i18n("Setup_VersionWarning")
0416: + "\n"
0417: + VAGlobals.i18n("Setup_Current")
0418: + uInfo_.lastVersion()
0419: + ", "
0420: + VAGlobals.i18n("Setup_ThisOne")
0421: + VAGlobals.APP_VERSION
0422: + "\n"
0423: + VAGlobals.i18n("Setup_ReinstallFirst")));
0424: }
0425: if (uInfo_.lastVersion().compareTo(
0426: VAGlobals.APP_VERSION) > 0) {
0427: ui_.showError(new Exception(VAGlobals
0428: .i18n("Setup_VersionWarning")
0429: + "\n"
0430: + VAGlobals.i18n("Setup_Current")
0431: + uInfo_.lastVersion()
0432: + ", "
0433: + VAGlobals.i18n("Setup_ThisOne")
0434: + VAGlobals.APP_VERSION
0435: + "\n"
0436: + VAGlobals
0437: .i18n("Setup_CurrentVersionNewer")
0438: + "\n"
0439: + VAGlobals.i18n("Setup_MayCancelUpdate")));
0440: }
0441: VAGlobals.printDebug("Upgrade from version: "
0442: + uInfo_.lastVersion());
0443: VAGlobals.DEST_PATH = uInfo_.lastPath()
0444: .getAbsolutePath();
0445: uInfo_.paths = null;
0446: VAGlobals.printDebug("INSTALL");
0447: state_ = INSTALL;
0448: setActionEnabled(BACK | NEXT | CANCEL);
0449: step_ = ui_.createInstallStep();
0450: } else {
0451: if (uInfo_.forceUpgrade)
0452: exitOnError(new Exception(VAGlobals
0453: .i18n("Setup_UpdateOnly")
0454: + "\n"
0455: + VAGlobals.i18n("Setup_NoSuitableVersion")
0456: + "\n" + VAGlobals.APP_NAME));
0457: VAGlobals.printDebug("DIRECTORY");
0458: state_ = DIRECTORY;
0459: setActionEnabled(BACK | NEXT | CANCEL);
0460: step_ = ui_.createDirectoryStep();
0461: ((VADirectoryStep) step_).setDirectory(new File(
0462: VAGlobals.DEST_PATH));
0463: }
0464: break;
0465: }
0466: case DIRECTORY: {
0467: VADirectoryStep dstep = (VADirectoryStep) step_;
0468: File file = (dstep).getDirectory();
0469: if (file == null)
0470: return;
0471: File errorDir = checkDirectory(file);
0472: if (errorDir != null) {
0473: dstep.roDirectory(errorDir);
0474: VAGlobals.printDebug(" " + errorDir.getAbsolutePath()
0475: + " read-only");
0476: return;
0477: }
0478: if (uInfo_.paths != null) {
0479: for (int i = 0; i < uInfo_.paths.length; i++) {
0480: if ((uInfo_.paths[i] != null)
0481: && (uInfo_.paths[i].getAbsolutePath()
0482: .equals(file.getAbsolutePath()))) {
0483: dstep.rejectDirectory();
0484: VAGlobals.printDebug(" "
0485: + file.getAbsolutePath() + " rejected");
0486: return;
0487: }
0488: }
0489: }
0490: if (!dstep.acceptDirectory()) {
0491: VAGlobals.printDebug(" " + file.getAbsolutePath()
0492: + " refused by user");
0493: return;
0494: }
0495:
0496: VAGlobals.DEST_PATH = file.getAbsolutePath();
0497: VAGlobals.printDebug(" " + file.getAbsolutePath()
0498: + " accepted");
0499: VAGlobals.printDebug("INSTALL");
0500: state_ = INSTALL;
0501: setActionEnabled(BACK | NEXT | CANCEL);
0502: step_ = ui_.createInstallStep();
0503: break;
0504: }
0505: case INSTALL: {
0506: if (VAGlobals.APP_VERSION == null)
0507: exitOnError(new Exception(VAGlobals
0508: .i18n("Setup_VersionWarning")
0509: + "\n"
0510: + VAGlobals.i18n("Setup_Current")
0511: + uInfo_.lastVersion()
0512: + ", "
0513: + VAGlobals.i18n("Setup_ThisOne")
0514: + "is null"
0515: + "\n" + VAGlobals.i18n("Setup_ReinstallFirst")));
0516: setActionEnabled(0);
0517: startInstall(sharedDir_, uInfo_);
0518: // On saute les raccourcis en mode mise a jour
0519: if (uInfo_.upgrade || uInfo_.forceUpgrade) {
0520: try {
0521: if (!uInfo_.module) {
0522: if (cleanShortcuts(new File(sharedDir_
0523: .getAbsolutePath()
0524: + File.separator
0525: + "vai_"
0526: + VAGlobals.APP_NAME
0527: + "_"
0528: + VAGlobals.APP_VERSION
0529: + File.separator)))
0530: createShortcuts(sharedDir_);
0531: } else {
0532: createShortcuts(sharedDir_);
0533: }
0534: } catch (IOException e) {
0535: System.out.println("[Setup]: " + e.getMessage());
0536: }
0537: VAGlobals.printDebug("END");
0538: state_ = END;
0539: setActionEnabled(FINISH);
0540: step_ = ui_.createEndStep();
0541: ((VAEndStep) step_).setStats(stats_);
0542: } else {
0543: VAGlobals.printDebug("SHORTCUTS");
0544: state_ = SHORTCUTS;
0545: setActionEnabled(NEXT);
0546: step_ = ui_.createShortcutStep();
0547: }
0548: break;
0549: }
0550: case SHORTCUTS: {
0551: if (((VAShortcutStep) step_).isShortcutAccepted()) {
0552: createShortcuts(sharedDir_);
0553: }
0554: VAGlobals.printDebug("END");
0555: state_ = END;
0556: setActionEnabled(FINISH);
0557: step_ = ui_.createEndStep();
0558: ((VAEndStep) step_).setStats(stats_);
0559: break;
0560: }
0561: case END: {
0562: ui_.quitUI();
0563: quit();
0564: }
0565: }
0566: }
0567:
0568: private void nextStepUpgrade() {
0569: VAGlobals.printDebug("UPGRADE");
0570: state_ = UPGRADE;
0571: setActionEnabled(0);
0572: step_ = ui_.createUpgradeStep();
0573: VAGlobals.printDebug(IS_ROOT ? "Root install" : "User install");
0574: sharedDir_ = findVAISharedDir();
0575: VAGlobals.printDebug("vainstall directory: !!!" + sharedDir_);
0576: checkUpgrade(sharedDir_, uInfo_);
0577: if ((!(VAGlobals.APP_VERSION.equals(uInfo_.lastVersion())))
0578: && uInfo_.upgrade && (!uInfo_.forceUpgrade)) {
0579: VAUpgradeStep ustep = (VAUpgradeStep) step_;
0580: ustep.setChoiceEnabled(true);
0581: }
0582: setActionEnabled(BACK | NEXT | CANCEL);
0583: }
0584:
0585: public void previousStep() {
0586: switch (state_) {
0587: case WELCOME: {
0588: VAGlobals.printDebug("LANGUAGE");
0589: state_ = START;
0590: setActionEnabled(NEXT | CANCEL);
0591: VAGlobals.setLanguage(originalLanguage);
0592: step_ = ui_.createSetupLanguageStep();
0593: break;
0594: }
0595: case LICENSE: {
0596: VAGlobals.printDebug("WELCOME");
0597: state_ = WELCOME;
0598: if (choosedLanguage == false) {
0599: setActionEnabled(NEXT | CANCEL);
0600: } else {
0601: setActionEnabled(BACK | NEXT | CANCEL);
0602: }
0603: step_ = ui_.createWelcomeStep();
0604: break;
0605: }
0606: case README: {
0607: VAGlobals.printDebug("LICENSE");
0608: state_ = LICENSE;
0609: setActionEnabled(BACK | NEXT | CANCEL);
0610: step_ = ui_.createLicenseStep();
0611: ((VAReadmeStep) step_).setText(getClass()
0612: .getResourceAsStream("license.txt"));
0613: break;
0614: }
0615: case LICENSE_KEY: {
0616: VAGlobals.printDebug("README");
0617: state_ = README;
0618: setActionEnabled(BACK | NEXT | CANCEL);
0619: step_ = ui_.createReadmeStep();
0620: ((VAReadmeStep) step_).setText(getClass()
0621: .getResourceAsStream("readme.txt"));
0622: break;
0623: }
0624: case UPGRADE: {
0625: if (licenseKeySupport.needsLicenseKey()) {
0626: VAGlobals.printDebug("LICENSE_KEY");
0627: state_ = LICENSE_KEY;
0628: setActionEnabled(0);
0629: step_ = ui_.createLicenseKeyStep();
0630: ((VALicenseKeyStep) step_)
0631: .setLicenseKeySupport(licenseKeySupport);
0632: setActionEnabled(BACK | NEXT | CANCEL);
0633: } else {
0634: VAGlobals.printDebug("README");
0635: state_ = README;
0636: setActionEnabled(BACK | NEXT | CANCEL);
0637: step_ = ui_.createReadmeStep();
0638: ((VAReadmeStep) step_).setText(getClass()
0639: .getResourceAsStream("readme.txt"));
0640: }
0641: break;
0642: }
0643: case DIRECTORY: {
0644: VAGlobals.printDebug("UPGRADE");
0645: state_ = UPGRADE;
0646: setActionEnabled(0);
0647: step_ = ui_.createUpgradeStep();
0648: checkUpgrade(sharedDir_, uInfo_);
0649: if ((!(VAGlobals.APP_VERSION.equals(uInfo_.lastVersion())))
0650: && uInfo_.upgrade && (!uInfo_.forceUpgrade)) {
0651: VAUpgradeStep ustep = (VAUpgradeStep) step_;
0652: ustep.setChoiceEnabled(true);
0653: }
0654: setActionEnabled(BACK | NEXT | CANCEL);
0655: break;
0656: }
0657: case INSTALL: {
0658: if (uInfo_.upgrade) {
0659: state_ = UPGRADE;
0660: setActionEnabled(0);
0661: step_ = ui_.createUpgradeStep();
0662: checkUpgrade(sharedDir_, uInfo_);
0663: setActionEnabled(BACK | NEXT | CANCEL);
0664: } else {
0665: VAGlobals.printDebug("DIRECTORY");
0666: state_ = DIRECTORY;
0667: setActionEnabled(BACK | NEXT | CANCEL);
0668: step_ = ui_.createDirectoryStep();
0669: ((VADirectoryStep) step_).setDirectory(new File(
0670: VAGlobals.DEST_PATH));
0671: }
0672: break;
0673: }
0674: default: {
0675: VAGlobals.printDebug("can't go back...");
0676: break;
0677: }
0678: }
0679: }
0680:
0681: public void redoStep() {
0682: switch (state_) {
0683: case START: {
0684: setActionEnabled(NEXT | CANCEL);
0685: step_ = ui_.createSetupLanguageStep();
0686: break;
0687: }
0688: case WELCOME: {
0689: {
0690: setActionEnabled(BACK | NEXT | CANCEL);
0691: }
0692: step_ = ui_.createWelcomeStep();
0693: break;
0694: }
0695: case LICENSE: {
0696: setActionEnabled(BACK | NEXT | CANCEL);
0697: step_ = ui_.createLicenseStep();
0698: ((VALicenseStep) step_).setText(getClass()
0699: .getResourceAsStream("license.txt"));
0700: break;
0701: }
0702: case README: {
0703: setActionEnabled(BACK | NEXT | CANCEL);
0704: step_ = ui_.createReadmeStep();
0705: ((VAReadmeStep) step_).setText(getClass()
0706: .getResourceAsStream("readme.txt"));
0707: break;
0708: }
0709: case LICENSE_KEY: {
0710: VAGlobals.printDebug("LICENSE_KEY");
0711: state_ = LICENSE_KEY;
0712: setActionEnabled(0);
0713: step_ = ui_.createLicenseKeyStep();
0714: ((VALicenseKeyStep) step_)
0715: .setLicenseKeySupport(licenseKeySupport);
0716: setActionEnabled(BACK | NEXT | CANCEL);
0717: break;
0718: }
0719: case UPGRADE: {
0720: setActionEnabled(0);
0721: step_ = ui_.createUpgradeStep();
0722: checkUpgrade(sharedDir_, uInfo_);
0723: if ((!(VAGlobals.APP_VERSION.equals(uInfo_.lastVersion())))
0724: && uInfo_.upgrade && (!uInfo_.forceUpgrade)) {
0725: VAUpgradeStep ustep = (VAUpgradeStep) step_;
0726: ustep.setChoiceEnabled(true);
0727: }
0728: setActionEnabled(BACK | NEXT | CANCEL);
0729: break;
0730: }
0731: case DIRECTORY: {
0732: setActionEnabled(BACK | NEXT | CANCEL);
0733: step_ = ui_.createDirectoryStep();
0734: ((VADirectoryStep) step_).setDirectory(new File(
0735: VAGlobals.DEST_PATH));
0736: break;
0737: }
0738: case INSTALL: {
0739: setActionEnabled(BACK | NEXT | CANCEL);
0740: step_ = ui_.createInstallStep();
0741: break;
0742: }
0743: case SHORTCUTS: {
0744: setActionEnabled(NEXT);
0745: step_ = ui_.createShortcutStep();
0746: break;
0747: }
0748: case END: {
0749: setActionEnabled(FINISH);
0750: step_ = ui_.createEndStep();
0751: ((VAEndStep) step_).setStats(stats_);
0752: break;
0753: }
0754: }
0755: }
0756:
0757: private File checkDirectory(File dir) {
0758: File res = null;
0759: // CVDL CHANGED if one wants to install into the existing directory
0760: // /tmp,
0761: // the installer doesn't have to check if / is writable therefore parten
0762: // = dir and NOT parent = dir.getParentFile()
0763: //OK...
0764: //File parent=dir.getParentFile();
0765: File parent = new VAFile(dir);
0766: while (parent != null) {
0767: if (parent.exists()) {
0768: if (!parent.canWrite() && !IS_WIN)
0769: res = parent;
0770: else
0771: res = null;
0772: break;
0773: }
0774: parent = parent.getParentFile();
0775: }
0776: return res;
0777: }
0778:
0779: private void createShortcuts(File sharedDir) {
0780: int i, numEntries;
0781: LogInfo logInfo;
0782: Vector oldLog;
0783: PrintWriter log;
0784: Set shortcuts;
0785: Iterator scIt;
0786: String tempString;
0787: try {
0788: Vector oldlog = loadLogFile(new File(sharedDir
0789: .getAbsolutePath()
0790: + File.separator
0791: + "vai_"
0792: + VAGlobals.APP_NAME
0793: + "_"
0794: + VAGlobals.APP_VERSION
0795: + File.separator
0796: + "shortcuts.vai"));
0797: log = new PrintWriter(new FileWriter(sharedDir
0798: .getAbsolutePath()
0799: + File.separator
0800: + "vai_"
0801: + VAGlobals.APP_NAME
0802: + "_"
0803: + VAGlobals.APP_VERSION
0804: + File.separator
0805: + "shortcuts.vai"));
0806: logInfo = new LogInfo();
0807: logInfo.log = log;
0808: logInfo.oldlog = oldlog;
0809: scIt = oldlog.iterator();
0810: shortcuts = new java.util.LinkedHashSet();
0811: while (scIt.hasNext()) {
0812: shortcuts.add(scIt.next());
0813: }
0814: if (VAGlobals.CREATE_UNINSTALL_SHORTCUT) {
0815: if (VAGlobals.DEBUG)
0816: VAGlobals.printDebug("create uninstall shortcut");
0817: List entry = new ArrayList();
0818: if (launchparms != null)
0819: entry.addAll(Arrays.asList(launchparms));
0820: String exe = VAGlobals.DEST_PATH
0821: + System.getProperty("file.separator")
0822: + "uninstall_" + VAGlobals.APP_NAME + "_"
0823: + VAGlobals.APP_VERSION;
0824: if (IS_WIN)
0825: exe += ".bat";
0826: else if (IS_UNIX)
0827: exe += ".sh";
0828: VAShortcutEntry e = new VAShortcutEntry("Uninstall "
0829: + VAGlobals.APP_NAME, exe);
0830: e.setUninstall(true);
0831: e.setCreateOnDesktop(false);
0832: entry.add(e);
0833: launchparms = new VAShortcutEntry[entry.size()];
0834: entry.toArray(launchparms);
0835: }
0836: if (launchparms != null) {
0837: if (IS_UNIX) {
0838: VALinkLinux.createAll(this .launchparms, shortcuts);
0839:
0840: } else if (IS_WIN) {
0841: VALinkWindows.create(launchparms, sharedDir,
0842: installClassName_, shortcuts);
0843: } else {
0844: ui_.showError(new Exception(VAGlobals
0845: .i18n("Setup_SorryFeatureNotImplemented")));
0846: }
0847: }
0848: scIt = shortcuts.iterator();
0849: while (scIt.hasNext()) {
0850: tempString = (String) scIt.next();
0851: log.println(tempString);
0852: }
0853: logInfo.close();
0854: } catch (IOException e) {
0855: ui_.showError(e);
0856: }
0857: }
0858:
0859: private void startInstall(File sharedDir, UpgradeInfo uInfo) {
0860: VAInstallStep step = (VAInstallStep) step_;
0861: LogInfo logInfo = new LogInfo();
0862: ObjectInputStream infos = null;
0863: try {
0864: if (uInfo.upgrade)
0865: VAGlobals.printDebug("Upgrade mode");
0866: else
0867: VAGlobals.printDebug("Install mode");
0868: step.status(VAGlobals.i18n("Setup_ExtractingUninstaller"));
0869: File oldlogfile = extractUninstallFiles(sharedDir,
0870: uInfo.upgrade, uInfo.lastVersion());
0871: infos = new ObjectInputStream(getClass()
0872: .getResourceAsStream("archive_infos"));
0873: // open log of installed file names
0874: FileWriter filewrtr = new FileWriter(sharedDir
0875: .getAbsolutePath()
0876: + File.separator
0877: + "vai_"
0878: + VAGlobals.APP_NAME
0879: + "_"
0880: + VAGlobals.APP_VERSION
0881: + File.separator
0882: + "uninstall.vai");
0883: logInfo.log = new PrintWriter(filewrtr);
0884: logInfo.filewriter = filewrtr;
0885: // create directory for installed package
0886: File destPath = new File(computeLocalPath(""));
0887: step.details("D " + destPath);
0888: if ((!destPath.exists()) && (!destPath.mkdirs())) {
0889: throw new IOException(VAGlobals
0890: .i18n("Setup_NotCreateDirectory")
0891: + " " + destPath);
0892: }
0893: logInfo.log.println(destPath.getAbsolutePath());
0894: // custom pre-processing
0895: if (customPrePostClassName_.length() > 0)
0896: callPreCustom(logInfo.log, uInfo_, step);
0897: step.status(VAGlobals.i18n("Setup_DecompressingFiles"));
0898: int nbrFiles = infos.readInt();
0899: unzip(licenseKeySupport.decodeStream(getClass()
0900: .getResourceAsStream("archive.zip")), nbrFiles,
0901: sharedDir, uInfo.upgrade, logInfo, oldlogfile);
0902: step
0903: .status(VAGlobals
0904: .i18n("Setup_GeneratingLaunchScripts"));
0905: launchparms = generateLaunchScripts(infos, logInfo);
0906: ui_.uiSleep(2000);
0907: step.status(VAGlobals.i18n("Setup_LaunchScriptsGenerated"));
0908: ui_.uiSleep(1000);
0909: step.status(VAGlobals
0910: .i18n("Setup_GeneratingUninstallScript"));
0911: generateUninstallScripts(sharedDir, logInfo);
0912: ui_.uiSleep(2000);
0913: step.status(VAGlobals
0914: .i18n("Setup_UninstallScriptGenerated"));
0915: ui_.uiSleep(1000);
0916: if (IS_UNIX) {
0917: step.status(VAGlobals
0918: .i18n("Setup_RestoringExeAttributes"));
0919: restoreUnixExecutables(infos);
0920: ui_.uiSleep(2000);
0921: step.status(VAGlobals
0922: .i18n("Setup_ExeAttributesRestored"));
0923: ui_.uiSleep(1000);
0924: }
0925: if (IS_WIN) {
0926: step.status(VAGlobals
0927: .i18n("Setup_UpdatingWindowsRegistry"));
0928: updateWindowsRegistry(sharedDir, uInfo.upgrade, uInfo
0929: .lastVersion());
0930: ui_.uiSleep(2000);
0931: step.status(VAGlobals
0932: .i18n("Setup_WindowsRegistryUpdated"));
0933: ui_.uiSleep(1000);
0934: }
0935: if (customPrePostClassName_.length() > 0)
0936: callPostCustom(logInfo.log, uInfo_, step);
0937: logInfo.close();
0938: infos.close();
0939: infos = null;
0940: if (oldlogfile != null)
0941: copyShortcutLog(sharedDir, new File(oldlogfile
0942: .getParent(), "shortcuts.vai"));
0943: removeBackups();
0944: step.status(VAGlobals.i18n("Setup_InstallationComplete"));
0945: ui_.uiSleep(1000);
0946: } catch (Exception e) {
0947: System.err.println(e.getMessage()); // DEBUG
0948: e.printStackTrace(System.err);
0949: VAGlobals.printDebug("exception caught - closing logInfo"); //DEBUG
0950: if (logInfo != null) {
0951: logInfo.close();
0952: } else {
0953: VAGlobals.printDebug("logInfo is null!"); //DEBUG
0954: }
0955: if (infos != null) {
0956: try {
0957: infos.close();
0958: } catch (IOException iox) {
0959: VAGlobals.printDebug("exception closing infos "
0960: + iox.getMessage());
0961: //DEBUG
0962: }
0963: }
0964: exitOnError(e);
0965: } //catch
0966: }
0967:
0968: protected void exitOnError(Throwable e) {
0969: cleanInstall(sharedDir_, uInfo_);
0970: super .exitOnError(e);
0971: }
0972:
0973: private final void callPostCustom(PrintWriter filelog,
0974: UpgradeInfo uInfo, VAInstallStep step) throws Exception {
0975: boolean rc;
0976: VAGlobals.printDebug("begin custom post-install/upgrade");
0977: step.status(VAGlobals.i18n("Custom_Mopping..."));
0978:
0979: if (uInfo.upgrade)
0980: rc = customUpgrade("postUpgrade", filelog, uInfo
0981: .lastVersion(), step);
0982: else
0983: rc = customInstall("postInstall", filelog, step);
0984: if (!rc) {
0985: VAGlobals
0986: .printDebug("custom postInstall/postUpgrade returned false");
0987: throw new IOException(VAGlobals.i18n("Setup_PostFailed"));
0988: }
0989: VAGlobals.printDebug("end custom post-install/upgrade - OK");
0990: }
0991:
0992: private final void callPreCustom(PrintWriter filelog,
0993: UpgradeInfo uInfo, VAInstallStep step) throws Exception {
0994: VAGlobals.printDebug("begin custom pre-install/upgrade");
0995: step.status(VAGlobals.i18n("Custom_Preparing..."));
0996: boolean rc;
0997: if (uInfo.upgrade)
0998: rc = customUpgrade("preUpgrade", filelog, uInfo
0999: .lastVersion(), step);
1000: else
1001: rc = customInstall("preInstall", filelog, step);
1002: if (!rc) {
1003: VAGlobals
1004: .printDebug("custom preInstall/preUpgrade returned false");
1005: throw new IOException(VAGlobals.i18n("Setup_PreFailed"));
1006: }
1007: VAGlobals.printDebug("end custom pre-install/upgrade - OK");
1008: }
1009:
1010: // static for use by VAUninstall
1011: /** Call method that returns boolean. Unwrap InvocationTargetException. */
1012: public static final boolean callReflect(Method method, Object[] args)
1013: throws Exception {
1014: try {
1015: return ((Boolean) method.invoke(null, args)).booleanValue();
1016: } catch (InvocationTargetException tie) {
1017: throw ((Exception) tie.getTargetException());
1018: }
1019: }
1020:
1021: private final boolean customInstall(String method_name,
1022: PrintWriter filelog, VAInstallStep step) throws Exception {
1023: Class custom = Class.forName(customPrePostClassName_);
1024: Method method = custom.getMethod(method_name, new Class[] {
1025: PrintWriter.class, VAInstallStep.class });
1026: Object[] args = new Object[] { filelog, step };
1027: return callReflect(method, args);
1028: }
1029:
1030: private final boolean customUpgrade(String method_name,
1031: PrintWriter filelog, String last_version, VAInstallStep step)
1032: throws Exception {
1033: Class custom = Class.forName(customPrePostClassName_);
1034: Method method = custom.getMethod(method_name, new Class[] {
1035: String.class, PrintWriter.class, VAInstallStep.class });
1036: Object[] args = new Object[] { last_version, filelog, step };
1037: return callReflect(method, args);
1038: }
1039:
1040: private File findVAISharedDir() {
1041: File destPath = null;
1042: if (IS_ROOT) {
1043: if (IS_WIN) {
1044: // chercher dans la Regedit l'emplacement de C:\Program
1045: // Files\Common Files
1046: // ou C:\windows\Application Data
1047: // Key:
1048: // HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\CommonFilesDir
1049: try {
1050: RegistryKey sharedDirKey = Registry.HKEY_LOCAL_MACHINE
1051: .openSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion");
1052: destPath = new File(sharedDirKey
1053: .getStringValue("CommonFilesDir"));
1054: sharedDirKey.closeKey();
1055: } catch (Exception rex) {
1056: destPath = null;
1057: }
1058: } else if (IS_UNIX) {
1059: // on cherche /usr/share
1060: destPath = new File("/usr/share");
1061: if (!destPath.exists()) {
1062: // on cherche /opt/share
1063: destPath = new File("/opt/share");
1064: }
1065: }
1066: // if (!destPath.exists()) {
1067: // throw new IOException(VAGlobals
1068: // .i18n("Setup_CouldNotFindVAInstall"));
1069: // }
1070: if (destPath != null) {
1071: destPath = new File(destPath, "vainstall");
1072: if (!destPath.exists()) {
1073: destPath.mkdirs();
1074: }
1075: }
1076: }
1077: //this is created by default below
1078: // else { // user mode
1079: // if (IS_WIN) {
1080: // destPath = new File(System.getProperty("user.home")
1081: // + File.separator + "vainstall");
1082: // } else if (IS_UNIX) {
1083: // destPath = new File(System.getProperty("user.home")
1084: // + File.separator + ".vainstall");
1085: // }
1086: // if (!destPath.exists()) {
1087: // destPath.mkdirs();
1088: // }
1089: // }
1090: //if the dest path doesn't exist (USER MODE) or if the dest path can't
1091: // be written
1092: //we create the default one 'user.home/.vainstall'
1093: if ((destPath == null) || (!destPath.exists())
1094: || !new VAFile(destPath).canWrite()) {
1095: //root mode: warn that we use the default path
1096: if (IS_ROOT) {
1097: VAGlobals
1098: .printDebug("Could not find common dir in registry:");
1099: VAGlobals.printDebug("user.home/.vainstall");
1100: }
1101: destPath = new File(System.getProperty("user.home")
1102: + File.separator + ".vainstall");
1103: if (!destPath.exists())
1104: destPath.mkdirs();
1105: }
1106: return destPath;
1107: }
1108:
1109: private File extractUninstallFiles(File _destPath, boolean upgrade,
1110: String lastVer) throws IOException {
1111: boolean oldClassCopied = false;
1112: File destPath = new File(_destPath, "vai_" + VAGlobals.APP_NAME
1113: + "_" + VAGlobals.APP_VERSION);
1114: File oldlog = null;
1115: if (upgrade) {
1116: File lastVerPath = new File(_destPath, "vai_"
1117: + VAGlobals.APP_NAME + "_" + lastVer);
1118: // intended dest matches existing dest; rename existing dest
1119: if (destPath.equals(lastVerPath)) {
1120: File bkdir = new File(destPath.getAbsolutePath()
1121: + ".bak");
1122: if (!destPath.renameTo(bkdir)) {
1123: // Lame message, but I don't understand resource system
1124: throw new IOException(VAGlobals
1125: .i18n("Setup_NotCreateDirectory")
1126: + " " + destPath);
1127: } //destPath.rename
1128: oldlog = new File(bkdir.getAbsolutePath()
1129: + System.getProperty("file.separator")
1130: + "uninstall.vai");
1131: lastVerPath = bkdir;
1132: } else { //destPath.equals
1133: oldlog = new File(lastVerPath.getAbsolutePath()
1134: + System.getProperty("file.separator")
1135: + "uninstall.vai");
1136: }
1137: // create destination dir
1138: if ((!destPath.exists()) && (!destPath.mkdirs())) {
1139: throw new IOException(VAGlobals
1140: .i18n("Setup_NotCreateDirectory")
1141: + " " + destPath);
1142: }
1143: if (uInfo_.module)
1144: oldClassCopied = copyOldSetupClass(lastVerPath,
1145: destPath);
1146: } else { // not upgrade
1147: if ((!destPath.exists()) && (!destPath.mkdirs())) {
1148: throw new IOException(VAGlobals
1149: .i18n("Setup_NotCreateDirectory")
1150: + " " + destPath);
1151: }
1152: } // not upgrade
1153: dirty_ = true;
1154: // nettoyage
1155: File[] ls = destPath.listFiles(); // shouldn't be needed; kept to be
1156: // safe
1157: for (int i = 0; i < ls.length; i++) {
1158: if (!oldClassCopied)
1159: ls[i].delete();
1160: else if (!ls[i].getPath().equals(
1161: destPath.getAbsolutePath() + File.separator
1162: + installClassName_ + ".class"))
1163: ls[i].delete();
1164: }
1165: FileOutputStream out = null;
1166: FileInputStream in = null;
1167: byte[] buf = new byte[0];
1168: int read = 0;
1169: // Install class
1170: if (!oldClassCopied
1171: && (installClassSize_ > 0 || jarOffset_ > 0)) { // !=
1172: // jar
1173: // target
1174: out = new FileOutputStream(destPath.getAbsolutePath()
1175: + File.separator + installClassName_ + ".class");
1176: in = new FileInputStream(fileWithArchive_);
1177: if (installClassOffset_ > 0) {
1178: in.skip(installClassOffset_);
1179: }
1180: buf = new byte[0];
1181: if (installClassSize_ < 0)
1182: buf = new byte[(int) jarOffset_];
1183: else
1184: buf = new byte[(int) installClassSize_];
1185: read = in.read(buf, 0, buf.length);
1186: out.write(buf, 0, read);
1187: out.close();
1188: in.close();
1189: }
1190: // install.jar
1191: in = new FileInputStream(fileWithArchive_);
1192: if (jarOffset_ > 0) {
1193: in.skip(jarOffset_);
1194: }
1195: JarInputStream jar = new JarInputStream(in);
1196: JarOutputStream outJar = new JarOutputStream(
1197: new FileOutputStream(destPath.getAbsolutePath()
1198: + File.separator + "install.jar"));
1199: ZipEntry entry = jar.getNextEntry();
1200: while (entry != null) {
1201: String entryName = entry.getName();
1202: if (entryName
1203: .equals("com/memoire/vainstall/resources/vainstall.properties")) {
1204: // we handle this file special
1205: } else if (entryName.equals(installClassName_ + ".class")
1206: && !oldClassCopied) {
1207: // this is the install class which is inside the jar in jar
1208: // target
1209: // we have to extract it and put it in the uninstall dir
1210: out = new FileOutputStream(destPath.getAbsolutePath()
1211: + File.separator + installClassName_ + ".class");
1212: buf = new byte[2048];
1213: read = jar.read(buf);
1214: while (read > 0) {
1215: out.write(buf, 0, read);
1216: read = jar.read(buf);
1217: }
1218: out.close();
1219: } else if (!entryName.endsWith(".zip")) {
1220: // *******************
1221: buf = new byte[2048];
1222: ByteArrayOutputStream bos = new ByteArrayOutputStream();
1223: while (true) {
1224: read = jar.read(buf);
1225: if (read == -1)
1226: break;
1227: bos.write(buf, 0, read);
1228: }
1229: ByteArrayInputStream bais = new ByteArrayInputStream(
1230: bos.toByteArray());
1231: bos.close();
1232: // *******************
1233: ZipEntry outEntry = new ZipEntry(entryName);
1234: CRC32 crc = new CRC32();
1235: outEntry.setCrc(crc.getValue());
1236: // outEntry.setSize(entry.getSize());
1237: outEntry.setSize(bos.toByteArray().length);
1238: outJar.putNextEntry(outEntry);
1239: buf = new byte[512];
1240: read = bais.read(buf, 0, buf.length);
1241: while (read > 0) {
1242: crc.update(buf, 0, read);
1243: outJar.write(buf, 0, read);
1244: read = bais.read(buf, 0, buf.length);
1245: }
1246: bais.close();
1247: outEntry.setCrc(crc.getValue());
1248: outJar.closeEntry();
1249: }
1250: jar.closeEntry();
1251: entry = jar.getNextEntry();
1252: }
1253: // HFALK
1254: // get vainstall.properties
1255: InputStream pin = getClass().getResourceAsStream(
1256: "resources/vainstall.properties");
1257: Properties prop = new Properties();
1258: try {
1259: prop.load(pin);
1260: } catch (IOException exc) {
1261: // ignore at the moment
1262: }
1263: if (language == null)
1264: language = "default";
1265: prop.setProperty("vainstall.destination.language", language);
1266: ZipEntry outEntry = new ZipEntry(
1267: "com/memoire/vainstall/resources/vainstall.properties");
1268: CRC32 crc = new CRC32();
1269: outEntry.setCrc(crc.getValue());
1270: outEntry.setSize(prop.size());
1271: outJar.putNextEntry(outEntry);
1272: prop.store(outJar, VAGlobals.NAME + " " + VAGlobals.VERSION);
1273: outEntry.setCrc(crc.getValue());
1274: outJar.closeEntry();
1275: jar.close();
1276: outJar.close();
1277: in.close();
1278: return oldlog;
1279: }
1280:
1281: private VAShortcutEntry[] generateLaunchScripts(
1282: ObjectInputStream infos, LogInfo logInfo)
1283: throws IOException {
1284: VAInstallStep step = (VAInstallStep) step_;
1285: Vector scripts = null;
1286: try {
1287: scripts = (Vector) infos.readObject();
1288: } catch (ClassNotFoundException e) {
1289: ui_.showError(new Exception(VAGlobals
1290: .i18n("Setup_UnableToGenerateScripts")));
1291: return null;
1292: }
1293: if (scripts == null) {
1294: step.status(VAGlobals.i18n("Setup_NoLaunchScript"));
1295: VAGlobals.printDebug("No launch script");
1296: return null;
1297: }
1298: if (!IS_WIN && !IS_UNIX) {
1299: ui_.showError(new Exception(VAGlobals
1300: .i18n("Setup_SorryScriptsNotSupported")));
1301: return null;
1302: }
1303: String NL = System.getProperty("line.separator");
1304: ArrayList links = new ArrayList();
1305: for (int i = 0; i < scripts.size(); i++) {
1306: String sbloc = (String) scripts.get(i);
1307: StringTokenizer tk = new StringTokenizer(sbloc, "\n");
1308: if (tk.countTokens() % 2 != 0) {
1309: ui_.showError(new Exception(VAGlobals
1310: .i18n("Setup_UnSupportedScriptSkipping")));
1311: System.err.println("bad bloc= " + sbloc);
1312: return null;
1313: }
1314:
1315: Properties props = new Properties();
1316: while (tk.hasMoreTokens()) {
1317: props.put(tk.nextToken(), tk.nextToken());
1318: }
1319: //tk must not be used ...
1320: tk = null;
1321: final String cmd = props.getProperty("CMD");
1322: if ("JavaLauncher".equals(cmd) || "JarLauncher".equals(cmd)) {
1323: StringBuffer script = new StringBuffer(300);
1324: if (IS_WIN) {
1325: script.append("REM Launch script").append(NL)
1326: .append(NL);
1327: script.append("set VA_APP_HOME=").append(
1328: VAGlobals.DEST_PATH).append(
1329: File.separatorChar).append(NL);
1330: } else {
1331: script.append("#!/bin/sh").append(NL);
1332: script.append("### Launch Script for ").append(
1333: VAGlobals.APP_NAME).append(NL).append(NL);
1334: script.append("VA_APP_HOME=`dirname $0`")
1335: .append(NL);
1336: }
1337: //cd to the request directory
1338: String cdTo = props.getProperty(
1339: "ScriptChangeDirectory", null);
1340: if (cdTo != null) {
1341: File dirTo = new File(expandDirectory(cdTo, false,
1342: uInfo_));
1343: if (dirTo.exists()) {
1344: if (IS_UNIX) {
1345: script.append("cd ").append('"').append(
1346: dirTo.getAbsolutePath())
1347: .append('"').append(NL);
1348: } else {
1349: String s = dirTo.getAbsolutePath();
1350: int idx = s.indexOf(":");
1351: //to split 'C:My Path' to
1352: //C:
1353: //cd 'My Path'
1354: if (idx > 0) {
1355: script.append(s.substring(0, idx + 1))
1356: .append(NL);
1357: script.append("cd ").append('"')
1358: .append(s);
1359: if (s.endsWith(":"))
1360: script
1361: .append(File.pathSeparatorChar);
1362: script.append('"');
1363: } else
1364: script.append("cd ").append('"')
1365: .append(s).append('"');
1366: }
1367: script.append(NL);
1368: }
1369: }
1370: StringBuffer commandBuf = new StringBuffer();
1371: String javaMode = props.getProperty("JavaMode",
1372: "console");
1373: if ("console".equals(javaMode)) {
1374: commandBuf.append(JAVA_EXE_QUOTE).append(JAVA_EXE)
1375: .append(JAVA_EXE_QUOTE);
1376: } else {
1377: //we use the start command to close the dos frame just
1378: // after java is launched
1379: if (IS_WIN)
1380: commandBuf.append("start ");
1381: commandBuf.append(JAVA_EXE_QUOTE).append(JAVAW_EXE)
1382: .append(JAVA_EXE_QUOTE);
1383: }
1384: //java args
1385: String JavaArgs = props.getProperty("JavaArgs", "");
1386: StringTokenizer tokenizer = new StringTokenizer(
1387: JavaArgs, ",");
1388: while (tokenizer.hasMoreTokens())
1389: commandBuf.append(" ")
1390: .append(tokenizer.nextToken());
1391: //class path
1392: String classPath = props.getProperty("ClassPath", "");
1393: tokenizer = new StringTokenizer(classPath, ",");
1394: if (tokenizer.countTokens() > 0) {
1395: commandBuf.append(" -cp \"");
1396: int n = 0;
1397: while (tokenizer.hasMoreElements()) {
1398: String cp = "";
1399: if (n++ > 0)
1400: cp += File.pathSeparator;
1401: String cpi = tokenizer.nextToken();
1402: if (!new File(cpi).isAbsolute()) {
1403: if (IS_WIN)
1404: cp += "%VA_APP_HOME%";
1405: else
1406: cp += "$VA_APP_HOME/";
1407: }
1408:
1409: cp += cpi;
1410: if (File.separatorChar != '/')
1411: cp = cp.replace('/', File.separatorChar);
1412: commandBuf.append(cp);
1413: }
1414: commandBuf.append("\"");
1415: }
1416: String appArgs = "";
1417: if ("JavaLauncher".equals(cmd)) {
1418: String tkClass = props.getProperty("Class");
1419: commandBuf.append(" ").append(tkClass);
1420: appArgs = props.getProperty("ClassArgs", "");
1421: } else if ("JarLauncher".equals(cmd)) {
1422: String tkJar = props.getProperty("Jar");
1423: if (!new File(tkJar).isAbsolute()) {
1424: if (IS_WIN)
1425: tkJar = "%VA_APP_HOME%" + tkJar;
1426: else
1427: tkJar = "$VA_APP_HOME/" + tkJar;
1428: }
1429:
1430: commandBuf.append(" -jar \"").append(tkJar).append(
1431: '"');
1432: appArgs = props.getProperty("JarArgs", "");
1433: }
1434: tokenizer = new StringTokenizer(appArgs, ",");
1435: if (tokenizer.countTokens() > 0) {
1436: while (tokenizer.hasMoreTokens()) {
1437: commandBuf.append(" \"").append(
1438: tokenizer.nextToken()).append("\"");
1439: }
1440: }
1441: commandBuf = new StringBuffer(replace(commandBuf
1442: .toString(), "[INSTALL_DIR]", new File(
1443: VAGlobals.DEST_PATH).getAbsolutePath()));
1444: if (IS_WIN)
1445: commandBuf.append(" %1 %2 %3 %4 %5 %6 %7 %8 %9");
1446: else
1447: commandBuf.append(" $*");
1448:
1449: String sname = props.getProperty("ScriptName");
1450: String exe = computeLocalPath(sname);
1451: if (IS_WIN)
1452: exe += ".bat";
1453: else
1454: exe += ".sh";
1455: File sfile = new File(exe);
1456: step.details(sfile.getAbsolutePath());
1457: if (!logInfo.oldlog.contains(sfile.getAbsolutePath()))
1458: logInfo.log.println(sfile.getAbsolutePath());
1459: PrintWriter scriptFile = null;
1460: try {
1461: scriptFile = new PrintWriter(new FileWriter(sfile));
1462: script.append(commandBuf.toString());
1463: scriptFile.println(script.toString());
1464: } catch (IOException _e) {
1465: _e.printStackTrace();
1466: throw _e;
1467: } finally {
1468: if (scriptFile != null)
1469: scriptFile.close();
1470: }
1471:
1472: VAShortcutEntry e = new VAShortcutEntry(exe);
1473: String s = props.getProperty("CreateShortcutOnDesktop",
1474: null);
1475: if (s != null && s.toLowerCase().equals("false")) {
1476: e.setCreateOnDesktop(false);
1477: }
1478: if ("console".equals(javaMode))
1479: e.setLaunchInTerminal(true);
1480: e.setName(sname);
1481: links.add(e);
1482: }
1483: }
1484: VAShortcutEntry[] r = new VAShortcutEntry[links.size()];
1485: links.toArray(r);
1486: return r;
1487: }
1488:
1489: private void generateUninstallScripts(File destPath, LogInfo logInfo)
1490: throws IOException {
1491: VAInstallStep step = (VAInstallStep) step_;
1492: if (!IS_WIN && !IS_UNIX) {
1493: ui_.showError(new Exception(VAGlobals
1494: .i18n("Setup_NotGenerateScript")));
1495: return;
1496: }
1497: destPath = new File(destPath, "vai_" + VAGlobals.APP_NAME + "_"
1498: + VAGlobals.APP_VERSION);
1499: if ((!destPath.exists()) && (!destPath.mkdirs())) {
1500: ui_.showError(new Exception(VAGlobals
1501: .i18n("Setup_NotCreateDirectory")
1502: + " " + destPath));
1503: return;
1504: }
1505: // nettoyage
1506: File[] ls = new File(VAGlobals.DEST_PATH)
1507: .listFiles(new SetupFileFilter("uninstall_"
1508: + VAGlobals.APP_NAME + "_",
1509: SetupFileFilter.STARTS_WITH,
1510: SetupFileFilter.FILTER));
1511: for (int i = 0; i < ls.length; i++) {
1512: ls[i].delete();
1513: }
1514: String sname = "uninstall_" + VAGlobals.APP_NAME + "_"
1515: + VAGlobals.APP_VERSION;
1516: sname = computeLocalPath(sname);
1517: if (IS_WIN)
1518: sname += ".bat";
1519: else
1520: sname += ".sh";
1521: step.details(sname);
1522: File sfile = new File(sname);
1523: PrintWriter script = new PrintWriter(new FileWriter(sfile));
1524: logInfo.log.println(sfile.getAbsolutePath());
1525: if (IS_WIN) {
1526: script.println("REM Uninstall script for "
1527: + VAGlobals.APP_NAME + "_" + VAGlobals.APP_VERSION);
1528: script.println();
1529: } else {
1530: script.println("#!/bin/sh");
1531: script.println("### Uninstall script for "
1532: + VAGlobals.APP_NAME + "_" + VAGlobals.APP_VERSION);
1533: script.println();
1534: script.println("stty -icanon");
1535: }
1536: String scriptStr = "";
1537: scriptStr += JAVA_EXE_QUOTE + JAVA_EXE + JAVA_EXE_QUOTE
1538: + " -cp \"" + destPath.getAbsolutePath() + "\" ";
1539: if ("no".equalsIgnoreCase(System.getProperty("bluescreen")))
1540: scriptStr += "-Dbluescreen=no ";
1541: scriptStr += installClassName_ + " uninstall \""
1542: + destPath.getAbsolutePath() + "\"";
1543: script.println(scriptStr);
1544: if (IS_UNIX) {
1545: script.println("stty icanon");
1546: }
1547: script.close();
1548: }
1549:
1550: private Vector loadLogFile(File _logfile) throws IOException {
1551: Vector res = new Vector();
1552: if (_logfile == null)
1553: return res;
1554: File logfile = _logfile;
1555: if (logfile != null && logfile.exists()) {
1556: LineNumberReader log = new LineNumberReader(new FileReader(
1557: logfile));
1558: String line = log.readLine();
1559: while (line != null) {
1560: res.add(line);
1561: line = log.readLine();
1562: }
1563: log.close();
1564: } else {
1565: VAGlobals.printDebug("log not found"
1566: + _logfile.getAbsoluteFile());
1567: }
1568: return res;
1569: }
1570:
1571: private void unzip(InputStream archive, int nbrFiles,
1572: File sharedDir, boolean upgrade, LogInfo logInfo,
1573: File oldlogfile) throws IOException {
1574: VAInstallStep step = (VAInstallStep) step_;
1575: int count = 0;
1576: ZipInputStream zip = new ZipInputStream(new GZIPInputStream(
1577: archive));
1578: step.setProgression(0);
1579: dirty_ = true;
1580: Vector oldlog = loadLogFile(oldlogfile);
1581: PrintWriter log = logInfo.log;
1582: logInfo.oldlog = oldlog;
1583: for (int i = 1; i < oldlog.size(); i++) {
1584: String sentry = (String) oldlog.get(i);
1585: String s = sentry;
1586: int ind = sentry.lastIndexOf(File.separator);
1587: if (ind > 0)
1588: s = sentry.substring(ind + 1);
1589: if (!s.startsWith("uninstall_" + VAGlobals.APP_NAME + "_"))
1590: log.println(sentry);
1591: }
1592: ZipEntry entry = zip.getNextEntry();
1593: while (entry != null) {
1594: File dest = new File(computeLocalPath(entry.getName()));
1595: if (entry.isDirectory()) {
1596: if (!dest.exists()) {
1597: makeDirs(dest, logInfo);
1598: //zip.closeEntry(); close at line 1733
1599: }
1600: } else {
1601: File destParent = dest.getParentFile();
1602: if ((destParent != null) && (!destParent.exists())) {
1603: makeDirs(destParent, logInfo);
1604: }
1605: step.details("F " + dest);
1606: boolean fileExist = dest.exists();
1607: if (fileExist && !dest.canWrite()) {
1608: ui_.showError(new Exception(VAGlobals
1609: .i18n("Setup_DirectoryNotAccessible")
1610: + "\n" + dest.getAbsolutePath()));
1611: }
1612:
1613: FileOutputStream out = null;
1614: try {
1615: out = new FileOutputStream(dest);
1616: byte buf[] = new byte[4096];
1617: int read = zip.read(buf, 0, buf.length);
1618: while (read > 0) {
1619: out.write(buf, 0, read);
1620: read = zip.read(buf, 0, buf.length);
1621: }
1622: } catch (FileNotFoundException _e) {
1623:
1624: } catch (IOException _e) {
1625: if (fileExist)
1626: ui_.showError(new Exception(VAGlobals
1627: .i18n("Setup_FileNotCreated")
1628: + "\n" + dest.getName()));
1629: else
1630: throw _e;
1631: return;
1632: } finally {
1633: if (out != null)
1634: out.close();
1635: }
1636: stats_.addFile(dest, VAStats.SUCCESS);
1637: if (!oldlog.contains(dest.getAbsolutePath()))
1638: log.println(dest.getAbsolutePath());
1639: step.setProgression(++count * 100 / nbrFiles);
1640: }
1641:
1642: zip.closeEntry();
1643: entry = zip.getNextEntry();
1644: }
1645: zip.close();
1646: }
1647:
1648: private void makeDirs(File dir, LogInfo logInfo) throws IOException {
1649: VAInstallStep step = (VAInstallStep) step_;
1650: File parent = dir.getParentFile();
1651: if ((parent != null) && (!parent.exists())) {
1652: makeDirs(parent, logInfo);
1653: }
1654: step.details("D " + dir);
1655: if (dir.mkdir()) {
1656: stats_.addDirectory(dir, VAStats.SUCCESS);
1657: if (!logInfo.oldlog.contains(dir.getAbsolutePath()))
1658: logInfo.log.println(dir.getAbsolutePath());
1659: } else {
1660: throw new IOException(VAGlobals
1661: .i18n("Setup_NotCreateDirectory")
1662: + " " + dir);
1663: }
1664: }
1665:
1666: protected void removeBackups() {
1667: File destPath = new File(sharedDir_, "vai_"
1668: + VAGlobals.APP_NAME + "_" + VAGlobals.APP_VERSION);
1669: if ((uInfo_ == null) || (!uInfo_.upgrade))
1670: return;
1671: File lastVerPath = new File(sharedDir_, "vai_"
1672: + VAGlobals.APP_NAME + "_" + uInfo_.lastVersion());
1673: if (destPath.equals(lastVerPath)) {
1674: lastVerPath = new File(destPath.getAbsolutePath() + ".bak");
1675: }
1676: if (lastVerPath.exists())
1677: deleteDirRecursive(lastVerPath);
1678: }
1679:
1680: protected void updateWindowsRegistry(File sharedDir,
1681: boolean upgrade, String lastVer) throws IOException {
1682: File destPath = new File(sharedDir.getAbsolutePath()
1683: + File.separator + "vai_" + VAGlobals.APP_NAME + "_"
1684: + VAGlobals.APP_VERSION);
1685: if (upgrade) {
1686: try {
1687: RegistryKey uninstallKey = Registry.HKEY_LOCAL_MACHINE
1688: .openSubKey(
1689: "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
1690: RegistryKey.ACCESS_WRITE);
1691: uninstallKey.deleteSubKey(VAGlobals.APP_NAME + " "
1692: + lastVer);
1693: uninstallKey.closeKey();
1694: } catch (Exception e) {
1695: ui_.showError(new Exception(VAGlobals
1696: .i18n("Setup_NotDeleteRegistryKey")
1697: + " " + VAGlobals.APP_NAME + " " + lastVer));
1698: }
1699: }
1700: try {
1701: RegistryKey newKey = Registry.HKEY_LOCAL_MACHINE
1702: .createSubKey(
1703: "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"
1704: + VAGlobals.APP_NAME + " "
1705: + VAGlobals.APP_VERSION, "",
1706: RegistryKey.ACCESS_WRITE);
1707: RegStringValue displayName = new RegStringValue(newKey,
1708: "DisplayName");
1709: displayName.setData(VAGlobals.APP_NAME + " "
1710: + VAGlobals.APP_VERSION);
1711: newKey.setValue(displayName);
1712: RegStringValue uninstallString = new RegStringValue(newKey,
1713: "UninstallString");
1714: String scriptStr = "";
1715: scriptStr += JAVA_EXE_QUOTE + JAVA_EXE + JAVA_EXE_QUOTE
1716: + " -cp \"" + destPath.getAbsolutePath() + "\" ";
1717: if ("no".equalsIgnoreCase(System.getProperty("bluescreen")))
1718: scriptStr += "-Dbluescreen=no ";
1719: scriptStr += installClassName_ + " uninstall \""
1720: + destPath.getAbsolutePath() + "\"";
1721: uninstallString.setData(scriptStr);
1722: newKey.setValue(uninstallString);
1723: newKey.closeKey();
1724: } catch (Exception e) {
1725: throw new IOException("" + e);
1726: }
1727: }
1728:
1729: /**
1730: * 0 : not known 1 : java in path -1: not in path Comment for
1731: * <code>isJavaInPath</code>
1732: */
1733: static int isJavaInPath = 0;
1734:
1735: /** @parm winsuffix should be either .exe or w.exe. */
1736: private static final String getJavaExe(String winsuffix) {
1737: StringBuffer sb = new StringBuffer(260);
1738: //fred we test if the the java executable is in path
1739: //if false, we use the full path to the exe
1740: if (isJavaInPath == 0) {
1741: if (VAGlobals.USE_FULL_JAVA_PATH) {
1742: isJavaInPath = -1;
1743: }
1744: //On MS Windows use full path for Java version
1745: // 1.2 or less
1746: else if (IS_WIN
1747: && ("1.2".compareTo(System.getProperty(
1748: "java.version").substring(0, 3)) > -1)) {
1749: isJavaInPath = -1;
1750: } else {
1751: try {
1752: Runtime.getRuntime().exec(
1753: new String[] { "java", "-version" });
1754: isJavaInPath = 1;
1755: } catch (IOException _e) {
1756: isJavaInPath = -1;
1757: }
1758: if (VAGlobals.DEBUG) {
1759: if (isJavaInPath > 0)
1760: VAGlobals.printDebug("java is in path");
1761: else
1762: VAGlobals.printDebug("java is NOT in path");
1763: }
1764:
1765: }
1766: }
1767:
1768: if (isJavaInPath < 0) {
1769: sb.append(JAVA_HOME).append(File.separator).append("bin")
1770: .append(File.separator);
1771: }
1772: sb.append("java").append(IS_WIN ? winsuffix : "");
1773: return sb.toString();
1774: }
1775:
1776: /**
1777: * Replace parts of a string.
1778: *
1779: * @param _s
1780: * the initial string
1781: * @param _a
1782: * the string to be found
1783: * @param _b
1784: * the string which will replace
1785: * @return the modified string
1786: */
1787: public static final String replace(String _s, String _a, String _b) {
1788: String r = _s;
1789: int i = 0;
1790:
1791: if (_b == null)
1792: _b = "";
1793:
1794: while ((i = r.indexOf(_a, i)) >= 0) {
1795: r = r.substring(0, i) + _b + r.substring(i + _a.length());
1796: i = i + _b.length();
1797: }
1798: return r;
1799: }
1800:
1801: private void restoreUnixExecutables(ObjectInputStream infos) {
1802: VAInstallStep step = (VAInstallStep) step_;
1803: try {
1804: Vector exes = (Vector) infos.readObject();
1805: String[] chmodStr = new String[4 + exes.size()];
1806: chmodStr[0] = "chmod";
1807: chmodStr[1] = "-R";
1808: chmodStr[2] = "a+x";
1809: chmodStr[3] = "";
1810: if (IS_WIN || IS_UNIX) {
1811: chmodStr[3] += computeLocalPath("uninstall_"
1812: + VAGlobals.APP_NAME + "_"
1813: + VAGlobals.APP_VERSION);
1814: if (IS_WIN)
1815: chmodStr[3] += ".bat";
1816: else
1817: chmodStr[3] += ".sh";
1818: }
1819: for (int i = 0; i < exes.size(); i++) {
1820: String exe = (String) exes.get(i);
1821: if (exe.startsWith("[SCRIPT]")) {
1822: exe = exe.substring(8);
1823: if (IS_WIN)
1824: exe += ".bat";
1825: else
1826: exe += ".sh";
1827: }
1828: chmodStr[4 + i] = computeLocalPath(exe);
1829: step.details(exe);
1830: stats_.addExecutable(new File(chmodStr[4 + i]));
1831: }
1832: String chmod = "";
1833: for (int i = 0; i < chmodStr.length; i++) {
1834: chmod += chmodStr[i] + " ";
1835: }
1836: VAGlobals.printDebug(chmod);
1837: // PrintStream out=System.out;
1838: // PrintStream err=System.err;
1839: // PrintStream nullStream=new PrintStream(new
1840: // ByteArrayOutputStream());
1841: // System.setErr(nullStream);
1842: // System.setOut(nullStream);
1843: Process p = Runtime.getRuntime().exec(chmodStr);
1844: boolean interr = true;
1845: while (interr) {
1846: try {
1847: p.waitFor();
1848: interr = false;
1849: } catch (InterruptedException e) {
1850: interr = true;
1851: }
1852: }
1853: // nullStream.close();
1854: // System.setErr(err);
1855: // System.setOut(out);
1856: if (p.exitValue() != 0)
1857: throw new Exception();
1858: } catch (Exception e) {
1859: VAGlobals.printDebug(e.toString());
1860: ui_.showError(new Exception(VAGlobals
1861: .i18n("Setup_ErrorRestoringExeAttributes")));
1862: }
1863: }
1864:
1865: public static String getWithoutMacro(String dir) {
1866: if (dir == null)
1867: return "";
1868: if (!dir.startsWith("["))
1869: return dir;
1870: int index = dir.indexOf(']');
1871: return dir.substring(index + 1);
1872: }
1873:
1874: public static String expandDirectory(String dir, boolean block,
1875: UpgradeInfo uInfo) throws IOException {
1876: String res = null;
1877: if (!dir.startsWith("["))
1878: res = dir;
1879: else {
1880: int index = dir.indexOf(']');
1881: String prefix = dir.substring(1, index).trim();
1882: // we'll try to handle lookup of environment varibles here. The
1883: // syntax is
1884: // [ENV{<name of environment variable}]
1885: // CVDL Debug
1886: /*
1887: * VAGlobals.printDebug("Trying to resolve a shortcut...");
1888: * //ENVIRONMENT VARIABLE if (prefix.startsWith("ENV")) {
1889: * VAGlobals.printDebug("Trying to resolve ENV (original: " +
1890: * prefix); int startOfEnvVariable = prefix.indexOf('{'); int
1891: * endOfEnvVariable = prefix.indexOf('}'); if (startOfEnvVariable >=
1892: * 0 && endOfEnvVariable > startOfEnvVariable + 1) { String
1893: * variableName = prefix.substring(startOfEnvVariable + 1,
1894: * endOfEnvVariable); try { // CVDL Debug
1895: * VAGlobals.printDebug("Trying to get environment variable for " +
1896: * variableName); res =
1897: * ReadEnv.getEnvVars().getProperty(variableName);
1898: * VAGlobals.printDebug("Resovled " + variableName + " to " + res); }
1899: * catch (Throwable e) { VAGlobals.printDebug("received exception " +
1900: * e); throw new IOException(e.getMessage()); } if (res == null) {
1901: * VAGlobals.printDebug("resolved path is null -> exception"); throw
1902: * new IOException("Cannot find variable named " + variableName); } }
1903: * else { new IllegalStateException("Cannot parse the destination
1904: * path!"); } } else
1905: */
1906: //END
1907: if ("HOME".equals(prefix)) {
1908: res = System.getProperty("user.home");
1909: if (IS_WIN && IS_ROOT) {
1910: if ((res != null) && (res.length() > 3)
1911: && (res.charAt(1) == ':'))
1912: res = res.substring(0, 3);
1913: }
1914: }
1915: if ("INSTALL_DIR".equals(prefix)) {
1916: res = new File(VAGlobals.DEST_PATH).getAbsolutePath();
1917: } else if ("C:".equals(prefix)) {
1918: if (IS_WIN)
1919: res = "C:\\";
1920: else if (IS_UNIX)
1921: res = "/";
1922: else if (IS_MAC)
1923: res = ":";
1924: } else if ("PROGRAM".equals(prefix)) {
1925: if (!IS_ROOT) {
1926: res = System.getProperty("user.home");
1927: } else {
1928: if (IS_WIN) {
1929: try {
1930: RegistryKey programDirKey = Registry.HKEY_LOCAL_MACHINE
1931: .openSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion");
1932: res = programDirKey
1933: .getStringValue("ProgramFilesDir");
1934: programDirKey.closeKey();
1935: } catch (Exception re) {
1936: if (block)
1937: throw new IOException(re.getMessage());
1938: res = null;
1939: }
1940: if (res == null) {
1941: VAGlobals
1942: .printDebug("Could not find program dir in registry:");
1943: VAGlobals
1944: .printDebug("using 'C:\\Program Files'");
1945: res = "C:\\Program Files";
1946: }
1947: } else if (IS_UNIX)
1948: res = "/usr/local";
1949: }
1950: } else if ("UPDATE".equals(prefix)) {
1951: if (uInfo.versions == null) {
1952: uInfo.forceUpgrade = true;
1953: return dir;
1954: } else
1955: res = uInfo.lastPath().getAbsolutePath();
1956: } else if ("MODULE".equals(prefix)) {
1957: if (uInfo.versions == null) {
1958: uInfo.forceUpgrade = true;
1959: uInfo.module = true;
1960: return dir;
1961: } else
1962: res = uInfo.lastPath().getAbsolutePath();
1963: } else if ((prefix.length() == 2) && (prefix.endsWith(":"))) {
1964: if (IS_WIN)
1965: res = prefix.substring(0, 2) + File.separator;
1966: else if (IS_UNIX)
1967: res = "/";
1968: }
1969: String suffix = "";
1970: if (dir.length() > (index + 1)) {
1971: suffix += dir.substring(index + 1).trim();
1972: if (suffix.startsWith("/"))
1973: suffix = suffix.substring(1);
1974: }
1975: if (res == null) {
1976: if (block)
1977: throw new IOException(VAGlobals
1978: .i18n("Setup_InvalidDir")
1979: + dir);
1980: res = System.getProperty("user.dir");
1981: }
1982: res += (File.separator + suffix);
1983: }
1984: return res;
1985: }
1986:
1987: private String computeLocalPath(String entry) throws IOException {
1988: String res = expandDirectory(entry, false, uInfo_);
1989: if (res == null)
1990: throw new IOException(VAGlobals.i18n("Setup_InvalidPath")
1991: + entry);
1992: if ((!res.startsWith("/")) && (!res.startsWith("C:\\"))
1993: && (!res.startsWith(":"))
1994: && (!new File(res).isAbsolute()))
1995: res = VAGlobals.DEST_PATH + "/" + res;
1996: return res.replace('/', File.separatorChar);
1997: }
1998:
1999: private void copyShortcutLog(File sharedDir, File oldShortcutLogFile)
2000: throws IOException {
2001: Vector oldlog = loadLogFile(oldShortcutLogFile);
2002: FileWriter filewrtr = new FileWriter(sharedDir
2003: .getAbsolutePath()
2004: + File.separator
2005: + "vai_"
2006: + VAGlobals.APP_NAME
2007: + "_"
2008: + VAGlobals.APP_VERSION
2009: + File.separator
2010: + "shortcuts.vai");
2011: PrintWriter log = new PrintWriter(filewrtr);
2012: for (int i = 0; i < oldlog.size(); i++) {
2013: String sentry = (String) oldlog.get(i);
2014: String s = sentry;
2015: log.println(sentry);
2016: }
2017: log.flush();
2018: log.close();
2019: }
2020:
2021: private boolean copyOldSetupClass(File lastVerPath, File destPath)
2022: throws java.io.FileNotFoundException, IOException {
2023: byte[] buf;
2024: File oldClass = new File(lastVerPath.getAbsolutePath()
2025: + File.separator + installClassName_ + ".class");
2026: if (oldClass.exists()) {
2027: FileOutputStream out = new FileOutputStream(destPath
2028: .getAbsolutePath()
2029: + File.separator + installClassName_ + ".class");
2030: FileInputStream in = new FileInputStream(oldClass);
2031: buf = new byte[(new Long(oldClass.length())).intValue()];
2032: int read = in.read(buf, 0, buf.length);
2033: out.write(buf, 0, read);
2034: out.close();
2035: in.close();
2036: return true;
2037: } else
2038: return false;
2039: }
2040: }
2041:
2042: class UpgradeInfo {
2043: public boolean module = false;
2044:
2045: public boolean forceUpgrade = false;
2046:
2047: public boolean upgrade = false;
2048:
2049: public String[] versions = null;
2050:
2051: public File[] paths = null;
2052:
2053: public String lastVersion() {
2054: return versions == null ? null : versions[versions.length - 1];
2055: }
2056:
2057: public File lastPath() {
2058: return paths == null ? null : paths[paths.length - 1];
2059: }
2060: }
2061:
2062: class LogInfo {
2063: public PrintWriter log = null;
2064:
2065: public FileWriter filewriter = null;
2066:
2067: public Vector oldlog = null;
2068:
2069: public void close() {
2070: if (log == null) {
2071: if (VAGlobals.DEBUG)
2072: System.out.println("log file null");
2073: } else {
2074: log.flush();
2075: log.close();
2076: }
2077: try {
2078: if (filewriter != null)
2079: filewriter.close();
2080: } catch (IOException e) {
2081: VAGlobals.printDebug("LogInfo.close IOException: "
2082: + e.getMessage());
2083: }
2084: if (oldlog != null)
2085: oldlog.clear();
2086: oldlog = null;
2087: }
2088: }
2089:
2090: class SetupFileFilter implements FilenameFilter {
2091: public final static int STARTS_WITH = 1;
2092:
2093: public final static int ENDS_WITH = 2;
2094:
2095: public final static int FILTER = 1;
2096:
2097: public final static int EXCEPT = 2;
2098:
2099: private String pattern_;
2100:
2101: private int mode_;
2102:
2103: private int filter_;
2104:
2105: public SetupFileFilter(String patt, int mode, int filter) {
2106: pattern_ = patt;
2107: mode_ = mode;
2108: filter_ = filter;
2109: }
2110:
2111: public boolean accept(File dir, String f) {
2112: boolean res = true;
2113: if (mode_ == STARTS_WITH)
2114: res = f.startsWith(pattern_);
2115: else if (mode_ == ENDS_WITH)
2116: res = f.endsWith(pattern_);
2117: if (filter_ == EXCEPT)
2118: res = !res;
2119: return res;
2120: }
2121: }
2122: //DONT WORK WITH JAVAW
2123: // class ReadEnv {
2124: // public static Properties getEnvVars() throws Throwable {
2125: // Process p = null;
2126: // Properties envVars = new Properties();
2127: // Runtime r = Runtime.getRuntime();
2128: // String OS = System.getProperty("os.name").toLowerCase();
2129: // // System.out.println(OS);
2130: // if (OS.indexOf("windows 9") > -1) {
2131: // p = r.exec( "command.com /c set" );
2132: // }
2133: // else if ( (OS.indexOf("nt") > -1) || (OS.indexOf("windows 2000") > -1) ) {
2134: // p = r.exec( "cmd.exe /c set" );
2135: // }
2136: // else {
2137: // // our last hope, we assume Unix (thanks to H. Ware for the fix)
2138: // p = r.exec( "env" );
2139: // }
2140: // BufferedReader br = new BufferedReader
2141: // ( new InputStreamReader( p.getInputStream() ) );
2142: // String line;
2143: // while( (line = br.readLine()) != null ) {
2144: // int idx = line.indexOf( '=' );
2145: // String key = line.substring( 0, idx );
2146: // String value = line.substring( idx+1 );
2147: // envVars.setProperty( key, value );
2148: // // System.out.println( key + " = " + value );
2149: // }
2150: // return envVars;
2151: // }
2152: // }
2153: //
|