]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/JschContextSession.java
add some private constructors with no arg, some getters & setters and some ids to...
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / jsch / JschContextSession.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 org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.springframework.beans.factory.DisposableBean;
22 import org.springframework.beans.factory.InitializingBean;
23
24 import com.jcraft.jsch.Session;
25
26 /** Caches a JSCH session in the the ssh target. */
27 public class JschContextSession extends AbstractJschTask implements
28 InitializingBean, DisposableBean {
29 private final static Log log = LogFactory.getLog(JschContextSession.class);
30 private Boolean autoconnect = false;
31
32 @Override
33 void run(Session session) {
34 // clear();
35 getSshTarget().setSession(session);
36 if (log.isDebugEnabled())
37 log.debug("Cached SSH context session to " + getSshTarget());
38 }
39
40 public void afterPropertiesSet() throws Exception {
41 // if (log.isDebugEnabled())
42 // log.debug(getClass() + ".afterPropertiesSet(), " + beanName + ", "
43 // + this);
44 if (autoconnect)
45 try {
46 run();
47 } catch (Exception e) {
48 log.error("Could not automatically open session", e);
49 }
50 }
51
52 public void destroy() throws Exception {
53 clear();
54 }
55
56 public void clear() {
57 SshTarget sshTarget = getSshTarget();
58 synchronized (sshTarget) {
59 if (sshTarget.getSession() != null) {
60 sshTarget.getSession().disconnect();
61 sshTarget.setSession(null);
62 if (log.isDebugEnabled())
63 log.debug("Cleared cached SSH context session to "
64 + getSshTarget());
65 }
66 }
67 }
68
69 public void setAutoconnect(Boolean autoconnect) {
70 this.autoconnect = autoconnect;
71 }
72
73 }