📄 shellapp.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: ShellApp.java,v 1.42 2004/06/12 21:34:02 bondolo Exp $ */package net.jxta.impl.shell;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.io.Reader;import java.io.StringWriter;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.io.IOException;import net.jxta.peergroup.PeerGroup;import net.jxta.pipe.InputPipe;import net.jxta.pipe.OutputPipe;import net.jxta.platform.Application;import net.jxta.document.Advertisement;import net.jxta.endpoint.Message;import net.jxta.endpoint.MessageElement;import net.jxta.endpoint.StringMessageElement;import net.jxta.endpoint.TextMessageElement;import net.jxta.id.ID;/** * This class is the base class any JXTA Shell application must extend. */public abstract class ShellApp implements Application { /** * The command is still running. */ public final static int appSpawned = -1; /** * The command completed successfully. */ public final static int appNoError = 0; /** * An error occurred resulting from incorrect or missing parameters. */ public final static int appParamError = 1; /** * Something bad happened. Don't know what it means, don't care why it * happened. */ public final static int appMiscError = Integer.MAX_VALUE; /** * The default peergroup associated with this app. **/ private PeerGroup group = null; /** * The assigned id for this application, if any. **/ private ID id = null; /** * The implementation advertisement for this application, if any. **/ private Advertisement implAdv = null; /** * The environment for this application. Copied from the host shell. **/ private ShellEnv env = null; /** * The "stdin" input pipe for this command. **/ private InputPipe inputPipe = null; /** * The "stdout" output pipe for this command. **/ private OutputPipe outputPipe = null; /** * The console input. Differs from the stdin if stdin has been * redirected. consin is not normally redirected. **/ private InputPipe consin = null; /** * The console output. Differs from the stdout if stdout has been * redirected. consout is not normally redirected. **/ private OutputPipe consout = null; /** * if the result is a shell object then store it using this name. **/ private String returnVarName = null; /** * Has this app begun running? **/ protected volatile boolean started = false; /** * Has this app in the process of quitting? **/ protected volatile boolean stopped = false; /** * If this thread is enabled, then this is the root thread of another * command which this command can use to determine when it should finish. **/ private Thread dependsOn = null; /** * private buffer for input pipe **/ private List buffered = new ArrayList(); /** * private buffer for console input pipe. **/ private List consbuffer = new ArrayList(); /** * {@inheritDoc} **/ public final void init(PeerGroup pg, ID assignedID, Advertisement impl) { setGroup(pg); setAssignedID(assignedID); setImplAdvertisement(impl); started = true; } /** * {@inheritDoc} **/ public void stopApp() { stopped = true; } /** * Return a String containting a single line description of the function of * this ShellApp. **/ public String getDescription() { return "No description available for this ShellApp"; } /** * Print to the stdout a full description of the functionality of this * Shell command. **/ public void help() { println("No help available for this ShellApp"); } // Accessor Methods /** * Return the group in which this ShellApp is executing. Most ShellApps * should instead use the value of the "stdgroup" enviroment variable. * * @return the current peer group. **/ protected PeerGroup getGroup() { return group; } /** * Set the group will be the default group for this ShellApp. Most ShellApps * should instead use the value of the "stdgroup" enviroment variable. **/ private final PeerGroup setGroup(PeerGroup g) { PeerGroup old = group; group = g; return old; } /** * Return the assignedID for this ShellApp if any. * * @return the assigned ID for this ShellApp or <code>null</code> if none * was specified. **/ protected final ID getAssignedID() { return this.id; } /** * Set the assignedID for this ShellApp * * @param id The assigned ID for this application **/ private final void setAssignedID(ID id) { this.id = id; } /** * Return the implementation advertisement for this ShellApp, if any. * * @return implementation Advertisement for this ShellApp or <code>null</code> * if none was specified. **/ protected final Advertisement getImplAdvertisement() { return this.implAdv; } /** * Sets the implementation advertisement for this ShellApp. * * @param adv The implementation advertisement for this ShellApp. **/ private final Advertisement setImplAdvertisement(Advertisement adv) { Advertisement old = this.implAdv; this.implAdv = adv; return old; } /** * Return a modifiable instance of this ShellApp's environment. * * @return This ShellApp's environment. **/ protected final ShellEnv getEnv() { return env; } /** * Sets this ShellApp's environment. * * @return The environment for this ShellApp to use. **/ protected final ShellEnv setEnv(ShellEnv e) { ShellEnv old = env; env = e; return old; } protected final Thread setJoinedThread(Thread dependsOn) { Thread old = this.dependsOn; this.dependsOn = dependsOn; return old; } /** * Return the standard input pipe. * * @return the standard input pipe. **/ protected final InputPipe getInputPipe() { return inputPipe; } /** * Set the standard input pipe to the provided pipe. The previous pipe is * returned. * * @param ip the new standard input pipe. * @return the old standard input pipe. **/ protected final InputPipe setInputPipe(InputPipe ip) { InputPipe old = inputPipe; inputPipe = ip; buffered.clear(); return old; } /** * Return the standard output pipe. * * @return the standard output pipe. **/ protected final OutputPipe getOutputPipe() { return outputPipe; } /** * Set the standard output pipe to the provided pipe. The previous pipe is * returned. * * @param op the new standard output pipe. * @return the old standard output pipe. **/ protected final OutputPipe setOutputPipe(OutputPipe op) { OutputPipe old = outputPipe; outputPipe = op; return old; } /** * Return the console input pipe. * * @return the console input pipe. **/ protected final InputPipe getInputConsPipe() { return consin; } /** * Set the console input pipe to the provided pipe. The previous pipe is * returned. * * @param ip the new input output pipe. * @return the old input output pipe. **/ protected final InputPipe setInputConsPipe(InputPipe ip) { InputPipe old = consin; consin = ip; consbuffer.clear(); return old; } /** * Return the console output pipe. * * @return the console output pipe. **/ protected final OutputPipe getOutputConsPipe() { return consout; } /** * Set the console output pipe to the provided pipe. The previous pipe is * returned. * * @param op the new console output pipe. * @return the old console output pipe. **/ protected final OutputPipe setOutputConsPipe(OutputPipe op) { OutputPipe old = consout; consout = op; return old; } /** * Return the name of the environment into which this ShellApp should * return any environment variable result. * * @return the name of the environment variable or null if no name has been * set. **/ protected final String getReturnVariable() { return returnVarName; } /** * Set the name of the environment into which this ShellApp should * return any environment variable result. * * @param varName name of the environment variable. **/ protected final void setReturnVariable(String varName) { returnVarName = varName; } // IO Operations /** * Print to standard output * * @param line the line to print **/ protected final void print(String line) { if( null == outputPipe ) { return; } pipePrint(outputPipe, line); } /** * Print to standard output appending a newline. * * @param line the line to print **/ protected final void println(String line) { if( null == outputPipe ) { return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -