📄 shell.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: Shell.java,v 1.84 2006/06/14 16:37:12 bondolo Exp $ */package net.jxta.impl.shell.bin.Shell;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.List;import java.util.StringTokenizer;import java.util.logging.Level;import java.util.logging.Logger;import net.jxta.document.AdvertisementFactory;import net.jxta.id.IDFactory;import net.jxta.peergroup.PeerGroup;import net.jxta.pipe.InputPipe;import net.jxta.pipe.OutputPipe;import net.jxta.pipe.PipeService;import net.jxta.protocol.PipeAdvertisement;import net.jxta.impl.shell.GetOpt;import net.jxta.impl.shell.ShellApp;import net.jxta.impl.shell.ShellConsole;import net.jxta.impl.shell.ShellEnv;import net.jxta.impl.shell.ShellInputPipe;import net.jxta.impl.shell.ShellObject;import net.jxta.impl.shell.ShellOutputPipe;import net.jxta.impl.shell.bin.history.HistoryQueue;import net.jxta.impl.shell.bin.join.join.PeerGroupShellObject;/** * This class implements a JXTA Shell **/public class Shell extends ShellApp implements Runnable { /** * Log4J Logger **/ private static final transient java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(Shell.class.getName());; /** * Description of the Field */ public final static String HISTORY_ENV_NAME = "History"; /** * Description of the Field */ public final static String JXTA_SHELL_EMBEDDED_KEY = "JXTA_SHELL_EMBEDDED"; /** * Description of the Field */ public final static String PARENT_SHELL_ENV_NAME = "parentShell"; /** * Description of the Field */ public final static String CMD_PROMPT = "JXTA>"; private static int shellInstance = 1; private final int thisInstance; private ShellConsole cons = null; private boolean execShell = true; private boolean gotMyOwnFrame = false; /** * If true then this shell is a sub-shell of a another shell. */ private boolean gotParent = false; /** * The shell environment of our parent shell. */ private ShellEnv parentEnv = null; private String pipecmd = null; private BufferedReader scriptReader = null; /** * Child Shells will install a env var in their parent for their instance **/ private String parentEnvEnvName = null; private Thread thread = null; /** * Default constructor (don't delete) **/ public Shell() { synchronized( this.getClass() ) { thisInstance = shellInstance++; } } /** * Create a new shell with the specified console **/ public Shell( ShellConsole console) { this(); cons = console; } /** * Create a new shell with embedded functionality. * * @param embedded */ public Shell(boolean embedded) { this(); System.setProperty(JXTA_SHELL_EMBEDDED_KEY, Boolean.toString(embedded) ); } /** * Main processing method for the Shell object */ public void run() { try { if (null != pipecmd) { startApp(new String[0]); } else { runShell(); } } catch (Throwable all) { System.out.flush(); System.err.println("Uncaught Throwable in thread :" + Thread.currentThread().getName()); all.printStackTrace(System.err); } } /** * {@inheritDoc} **/ public int startApp(String[] argv) { GetOpt options = new GetOpt(argv, "xsf:e:"); while (true) { int option; try { option = options.getNextOption(); } catch( IllegalArgumentException badopt ) { consoleMessage( "Illegal argument :" + badopt ); return syntaxError(); } if (-1 == option) { break; } switch (option) { case 'f': String scriptfile = options.getOptionArg(); if (!initScriptFile(scriptfile)) { consoleMessage("Cannot access " + scriptfile); return ShellApp.appMiscError; } break; case 'e': execScript(options.getOptionArg()); return Shell.appNoError; case 's': gotMyOwnFrame = true; break; case 'x': execShell = false; break; default: return syntaxError(); } } ShellEnv env = getEnv(); if ( null == env ) { // There is no Parent Shell gotMyOwnFrame = true; env = new ShellEnv(); ShellObject stdgrpobj = new PeerGroupShellObject("Default Group", getGroup()); env.add("stdgroup", stdgrpobj ); PeerGroup child = null; PeerGroup current = getGroup(); while (true) { PeerGroup next = current.getParentGroup(); if (next == null) { break; } child = current; current = next; } // Unless one of our ancestor groups does not support getParent and thus we // know nothing, we can always find the platform. if (current != null) { ShellObject worldgrpobj; if( current.getPeerGroupID().equals( getGroup().getPeerGroupID() ) ) { worldgrpobj = stdgrpobj; } else { worldgrpobj = new PeerGroupShellObject("World Peer Group", current); } env.add("worldgroup", worldgrpobj); } // Unless our initial group is the platform, our before-last ancestor is // the netpg. if (child != null) { ShellObject rootgrpobj; if( child.getPeerGroupID().equals( getGroup().getPeerGroupID() ) ) { rootgrpobj = stdgrpobj; } else { rootgrpobj = new PeerGroupShellObject("Root Peer Group", child); } env.add("rootgroup", rootgrpobj); } } else { // This is a child Shell. gotParent = true; // Recover the parent env, and duplicate it parentEnv = env; env = new ShellEnv(parentEnv); parentEnvEnvName = parentEnv.createName(); // Store this Shell into the parent's environment parentEnv.add( parentEnvEnvName, new ShellObject("Child Shell " + Integer.toString(thisInstance), this) ); // and store our parent into our environment ShellObject parentShell = parentEnv.get( "shell" ); if( parentShell != null) { env.add( PARENT_SHELL_ENV_NAME, parentShell ); } } setEnv(env); // Store this Shell into the environment /* * Hardwiring the shell environment variable here allows us to retrieve * it from exit (or wherever), when we need to get the current instance * of the shell. */ env.add( "shell", new ShellObject("Shell " + Integer.toString(thisInstance), this) ); if (gotMyOwnFrame) { if( null == cons ) { cons = ShellConsole.newConsole( this, "JXTA Shell - (" + getGroup().getPeerName() + ")" ); } env.add("console", new ShellObject("console", cons) ); cons.setStatusGroup( getGroup() ); // Create the default InputPipe ShellInputPipe defaultInputPipe = new ShellInputPipe(getGroup(), cons); env.add("stdin", new ShellObject("Default InputPipe", defaultInputPipe)); env.add("consin", new ShellObject("Default Console InputPipe", defaultInputPipe));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -