提交 d8d031f1 authored 作者: Thomas Mueller's avatar Thomas Mueller

New TaskProcess tool

上级 e2bac4d3
/*
* Copyright 2004-2009 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.h2.test.unit.SelfDestructor;
/**
* A task that can be run as a separate process.
*/
public abstract class Task {
/**
* Run the class. This method is called by the task framework, and should
* not be called directly from the application.
*
* @param args the command line arguments
*/
public static void main(String[] args) {
SelfDestructor.startCountdown(60);
Task task;
try {
String className = args[0];
task = (Task) Class.forName(className).newInstance();
System.out.println("running");
} catch (Throwable t) {
System.out.println("init error: " + t);
t.printStackTrace();
return;
}
try {
String[] taskArgs = new String[args.length - 1];
System.arraycopy(args, 0, taskArgs, 0, args.length - 1);
task.run(taskArgs);
} catch (Throwable t) {
System.out.println("error: " + t);
t.printStackTrace();
}
}
/**
* Run the task.
*
* @param args the command line arguments
*/
abstract void run(String[] args) throws Exception;
/**
* Receive a message from the process over the standard output.
*
* @return the message
*/
protected String receive() {
try {
return new BufferedReader(new InputStreamReader(System.in)).readLine();
} catch (IOException e) {
throw new RuntimeException("Error reading from input", e);
}
}
/**
* Send a message to the process over the standard input.
*
* @param message the message
*/
protected void send(String message) {
System.out.println(message);
System.out.flush();
}
}
/*
* Copyright 2004-2009 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.db;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import org.h2.test.unit.SelfDestructor;
import org.h2.util.StringUtils;
/**
* A task that is run as an external process. This class communicates over
* standard input / output with the process. The standard error stream of the
* process is directly send to the standard error stream of this process.
*/
public class TaskProcess {
private final Task taskDef;
private Process process;
private BufferedReader reader;
private BufferedWriter writer;
/**
* Construct a new task process. The process is not started yet.
*
* @param taskDef the task
*/
public TaskProcess(Task taskDef) {
this.taskDef = taskDef;
}
/**
* Start the task with the given arguments.
*
* @param args the arguments, or null
*/
public void start(String[] args) {
try {
String selfDestruct = SelfDestructor.getPropertyString(60);
ArrayList list = new ArrayList();
list.add("java");
list.add(selfDestruct);
list.add("-cp");
list.add("bin" + File.pathSeparator + ".");
list.add(Task.class.getName());
list.add(taskDef.getClass().getName());
if (args != null && args.length > 0) {
list.addAll(Arrays.asList(args));
}
String[] procDef = new String[list.size()];
list.toArray(procDef);
traceOperation("start: " + StringUtils.arrayCombine(procDef, ' '));
process = Runtime.getRuntime().exec(procDef);
copyInThread(process.getErrorStream(), System.err);
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
String line = reader.readLine();
if (line == null) {
throw new RuntimeException("No reply from process, command: " + StringUtils.arrayCombine(procDef, ' '));
} else if (line.startsWith("running")) {
traceOperation("got reply: " + line);
} else if (line.startsWith("init error")) {
throw new RuntimeException(line);
}
} catch (Throwable t) {
throw new RuntimeException("Error starting task", t);
}
}
private void copyInThread(final InputStream in, final OutputStream out) {
new Thread() {
public void run() {
try {
while (true) {
int x = in.read();
if (x < 0) {
return;
}
if (out != null) {
out.write(x);
}
}
} catch (Exception e) {
throw new Error("Error: " + e, e);
}
}
} .start();
}
/**
* Receive a message from the process over the standard output.
*
* @return the message
*/
public String receive() {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException("Error reading", e);
}
}
/**
* Send a message to the process over the standard input.
*
* @param message the message
*/
public void send(String message) {
try {
writer.write(message + "\n");
writer.flush();
} catch (IOException e) {
throw new RuntimeException("Error writing " + message, e);
}
}
/**
* Kill the process if it still runs.
*/
public void destroy() {
process.destroy();
}
private void traceOperation(String s) {
// ignore
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论