view src/testKonoha/KonohaInvoker.java @ 2:b101ee77e78c draft

add testKonoha
author one
date Tue, 29 May 2012 00:46:14 +0900
parents
children 178aeb91d108
line wrap: on
line source

package testKonoha;

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

public class KonohaInvoker {

	final static String KONOHA = "/usr/local/bin/konoha";

	InputStream in, errin;

	void runKonoha(String[] cmd) throws KonohaException {
		try {
			Runtime run = Runtime.getRuntime();
			Process p = run.exec(cmd);
			in = p.getInputStream();
			errin = p.getInputStream();

			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			BufferedReader ebr = new BufferedReader(
					new InputStreamReader(errin));

			String kout = readMessege(br);
			String kerr = readMessege(ebr);
			p.waitFor();

			System.out.println(kout);
			System.out.println(kerr);

			if (kerr != null) {
				throw new KonohaException(kout);
			}

		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
				errin.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	void invoke(String filename, String message) throws KonohaException {
		String[] cmd = {KONOHA, filename, message};
		runKonoha(cmd);
	}
	void invoke(String[] messages) throws KonohaException {
		int num = messages.length + 1;
		String[] cmd = new String[num];
		cmd[0] = KONOHA;
		for (int i=0; i < messages.length; i++) {
			cmd[i+1] = messages[i];
		}
		runKonoha(cmd);
	}

	String readMessege(BufferedReader br) throws IOException {
		String str = "";
		String line = null;
		while ((line = br.readLine()) != null) {
			str = str + line + "\n";
		}
		return str;

	}

	public static void main(String[] args) {
		KonohaInvoker konoha = new KonohaInvoker();
		String filename = "/Users/aotokage/hg/nobuyasu/konoha/test/math.k";
		String message = "";
		try {
			konoha.invoke(filename, message);
		} catch (KonohaException e) {
			System.out.println(e);
		}

	}

}