Return-Path: Delivered-To: apmail-camel-commits-archive@www.apache.org Received: (qmail 51359 invoked from network); 27 Apr 2010 04:39:13 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 27 Apr 2010 04:39:13 -0000 Received: (qmail 10121 invoked by uid 500); 27 Apr 2010 04:39:12 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 10071 invoked by uid 500); 27 Apr 2010 04:39:12 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 10064 invoked by uid 99); 27 Apr 2010 04:39:11 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 27 Apr 2010 04:39:11 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 27 Apr 2010 04:39:09 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id E805723889DE; Tue, 27 Apr 2010 04:38:25 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@camel.apache.org From: davsclaus@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100427043825.E805723889DE@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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, - * InputStream with the output of the execResult. - * If there is {@link ExecCommand#getOutFile()}, its content is preferred to - * {@link ExecResult#getStdout()}. Returns null if the stdout - * is null, or if the execResult is null. + * Returns InputStream object with the output 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 null returns the stderr of + * the exec result.
+ * If the output stream is of type ByteArrayInputStream, its + * reset() 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 null, but can not be found + * @param execResult ExecResult object to convert to InputStream. + * @return InputStream object with the output of the executable. + * Returns null if both {@link ExecResult#getStdout()} + * and {@link ExecResult#getStderr()} are null , or if + * the execResult is null. + * @throws FileNotFoundException if the {@link ExecCommand#getOutFile()} can + * not be opened. In this case the out file must have had a not + * null 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 args = buildArgs(commandArgument); final String javaAbsolutePath = buildJavaExecutablePath(); - + return producerTemplate.send(new Processor() { public void process(Exchange exchange) throws Exception { exchange.getIn().setBody(body);