2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.jsch
;
19 import java
.io
.IOException
;
22 import org
.apache
.commons
.exec
.CommandLine
;
23 import org
.apache
.commons
.exec
.ExecuteException
;
24 import org
.apache
.commons
.exec
.ExecuteResultHandler
;
25 import org
.apache
.commons
.exec
.ExecuteStreamHandler
;
26 import org
.apache
.commons
.exec
.ExecuteWatchdog
;
27 import org
.apache
.commons
.exec
.Executor
;
28 import org
.apache
.commons
.exec
.ProcessDestroyer
;
30 /** A Commons Exec executor executing remotely via SSH */
31 public class JschExecutor
implements Executor
{
32 private File workingDirectory
;
33 private ExecuteStreamHandler streamHandler
;
35 private SshTarget sshTarget
;
37 public void setExitValue(int value
) {
38 // TODO Auto-generated method stub
42 public void setExitValues(int[] values
) {
43 // TODO Auto-generated method stub
47 public boolean isFailure(int exitValue
) {
48 return Executor
.INVALID_EXITVALUE
== exitValue
;
51 public ExecuteStreamHandler
getStreamHandler() {
55 public void setStreamHandler(ExecuteStreamHandler streamHandler
) {
56 this.streamHandler
= streamHandler
;
59 public ExecuteWatchdog
getWatchdog() {
60 // TODO Auto-generated method stub
64 public void setWatchdog(ExecuteWatchdog watchDog
) {
65 // TODO Auto-generated method stub
69 public ProcessDestroyer
getProcessDestroyer() {
70 // TODO Auto-generated method stub
74 public void setProcessDestroyer(ProcessDestroyer processDestroyer
) {
75 // TODO Auto-generated method stub
79 public File
getWorkingDirectory() {
80 return workingDirectory
;
83 public void setWorkingDirectory(File workingDirectory
) {
84 this.workingDirectory
= workingDirectory
;
87 public int execute(CommandLine command
) throws ExecuteException
,
89 return execute(command
, (Map
) null);
92 public int execute(CommandLine command
, Map environment
)
93 throws ExecuteException
, IOException
{
94 String cmd
= command
.toString();
95 if(workingDirectory
!=null)
96 cmd
= "cd "+workingDirectory
.getPath()+" && "+cmd
;
97 RemoteExec remoteExec
= new RemoteExec();
98 remoteExec
.setSshTarget(sshTarget
);
99 remoteExec
.setStreamHandler(streamHandler
);
100 remoteExec
.setCommand(cmd
);
101 if (environment
!= null)
102 remoteExec
.setEnv(environment
);
104 return remoteExec
.getLastExitStatus() != null ? remoteExec
105 .getLastExitStatus() : Executor
.INVALID_EXITVALUE
;
108 public void execute(CommandLine command
, ExecuteResultHandler handler
)
109 throws ExecuteException
, IOException
{
110 // TODO Auto-generated method stub
114 public void execute(CommandLine command
, Map environment
,
115 ExecuteResultHandler handler
) throws ExecuteException
, IOException
{
119 public SshTarget
getSshTarget() {
123 public void setSshTarget(SshTarget sshTarget
) {
124 this.sshTarget
= sshTarget
;