中国开发网: 论坛: Java/J2ME: 贴子 228232
空山新雨: ScriptRunner.java
/*
* Created on Jun 8, 2005
*
* The action specific to execute PNLC process.
*/
package com.bnpparibas.command;

import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

import org.apache.log4j.Logger;

/**
* @author Alex SUO
*
* The action for the business logic to PNLC process.
*/
public class ScriptRunner {

/** The logger for this class. */
private Logger LOGGER = Logger.getLogger(this.getClass());

/** The script to be run by the action. */
protected String script;

/**
* Constructor.
* @param script The script string which will be
* run by the action class.
*/
public ScriptRunner(String script) {
this.script = script;
}

/**
* Set the parameter in the script. The parameter is marked
* by the passed key with 2 "%" at each end of it. The method
* will replace "%key%" in the script and replace it by the value.
* @param key The key in the script.
* @param value The value of the parameter.
*/
public void setParam(String key, String value) {
script = script.replaceAll("%" + key + "%", value);
}

/**
* Execute the batch script defined. The commands have to be
* run line by line, otherwise it doesn't work.
*/
public void execute() throws Exception {
String[] cmd = script.split("\n");
for (int i = 0; i < cmd.length; i++) {
execute(cmd[i]);
}
}

/**
* Launch a script process.
* @throws IOException If any caused by the runtime.
* @see com.bnpparibas.command.action.Action#execute()
*/
protected synchronized void execute(String content) throws Exception {
LOGGER.debug(content);

String cmd[] = {"cmd.exe", "/c", content};
Process p = Runtime.getRuntime().exec(cmd);

//The following redirection is necessary, otherwise the command prompt
//buffer will overflow and the process will hang.
final Thread curThread = Thread.currentThread();
final BufferedInputStream is = new BufferedInputStream(p.getInputStream());
final BufferedReader br = new BufferedReader(new InputStreamReader(is));
final BufferedInputStream es = new BufferedInputStream(p.getErrorStream());
final BufferedReader er = new BufferedReader(new InputStreamReader(es));
String line = null;

while ( null != (line = br.readLine()) ) {
LOGGER.debug("command line: " + line);

//absorb the output in the err stream, otherwise the process will hang.
int exitValue = -1;
try {
exitValue = p.exitValue();
} catch (IllegalThreadStateException e) {
//do nothing here since this is expected when the thread isn't finished.
}
if (0 == exitValue) {
p.destroy();
}

//read the err stream
Thread err = new Thread() {
public void run() {
getErr();
try {
join(1000);
} catch (InterruptedException e) {
}
}
public synchronized boolean getErr() {
boolean b = false;
try {
String errLine;
while ( null != (errLine = er.readLine()) ) {
if ("\n" != errLine.trim() && "\r" != errLine.trim()) {
LOGGER.error(errLine);
try {
curThread.interrupt();
} catch (Exception e) {
} finally {
Thread.yield();
}
}
}
b = true;
} catch (IOException ioe) {
} catch (Exception e) {
b = false;
} finally {
try {
join(3000);
} catch (InterruptedException e) {
destroy();
}
}
return b;
}
};
err.start();
}
br.close();
is.close();
es.close();
er.close();

LOGGER.debug("Process finished with value " + String.valueOf(p.waitFor()));
}
}

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录