Author: ningjiang Date: Thu Jul 12 08:51:20 2012 New Revision: 1360584 URL: http://svn.apache.org/viewvc?rev=1360584&view=rev Log: CAMEL-5439 support to return the stander error and shell exist status in the camel-ssh component
Added: camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshResult.java Modified: camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshConsumer.java camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshEndpoint.java camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshProducer.java camel/trunk/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/EchoCommandFactory.java camel/trunk/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshComponentConsumerTest.java camel/trunk/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshComponentProducerTest.java Modified: camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshConsumer.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshConsumer.java?rev=1360584&r1=1360583&r2=1360584&view=diff ============================================================================== --- camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshConsumer.java (original) +++ camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshConsumer.java Thu Jul 12 08:51:20 2012 @@ -31,10 +31,12 @@ public class SshConsumer extends Schedul @Override protected int poll() throws Exception { String command = endpoint.getPollCommand(); - byte[] result = endpoint.sendExecCommand(command); + SshResult result = endpoint.sendExecCommand(command); Exchange exchange = endpoint.createExchange(); - exchange.getIn().setBody(result); + exchange.getIn().setBody(result.getStdout()); + exchange.getIn().setHeader(SshResult.EXIT_VALUE, result.getExitValue()); + exchange.getIn().setHeader(SshResult.STDERR, result.getStderr()); try { // send message to next processor in the route Modified: camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshEndpoint.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshEndpoint.java?rev=1360584&r1=1360583&r2=1360584&view=diff ============================================================================== --- camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshEndpoint.java (original) +++ camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshEndpoint.java Thu Jul 12 08:51:20 2012 @@ -75,8 +75,8 @@ public class SshEndpoint extends Schedul return false; } - public byte[] sendExecCommand(String command) throws Exception { - byte[] result = null; + public SshResult sendExecCommand(String command) throws Exception { + SshResult result = null; if (getConfiguration() == null) { throw new IllegalStateException("Configuration must be set"); @@ -133,12 +133,14 @@ public class SshEndpoint extends Schedul ByteArrayOutputStream err = new ByteArrayOutputStream(); channel.setErr(err); - OpenFuture openFuture = channel.open(); openFuture.await(getTimeout()); if (openFuture.isOpened()) { channel.waitFor(ClientChannel.CLOSED, 0); - result = out.toByteArray(); + result = new SshResult(command, channel.getExitStatus(), + new ByteArrayInputStream(out.toByteArray()), + new ByteArrayInputStream(err.toByteArray())); + } return result; Modified: camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshProducer.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshProducer.java?rev=1360584&r1=1360583&r2=1360584&view=diff ============================================================================== --- camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshProducer.java (original) +++ camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshProducer.java Thu Jul 12 08:51:20 2012 @@ -35,8 +35,10 @@ public class SshProducer extends Default String command = in.getMandatoryBody(String.class); try { - byte[] result = endpoint.sendExecCommand(command); - exchange.getOut().setBody(result); + SshResult result = endpoint.sendExecCommand(command); + exchange.getOut().setBody(result.getStdout()); + exchange.getOut().setHeader(SshResult.EXIT_VALUE, result.getExitValue()); + exchange.getOut().setHeader(SshResult.STDERR, result.getStderr()); } catch (Exception e) { throw new CamelExchangeException("Cannot execute command: " + command, exchange, e); } Added: camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshResult.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshResult.java?rev=1360584&view=auto ============================================================================== --- camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshResult.java (added) +++ camel/trunk/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshResult.java Thu Jul 12 08:51:20 2012 @@ -0,0 +1,70 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.ssh; + +import java.io.InputStream; + +public class SshResult { + + /** + * The value of this header is a {@link InputStream} with the standard error + * stream of the executable. + */ + public static final String STDERR = "CamelSshStderr"; + + /** + * The value of this header is the exit value that is returned, after the + * execution. By convention a non-zero status exit value indicates abnormal + * termination. <br> + * <b>Note that the exit value is OS dependent.</b> + */ + public static final String EXIT_VALUE = "CamelSshExitValue"; + + private final String command; + + private final int exitValue; + + private final InputStream stdout; + + private final InputStream stderr; + + public SshResult(String command, int exitValue, InputStream out, InputStream err) { + this.command = command; + this.exitValue = exitValue; + this.stdout = out; + this.stderr = err; + } + + public String getCommand() { + return command; + } + + public int getExitValue() { + return exitValue; + } + + public InputStream getStdout() { + return stdout; + } + + public InputStream getStderr() { + return stderr; + } + + + +} Modified: camel/trunk/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/EchoCommandFactory.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/EchoCommandFactory.java?rev=1360584&r1=1360583&r2=1360584&view=diff ============================================================================== --- camel/trunk/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/EchoCommandFactory.java (original) +++ camel/trunk/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/EchoCommandFactory.java Thu Jul 12 08:51:20 2012 @@ -35,6 +35,7 @@ public class EchoCommandFactory implemen protected static class EchoCommand implements Command, Runnable { private String command; private OutputStream out; + private OutputStream err; private ExitCallback callback; private Thread thread; @@ -53,6 +54,7 @@ public class EchoCommandFactory implemen @Override public void setErrorStream(OutputStream err) { + this.err = err; } @Override @@ -76,6 +78,10 @@ public class EchoCommandFactory implemen boolean succeeded = true; String message = null; try { + // we set the error with the same command message + err.write("Error:".getBytes()); + err.write(command.getBytes()); + err.flush(); out.write(command.getBytes()); out.flush(); } catch (Exception e) { Modified: camel/trunk/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshComponentConsumerTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshComponentConsumerTest.java?rev=1360584&r1=1360583&r2=1360584&view=diff ============================================================================== --- camel/trunk/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshComponentConsumerTest.java (original) +++ camel/trunk/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshComponentConsumerTest.java Thu Jul 12 08:51:20 2012 @@ -27,7 +27,8 @@ public class SshComponentConsumerTest ex MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMinimumMessageCount(1); mock.expectedBodiesReceived("test\r"); - + mock.expectedHeaderReceived(SshResult.EXIT_VALUE, 0); + mock.expectedHeaderReceived(SshResult.STDERR, "Error:test\r"); assertMockEndpointsSatisfied(); } @@ -36,7 +37,7 @@ public class SshComponentConsumerTest ex return new RouteBuilder() { @Override public void configure() { - from("ssh://smx:smx@localhost:" + port + "?useFixedDelay=true&delay=5000&pollCommand=test%0D") + from("ssh://smx:smx@localhost:" + port + "?useFixedDelay=true&delay=40000&pollCommand=test%0D") .to("mock:result"); } }; Modified: camel/trunk/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshComponentProducerTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshComponentProducerTest.java?rev=1360584&r1=1360583&r2=1360584&view=diff ============================================================================== --- camel/trunk/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshComponentProducerTest.java (original) +++ camel/trunk/components/camel-ssh/src/test/java/org/apache/camel/component/ssh/SshComponentProducerTest.java Thu Jul 12 08:51:20 2012 @@ -30,6 +30,8 @@ public class SshComponentProducerTest ex MockEndpoint mock = getMockEndpoint("mock:password"); mock.expectedMinimumMessageCount(1); mock.expectedBodiesReceived(msg); + mock.expectedHeaderReceived(SshResult.EXIT_VALUE, 0); + mock.expectedHeaderReceived(SshResult.STDERR, "Error:test\n"); template.sendBody("direct:ssh", msg);