⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shellapp.java

📁 有关java 的p2p应用,是一般很好的教程,有兴趣的朋友应该好好阅读一下
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }                pipePrintln(outputPipe, line);    }        /**     *  Poll for input on standard input.     *     *  @return an input line or <code>null</code> if no input is available.     **/    protected final String pollInput() throws IOException {        if( null == inputPipe ) {            return null;        }                if(inputPipe == consin)            return consPollInput();        else            return pipePollInput(inputPipe, buffered);    }        /**     *  Wait for input on standard input.     *     *  @return an input line or <code>null</code> if standard input has been     *  closed.     **/    protected final String waitForInput() throws IOException {        if( null == inputPipe ) {            return null;        }                if(inputPipe == consin)            return consWaitForInput();        else            return pipeWaitForInput(inputPipe, buffered, true);    }        /**     *  Print to the console output.     *     *  @param line the line to print     **/    protected final void consprint(String line) {        if( null == consout ) {            return;        }                pipePrint(consout, line);    }        /**     *  Print to console output appending a newline.     *     *  @param line the line to print     **/    protected final void consprintln(String line) {        if( null == consout ) {            return;        }                pipePrintln(consout, line);    }        /**     *  Poll for input on console input.     *     *  @return an input line or <code>null</code>  if no input is available.     **/    protected final String consPollInput() throws IOException {        if( null == consin ) {            return null;        }                return pipePollInput(consin, consbuffer);    }        /**     *  Wait for input on console input.     *     *  @return an input line or <code>null</code> if standard input has been     *  closed.     **/    protected final String consWaitForInput() throws IOException {        if( null == consin ) {            return null;        }                String msg = pipeWaitForInput(consin, consbuffer, false);                // create an EOT        if(msg != null) {            if(-1 != msg.indexOf('\u0004')) {                msg = null;            }        }                return msg;    }        protected final String getCmdShortName() {        return getCmdShortName(this.getClass());    }        public final static String getCmdShortName(Class clas) {        String cmdClass = clas.getName();        String cmdName;                int lastDot = cmdClass.lastIndexOf(".");                if(-1 != lastDot) {            int secondLast = cmdClass.lastIndexOf(".", lastDot - 1);                        String cmdPackage = "";            if(-1 != secondLast) {                cmdPackage = cmdClass.substring(secondLast + 1, lastDot);            }            cmdName = cmdClass.substring(lastDot + 1);                        if(!cmdName.equals(cmdPackage)) {                cmdName = cmdPackage + "." + cmdName;            }        } else {            cmdName = cmdClass;        }                return cmdName;    }        /**     *  print a message from the specified class on the specified pipe.     *     *  @param clas the class which is printing the message     *  @param consout the pipe on which to print the message     *  @param message the message to print.     **/    public final static void consoleMessage(Class clas, OutputPipe consout, String message) {                pipePrintln(consout, "# " + getCmdShortName(clas) + " - " + message);    }        /**     *  Print a message on the console. The message will identify this ShellApp     *  as the source of the message.     *     *  @param message The message to print.     **/    protected final void consoleMessage(String message) {        consoleMessage(getClass(), consout, message);    }        /**     *  Print a stack trace to the console with the specified annotation. The     *  message will identify this ShellApp as the source of the exception.     *     *  @param clas the class which is printing the message     *  @param consout the pipe on which to print the message     *  @param annotation Explanation or annotation for the stack trace.     *  @param failure the stack trace.     **/    public final static void printStackTrace(Class clas, OutputPipe consout, String annotation, Throwable failure) {        consoleMessage(clas, consout, annotation);                StringWriter    theStackTrace = new StringWriter();        failure.printStackTrace(new PrintWriter(theStackTrace));        pipePrintln(consout, theStackTrace.toString());    }        /**     *  Print a stack trace to the console with the specified annotation. The     *  message will identify this ShellApp as the source of the exception.     *     *  @param annotation Explanation or annotation for the stack trace.     *  @param failure the stack trace.     **/    protected final void printStackTrace(String annotation, Throwable failure) {        printStackTrace(getClass(), consout, annotation, failure);    }        /**     *  Load a shell application.     *     *  @param returnvar    The env variable in which the command should put its result.     *  @param appName      The name of the application to load.     *  @param env          The enviroment to use.     **/    protected ShellApp loadApp(String returnvar, String appName, ShellEnv env) {        ShellApp app = null;                try {            app = new ShellCmds(env).getInstance(appName);        } catch( Exception failed ) {            printStackTrace("Exception in command : " + appName, failed );            return null;        }                if( null == app ) {            return null;        }                // Set up the default environment and pipes                app.setEnv(env);                app.setJoinedThread(dependsOn);                ShellObject obj = env.get("consin");        if( null != obj ) {            app.setInputConsPipe((InputPipe) obj.getObject());        }                obj = env.get("consout");        if( null != obj ) {            app.setOutputConsPipe((OutputPipe) obj.getObject());        }                obj = env.get("stdin");        if( null != obj ) {            app.setInputPipe((InputPipe) obj.getObject());        }                obj = env.get("stdout");        if( null != obj ) {            app.setOutputPipe((OutputPipe) obj.getObject());        }                // Set the variable name for the return value (if any).        app.setReturnVariable(returnvar);                // now init it!        app.init((PeerGroup) env.get("stdgroup").getObject(), null, null);                return app;    }        /**     *  Load and run a shell application.     *     *  @param returnvar    The env variable in which the command should put its result.     *  @param appName      The name of the application to load.     *  @param env          The enviroment to use.     **/    protected int exec(String returnvar, String appName, String [] args, ShellEnv env) {        ShellApp app = loadApp(returnvar, appName, env);                if( null == app ) {            consoleMessage( "Could not load application : " + appName );            return ShellApp.appMiscError;        }                return exec(app, args);    }        /**     *  Run a loaded shell Application.     *     *  @param app the application     *  @param args arguments for the application.     **/    protected int exec( ShellApp app, String[] args ) {        try {            // start the app            int result = app.startApp(args);                        if( ShellApp.appSpawned != result ) {                if( (ShellApp.appNoError != result) && (getEnv().contains("echo")) ) {                    consoleMessage( "'" + app + "' returned error code : " + result );                }                                app.stopApp();            }                        return result;        } catch (Throwable e) {            printStackTrace("Exception in command : " + app, e );            return ShellApp.appMiscError;        }    }        // Private implementations        /**     *  print to the specified pipe.     *     *  @param pipe The destination pipe.     *  @param line The text to print.     **/    public static void pipePrint(OutputPipe pipe, String line) {        if (null == pipe) {            return;        }                //Create a message off this string.        try {            Message msg = new Message();                        MessageElement elem = new StringMessageElement("ShellOutputPipe", line, null);                        msg.addMessageElement(elem);                        pipe.send(msg);        } catch( IOException failure ) {            failure.printStackTrace();        }    }        /**     *  print to the specified pipe appending a newline after text.     *     *  @param pipe The destination pipe.     *  @param line The text to print.     **/    public static void pipePrintln(OutputPipe pipe, String line) {        pipePrint(pipe, line + "\n");    }        /*     *  Return a string containing one line of input from the specified pipe.     *     *  @param input the pipe to poll.     *  @param lineBuffer a vector containing buffered lines associated with     *  this pipe. If not present then it is VERY LIKELY YOU WILL LOSE MESSAGES.     *  @return String containing the new line or null if no line was available.     *  @throws IOException if the input pipe somehow becomes uninitialized.     */    protected String pipePollInput(InputPipe input, List lineBuffer) throws IOException {        boolean onePoll = false;                do {            if(null != lineBuffer) {                // Check in the buffer first                synchronized(lineBuffer) {                    if ( !lineBuffer.isEmpty() ) {                        try {                            String val = (String) lineBuffer.remove(0);                            return val;                        } catch (Exception e) {                            // This is a very strange case, but if that happens, let just                            // wait for a message on the InputPipe.                        }                    }                }            }                        if (input == null) {                throw new IOException("Input pipe null");            }                        if(onePoll) {                return null;            }                        Message msg = null;                        try {                msg = (Message) input.poll(1000);            } catch(InterruptedException woken) {                Thread.interrupted();            }                        // no message was waiting.            if(null == msg) {                return null;            }                        onePoll = true;                        // Get all the chunks of data in the message            Iterator elems = msg.getMessageElementsOfNamespace(null);            while (elems.hasNext()) {                Reader plainReader = null;                BufferedReader inputReader = null;                try {                    MessageElement elem = (MessageElement) elems.next();                    elems.remove();                    String name = elem.getElementName();                                        // because of pipe redirection we accept both input and                    // and output elements.                    if (!name.equals("ShellInputPipe") && !name.equals("ShellOutputPipe")) {                        continue;                    }                                        if(elem instanceof TextMessageElement) {                        plainReader = ((TextMessageElement)elem).getReader();                    } else {                        InputStream inputStream = elem.getStream();                        plainReader = new InputStreamReader(inputStream);                    }                                        inputReader = new BufferedReader(plainReader);                                        do {                        String command = inputReader.readLine();                        if (null != command)                            if (null != lineBuffer)                                synchronized(lineBuffer) {                                    lineBuffer.add(command);                                }                            else                                return command;                        else                            break;                    } while(true);                } catch (Throwable e) {                    e.printStackTrace();                    continue;                } finally {                    try {                        if(null != plainReader) {                            plainReader.close();                        }                    } catch (IOException ignored) {}                                        try {                        if(null != inputReader) {                            inputReader.close();                        }                    } catch (IOException ignored) {}                }            }            msg.clear();        } while(true);    }        private String pipeWaitForInput(InputPipe input, List lineBuffer, boolean join) throws IOException {        String msg = null;                while(true) {            msg = pipePollInput(input, lineBuffer);                        if((null != msg) || stopped)                break;                        if(join && ((null == dependsOn) || (!dependsOn.isAlive())))                break;        }                return msg;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -