package org.ngbw.pise.commandrenderer; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import org.apache.log4j.Logger; public class PerlEval { private static final Logger logger = Logger.getLogger(PerlEval.class); Process worker = null; BufferedReader stdout = null; BufferedReader stderr = null; PrintStream stdin = null; public static void main ( String[] args ) throws Exception { String[] perlStatements = { "print \"hi there\"\n", "print \"two\\nlines\\n\"\n", "(! 1 )? print \" -endgaps\": print \"\"\n", "((\"-align\" =~ /align/ ))? print \"true\" : print \"false\";\n", "print <<\"EOT\"\nset Increase = no;\\n\nEOT\n", "print <<\"EOT\"\nset Increase = no;\\nsecond line\n\n\nEOT\n", "print" }; PerlEval perlEval = null; try { String result; perlEval = new PerlEval(); perlEval.initialize(); for (int i = 0; i < perlStatements.length; i++) { logger.debug("Statement: " + perlStatements[i]); result = perlEval.evaluateStatement(perlStatements[i]); logger.debug("Evaluates to: " + result); } perlEval.terminate(); } catch ( Exception e ) { logger.error("", e); perlEval.cleanup(); } } public PerlEval () {} public void initialize () throws Exception { String[] command = new String[1]; try { command[0] = "piseEval"; logger.info("initialize()::Running piseEval ..."); worker = Runtime.getRuntime().exec(command); stdout = new BufferedReader(new InputStreamReader(worker.getInputStream())); stderr = new BufferedReader(new InputStreamReader(worker.getErrorStream())); stdin = new PrintStream(new BufferedOutputStream(worker.getOutputStream(), 8192)); } catch ( Exception e ) { logger.error("Runtime exec of piseEval failed. Make sure piseEval script is in the path and executable.", e); throw e; } } public void terminate () throws Exception { logger.debug("BEGIN: terminate()::void"); try { stdin.close(); stdin = null; // todo: need to read stdout and stderr? final int exitCode = worker.waitFor(); if (exitCode != 0) { throw new RuntimeException("Exit value was not 0 but " + exitCode); } } finally { cleanup(); } logger.debug("END: terminate()::void"); } public void cleanup () { logger.debug("BEGIN: cleanup()::void"); try { if (stdin != null) { stdin.close(); stdin = null; } if (stdout != null) { stdout.close(); stdout = null; } if (stderr != null) { stderr.close(); stderr = null; } } catch ( Exception e ) { logger.error("Caught exception:" + e.getMessage()); } logger.debug("END: cleanup()::void"); } public String evaluateStatement ( String statement ) throws Exception { String response; String error = null; String result = ""; //log.debug("Sending:" + statement); if (!statement.endsWith("\n")) { statement += "\n"; } stdin.print(statement); stdin.print("piseEnd\n"); stdin.flush(); while ((response = stdout.readLine()) != null) { //log.debug("Read:" + response); if (response.equals("PiseEval:")) { break; } else if (response.startsWith("PiseEvalError=")) { logger.error("Starting error message: " + error); error = response + "\n"; } else if (error != null) { logger.error("Adding to error message: " + error); error += (response + "\n"); } else { //log.debug("Adding to result: " + result); result += (response + "\n"); } } if (response == null) { throw new Exception("Unexepected EOF from piseEval"); } if (error != null) { logger.error(error); throw new Exception("piseEval error"); } return result.trim(); } }