]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/AbstractJschTask.java
f26108f4aff46f42c39dcf5134bb2d5eedbbe406
1 package org
.argeo
.slc
.jsch
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
6 import org
.apache
.commons
.logging
.Log
;
7 import org
.apache
.commons
.logging
.LogFactory
;
8 import org
.argeo
.slc
.SlcException
;
10 import com
.jcraft
.jsch
.JSch
;
11 import com
.jcraft
.jsch
.JSchException
;
12 import com
.jcraft
.jsch
.Session
;
14 public abstract class AbstractJschTask
implements Runnable
{
15 private final static Log log
= LogFactory
.getLog(AbstractJschTask
.class);
17 private SshTarget sshTarget
;
19 protected Session
openSession() {
20 if (sshTarget
.getSession() != null) {
21 Session session
= sshTarget
.getSession();
22 if (session
.isConnected()) {
23 if (log
.isTraceEnabled())
24 log
.debug("Using cached session to " + getSshTarget()
31 JSch jsch
= new JSch();
32 if (sshTarget
.getUsePrivateKey()
33 && sshTarget
.getLocalPrivateKey().exists())
34 jsch
.addIdentity(sshTarget
.getLocalPrivateKey()
36 Session session
= jsch
.getSession(getSshTarget().getUser(),
37 getSshTarget().getHost(), getSshTarget().getPort());
39 session
.setUserInfo(getSshTarget().getUserInfo());
41 if (log
.isDebugEnabled())
42 log
.debug("Connected to " + getSshTarget() + " via SSH");
43 if (sshTarget
.getSession() != null) {
44 if (log
.isDebugEnabled())
45 log
.debug("The cached session to " + getSshTarget()
46 + " was disconnected and was reste.");
47 sshTarget
.setSession(session
);
50 } catch (JSchException e
) {
51 throw new SlcException("Could not open session to "
56 public final void run() {
57 Session session
= openSession();
61 if (sshTarget
.getSession() == null) {
63 if (log
.isDebugEnabled())
64 log
.debug("Disconnected from " + getSshTarget()
70 abstract void run(Session session
);
72 protected int checkAck(InputStream in
) throws IOException
{
74 // b may be 0 for success,
81 return b
;// throw new SlcException("SSH ack returned -1");
82 else if (b
== 1 || b
== 2) {
83 StringBuffer sb
= new StringBuffer();
89 if (b
== 1) { // error
90 throw new SlcException("SSH ack error: " + sb
.toString());
92 if (b
== 2) { // fatal error
93 throw new SlcException("SSH fatal error: " + sb
.toString());
99 public SshTarget
getSshTarget() {
100 if (sshTarget
== null)
101 throw new SlcException("No SSH target defined.");
105 public void setSshTarget(SshTarget sshTarget
) {
106 this.sshTarget
= sshTarget
;