]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/AbstractJschTask.java
237c58c43e56429dbedf2d6410dd0ff2119a843c
2 * Copyright (C) 2007-2012 Argeo GmbH
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.
16 package org
.argeo
.slc
.jsch
;
18 import java
.io
.IOException
;
19 import java
.io
.InputStream
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.argeo
.slc
.SlcException
;
25 import com
.jcraft
.jsch
.JSch
;
26 import com
.jcraft
.jsch
.JSchException
;
27 import com
.jcraft
.jsch
.Session
;
29 public abstract class AbstractJschTask
implements Runnable
{
30 private final Log log
= LogFactory
.getLog(getClass());
32 private SshTarget sshTarget
;
34 protected Session
openSession() {
35 if (sshTarget
.getSession() != null) {
36 Session session
= sshTarget
.getSession();
37 if (session
.isConnected()) {
38 if (log
.isTraceEnabled())
39 log
.debug("Using cached session to " + getSshTarget()
46 JSch jsch
= new JSch();
47 if (sshTarget
.getUsePrivateKey()
48 && sshTarget
.getLocalPrivateKey().exists())
49 jsch
.addIdentity(sshTarget
.getLocalPrivateKey()
51 Session session
= jsch
.getSession(getSshTarget().getUser(),
52 getSshTarget().getHost(), getSshTarget().getPort());
54 session
.setUserInfo(getSshTarget().getUserInfo());
55 session
.setServerAliveInterval(1000);
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
);
66 } catch (JSchException e
) {
67 if (sshTarget
.getUserInfo() instanceof SimpleUserInfo
)
68 ((SimpleUserInfo
) sshTarget
.getUserInfo()).reset();
69 throw new SlcException("Could not open session to "
75 Session session
= openSession();
79 if (sshTarget
!= null && sshTarget
.getSession() == null) {
81 if (log
.isDebugEnabled())
82 log
.debug("Disconnected from " + getSshTarget()
88 abstract void run(Session session
);
90 protected int checkAck(InputStream in
) throws IOException
{
92 // b may be 0 for success,
99 return b
;// throw new SlcException("SSH ack returned -1");
100 else if (b
== 1 || b
== 2) {
101 StringBuffer sb
= new StringBuffer();
107 if (b
== 1) { // error
108 throw new SlcException("SSH ack error: " + sb
.toString());
110 if (b
== 2) { // fatal error
111 throw new SlcException("SSH fatal error: " + sb
.toString());
117 public SshTarget
getSshTarget() {
118 if (sshTarget
== null)
119 throw new SlcException("No SSH target defined.");
123 public void setSshTarget(SshTarget sshTarget
) {
124 this.sshTarget
= sshTarget
;