camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r938323 - in /camel/trunk/components/camel-exec/src: main/java/org/apache/camel/component/exec/ExecResultConverter.java test/java/org/apache/camel/component/exec/ExecJavaProcessTest.java
Date Tue, 27 Apr 2010 04:38:25 GMT
Author: davsclaus
Date: Tue Apr 27 04:38:25 2010
New Revision: 938323

URL: http://svn.apache.org/viewvc?rev=938323&view=rev
Log:
CAMEL-2549: Applied patch with thanks to Mitko Kolev.

Modified:
    camel/trunk/components/camel-exec/src/main/java/org/apache/camel/component/exec/ExecResultConverter.java
    camel/trunk/components/camel-exec/src/test/java/org/apache/camel/component/exec/ExecJavaProcessTest.java

Modified: camel/trunk/components/camel-exec/src/main/java/org/apache/camel/component/exec/ExecResultConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-exec/src/main/java/org/apache/camel/component/exec/ExecResultConverter.java?rev=938323&r1=938322&r2=938323&view=diff
==============================================================================
--- camel/trunk/components/camel-exec/src/main/java/org/apache/camel/component/exec/ExecResultConverter.java
(original)
+++ camel/trunk/components/camel-exec/src/main/java/org/apache/camel/component/exec/ExecResultConverter.java
Tue Apr 27 04:38:25 2010
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.exec;
 
+import java.io.ByteArrayInputStream;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
@@ -50,7 +51,12 @@ public final class ExecResultConverter {
 
     @Converter
     public static byte[] convertToByteArray(ExecResult result, Exchange exchange) throws
FileNotFoundException, IOException {
-        return IOUtils.toByteArray(toInputStream(result));
+        InputStream stream = toInputStream(result);
+        try {
+            return IOUtils.toByteArray(stream);
+        } finally {
+            IOUtils.closeQuietly(stream);
+        }
     }
 
     @Converter
@@ -85,30 +91,56 @@ public final class ExecResultConverter {
     }
 
     /**
-     * If the ExecResult contains out file,
-     * <code>InputStream<code> with the output of the <code>execResult</code>.
-     * If there is {@link ExecCommand#getOutFile()}, its content is preferred to
-     * {@link ExecResult#getStdout()}. Returns <code>null</code> if the stdout
-     * is null, or if the <code>execResult</code> is <code>null</code>.
+     * Returns <code>InputStream</code> object with the <i>output</i>
of the
+     * executable. If there is {@link ExecCommand#getOutFile()}, its content is
+     * preferred to {@link ExecResult#getStdout()}. If no out file is set, and
+     * the stdout of the exec result is <code>null</code> returns the stderr
of
+     * the exec result. <br>
+     * If the output stream is of type <code>ByteArrayInputStream</code>, its
+     * <code>reset()</code> method is called.
      * 
-     * @param execResult ExecResult object.
-     * @return InputStream object if the output of the executable.
-     * @throws FileNotFoundException if the {@link ExecCommand#getOutFile()} is
-     *             not <code>null</code>, but can not be found
+     * @param execResult ExecResult object to convert to InputStream.
+     * @return InputStream object with the <i>output</i> of the executable.
+     *         Returns <code>null</code> if both {@link ExecResult#getStdout()}
+     *         and {@link ExecResult#getStderr()} are <code>null</code> , or
if
+     *         the <code>execResult</code> is <code>null</code>.
+     * @throws FileNotFoundException if the {@link ExecCommand#getOutFile()} can
+     *             not be opened. In this case the out file must have had a not
+     *             <code>null</code> value
      */
     public static InputStream toInputStream(ExecResult execResult) throws FileNotFoundException
{
         if (execResult == null) {
             LOG.warn("Received a null ExecResult instance to convert!");
             return null;
         }
-        // prefer generic file conversion
+        // prefer the out file for output
+        InputStream result;
         if (execResult.getCommand().getOutFile() != null) {
-            return new FileInputStream(execResult.getCommand().getOutFile());
+            result = new FileInputStream(execResult.getCommand().getOutFile());
         } else {
+            // if the stdout is null, return the stderr.
             if (execResult.getStdout() == null) {
-                LOG.warn("Received null stdout of the ExecResult for conversion!");
+                LOG.warn("ExecResult has no stdout, will fallback to use stderr.");
+                result = execResult.getStderr();
+            } else {
+                result = execResult.getStdout();
+            }
+        }
+        // reset the stream if it was already read.
+        resetIfByteArrayInputStream(result);
+        return result;
+    }
+
+    /**
+     * Resets the stream, only if it's a ByteArrayInputStream.
+     */
+    private static void resetIfByteArrayInputStream(InputStream stream) {
+        if (stream != null && stream instanceof ByteArrayInputStream) {
+            try {
+                stream.reset();
+            } catch (IOException ioe) {
+                LOG.error("Unable to reset the stream ", ioe);
             }
-            return execResult.getStdout();
         }
     }
 }

Modified: camel/trunk/components/camel-exec/src/test/java/org/apache/camel/component/exec/ExecJavaProcessTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-exec/src/test/java/org/apache/camel/component/exec/ExecJavaProcessTest.java?rev=938323&r1=938322&r2=938323&view=diff
==============================================================================
--- camel/trunk/components/camel-exec/src/test/java/org/apache/camel/component/exec/ExecJavaProcessTest.java
(original)
+++ camel/trunk/components/camel-exec/src/test/java/org/apache/camel/component/exec/ExecJavaProcessTest.java
Tue Apr 27 04:38:25 2010
@@ -29,6 +29,7 @@ import org.apache.camel.builder.RouteBui
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.apache.commons.io.IOUtils;
+
 import org.junit.Test;
 
 import static org.apache.camel.component.exec.ExecBinding.EXEC_COMMAND_ARGS;
@@ -41,6 +42,7 @@ import static org.apache.camel.component
 import static org.apache.camel.component.exec.ExecTestUtils.buildJavaExecutablePath;
 import static org.apache.camel.component.exec.ExecutableJavaProgram.EXIT_WITH_VALUE_0;
 import static org.apache.camel.component.exec.ExecutableJavaProgram.EXIT_WITH_VALUE_1;
+import static org.apache.camel.component.exec.ExecutableJavaProgram.PRINT_IN_STDERR;
 import static org.apache.camel.component.exec.ExecutableJavaProgram.PRINT_IN_STDOUT;
 import static org.apache.camel.component.exec.ExecutableJavaProgram.READ_INPUT_LINES_AND_PRINT_THEM;
 import static org.apache.camel.component.exec.ExecutableJavaProgram.SLEEP_WITH_TIMEOUT;
@@ -110,6 +112,39 @@ public class ExecJavaProcessTest extends
     }
 
     @Test
+    public void testByteArrayInputStreamIsResetInConverter() throws Exception {
+        String commandArgument = PRINT_IN_STDOUT;
+        output.setExpectedMessageCount(1);
+
+        Exchange e = sendExchange(commandArgument, NO_TIMEOUT);
+        String out1 = e.getIn().getBody(String.class);
+        // the second conversion should not need a reset, this is handled
+        // in the type converter.
+        String out2 = e.getIn().getBody(String.class);
+        
+        output.assertIsSatisfied();
+        assertEquals(PRINT_IN_STDOUT, out1);
+        assertEquals(out1, out2);
+    }
+
+    @Test
+    public void testIfStdoutIsNullStderrIsReturnedInConverter() throws Exception {
+        // this will be printed
+        String commandArgument = PRINT_IN_STDERR;
+        output.setExpectedMessageCount(1);
+
+        Exchange e = sendExchange(commandArgument, NO_TIMEOUT);
+        ExecResult body = e.getIn().getBody(ExecResult.class);
+
+        output.assertIsSatisfied();
+        assertNull("the test executable must not print anything in stdout", body.getStdout());
+        assertNotNull("the test executable must print in stderr", body.getStderr());
+        // the converter must fall back to the stderr, because stdout is null
+        String stderr = e.getIn().getBody(String.class);
+        assertEquals(PRINT_IN_STDERR, stderr);
+    }
+
+    @Test
     public void testConvertResultToInputStream() throws Exception {
         String commandArgument = PRINT_IN_STDOUT;
         output.setExpectedMessageCount(1);
@@ -218,7 +253,7 @@ public class ExecJavaProcessTest extends
     protected Exchange sendExchange(final Object commandArgument, final long timeout, final
String body) {
         final List<String> args = buildArgs(commandArgument);
         final String javaAbsolutePath = buildJavaExecutablePath();
-        
+
         return producerTemplate.send(new Processor() {
             public void process(Exchange exchange) throws Exception {
                 exchange.getIn().setBody(body);



Mime
View raw message