Search in sources :

Example 6 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler in project tika by apache.

the class TesseractOCRParser method processImage.

/**
     * 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
     */
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();
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) TikaInputStream(org.apache.tika.io.TikaInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TemporaryResources(org.apache.tika.io.TemporaryResources) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) TikaException(org.apache.tika.exception.TikaException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 7 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler in project opennms by OpenNMS.

the class RrdtoolXportFetchStrategy method fetchMeasurements.

/**
 * {@inheritDoc}
 */
@Override
protected FetchResults fetchMeasurements(long start, long end, long step, int maxrows, Map<Source, String> rrdsBySource, Map<String, Object> constants) throws RrdException {
    String rrdBinary = System.getProperty("rrd.binary");
    if (rrdBinary == null) {
        throw new RrdException("No RRD binary is set.");
    }
    final long startInSeconds = (long) Math.floor(start / 1000d);
    final long endInSeconds = (long) Math.floor(end / 1000d);
    long stepInSeconds = (long) Math.floor(step / 1000d);
    // The step must be strictly positive
    if (stepInSeconds <= 0) {
        stepInSeconds = 1;
    }
    final CommandLine cmdLine = new CommandLine(rrdBinary);
    cmdLine.addArgument("xport");
    cmdLine.addArgument("--step");
    cmdLine.addArgument("" + stepInSeconds);
    cmdLine.addArgument("--start");
    cmdLine.addArgument("" + startInSeconds);
    cmdLine.addArgument("--end");
    cmdLine.addArgument("" + endInSeconds);
    if (maxrows > 0) {
        cmdLine.addArgument("--maxrows");
        cmdLine.addArgument("" + maxrows);
    }
    // Use labels without spaces when executing the xport command
    // These are mapped back to the requested labels in the response
    final Map<String, String> labelMap = Maps.newHashMap();
    int k = 0;
    for (final Map.Entry<Source, String> entry : rrdsBySource.entrySet()) {
        final Source source = entry.getKey();
        final String rrdFile = entry.getValue();
        final String tempLabel = Integer.toString(++k);
        labelMap.put(tempLabel, source.getLabel());
        cmdLine.addArgument(String.format("DEF:%s=%s:%s:%s", tempLabel, Utils.escapeColons(rrdFile), Utils.escapeColons(source.getEffectiveDataSource()), source.getAggregation()));
        cmdLine.addArgument(String.format("XPORT:%s:%s", tempLabel, tempLabel));
    }
    // Use commons-exec to execute rrdtool
    final DefaultExecutor executor = new DefaultExecutor();
    // Capture stdout/stderr
    final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    final ByteArrayOutputStream stderr = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(stdout, stderr, null));
    // Fail if we get a non-zero exit code
    executor.setExitValue(0);
    // Fail if the process takes too long
    final ExecuteWatchdog watchdog = new ExecuteWatchdog(XPORT_TIMEOUT_MS);
    executor.setWatchdog(watchdog);
    // Export
    RrdXport rrdXport;
    try {
        LOG.debug("Executing: {}", cmdLine);
        executor.execute(cmdLine);
        final XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        final SAXSource source = new SAXSource(xmlReader, new InputSource(new StringReader(stdout.toString())));
        final JAXBContext jc = JAXBContext.newInstance(RrdXport.class);
        final Unmarshaller u = jc.createUnmarshaller();
        rrdXport = (RrdXport) u.unmarshal(source);
    } catch (IOException e) {
        throw new RrdException("An error occured while executing '" + StringUtils.join(cmdLine.toStrings(), " ") + "' with stderr: " + stderr.toString(), e);
    } catch (SAXException | JAXBException e) {
        throw new RrdException("The output generated by 'rrdtool xport' could not be parsed.", e);
    }
    final int numRows = rrdXport.getRows().size();
    final int numColumns = rrdXport.getMeta().getLegends().size();
    final long xportStartInMs = rrdXport.getMeta().getStart() * 1000;
    final long xportStepInMs = rrdXport.getMeta().getStep() * 1000;
    final long[] timestamps = new long[numRows];
    final double[][] values = new double[numColumns][numRows];
    // Convert rows to columns
    int i = 0;
    for (final XRow row : rrdXport.getRows()) {
        // Derive the timestamp from the start and step since newer versions
        // of rrdtool no longer include it as part of the rows
        timestamps[i] = xportStartInMs + xportStepInMs * i;
        for (int j = 0; j < numColumns; j++) {
            if (row.getValues() == null) {
                // NMS-7710: Avoid NPEs, in certain cases the list of values may be null
                throw new RrdException("The output generated by 'rrdtool xport' was not recognized. Try upgrading your rrdtool binaries.");
            }
            values[j][i] = row.getValues().get(j);
        }
        i++;
    }
    // Map the columns by label
    // The legend entries are in the same order as the column values
    final Map<String, double[]> columns = Maps.newHashMapWithExpectedSize(numColumns);
    i = 0;
    for (String label : rrdXport.getMeta().getLegends()) {
        columns.put(labelMap.get(label), values[i++]);
    }
    return new FetchResults(timestamps, columns, xportStepInMs, constants);
}
Also used : InputSource(org.xml.sax.InputSource) JAXBContext(javax.xml.bind.JAXBContext) XRow(org.opennms.netmgt.rrd.model.XRow) RrdXport(org.opennms.netmgt.rrd.model.RrdXport) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) Source(org.opennms.netmgt.measurements.model.Source) SAXException(org.xml.sax.SAXException) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) FetchResults(org.opennms.netmgt.measurements.api.FetchResults) StringReader(java.io.StringReader) RrdException(org.jrobin.core.RrdException) Unmarshaller(javax.xml.bind.Unmarshaller) XMLReader(org.xml.sax.XMLReader) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) JAXBException(javax.xml.bind.JAXBException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) CommandLine(org.apache.commons.exec.CommandLine) SAXSource(javax.xml.transform.sax.SAXSource) Map(java.util.Map)

Example 8 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler in project oxCore by GluuFederation.

the class ProcessHelper method executeProgram.

public static boolean executeProgram(CommandLine commandLine, String workingDirectory, boolean executeInBackground, int successExitValue, OutputStream outputStream) {
    long printJobTimeout = PRINT_JOB_TIMEOUT;
    ExecuteStreamHandler streamHandler = null;
    if (outputStream != null) {
        streamHandler = new PumpStreamHandler(outputStream);
    }
    PrintResultHandler printResult = null;
    try {
        LOG.debug(String.format("Preparing to start process %s", commandLine.toString()));
        printResult = executeProgram(commandLine, workingDirectory, printJobTimeout, executeInBackground, successExitValue, streamHandler);
        LOG.debug(String.format("Successfully start process %s", commandLine.toString()));
    } catch (Exception ex) {
        LOG.trace(String.format("Problem during starting process %s", commandLine.toString()), ex);
        ex.printStackTrace();
        return false;
    }
    // come back to check the print result
    LOG.debug(String.format("Waiting for the proces %s finish", commandLine.toString()));
    try {
        if (printResult == null) {
            return false;
        }
        printResult.waitFor();
    } catch (InterruptedException ex) {
        LOG.error(String.format("Problem during process execution %s", commandLine.toString()), ex);
    }
    LOG.debug(String.format("Process %s has finished", commandLine.toString()));
    return true;
}
Also used : PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteStreamHandler(org.apache.commons.exec.ExecuteStreamHandler) IOException(java.io.IOException) ExecuteException(org.apache.commons.exec.ExecuteException)

Example 9 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler in project project-build-plugin by axonivy.

the class EngineControl method executeSynch.

/**
 * Run a short living engine command where we expect a process failure as the engine invokes <code>System.exit(-1)</code>.
 * @param statusCmd
 * @return the output of the engine command.
 */
private String executeSynch(CommandLine statusCmd) {
    String engineOutput = null;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, System.err);
    Executor executor = createEngineExecutor();
    executor.setStreamHandler(streamHandler);
    executor.setExitValue(-1);
    try {
        executor.execute(statusCmd);
    } catch (IOException ex) {
    // expected!
    } finally {
        engineOutput = outputStream.toString();
        IOUtils.closeQuietly(outputStream);
    }
    return engineOutput;
}
Also used : PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 10 with PumpStreamHandler

use of org.apache.commons.exec.PumpStreamHandler in project camel by apache.

the class DefaultExecCommandExecutor method execute.

@Override
public ExecResult execute(ExecCommand command) {
    notNull(command, "command");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayOutputStream err = new ByteArrayOutputStream();
    DefaultExecutor executor = prepareDefaultExecutor(command);
    // handle error and output of the process and write them to the given
    // out stream
    PumpStreamHandler handler = new PumpStreamHandler(out, err, command.getInput());
    executor.setStreamHandler(handler);
    CommandLine cl = toCommandLine(command);
    try {
        int exitValue = executor.execute(cl);
        // if the size is zero, we have no output, so construct the result
        // with null (required by ExecResult)
        InputStream stdout = out.size() == 0 ? null : new ByteArrayInputStream(out.toByteArray());
        InputStream stderr = err.size() == 0 ? null : new ByteArrayInputStream(err.toByteArray());
        ExecResult result = new ExecResult(command, stdout, stderr, exitValue);
        return result;
    } catch (ExecuteException ee) {
        LOG.error("ExecException while executing command: " + command.toString() + " - " + ee.getMessage());
        InputStream stdout = out.size() == 0 ? null : new ByteArrayInputStream(out.toByteArray());
        InputStream stderr = err.size() == 0 ? null : new ByteArrayInputStream(err.toByteArray());
        throw new ExecException("Failed to execute command " + command, stdout, stderr, ee.getExitValue(), ee);
    } catch (IOException ioe) {
        InputStream stdout = out.size() == 0 ? null : new ByteArrayInputStream(out.toByteArray());
        InputStream stderr = err.size() == 0 ? null : new ByteArrayInputStream(err.toByteArray());
        // use 0 as exit value as the executor didn't return the value
        int exitValue = 0;
        if (executor instanceof ExecDefaultExecutor) {
            // get the exit value from the executor as it captures this to work around the common-exec bug
            exitValue = ((ExecDefaultExecutor) executor).getExitValue();
        }
        // workaround to ignore if the stream was already closes due some race condition in commons-exec
        String msg = ioe.getMessage();
        if (msg != null && "stream closed".equals(msg.toLowerCase(Locale.ENGLISH))) {
            LOG.debug("Ignoring Stream closed IOException", ioe);
            ExecResult result = new ExecResult(command, stdout, stderr, exitValue);
            return result;
        }
        // invalid working dir
        LOG.error("IOException while executing command: " + command.toString() + " - " + ioe.getMessage());
        throw new ExecException("Unable to execute command " + command, stdout, stderr, exitValue, ioe);
    } finally {
        // the inputStream must be closed after the execution
        IOUtils.closeQuietly(command.getInput());
    }
}
Also used : ExecDefaultExecutor(org.apache.camel.component.exec.ExecDefaultExecutor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ExecException(org.apache.camel.component.exec.ExecException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ExecEndpoint(org.apache.camel.component.exec.ExecEndpoint) ExecDefaultExecutor(org.apache.camel.component.exec.ExecDefaultExecutor) CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) ExecuteException(org.apache.commons.exec.ExecuteException) ExecResult(org.apache.camel.component.exec.ExecResult)

Aggregations

PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)25 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)20 CommandLine (org.apache.commons.exec.CommandLine)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 IOException (java.io.IOException)16 ExecuteException (org.apache.commons.exec.ExecuteException)11 ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)10 File (java.io.File)5 InputStream (java.io.InputStream)4 PipedInputStream (java.io.PipedInputStream)4 PipedOutputStream (java.io.PipedOutputStream)4 OutputStream (java.io.OutputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 Map (java.util.Map)3 ExecuteResultHandler (org.apache.commons.exec.ExecuteResultHandler)3 ExecuteStreamHandler (org.apache.commons.exec.ExecuteStreamHandler)3 Executor (org.apache.commons.exec.Executor)3 BufferedWriter (java.io.BufferedWriter)2 DataInputStream (java.io.DataInputStream)2 FileOutputStream (java.io.FileOutputStream)2