📄 main.java
字号:
/* * @(#)Main.java 1.9 03/12/19 * * Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * -Redistribution of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * -Redistribution in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of contributors may * be used to endorse or promote products derived from this software without * specific prior written permission. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of any * nuclear facility. */package jnlp.sample.JreInstaller;import java.io.*;import java.net.*;import java.awt.*;import java.util.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.text.MessageFormat;import javax.jnlp.DownloadService;import javax.jnlp.DownloadServiceListener;import javax.jnlp.ExtensionInstallerService;import java.security.*;/** * Main class for JRE installer */public class Main { private static final String JAVAWS_MERLIN_KEY = "Software\\JavaSoft\\Java Web Start\\1.0.1_02"; private static final String JAVAWS_HOPPER_KEY = "Software\\JavaSoft\\Java Web Start\\1.2"; private static final int BUFFER_SIZE = 32 * 1024; // Keep track of installer window private static JFrame _installerFrame; private static JLabel[] _stepLabels = null; private static final int STEP_LICENSE = 0; private static final int STEP_DOWNLOAD = 1; private static final int STEP_UNPACK = 2; private static final int STEP_INSTALL = 3; private static final int STEP_DONE = 4; /** Does install of JRE */ public static void install() { // Hide the JNLP Clients installer window and show own Config.getInstallService().hideStatusWindow(); showInstallerWindow(); // Make sure the destination exists. String path = Config.getInstallService().getInstallPath(); if (Config.isWindowsInstall()) { String defaultLocation = "C:\\Program Files\\Java\\j2re" + Config.getJavaVersion() + "\\"; File defaultDir = new File(defaultLocation); if (!defaultDir.exists()) { defaultDir.mkdirs(); } if (defaultDir.exists() && defaultDir.canWrite()) { path = defaultLocation; // use default if you can } } File installDir = new File(path); if (!installDir.exists()) { installDir.mkdirs(); if (!installDir.exists()) { // The installFailed string is only for debugging. No localization needed installFailed("couldntCreateDirectory", null); return; } } // Show license if neccesary enableStep(STEP_LICENSE); if (!showLicensing()) { // The installFailed string is only for debugging. No localization needed installFailed("Licensing was not accepted", null); }; // Make sure that the data JAR is downloaded enableStep(STEP_DOWNLOAD); if (!downloadInstallerComponent()) { // The installFailed string is only for debugging. No localization needed installFailed("Unable to download data component", null); } String nativeLibName = Config.getNativeLibName(); File installerFile = null; try { // Load native library into process if found if (nativeLibName != null && !Config.isSolarisInstall()) { System.loadLibrary(nativeLibName); } // Unpack installer enableStep(STEP_UNPACK); String installResource = Config.getInstallerResource(); Config.trace("Installer resource: " + installResource); installerFile = unpackInstaller(installResource); // To clean-up downloaded files Config.trace("Unpacked installer to: " + installerFile); if (installerFile == null) { // The installFailed string is only for debugging. No localization needed installFailed("Could not unpack installer components", null); return; } enableStep(STEP_INSTALL); setStepText(STEP_INSTALL, Config.getWindowStepWait(STEP_INSTALL)); boolean success = false; if (Config.isSolarisInstall()) { success = runSolarisInstaller(path, installerFile); } else { success = runWindowsInstaller(path, installerFile); } if (!success) { // The installFailed string is only for debugging. No localization needed installFailed("Could not run installer", null); return; } } catch(UnsatisfiedLinkError ule) { // The installFailed string is only for debugging. No localization needed installFailed("Unable to load library: " + nativeLibName, null); return; } finally { if (installerFile != null) { installerFile.delete(); } } setStepText(STEP_INSTALL, Config.getWindowStep(STEP_INSTALL)); enableStep(STEP_DONE); String execPath = path + Config.getJavaPath(); Config.trace(execPath); /** Remove installer JAR from cache */ removeInstallerComponent(); // If we're running anything after 1.0.1 or not on Windows, just call // finishedInstall. Otherwise, deny ExitVM permission so that we can // return here and do a reboot. We have to do this because we need to // call ExtensionInstallerService.finishedInstall(), which registers // that our extension (the JRE) is installed. Unfortunately pre-1.2 it // also does not understand that we are requesting a reboot, and calls // System.exit(). So for pre 1.2 we want to deny the permission to // exit the VM so we can return here and perform a reboot. boolean ispre12 = false; String version = Config.getJavaWSVersion(); // get first tuple String v = version.substring(version.indexOf('-')+1); int i2 = v.indexOf('.'); int v1 = Integer.parseInt(v.substring(0, i2)); // get second tuple v = v.substring(i2+1); i2 = v.indexOf('.'); if (i2 == -1) i2 = v.indexOf('-'); if (i2 == -1) i2 = v.indexOf('['); if (i2 == -1) i2 = v.length(); int v2 = Integer.parseInt(v.substring(0,i2)); // are we pre 1.2? if (v1 < 1 || (v1 == 1 && v2 < 2)) ispre12 = true; if (Config.isWindowsInstall() && ispre12 && Config.isHopper()) { // deny ExitVM permission then call finishedInstall ProtectionDomain pd = (new Object()).getClass().getProtectionDomain(); CodeSource cs = pd.getCodeSource(); AllPermissionExceptExitVM perm = new AllPermissionExceptExitVM(); PermissionCollection newpc = perm.newPermissionCollection(); newpc.add (perm); // run finishedInstall within the new context which excluded // just the ExitVM permission ProtectionDomain newpd = new ProtectionDomain(cs, newpc); AccessControlContext newacc = new AccessControlContext(new ProtectionDomain[] {newpd}); final String fExecPath = execPath; try { AccessController.doPrivileged(new PrivilegedExceptionAction() { public Object run() throws SecurityException { finishedInstall(fExecPath); return null; } }, newacc); } catch (PrivilegedActionException pae) { // swallow the exception because we want ExitVM to fail silent } catch (SecurityException se) { // swallow the exception because we want ExitVM to fail silent } } else { // just call finished Install finishedInstall(execPath); } if (Config.isWindowsInstall() && WindowsInstaller.IsRebootNecessary()) { // reboot if (!WindowsInstaller.askUserForReboot()) System.exit(0); } else { System.exit(0); } } private static final Permission exitVMPermission = new RuntimePermission("exitVM"); private static final class AllPermissionExceptExitVM extends Permission { public AllPermissionExceptExitVM() { super("<all permissions except Exit VM>"); } public AllPermissionExceptExitVM(String name, String actions) { this(); } public boolean implies(Permission p) { if (p instanceof RuntimePermission && p.equals(exitVMPermission)) { return false; } else return true; } public boolean equals(Object o) { return (o instanceof AllPermissionExceptExitVM); } public String getActions() { return new String("<all actions>"); } public int hashCode() { return 1; } public PermissionCollection newPermissionCollection() { return new AllPermissionExceptExitVMCollection(); } } private static final class AllPermissionExceptExitVMCollection extends PermissionCollection implements java.io.Serializable { private boolean all_allowed; // true if any all permissions have been added /** * Create an empty AllPermissions object. * */ public AllPermissionExceptExitVMCollection() { all_allowed = false; } /** * Adds a permission to the AllPermissions. The key for the hash is * permission.path. * * @param permission the Permission object to add. * * @exception IllegalArgumentException - if the permission is not a * AllPermission * * @exception SecurityException - if this AllPermissionCollection object * has been marked readonly */ public void add(Permission permission) { if (! (permission instanceof AllPermissionExceptExitVM)) throw new IllegalArgumentException("invalid permission: "+ permission); if (isReadOnly()) throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection"); all_allowed = true; } /** * Check and see if this set of permissions implies the permissions * expressed in "permission". * * @param p the Permission object to compare * * @return always returns true. */ public boolean implies(Permission permission) { return ((permission instanceof RuntimePermission && permission.equals(exitVMPermission)) ? false : all_allowed); } /** * Returns an enumeration of all the AllPermission objects in the * container. * * @return an enumeration of all the AllPermission objects. */ public Enumeration elements() { return new Enumeration() { private boolean hasMore = all_allowed; public boolean hasMoreElements() { return hasMore; } public Object nextElement() { hasMore = false; return new AllPermissionExceptExitVM(); } }; } } /** Download data component JAR */ static public boolean downloadInstallerComponent() { DownloadService downloadService = Config.getDownloadService(); DownloadServiceListener listener = downloadService.getDefaultProgressWindow(); String compName = Config.getInstallerLocation(); String compVer = Config.getInstallerVersion(); try { URL codebase = Config.getBasicService().getCodeBase(); URL url = new URL(codebase, compName); String urlstr = url.toString(); if (!downloadService.isResourceCached(url, compVer)) { // The installFailed string is only for debugging. No localization needed Config.trace("Downloading: " + urlstr); // Do download downloadService.loadResource(url, compVer, listener); } } catch(IOException ioe) { Config.trace("Unable to download: " + compName + "/" + compVer); return false; } return true; } /** Remove data component data JAR from cache */ static public void removeInstallerComponent() { DownloadService downloadService = Config.getDownloadService(); if (downloadService != null) { String component = Config.getInstallerLocation(); String version = Config.getInstallerVersion(); try { URL codebase = Config.getBasicService().getCodeBase(); URL url = new URL(codebase, component); component = url.toString(); Config.trace("Removing: " + component + "/" + version); downloadService.removeResource(url, version); } catch(IOException ioe) { Config.trace("Unable to remove " + component + "/" + version); } } else { Config.trace("No download service found"); } } /** Runs a Solaris installer */ static public boolean runSolarisInstaller(String installPath, File installFile) { /** Build temp. script file */ File script = null; boolean success = false; try { script = SolarisInstaller.createTempShellScript(); String[] args = new String[3]; args[0] = installPath; args[1] = script.getAbsolutePath(); args[2] = installFile.getAbsolutePath(); String execString = getExecuteString(args); success = SolarisInstaller.execute(execString); } catch(IOException ioe) { Config.trace("Got ioe: " + ioe); return false; } finally { if (script != null) script.delete(); } return success; } /** Runs a Windows installer */ static public boolean runWindowsInstaller(String installPath, File installFile) { boolean deleteHopperKey = false; boolean deleteMerlinKey = false; // If Hopper, and JavaWS can update, ask the user if they want // to update. if (Config.isHopper() &&
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -