]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.support/src/org/argeo/slc/jsch/JschContextSession.java
Adapt to changes in Argeo Commons
[gpl/argeo-slc.git] / legacy / org.argeo.slc.support / src / org / argeo / slc / jsch / JschContextSession.java
1 package org.argeo.slc.jsch;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.springframework.beans.factory.DisposableBean;
6 import org.springframework.beans.factory.InitializingBean;
7
8 import com.jcraft.jsch.Session;
9
10 /** Caches a JSCH session in the the ssh target. */
11 public class JschContextSession extends AbstractJschTask implements
12 InitializingBean, DisposableBean {
13 private final static Log log = LogFactory.getLog(JschContextSession.class);
14 private Boolean autoconnect = false;
15
16 @Override
17 void run(Session session) {
18 // clear();
19 getSshTarget().setSession(session);
20 if (log.isDebugEnabled())
21 log.debug("Cached SSH context session to " + getSshTarget());
22 }
23
24 public void afterPropertiesSet() throws Exception {
25 // if (log.isDebugEnabled())
26 // log.debug(getClass() + ".afterPropertiesSet(), " + beanName + ", "
27 // + this);
28 if (autoconnect)
29 try {
30 run();
31 } catch (Exception e) {
32 log.error("Could not automatically open session", e);
33 }
34 }
35
36 public void destroy() throws Exception {
37 clear();
38 }
39
40 public void clear() {
41 SshTarget sshTarget = getSshTarget();
42 synchronized (sshTarget) {
43 if (sshTarget.getSession() != null) {
44 sshTarget.getSession().disconnect();
45 sshTarget.setSession(null);
46 if (log.isDebugEnabled())
47 log.debug("Cleared cached SSH context session to "
48 + getSshTarget());
49 }
50 }
51 }
52
53 public void setAutoconnect(Boolean autoconnect) {
54 this.autoconnect = autoconnect;
55 }
56
57 }