View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
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
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
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.
15   */
16  package org.argeo.slc.jsch;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.util.Map;
21  
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;
29  
30  /** A Commons Exec executor executing remotely via SSH */
31  public class JschExecutor implements Executor {
32  	private File workingDirectory;
33  	private ExecuteStreamHandler streamHandler;
34  
35  	private SshTarget sshTarget;
36  
37  	public void setExitValue(int value) {
38  		// TODO Auto-generated method stub
39  
40  	}
41  
42  	public void setExitValues(int[] values) {
43  		// TODO Auto-generated method stub
44  
45  	}
46  
47  	public boolean isFailure(int exitValue) {
48  		return Executor.INVALID_EXITVALUE == exitValue;
49  	}
50  
51  	public ExecuteStreamHandler getStreamHandler() {
52  		return streamHandler;
53  	}
54  
55  	public void setStreamHandler(ExecuteStreamHandler streamHandler) {
56  		this.streamHandler = streamHandler;
57  	}
58  
59  	public ExecuteWatchdog getWatchdog() {
60  		// TODO Auto-generated method stub
61  		return null;
62  	}
63  
64  	public void setWatchdog(ExecuteWatchdog watchDog) {
65  		// TODO Auto-generated method stub
66  
67  	}
68  
69  	public ProcessDestroyer getProcessDestroyer() {
70  		// TODO Auto-generated method stub
71  		return null;
72  	}
73  
74  	public void setProcessDestroyer(ProcessDestroyer processDestroyer) {
75  		// TODO Auto-generated method stub
76  
77  	}
78  
79  	public File getWorkingDirectory() {
80  		return workingDirectory;
81  	}
82  
83  	public void setWorkingDirectory(File workingDirectory) {
84  		this.workingDirectory = workingDirectory;
85  	}
86  
87  	public int execute(CommandLine command) throws ExecuteException,
88  			IOException {
89  		return execute(command, (Map) null);
90  	}
91  
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);
103 		remoteExec.run();
104 		return remoteExec.getLastExitStatus() != null ? remoteExec
105 				.getLastExitStatus() : Executor.INVALID_EXITVALUE;
106 	}
107 
108 	public void execute(CommandLine command, ExecuteResultHandler handler)
109 			throws ExecuteException, IOException {
110 		// TODO Auto-generated method stub
111 
112 	}
113 
114 	public void execute(CommandLine command, Map environment,
115 			ExecuteResultHandler handler) throws ExecuteException, IOException {
116 
117 	}
118 
119 	public SshTarget getSshTarget() {
120 		return sshTarget;
121 	}
122 
123 	public void setSshTarget(SshTarget sshTarget) {
124 		this.sshTarget = sshTarget;
125 	}
126 
127 }