private void consumeLine(String line) { lastHeard = System.currentTimeMillis(); if (streamConsumer != null) { if (StringUtils.isBlank(prefix)) { streamConsumer.consumeLine(line); } else { streamConsumer.consumeLine(prefix + line); } } }
/** * processes the input String and produces an output String. * * @param input the String to input to the underlying process * @throws java.lang.Exception exceptions from Runtime and Process that this class uses * @return the output from the underlying process */ public String process(String input) throws Exception { String returnValue = new String(); StringBuffer buffer = new StringBuffer(); if (socketServer == true) { returnValue = client.process(input); ServerThread current = getServer(); if (current != null) { if (!current.isDaemon()) { current.getProcess().destroy(); } current.interrupt(); } } else { // not a socket server Runtime rt = Runtime.getRuntime(); String output = ""; String errors = ""; try { p = rt.exec(getCodeCall() + " " + getParams()); PrintStream processInputStream = new PrintStream(p.getOutputStream(), true, "utf-8"); processInputStream.println(input + "\n\u0003"); // put in garbage to kill Bikel's loop StreamConsumer errorConsumer = new StreamConsumer(p.getErrorStream(), "error", 1); StreamConsumer outputConsumer = new StreamConsumer(p.getInputStream(), "output", 10); outputConsumer.start(); errorConsumer.start(); int countloops = 0; int time = 0; String message = ""; while (!outputConsumer.isComplete()) { // wait Thread.sleep(100); countloops++; time += 100; // one tenth of a second if (time > waittime) { // just wait 5 minutes message = "exceeded waittime of " + waittime + " milliseconds"; break; } } errors = errorConsumer.getOutput(); output = outputConsumer.getOutput(); if (!message.equals("")) { errors = message; } } catch (IOException ioe) { System.err.println("Module error: " + getModuleName()); ioe.printStackTrace(); System.exit(0); } p.destroy(); if (errors.equals("")) { returnValue = output; } else { returnValue = errors; } } return returnValue; }