]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/AbstractJschTask.java
Improve SSH support
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / jsch / AbstractJschTask.java
1 package org.argeo.slc.jsch;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.argeo.slc.SlcException;
9
10 import com.jcraft.jsch.JSch;
11 import com.jcraft.jsch.JSchException;
12 import com.jcraft.jsch.Session;
13
14 public abstract class AbstractJschTask implements Runnable {
15 private final static Log log = LogFactory.getLog(AbstractJschTask.class);
16
17 private SshTarget sshTarget;
18
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()
25 + " via SSH");
26 return session;
27 }
28 }
29
30 try {
31 JSch jsch = new JSch();
32 if (sshTarget.getUsePrivateKey()
33 && sshTarget.getLocalPrivateKey().exists())
34 jsch.addIdentity(sshTarget.getLocalPrivateKey()
35 .getAbsolutePath());
36 Session session = jsch.getSession(getSshTarget().getUser(),
37 getSshTarget().getHost(), getSshTarget().getPort());
38
39 session.setUserInfo(getSshTarget().getUserInfo());
40 session.connect();
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);
48 }
49 return session;
50 } catch (JSchException e) {
51 throw new SlcException("Could not open session to "
52 + getSshTarget(), e);
53 }
54 }
55
56 public final void run() {
57 Session session = openSession();
58 try {
59 run(session);
60 } finally {
61 if (sshTarget.getSession() == null) {
62 session.disconnect();
63 if (log.isDebugEnabled())
64 log.debug("Disconnected from " + getSshTarget()
65 + " via SSH");
66 }
67 }
68 }
69
70 abstract void run(Session session);
71
72 protected int checkAck(InputStream in) throws IOException {
73 int b = in.read();
74 // b may be 0 for success,
75 // 1 for error,
76 // 2 for fatal error,
77 // -1
78 if (b == 0)
79 return b;
80 else if (b == -1)
81 return b;// throw new SlcException("SSH ack returned -1");
82 else if (b == 1 || b == 2) {
83 StringBuffer sb = new StringBuffer();
84 int c;
85 do {
86 c = in.read();
87 sb.append((char) c);
88 } while (c != '\n');
89 if (b == 1) { // error
90 throw new SlcException("SSH ack error: " + sb.toString());
91 }
92 if (b == 2) { // fatal error
93 throw new SlcException("SSH fatal error: " + sb.toString());
94 }
95 }
96 return b;
97 }
98
99 public SshTarget getSshTarget() {
100 if (sshTarget == null)
101 throw new SlcException("No SSH target defined.");
102 return sshTarget;
103 }
104
105 public void setSshTarget(SshTarget sshTarget) {
106 this.sshTarget = sshTarget;
107 }
108
109 }