@Override public JsonIterator iter(final Context context) throws Exception { final JsonString cmd = (JsonString) exprs[1].eval(context); if (cmd == null) { return JsonIterator.NULL; } iter = exprs[0].iter(context); if (iter.isNull()) { return JsonIterator.NULL; } proc = Runtime.getRuntime().exec(cmd.toString()); // TODO: add thread pool to context InputThread inputThread = new InputThread(); ErrorThread errorThread = new ErrorThread(); final MutableJsonString str = new MutableJsonString(); try { InputStream is = proc.getInputStream(); final BufferedReader reader = new BufferedReader(new InputStreamReader(is)); errorThread.start(); inputThread.start(); return new JsonIterator(str) { @Override public boolean moveNext() throws Exception { try { String s = reader.readLine(); if (s == null) { reader.close(); int rc = proc.waitFor(); if (rc != 0) { System.err.println("non-zero exit code from process [" + cmd + "]: " + rc); } return false; } str.setCopy(s); return true; // currentValue == str } catch (Throwable e) { if (error == null) { error = e; } proc.destroy(); if (error instanceof Exception) { throw (Exception) error; } throw new UndeclaredThrowableException(error); } } }; } catch (Throwable e) { if (error == null) { error = e; } proc.destroy(); if (error instanceof Exception) { throw (Exception) error; } throw new UndeclaredThrowableException(error); } }
private void initProcess(Context context) throws Exception { ProcessBuilder pb = new ProcessBuilder(); if (cmd instanceof JsonString) { String tmp = ((JsonString) cmd).toString(); String[] cmdArray = tmp.split(" "); ArrayList<String> array = new ArrayList<String>(); for (String s : cmdArray) { if (s.length() > 0) array.add(s.trim()); } pb.command(array); } Configuration cfg = new Configuration(); File directory = new File(cfg.get("mapred.local.dir", ".")); pb.directory(directory); // unset environment variables that jvm dump to stderr pb.environment().remove("_JAVA_OPTIONS"); pb.environment().remove("JAVA_TOOL_OPTIONS"); process = pb.start(); ErrorThread errorThread = new ErrorThread(); errorThread.start(); stdin = process.getInputStream(); stdout = process.getOutputStream(); OutputAdapter outAdapter = (OutputAdapter) JaqlUtil.getAdapterStore().output.getAdapter(writeOpts); if (!(outAdapter instanceof StreamOutputAdapter)) throw new IllegalArgumentException( "The adapter of writeOpts must be an instance of com.ibm.jaql.io.stream.StreamOutputAdapter"); ((StreamOutputAdapter) outAdapter).setDefaultOutput(stdout); outAdapter.open(); writer = outAdapter.getWriter(); InputAdapter inAdapter = (InputAdapter) JaqlUtil.getAdapterStore().input.getAdapter(readOpts); if (!(inAdapter instanceof StreamInputAdapter)) throw new IllegalArgumentException( "The adapter of readOpts must be an instance of com.ibm.jaql.io.stream.ExternalCallStreamInputAdapter"); ((StreamInputAdapter) inAdapter).setInputStream(stdin); inAdapter.open(); reader = inAdapter.iter(); }
private void init(InputStream in, OutputStream out, InputStream err) { m_out = new PrintWriter(out); m_isProgramDead = false; m_queue = new ArrayBlockingQueue<Message>(10); m_inputThread = new InputThread(in, m_queue); if (err != null) { m_errorThread = new ErrorThread(err, m_queue); m_errorThread.start(); } m_inputThread.start(); }