List of usage examples for org.apache.commons.exec DefaultExecutor setStreamHandler
public void setStreamHandler(final ExecuteStreamHandler streamHandler)
From source file:org.apache.stratos.integration.tests.SampleApplicationTests.java
private void executeCommand(String commandText) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try {//w ww . ja v a 2s . co m CommandLine commandline = CommandLine.parse(commandText); DefaultExecutor exec = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); exec.setStreamHandler(streamHandler); exec.execute(commandline); log.info(outputStream.toString()); } catch (Exception e) { log.error(outputStream.toString(), e); throw new RuntimeException(e); } }
From source file:org.apache.stratos.python.cartridge.agent.integration.tests.PythonAgentIntegrationTest.java
/** * Execute shell command/*from w w w . ja v a 2s . c om*/ * * @param commandText Command string to be executed */ protected ByteArrayOutputStreamLocal executeCommand(final String commandText, int timeout) { final ByteArrayOutputStreamLocal outputStream = new ByteArrayOutputStreamLocal(); try { CommandLine commandline = CommandLine.parse(commandText); DefaultExecutor exec = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); exec.setWorkingDirectory(new File(PythonAgentIntegrationTest.class.getResource(PATH_SEP).getPath() + PATH_SEP + ".." + PATH_SEP + PYTHON_AGENT_DIR_NAME)); exec.setStreamHandler(streamHandler); ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); exec.setWatchdog(watchdog); exec.execute(commandline, new ExecuteResultHandler() { @Override public void onProcessComplete(int i) { log.info(commandText + " process completed"); } @Override public void onProcessFailed(ExecuteException e) { log.error(commandText + " process failed", e); } }); executorList.put(commandText, exec); return outputStream; } catch (Exception e) { log.error(outputStream.toString(), e); throw new RuntimeException(e); } }
From source file:org.apache.stratos.python.cartridge.agent.test.PythonAgentTestManager.java
/** * Execute shell command// w w w . j a v a 2s . c om * * @param commandText */ protected ByteArrayOutputStreamLocal executeCommand(final String commandText) { final ByteArrayOutputStreamLocal outputStream = new ByteArrayOutputStreamLocal(); try { CommandLine commandline = CommandLine.parse(commandText); DefaultExecutor exec = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); exec.setWorkingDirectory(new File(PythonAgentTestManager.class.getResource(PATH_SEP).getPath() + PATH_SEP + ".." + PATH_SEP + PYTHON_AGENT_DIR_NAME)); exec.setStreamHandler(streamHandler); ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT); exec.setWatchdog(watchdog); exec.execute(commandline, new ExecuteResultHandler() { @Override public void onProcessComplete(int i) { log.info(commandText + " process completed"); } @Override public void onProcessFailed(ExecuteException e) { log.error(commandText + " process failed", e); } }); executorList.put(commandText, exec); return outputStream; } catch (Exception e) { log.error(outputStream.toString(), e); throw new RuntimeException(e); } }
From source file:org.apache.stratos.python.cartridge.agent.test.PythonCartridgeAgentTest.java
/** * Execute shell command// www . j a v a 2 s .c o m * * @param commandText */ private ByteArrayOutputStreamLocal executeCommand(final String commandText) { final ByteArrayOutputStreamLocal outputStream = new ByteArrayOutputStreamLocal(); try { CommandLine commandline = CommandLine.parse(commandText); DefaultExecutor exec = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); exec.setStreamHandler(streamHandler); ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT); exec.setWatchdog(watchdog); exec.execute(commandline, new ExecuteResultHandler() { @Override public void onProcessComplete(int i) { log.info(commandText + " process completed"); } @Override public void onProcessFailed(ExecuteException e) { log.error(commandText + " process failed", e); } }); executorList.put(commandText, exec); return outputStream; } catch (Exception e) { log.error(outputStream.toString(), e); throw new RuntimeException(e); } }
From source file:org.apache.tika.parser.geo.topic.GeoParser.java
public HashMap<String, ArrayList<String>> searchGeoNames(ArrayList<String> locationNameEntities) throws ExecuteException, IOException { CommandLine cmdLine = new CommandLine("lucene-geo-gazetteer"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); cmdLine.addArgument("-s"); for (String name : locationNameEntities) { cmdLine.addArgument(name);/* ww w . jav a2 s . com*/ } LOG.fine("Executing: " + cmdLine); DefaultExecutor exec = new DefaultExecutor(); exec.setExitValue(0); ExecuteWatchdog watchdog = new ExecuteWatchdog(60000); exec.setWatchdog(watchdog); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); exec.setStreamHandler(streamHandler); int exitValue = exec.execute(cmdLine, EnvironmentUtils.getProcEnvironment()); String outputJson = outputStream.toString("UTF-8"); JSONArray json = (JSONArray) JSONValue.parse(outputJson); HashMap<String, ArrayList<String>> returnHash = new HashMap<String, ArrayList<String>>(); for (int i = 0; i < json.size(); i++) { JSONObject obj = (JSONObject) json.get(i); for (Object key : obj.keySet()) { String theKey = (String) key; JSONArray vals = (JSONArray) obj.get(theKey); ArrayList<String> stringVals = new ArrayList<String>(vals.size()); for (int j = 0; j < vals.size(); j++) { String val = (String) vals.get(j); stringVals.add(val); } returnHash.put(theKey, stringVals); } } return returnHash; }
From source file:org.apache.tika.parser.ocr.TesseractOCRParser.java
/** * This method is used to process the image to an OCR-friendly format. * @param streamingObject input image to be processed * @param config TesseractOCRconfig class to get ImageMagick properties * @throws IOException if an input error occurred * @throws TikaException if an exception timed out *///from w w w . ja va2 s . c o m private void processImage(File streamingObject, TesseractOCRConfig config) throws IOException, TikaException { // fetch rotation script from resources InputStream in = getClass().getResourceAsStream("rotation.py"); TemporaryResources tmp = new TemporaryResources(); File rotationScript = tmp.createTemporaryFile(); Files.copy(in, rotationScript.toPath(), StandardCopyOption.REPLACE_EXISTING); String cmd = "python " + rotationScript.getAbsolutePath() + " -f " + streamingObject.getAbsolutePath(); String angle = "0"; DefaultExecutor executor = new DefaultExecutor(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); executor.setStreamHandler(streamHandler); // determine the angle of rotation required to make the text horizontal CommandLine cmdLine = CommandLine.parse(cmd); if (hasPython()) { try { executor.execute(cmdLine); angle = outputStream.toString("UTF-8").trim(); } catch (Exception e) { } } // process the image - parameter values can be set in TesseractOCRConfig.properties String line = "convert -density " + config.getDensity() + " -depth " + config.getDepth() + " -colorspace " + config.getColorspace() + " -filter " + config.getFilter() + " -resize " + config.getResize() + "% -rotate " + angle + " " + streamingObject.getAbsolutePath() + " " + streamingObject.getAbsolutePath(); cmdLine = CommandLine.parse(line); try { executor.execute(cmdLine); } catch (Exception e) { } tmp.close(); }
From source file:org.apache.tika.parser.pot.PooledTimeSeriesParser.java
private String computePoT(File input) throws IOException, TikaException { CommandLine cmdLine = new CommandLine("pooled-time-series"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); cmdLine.addArgument("-f"); cmdLine.addArgument(input.getAbsolutePath()); LOG.trace("Executing: {}", cmdLine); DefaultExecutor exec = new DefaultExecutor(); exec.setExitValue(0);//from w w w . j ava 2s . c om ExecuteWatchdog watchdog = new ExecuteWatchdog(60000); exec.setWatchdog(watchdog); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); exec.setStreamHandler(streamHandler); int exitValue = exec.execute(cmdLine, EnvironmentUtils.getProcEnvironment()); return outputStream.toString("UTF-8"); }
From source file:org.apache.zeppelin.interpreter.launcher.Kubectl.java
public int execute(String[] args, InputStream stdin, OutputStream stdout, OutputStream stderr) throws IOException { DefaultExecutor executor = new DefaultExecutor(); CommandLine cmd = new CommandLine(kubectlCmd); cmd.addArguments(args);//from w ww. ja v a 2 s. co m ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 1000); executor.setWatchdog(watchdog); PumpStreamHandler streamHandler = new PumpStreamHandler(stdout, stderr, stdin); executor.setStreamHandler(streamHandler); return executor.execute(cmd); }
From source file:org.apache.zeppelin.python.IPythonInterpreter.java
private void launchIPythonKernel(int ipythonPort) throws IOException { LOGGER.info("Launching IPython Kernel at port: " + ipythonPort); // copy the python scripts to a temp directory, then launch ipython kernel in that folder File pythonWorkDir = Files.createTempDirectory("zeppelin_ipython").toFile(); String[] ipythonScripts = { "ipython_server.py", "ipython_pb2.py", "ipython_pb2_grpc.py" }; for (String ipythonScript : ipythonScripts) { URL url = getClass().getClassLoader().getResource("grpc/python" + "/" + ipythonScript); FileUtils.copyURLToFile(url, new File(pythonWorkDir, ipythonScript)); }//w ww . j ava2s . co m CommandLine cmd = CommandLine.parse(pythonExecutable); cmd.addArgument(pythonWorkDir.getAbsolutePath() + "/ipython_server.py"); cmd.addArgument(ipythonPort + ""); DefaultExecutor executor = new DefaultExecutor(); ProcessLogOutputStream processOutput = new ProcessLogOutputStream(LOGGER); executor.setStreamHandler(new PumpStreamHandler(processOutput)); watchDog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT); executor.setWatchdog(watchDog); if (useBuiltinPy4j) { //TODO(zjffdu) don't do hard code on py4j here File py4jDestFile = new File(pythonWorkDir, "py4j-src-0.10.7.zip"); FileUtils.copyURLToFile(getClass().getClassLoader().getResource("python/py4j-src-0.10.7.zip"), py4jDestFile); if (additionalPythonPath != null) { // put the py4j at the end, because additionalPythonPath may already contain py4j. // e.g. PySparkInterpreter additionalPythonPath = additionalPythonPath + ":" + py4jDestFile.getAbsolutePath(); } else { additionalPythonPath = py4jDestFile.getAbsolutePath(); } } Map<String, String> envs = setupIPythonEnv(); executor.execute(cmd, envs, this); // wait until IPython kernel is started or timeout long startTime = System.currentTimeMillis(); while (true) { try { Thread.sleep(100); } catch (InterruptedException e) { LOGGER.error("Interrupted by something", e); } try { StatusResponse response = ipythonClient.status(StatusRequest.newBuilder().build()); if (response.getStatus() == IPythonStatus.RUNNING) { LOGGER.info("IPython Kernel is Running"); break; } else { LOGGER.info("Wait for IPython Kernel to be started"); } } catch (Exception e) { // ignore the exception, because is may happen when grpc server has not started yet. LOGGER.info("Wait for IPython Kernel to be started"); } if ((System.currentTimeMillis() - startTime) > ipythonLaunchTimeout) { throw new IOException( "Fail to launch IPython Kernel in " + ipythonLaunchTimeout / 1000 + " seconds"); } } }
From source file:org.apache.zeppelin.shell.ShellInterpreter.java
@Override public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) { logger.debug("Run shell command '" + cmd + "'"); long start = System.currentTimeMillis(); CommandLine cmdLine = CommandLine.parse("bash"); cmdLine.addArgument("-c", false); cmdLine.addArgument(cmd, false);/* w w w .j ava 2 s. c om*/ DefaultExecutor executor = new DefaultExecutor(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(outputStream)); executor.setWatchdog(new ExecuteWatchdog(commandTimeOut)); try { int exitValue = executor.execute(cmdLine); return new InterpreterResult(InterpreterResult.Code.SUCCESS, outputStream.toString()); } catch (ExecuteException e) { logger.error("Can not run " + cmd, e); return new InterpreterResult(Code.ERROR, e.getMessage()); } catch (IOException e) { logger.error("Can not run " + cmd, e); return new InterpreterResult(Code.ERROR, e.getMessage()); } }