📄 xletinstallerimpl.java
字号:
/* * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * 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 version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. */package com.sun.jumpimpl.module.installer;import com.sun.jump.common.JUMPAppModel;import com.sun.jump.common.JUMPApplication;//import com.sun.jump.executive.JUMPUserInputManager;import com.sun.jump.module.contentstore.JUMPContentStore;import com.sun.jump.module.contentstore.JUMPData;import com.sun.jump.module.contentstore.JUMPNode;import com.sun.jump.module.contentstore.JUMPStore;import com.sun.jump.module.contentstore.JUMPStoreFactory;import com.sun.jump.module.contentstore.JUMPStoreHandle;import com.sun.jump.module.download.JUMPDownloadDescriptor;import com.sun.jump.common.JUMPContent;import com.sun.jump.module.installer.JUMPInstallerModule;import java.io.BufferedReader;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;import java.util.Enumeration;import java.util.Hashtable;import java.util.Iterator;import java.util.Map;import java.util.Properties;import java.util.Vector;import java.util.jar.JarFile;import java.util.zip.ZipEntry;/** * XLETInstallerImpl contains the implementation of the JUMPInstallerModule * for XLET applications within JUMP. * Note: XLET and Main installations within JUMP in CDC behave almost the same. * For this reason, MAINInstallerImpl simply subclasses XLETInstallerImpl * and overrides a small set of methods to define specific Main application * behavior. In the future, if the behavior of XLET and Main application * installation start to differ much more, it may be wise to extract a base * class out of this class and create a subclass each for XLETs and Main * applications. */public class XLETInstallerImpl extends JUMPContentStore implements JUMPInstallerModule { /** * The filename extention for application descriptor files. */ private final static String APP_DESCRIPTOR_EXTENSION = ".app"; /** * The name of the directory to hold XLET and Main applications */ private final static String REPOSITORY_APPS_DIRNAME = "./apps"; /** * The name of the directory to hold XLET and Main icons */ private final static String REPOSITORY_ICONS_DIRNAME = "./icons"; /** * The name of the directory to hold XLET and Main application descriptor files */ private final static String REPOSITORY_DESCRIPTORS_DIRNAME = "./descriptors"; /** * Handle to the content store */ private JUMPStoreHandle storeHandle = null; /** * The root directory of the content store */ protected String contentStoreDir = null; /** * Holds the app id -> JUMPApplication object relationship */ private Hashtable installedAppIdHashtable = null; private int installedAppIdHashtableKey = 1; /** * The keys below are used within application descriptor files only. * These keys are not the sames keys as the ones used in the download module. */ private final static String DESCRIPTOR_BUNDLENAME_KEY = "bundle"; private final static String DESCRIPTOR_APPMODEL_KEY = "type"; private final static String DESCRIPTOR_JARPATH_KEY = "path"; private final static String DESCRIPTOR_TITLE_KEY = "title"; private final static String DESCRIPTOR_ICON_KEY = "icon"; private final static String DESCRIPTOR_SECURITYLEVEL_KEY = "icon"; private final static String DESCRIPTOR_ID_KEY = "id"; protected final static String DESCRIPTOR_INITIALCLASS_KEY = "xletName"; /** * Print out messages */ private boolean verbose = false; /** * */ private static final boolean ALLOW_MULTIPLE_APP_INSTALLS = false; private String getAvailableAppIdHashKey() { int tempHashKey = installedAppIdHashtableKey; while (installedAppIdHashtable.containsKey(Integer.toString(tempHashKey))) { if (tempHashKey == Integer.MAX_VALUE) { tempHashKey = 1; } else { tempHashKey++; } // For the very, very unlikely case that MAX_VALUE applications // are running and we cycled through all integer values. if (tempHashKey == installedAppIdHashtableKey) { return null; } } installedAppIdHashtableKey = tempHashKey; return Integer.toString(tempHashKey); } private void addInstalledAppIdEntry(Object key, Object app) { // Sanity check if (key == null || app == null) { return; } int keyVal = Integer.parseInt((String)key); if (keyVal < 1) { return; } else { installedAppIdHashtableKey = keyVal; } installedAppIdHashtable.put(key, app); } private void removeInstalledAppIdEntry(Object key) { if (key == null) { return; } Object app = installedAppIdHashtable.remove(key); } /** * Returns an instance of the content store to be used with the installer. * @return Instance of JUMPStore */ protected JUMPStore getStore() { return JUMPStoreFactory.getInstance().getModule(JUMPStoreFactory.TYPE_FILE); } /** * Implementation of JUMPInstaler.unload() */ public void unload() { contentStoreDir = null; verbose = false; if (storeHandle != null) { closeStore(storeHandle); } installedAppIdHashtable.clear(); installedAppIdHashtable = null; } /** * load the installer module * @param map the configuration data required for loading this service. */ public void load(Map map) { // check if verbose mode is used String verboseStr = System.getProperty("installer.verbose"); if (verboseStr == null && map != null) { verboseStr = (String) map.get("installer.verbose"); } if (verboseStr != null && verboseStr.toLowerCase().equals("true")) { verbose = true; } // the repository directory should be passed in as a system property contentStoreDir = System.getProperty("contentstore.root"); if (contentStoreDir == null && map != null) { contentStoreDir = (String)map.get("contentstore.root"); } if (contentStoreDir != null) { // remove any ending /'s' if (!contentStoreDir.endsWith("/")) { contentStoreDir = contentStoreDir.concat("/"); } } else { // default to the current directory contentStoreDir = "./"; } // Get the store handle from the JUMPContentStoreSubClass. storeHandle = openStore(true); // Make sure apps/, icons/, descriptors/ exist under the root // of content store. This is inherited CDC-specific behavior. try { // Create the directories within the repository if they don't exist' if (storeHandle.getNode(REPOSITORY_APPS_DIRNAME) == null) { storeHandle.createNode(REPOSITORY_APPS_DIRNAME); } if (storeHandle.getNode(REPOSITORY_ICONS_DIRNAME) == null) { storeHandle.createNode(REPOSITORY_ICONS_DIRNAME); } if (storeHandle.getNode(REPOSITORY_DESCRIPTORS_DIRNAME) == null) { storeHandle.createNode(REPOSITORY_DESCRIPTORS_DIRNAME); } } catch (Exception e) { e.printStackTrace(); } closeStore(storeHandle); // Populate the installedAppIdHashtable to keep track of the installed // ids of all currently installed applications of this type installedAppIdHashtableKey = 1; if (installedAppIdHashtable != null) { installedAppIdHashtable.clear(); } installedAppIdHashtable = new Hashtable(); JUMPContent[] content = getInstalled(); if (content != null) { for(int j = 0; j < content.length; j++) { JUMPApplication app = ((JUMPApplication)content[j]); int installedId = app.getId(); addInstalledAppIdEntry(Integer.toString(installedId), app); } } } /** * install content specified by the given descriptor and location. * @return the installed content * @param location URL of content to be installed * @param desc object describing the content to be installed */ public JUMPContent[] install(URL location, JUMPDownloadDescriptor desc) { // sanity check if (location == null || desc == null || !location.getProtocol().equals("file") || desc.getType() != JUMPDownloadDescriptor.TYPE_APPLICATION) { return null; } // This is the all important "name" value. This is the value retrieved // from the <name> in the Descriptor file. This value will be used // as the bundle name and is also used to name the jarfile and parent // directory in the repository. We need to restrict the characters used // within this name value, which means that all of the characters in the name // must be valid filename characters. This name value is not intended // for any display value purposes. For that, use <ddx:display>. // Here's an example. Let's say the name value is CasinoGames. Then // the resulting jarfile name will be: apps/CasinoGames/CasinoGames.jar. // If there is already a pathname that is the same, the jarfile name changes // a bit. We currently do not overwrite the existing duplicate file. Read // below for more details. String bundleName = desc.getName(); if (bundleName == null) { return null; } // We need to replace spaces because apparently java doesn't like // jarfiles with spaces in the name. Any further string substitutions // should be done here. bundleName = bundleName.replace(' ', '_'); trace(getString("Installing") + bundleName); String jarPathWithinStore = REPOSITORY_APPS_DIRNAME + '/' + bundleName + '/' + bundleName + ".jar"; String jarPath = null; if (ALLOW_MULTIPLE_APP_INSTALLS) { // createUniquePathName ensures that we don't overwrite an existing file by // retrieving a unique name for saving. If there exists a file of the same // name, using the example above the new name will look something like this: // apps/CasinoGames(2)/CasinoGames(2).jar. jarPath = createUniquePathName(contentStoreDir + jarPathWithinStore); } else { jarPath = contentStoreDir + jarPathWithinStore; if (new File(jarPath).exists()) { System.out.println("*** Error installing bundle: A bundle with this name is already installed."); return null; } } trace(getString("AttemptingToSave") + jarPath); // Because of the possibility of the filename being modified because of // an already exiting file, the bundle name will also need to change. // The new bundle name is within the filename itself, so extract the bundle // name. For the example above, the new bundle name will be CasinoGames(2), // not CasinoGames. Again, this is only in the rare case of duplicates. // This should be changed if the policy is to overwrite existing filenames. int dotindex = jarPath.lastIndexOf('.'); int fileSeparatorIndex = jarPath.lastIndexOf('/'); if (dotindex == -1 || fileSeparatorIndex == -1) { return null; } // The bundleName because the fileName minus the .jar bundleName = jarPath.substring(fileSeparatorIndex + 1, dotindex); String parentDir = jarPath.substring(0, fileSeparatorIndex); try { // This is the location of the file passed into the install method File origFile = new File(location.getFile()); if (!origFile.exists()) { trace(getString("CannotAccessFile") + ": " + location.getFile()); return null; } // First, create the parent directory for the saved application File destDir = new File(parentDir); destDir.mkdir(); if (!destDir.exists()) { trace(destDir.toString() + " " + getString("DoesNotExist")); return null; } // Move the file from the passed-in location File destFile = new File(jarPath); if (destFile == null) { return null; } boolean result = origFile.renameTo(destFile.getCanonicalFile()); if (!result) { trace(getString("CannotMoveFile")); // The file move didn't work. A reason for this could be that // the original file and destination file are in two different // filesystems. Try copying a buffer from the URL input stream. trace(getString("UsingURLInputStream")); byte[] buffer = copyBuffer(location.openStream(), desc.getSize()); if (buffer == null) { trace(getString("CannotSaveFile")); return null; } // Write out to a file within the repository FileOutputStream fos = new FileOutputStream(destFile); if (fos == null) { trace(getString("CannotSaveFile")); return null; } fos.write(buffer); fos.close(); origFile.delete(); } } catch (Exception ex) { ex.printStackTrace(); try { storeHandle.deleteNode(parentDir); } catch (IOException e) { e.printStackTrace(); } return null; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -