]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/AbstractJschTask.java
Add server capabilities to SLC RCP
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / jsch / AbstractJschTask.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.slc.jsch;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.argeo.slc.SlcException;
25
26 import com.jcraft.jsch.JSch;
27 import com.jcraft.jsch.JSchException;
28 import com.jcraft.jsch.Session;
29
30 public abstract class AbstractJschTask implements Runnable {
31 private final Log log = LogFactory.getLog(getClass());
32
33 private SshTarget sshTarget;
34
35 protected Session openSession() {
36 if (sshTarget.getSession() != null) {
37 Session session = sshTarget.getSession();
38 if (session.isConnected()) {
39 if (log.isTraceEnabled())
40 log.debug("Using cached session to " + getSshTarget()
41 + " via SSH");
42 return session;
43 }
44 }
45
46 try {
47 JSch jsch = new JSch();
48 if (sshTarget.getUsePrivateKey()
49 && sshTarget.getLocalPrivateKey().exists())
50 jsch.addIdentity(sshTarget.getLocalPrivateKey()
51 .getAbsolutePath());
52 Session session = jsch.getSession(getSshTarget().getUser(),
53 getSshTarget().getHost(), getSshTarget().getPort());
54
55 session.setUserInfo(getSshTarget().getUserInfo());
56 session.connect();
57 if (log.isDebugEnabled())
58 log.debug("Connected to " + getSshTarget() + " via SSH");
59 if (sshTarget.getSession() != null) {
60 if (log.isDebugEnabled())
61 log.debug("The cached session to " + getSshTarget()
62 + " was disconnected and was reset.");
63 sshTarget.setSession(session);
64 }
65 return session;
66 } catch (JSchException e) {
67 if (sshTarget.getUserInfo() instanceof SimpleUserInfo)
68 ((SimpleUserInfo) sshTarget.getUserInfo()).reset();
69 throw new SlcException("Could not open session to "
70 + getSshTarget(), e);
71 }
72 }
73
74 public void run() {
75 Session session = openSession();
76 try {
77 run(session);
78 } finally {
79 if (sshTarget != null && sshTarget.getSession() == null) {
80 session.disconnect();
81 if (log.isDebugEnabled())
82 log.debug("Disconnected from " + getSshTarget()
83 + " via SSH");
84 }
85 }
86 }
87
88 abstract void run(Session session);
89
90 protected int checkAck(InputStream in) throws IOException {
91 int b = in.read();
92 // b may be 0 for success,
93 // 1 for error,
94 // 2 for fatal error,
95 // -1
96 if (b == 0)
97 return b;
98 else if (b == -1)
99 return b;// throw new SlcException("SSH ack returned -1");
100 else if (b == 1 || b == 2) {
101 StringBuffer sb = new StringBuffer();
102 int c;
103 do {
104 c = in.read();
105 sb.append((char) c);
106 } while (c != '\n');
107 if (b == 1) { // error
108 throw new SlcException("SSH ack error: " + sb.toString());
109 }
110 if (b == 2) { // fatal error
111 throw new SlcException("SSH fatal error: " + sb.toString());
112 }
113 }
114 return b;
115 }
116
117 public SshTarget getSshTarget() {
118 if (sshTarget == null)
119 throw new SlcException("No SSH target defined.");
120 return sshTarget;
121 }
122
123 public void setSshTarget(SshTarget sshTarget) {
124 this.sshTarget = sshTarget;
125 }
126
127 }