📄 shellcmds.java
字号:
/* * Copyright (c) 2001 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: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions 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. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Sun Microsystems, Inc. for Project JXTA." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact Project JXTA at http://www.jxta.org. * * 5. Products derived from this software may not be called "JXTA", * nor may "JXTA" appear in their name, without prior written * permission of Sun. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of Project JXTA. For more * information on Project JXTA, please see * <http://www.jxta.org/>. * * This license is based on the BSD license adopted by the Apache Foundation. * * $Id: ShellCmds.java,v 1.20 2006/06/14 16:37:11 bondolo Exp $ */package net.jxta.impl.shell;import java.net.*;import java.util.*;import java.util.zip.*;import java.io.File;import java.util.jar.JarFile;import java.util.jar.JarEntry;import java.io.IOException;import java.lang.reflect.UndeclaredThrowableException;import java.util.logging.Logger;import java.util.logging.Level;/** * This class has methods for getting an array of all available Shell commands * and for instantiating of a specific Shell command (the standard constructor is used!). * It uses a ClassLoader that can load class-files from an arbirtrary number of jar-files. * The list of jar-files is encoded in the INST_JARS environment variable of the ShellApp * that instantiated this class. * The encoded String has following format: * <path1><PATH_SEPARATOR><path2><PATH_SEPARATOR>... * * Whenever a ShellApp needs access to the Shell commands this class should be used. * For example to get all available Shell commands use the following code: * <code> * ShellCmds cmds = new ShellCmds((ShellApp)this); * String[] cmdList = cmds.list(); * </code> * To instantiate a command you could use following code: * <code> * // Instantiate the 'man' command. * ShellApp app = cmds.getInstance("man"); * // Now set the group and the input and output pipes if neccessary. * ... * // For example we want to print the short description of the app. * println(app.getDescription()); * </code> */public final class ShellCmds { /** For debugging purposes. */ private static final java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(ShellCmds.class.getName());; /** Name of environment variable. */ public static final String INST_JARS = "instjars"; /** Char used to separate paths. */ public static final String PATH_SEPARATOR = System.getProperty("path.separator"); static final String BIN_PACKAGE = "net.jxta.impl.shell.bin"; static final String BIN_PATH = "net/jxta/impl/shell/bin/"; static final int BIN_PATH_LEN = BIN_PATH.length(); /** Reference to the ShellApp that instantiated this class. */ private final ShellEnv env; /** * Initializes a newly created ShellCmds object with a reference to the instantiating ShellApp. * This reference will be used to obtain its current ShellEnv object which serves for reading * and storing the INST_JARS environment variable. * * @param env the environment to use. */ public ShellCmds(ShellEnv env) { this.env = env; } /** * Static method to extract the list of paths out of a String. The format of the String is: * <path1><PATH_SEPARATOR><path2>... * Only correct paths are added to the later returned list. * * @param pathList the String the paths will be extracted from. * @return an array of all valid paths. If the given parameter contains no path or none * of the given paths is valid then an empty list is returned. */ public static URL [] parseClassPath( String pathList ) { List<URL> pathURLS = new ArrayList<URL>(); StringTokenizer tok = new StringTokenizer( pathList, PATH_SEPARATOR, false ); while (tok.hasMoreTokens()) { String fileName = tok.nextToken(); File file = new File(fileName); try { pathURLS.add( file.toURL() ); } catch( MalformedURLException bad ) { } } return pathURLS.toArray( new URL[pathURLS.size()] ); } /** * Static method to encode a list of paths into its corresponding String representation. * The format of the String is: <path1><PATH_SEPARATOR><path2>... * * @param pathList the list of paths to be encoded. * @return a String representation of the given list. */ public static String encodeInstJars(List pathList) { StringBuffer buf = new StringBuffer(); for (int i = 0, size = pathList.size(); i < size; i++) { buf.append(pathList.get(i)); if (i < size - 1) buf.append(PATH_SEPARATOR); } return buf.toString(); } /** * Stores the String representation of the given list of paths in the environment * of the ShellApp. * * @param pathList list of paths to be stored in the environment of the ShellApp. */ public void setInstJars(List pathList) { // Convert list into its corresponding String representation, String instJars = encodeInstJars(pathList); // and set the new value of environment variable INST_JARS. env.add(INST_JARS, new ShellObject("Installed Jar Files", instJars)); } /** * Retrieves the list of paths encoded as a String from the ShellApp's environment. * * @return a list of path objects. */ public URL [] getInstJars() { if (env.contains(INST_JARS)) { // If environment variable INST_JARS already exists, then // get environment variable INST_JARS, and // extract zip-files and directories and construct a list containing them. String instJars = (String)env.get(INST_JARS).getObject(); return parseClassPath(instJars); } else { // else return an empty list. return new URL[0]; } } /** * Returns an array of all available Shell commands. * * <p/>The current implementation tries to locate commands on the classpath * pf the classloader
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -