]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/AbstractJschTask.java
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.slc
.jsch
;
19 import java
.io
.IOException
;
20 import java
.io
.InputStream
;
22 import org
.apache
.commons
.logging
.Log
;
23 import org
.apache
.commons
.logging
.LogFactory
;
24 import org
.argeo
.slc
.SlcException
;
26 import com
.jcraft
.jsch
.JSch
;
27 import com
.jcraft
.jsch
.JSchException
;
28 import com
.jcraft
.jsch
.Session
;
30 public abstract class AbstractJschTask
implements Runnable
{
31 private final Log log
= LogFactory
.getLog(getClass());
33 private SshTarget sshTarget
;
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()
47 JSch jsch
= new JSch();
48 if (sshTarget
.getUsePrivateKey()
49 && sshTarget
.getLocalPrivateKey().exists())
50 jsch
.addIdentity(sshTarget
.getLocalPrivateKey()
52 Session session
= jsch
.getSession(getSshTarget().getUser(),
53 getSshTarget().getHost(), getSshTarget().getPort());
55 session
.setUserInfo(getSshTarget().getUserInfo());
56 session
.setServerAliveInterval(1000);
58 if (log
.isDebugEnabled())
59 log
.debug("Connected to " + getSshTarget() + " via SSH");
60 if (sshTarget
.getSession() != null) {
61 if (log
.isDebugEnabled())
62 log
.debug("The cached session to " + getSshTarget()
63 + " was disconnected and was reset.");
64 sshTarget
.setSession(session
);
67 } catch (JSchException e
) {
68 if (sshTarget
.getUserInfo() instanceof SimpleUserInfo
)
69 ((SimpleUserInfo
) sshTarget
.getUserInfo()).reset();
70 throw new SlcException("Could not open session to "
76 Session session
= openSession();
80 if (sshTarget
!= null && sshTarget
.getSession() == null) {
82 if (log
.isDebugEnabled())
83 log
.debug("Disconnected from " + getSshTarget()
89 abstract void run(Session session
);
91 protected int checkAck(InputStream in
) throws IOException
{
93 // b may be 0 for success,
100 return b
;// throw new SlcException("SSH ack returned -1");
101 else if (b
== 1 || b
== 2) {
102 StringBuffer sb
= new StringBuffer();
108 if (b
== 1) { // error
109 throw new SlcException("SSH ack error: " + sb
.toString());
111 if (b
== 2) { // fatal error
112 throw new SlcException("SSH fatal error: " + sb
.toString());
118 public SshTarget
getSshTarget() {
119 if (sshTarget
== null)
120 throw new SlcException("No SSH target defined.");
124 public void setSshTarget(SshTarget sshTarget
) {
125 this.sshTarget
= sshTarget
;