📄 msipackagegenerator.java
字号:
/* * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is * subject to license terms. * * This program is free software; you can redistribute it and/or modify * it under the terms of the Lesser GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */ package org.jdesktop.jdic.packager.impl;import java.io.IOException;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.TreeMap;import java.util.ArrayList;/** * Concrete implementation of interface PackageGenerator for msi packages * generating on Windows. */public class MsiPackageGenerator implements PackageGenerator { /** * The jar file containg these classes. */ private static final String SYS_JAR_FILE_NAME = "packager.jar"; /** * Jar file name for packaging all the jnlp relevant files. */ private static final String JNLP_FILES_JAR_NAME = "JnlpFiles.jar"; /** * Miscellanious MSI generatation needed files location in packager.jar. */ private static final String MSI_SUPPORT_FILES_HIERARCHY_PATH = "org/jdesktop/jdic/packager/impl/files/"; /** * The relevant path of makecab.exe in MSSDKDir. */ private static final String MAKECAB_EXE_PATH = "Samples\\sysmgmt\\msi\\Patching\\makecab.exe"; /** * The relevant path of msidb.exe in MSSDKDir. */ private static final String MSIDB_EXE_PATH = "bin\\msidb.exe"; /** * Bootstrapper file path. */ private static final String SYS_BOOTSTRAPPER_FILE_PATH = MSI_SUPPORT_FILES_HIERARCHY_PATH + "bootstrapper.exe"; /** * Custome dll file name. */ private static final String CUSTOM_DLL_FILE_NAME = "custom.dll"; /** * The text table containing the localized welcome message. */ private static final String LOCALIZED_WELCOME_MSG_FILE_NAME = "WelcomeMsg.idt"; /** * The name of the table containing the localized welcome message. */ public static final String LOCALIZED_WELCOME_MSG_TABLE_NAME = "WelcomeMsg"; /** * Default panel jpeg file name. */ private static final String PANEL_JPG_FILE_NAME = "panel.jpg"; /** * Banner jpeg file name. */ private static final String BANNER_JPG_FILE_NAME = "banner.jpg"; /** * Property for Java home directory. */ private static final String JAVA_HOME_DIR_PROPERTY = "java.home"; /** * Property for java class path. */ private static final String SYS_CLASS_PATH = "java.class.path"; /** * Property for author info. */ private static final String AUTHOR_INFO = "http://jdic.dev.java.net"; /** * Value index of field Text, table Control. */ private static final int VI_CONTROL_TEXT = 10; /** * Value index of field Argument, table ControlEvent. */ private static final int VI_CONTROLEVENT_ARGUMENT = 4; /** * Value index of field Event, table ControlEvent. */ private static final int VI_CONTROLEVENT_EVENT = 3; /** * Value index of field Value, table Property. */ private static final int VI_PROPERTY_VALUE = 2; /** * Property ID of property Revision Number, table Summary Info. */ private static final int PID_REVISION_NUMBER = 9; /** * Property ID of property Author, table Summary Info. */ private static final int PID_AUTHOR = 4; /** * Full path of file packager.jar. */ private static String packagerJarFilePath = null; /** * MsiPackageGenerator creator. * */ public MsiPackageGenerator() { } /** * Gets the location of packager.jar. * * @throws IOException If failed to get the location of packager.jar. */ private static void getPackagerJarFilePath() throws IOException { String sysClassPath = System.getProperty(SYS_CLASS_PATH); int fileNameIndex = sysClassPath.indexOf(SYS_JAR_FILE_NAME); int filePathIndex = -1; int i = fileNameIndex; char indexChar; while (i > 0) { i--; indexChar = sysClassPath.charAt(i); if ((indexChar == ';') || (i == 0)) { filePathIndex = i; break; } } if (filePathIndex > 0) { packagerJarFilePath = sysClassPath.substring( filePathIndex + 1, fileNameIndex + SYS_JAR_FILE_NAME.length()); } else if (filePathIndex == 0) { packagerJarFilePath = sysClassPath.substring( filePathIndex, fileNameIndex + SYS_JAR_FILE_NAME.length()); } else { throw new IOException( "Could not locate " + SYS_JAR_FILE_NAME + " in system CLASSPATH setting!"); } } /** * Modify template msi according to ShowLicense or not. * @param msiFilePath The given template msi file. * @param pkgInfo JnlpPackageInfo object. * @param localeIndex The index of the given locale. * @throws IOException If failed to modify the template msi file. */ private static void adjustMsiLicenseFields( String msiFilePath, JnlpPackageInfo pkgInfo, int localeIndex) throws IOException { TreeMap treeMap = new TreeMap(); File licenseFile; FileInputStream fis; //Set the license info field property if (pkgInfo.getShowLicense()) { try { String realLicenseInfo = new String(); realLicenseInfo += "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deftab720{\\fonttbl{\\f0\\" + "froman\\fprq2 Times New Roman;}}{\\colortbl\\red0\\green0\\" + "blue0;} \\deflang1033\\horzdoc{\\*\\fchars }{\\*\\lchars }\\" + "pard\\plain\\f0\\fs20"; // Get info from file licenseFile = new File( pkgInfo.getLicenseDirPath() + File.separator + JnlpConstants.LOCALES[localeIndex] + ".txt"); if (!licenseFile.exists()) { licenseFile = new File( pkgInfo.getLicenseDirPath() + File.separator + JnlpConstants.LOCALES[JnlpConstants.LOCALE_EN] + ".txt"); } fis = new FileInputStream(licenseFile); int fLen = (int) licenseFile.length(); byte[] bits = new byte[fLen]; fis.read(bits, 0, fLen); fis.close(); realLicenseInfo += new String(bits); realLicenseInfo += "\\par }"; treeMap.clear(); treeMap.put("AgreementText", realLicenseInfo); WinMsiUtility.winMsiSetProperty( msiFilePath, "Control", "Control", VI_CONTROL_TEXT, false, treeMap); } catch (IOException e) { throw new IOException( "License Agreement: Modify Control Table failed: " + "Set MSI LicenseAgreement field property failed!"); } try { treeMap.clear(); treeMap.put("AfterWelcomeDlg", "LicenseAgreementDlg"); WinMsiUtility.winMsiSetProperty( msiFilePath, "ControlEvent", "Argument", VI_CONTROLEVENT_ARGUMENT, false, treeMap); } catch (IOException e) { throw new IOException( "License Agreement: Modify ControlEvent Table failed: " + "Insert License Agreement dialog failed!"); } } else { try { treeMap.clear(); treeMap.put("AfterWelcomeDlg", "EndDialog"); WinMsiUtility.winMsiSetProperty( msiFilePath, "ControlEvent", "Argument", VI_CONTROLEVENT_EVENT, false, treeMap); } catch (IOException e) { throw new IOException( "No License Agreement: Modify ControlEvent Table failed: " + "AfterWelcomeDlg/EndDialog"); } try { treeMap.clear(); treeMap.put("AfterWelcomeDlg", "Return"); WinMsiUtility.winMsiSetProperty( msiFilePath, "ControlEvent", "Argument", VI_CONTROLEVENT_ARGUMENT, false, treeMap); } catch (IOException e) { throw new IOException( "No License Agreement: Modify ControlEvent Table failed: " + "AfterWelcomeDlg/Return"); } } //Setting proper welcome message for the control table. //Try to get the welcomemsg for current locole. String welcomeMsg = WinMsiUtility.getWelcomeMsg( msiFilePath, JnlpConstants.LOCALES[localeIndex], pkgInfo.getShowLicense()); try { treeMap.clear(); treeMap.put("SunButton", "[ButtonText_Next]"); // get relavant info from file treeMap.put( "WelcomeDescription", welcomeMsg); WinMsiUtility.winMsiSetProperty( msiFilePath, "Control", "Text", VI_CONTROL_TEXT, false, treeMap); } catch (IOException e) { throw new IOException( "Handling Agreement: Modify Control Table failed: Next " + "Button/WelcomeDescription"); } } /** * Customize the non-localized MSI fields based on the JnlpPackageInfo. * @param msiFilePath The given template msi file. * @param pkgInfo JnlpPackageInfo object. * @throws IOException If failed to modify the MSI file. */ private static void customizeNonLocalizedMsiFields(String msiFilePath, JnlpPackageInfo pkgInfo) throws IOException { TreeMap treeMap = new TreeMap(); // get uuid of msi String uuidProduct = "{" + WinMsiUtility.genUUID() + "}"; String uuidUpgrade = "{" + WinMsiUtility.genUUID() + "}"; //Set property field of property table in the destination msi file try { treeMap.clear(); treeMap.put("ARPCONTACT", AUTHOR_INFO); treeMap.put("ARPHELPLINK", AUTHOR_INFO); treeMap.put("ARPURLINFOABOUT", AUTHOR_INFO); treeMap.put("ARPURLUPDATEINFO", AUTHOR_INFO); treeMap.put( "ARPCOMMENTS", "Generated by JDIC Project"); treeMap.put("ProductCode", uuidProduct); treeMap.put("UpgradeCode", uuidUpgrade); //Will put release # together with version # treeMap.put( "ProductVersion", pkgInfo.getVersion() + "." + pkgInfo.getRelease()); treeMap.put("JnlpFileName", pkgInfo.getJnlpFileName());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -