📄 executecommands.java
字号:
package org.osu.ogsa.stream.util;/** * This ExecuteCommands class can execute a command and reture an inputStream * */import java.io.*;import java.nio.*;import java.lang.*;import java.util.*;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** One ExecuteCommand is created to handle each script. */class ExecuteCommands extends Thread{ private String commandLine = null; private Object synObject ; private ByteBuffer buffer; private String strBuffer; private int buf_len; private static Log logger = LogFactory.getLog(ExecuteCommands.class.getName()); ExecuteCommands(String commandLine, ByteBuffer buffer, Object synObject) { this.commandLine = commandLine; this.buffer = buffer; this.synObject = synObject; logger.debug("Creating ExecuteCommands for "+commandLine); } ExecuteCommands(String commandLine, String strBuffer, Object synObject) { this.commandLine = commandLine; this.strBuffer = strBuffer; this.synObject = synObject; logger.debug("Creating ExecuteCommands for "+commandLine); } public void run() { logger.debug("Executing "+commandLine); Runtime runtime = Runtime.getRuntime(); try { int scriptExitCode; Process process; //byte [] bufArray = new byte[1000]; byte [] bufArray = buffer.array(); int capacity = buffer.capacity(); // start the script process = runtime.exec(commandLine); InputStream processInputStream = process.getInputStream(); int read_len = 0; int read_num_bytes = 0; while((read_num_bytes = processInputStream.read(bufArray, read_len, 1)) != -1) { if(read_num_bytes == 0) continue;// logger.debug("read_num_bytes:" + read_num_bytes + " read_len: " + read_len); read_len ++ ; if(read_len == capacity) { logger.error("the size of the buffer is too small, there are still some data in the input stream"); bufArray[read_len - 1]= 0; String str = new String(bufArray); logger.error(str); break; } }// buffer.rewind(); scriptExitCode = process.waitFor(); processInputStream.close(); process.destroy(); /* examine error code following standard unix semantics, we expect a zero exit code to indicate successful completion. if non-zero, we log a warning, but other than that we don't change the way that we handle the output. */ if(scriptExitCode != 0) { logger.warn(commandLine+" returned a non-zero exit code: "+scriptExitCode); } // notify upwards that a change has occurred synchronized(synObject) { synObject.notifyAll(); } } catch (Exception e) { String errorString = "Execution error: " + e.getMessage ()+ " while processing "+commandLine; logger.error(errorString); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -