+ protected void readStdErr(final ChannelExec channel) {
+ new Thread("stderr " + getSshTarget()) {
+ public void run() {
+ BufferedReader stdErr = null;
+ try {
+ InputStream in = channel.getErrStream();
+ stdErr = new BufferedReader(new InputStreamReader(in));
+ String line = null;
+ while ((line = stdErr.readLine()) != null) {
+ if (!line.trim().equals(""))
+ log.error(line);
+ }
+ } catch (IOException e) {
+ if (log.isDebugEnabled())
+ log.error("Cannot read stderr from " + getSshTarget(),
+ e);
+ } finally {
+ IOUtils.closeQuietly(stdErr);
+ }
+ }
+ }.start();
+ }
+
+ protected void readStdOut(Channel channel) {
+ if (stdOut != null) {
+ OutputStream localStdOut = createOutputStream(stdOut);
+ try {
+ IOUtils.copy(channel.getInputStream(), localStdOut);
+ } catch (IOException e) {
+ throw new SlcException("Cannot redirect stdout", e);
+ } finally {
+ IOUtils.closeQuietly(localStdOut);
+ }
+ } else {
+ BufferedReader stdOut = null;
+ try {
+ InputStream in = channel.getInputStream();
+ stdOut = new BufferedReader(new InputStreamReader(in));
+ String line = null;
+ while ((line = stdOut.readLine()) != null) {
+ if (!line.trim().equals("")) {
+
+ if (stdOutLines != null) {
+ stdOutLines.add(line);
+ if (logEvenIfStdOutLines && !quiet)
+ log.info(line);
+ } else {
+ if (!quiet)
+ log.info(line);
+ }
+ }
+ }
+ } catch (IOException e) {
+ if (log.isDebugEnabled())
+ log.error("Cannot read stdout from " + getSshTarget(), e);
+ } finally {
+ IOUtils.closeQuietly(stdOut);
+ }
+ }
+ }
+
+ protected void checkExitStatus(Channel channel) {
+ if (channel.isClosed()) {
+ int exitStatus = channel.getExitStatus();
+ if (exitStatus == 0) {
+ if (log.isTraceEnabled())
+ log.trace("Remote execution exit status: " + exitStatus);
+ } else {
+ String msg = "Remote execution failed with " + " exit status: "
+ + exitStatus;
+ if (failOnBadExitStatus)
+ throw new SlcException(msg);
+ else
+ log.error(msg);
+ }
+ }
+
+ }
+
+ protected OutputStream createOutputStream(Resource target) {
+ FileOutputStream out = null;
+ try {
+
+ final File file;
+ if (executionResources != null)
+ file = new File(executionResources.getAsOsPath(target, true));
+ else
+ file = target.getFile();
+ out = new FileOutputStream(file, false);
+ } catch (IOException e) {
+ log.error("Cannot get file for " + target, e);
+ IOUtils.closeQuietly(out);
+ }
+ return out;
+ }
+