Skip to content

Commit 667628d

Browse files
committed
Control JSch session life cycle using SessionConnectionPool
1 parent deb795f commit 667628d

File tree

6 files changed

+127
-176
lines changed

6 files changed

+127
-176
lines changed
Lines changed: 38 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package root.common.server.implement;
22

3-
import java.io.IOException;
43
import java.io.InputStream;
5-
import java.util.Properties;
64

75
import org.apache.commons.io.IOUtils;
86

97
import com.jcraft.jsch.Channel;
108
import com.jcraft.jsch.ChannelExec;
11-
import com.jcraft.jsch.JSch;
129
import com.jcraft.jsch.JSchException;
1310
import com.jcraft.jsch.Session;
1411

@@ -17,139 +14,82 @@
1714

1815
@Slf4j
1916
public class JschServer {
20-
private JSch jsch;
21-
private Session session;
2217
private JschConnectionInfo jschConnectionInfo;
18+
private ServerSessionConnectionPoolFactory connPool = null;
2319

2420
public JschServer(JschConnectionInfo jschConnectionInfo) {
2521
this.jschConnectionInfo = jschConnectionInfo;
2622
}
2723

28-
public void init() {
29-
jsch = new JSch();
30-
session = null;
31-
32-
try {
33-
session = jsch.getSession(jschConnectionInfo.getUserName(), jschConnectionInfo.getHost(),
34-
Integer.valueOf(jschConnectionInfo.getPort()));
35-
session.setPassword(jschConnectionInfo.getPassword());
36-
37-
Properties config = new Properties();
38-
config.put("StrictHostKeyChecking", "no"); // 호스트 정보를 검사하지 않는다.
39-
config.put("PreferredAuthentications", "password");
40-
session.setConfig(config);
41-
42-
} catch (JSchException e) {
43-
log.error(e.getMessage());
44-
}
24+
public String getServerName() {
25+
return jschConnectionInfo.getServerName();
4526
}
4627

47-
public Session getSession() {
48-
if (session == null) {
49-
return null;
50-
}
51-
return session;
28+
public void init() {
29+
connPool = new ServerSessionConnectionPoolFactory();
5230
}
5331

54-
public Session connect(Session session) throws JSchException {
55-
if(session == null) {
56-
throw new NullPointerException("Session is null");
57-
}
58-
59-
if(session.isConnected()) {
60-
return session;
61-
}
62-
32+
public Session getSession() throws Exception {
6333
try {
64-
session.connect();
65-
} catch (JSchException e) {
34+
return connPool.makeObject(jschConnectionInfo).getObject();
35+
} catch (Exception e) {
6636
log.error(e.getMessage());
67-
throw e;
37+
return null;
6838
}
69-
70-
return session;
7139
}
7240

73-
public void disConnect(Session session) {
74-
if(session == null) {
75-
throw new NullPointerException("Session is null");
41+
public void disConnect(Session session) throws Exception {
42+
if (session != null && session.isConnected()) {
43+
connPool.destroyObject(jschConnectionInfo, connPool.wrap(session));
7644
}
77-
78-
session.disconnect();
7945
}
8046

81-
public Channel openExecChannel(Session session, String command) {
82-
if(session == null) {
83-
init();
84-
try {
85-
session = this.connect(this.getSession());
86-
} catch (JSchException e) {
87-
log.error(e.getMessage());
88-
}
89-
}
90-
91-
Channel channel = null;
92-
try {
93-
channel = session.openChannel("exec");
94-
// 채널접속
95-
ChannelExec channelExec = (ChannelExec) channel; // 명령 전송 채널사용
96-
// channelExec.setPty(true);
97-
channelExec.setCommand(command);
98-
} catch (JSchException e) {
99-
log.error(e.getMessage());
47+
public Channel openExecChannel(Session session, String command) throws JSchException {
48+
if (session == null || !session.isConnected()) {
49+
throw new JSchException("session is not valid");
10050
}
51+
52+
Channel channel = session.openChannel("exec");
53+
ChannelExec channelExec = (ChannelExec) channel;
54+
// channelExec.setPty(true);
55+
channelExec.setCommand(command);
10156
return channel;
10257
}
103-
104-
private Channel openExecChannel(String command) {
105-
return openExecChannel(session, command);
106-
}
10758

108-
public InputStream connectChannel(Channel channel) {
109-
InputStream in = null;
110-
try {
111-
// CallBack
112-
in = channel.getInputStream();
113-
((ChannelExec) channel).setErrStream(System.err);
114-
115-
channel.connect();
116-
} catch (Exception e) {
117-
log.error(e.getMessage());
118-
}
59+
public InputStream connectChannel(Channel channel) throws Exception {
60+
InputStream in = channel.getInputStream();
61+
((ChannelExec) channel).setErrStream(System.err);
62+
channel.connect();
11963
return in;
12064
}
12165

12266
public void disConnectChannel(Channel channel) {
123-
channel.disconnect();
124-
}
125-
126-
public String getServerName() {
127-
return this.jschConnectionInfo.getServerName();
67+
if (channel != null && channel.isConnected()) {
68+
channel.disconnect();
69+
}
12870
}
12971

130-
public String executeCommand(String command) throws JSchException, IOException {
72+
public String executeCommand(Session session, String command) throws Exception {
13173
log.debug(command);
132-
Channel channel = openExecChannel(command);
74+
Channel channel = openExecChannel(session, command);
13375
InputStream in = connectChannel(channel);
13476
String result = IOUtils.toString(in, "UTF-8");
13577
disConnectChannel(channel);
13678
disConnect(session);
137-
return result.trim();
79+
return result;
13880
}
139-
140-
public static boolean validateConn(Session session) {
141-
if (session == null) {
142-
log.error("JSch session is null");
143-
return false;
144-
}
14581

82+
public String executeCommand(String command) throws Exception {
83+
Session session = getSession();
84+
return executeCommand(session, command);
85+
}
86+
87+
public boolean validateConn(Session session) {
14688
try {
147-
session.connect(3000);
148-
} catch (JSchException e) {
89+
return connPool.validateObject(jschConnectionInfo, connPool.wrap(session));
90+
} catch (Exception e) {
14991
log.error(e.getMessage());
15092
return false;
15193
}
152-
153-
return session.isConnected();
15494
}
15595
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package root.common.server.implement;
2+
3+
import java.util.Properties;
4+
5+
import org.apache.commons.pool2.BaseKeyedPooledObjectFactory;
6+
import org.apache.commons.pool2.PooledObject;
7+
import org.apache.commons.pool2.impl.DefaultPooledObject;
8+
9+
import com.jcraft.jsch.JSch;
10+
import com.jcraft.jsch.Session;
11+
12+
import root.core.domain.JschConnectionInfo;
13+
14+
public class ServerSessionConnectionPoolFactory extends BaseKeyedPooledObjectFactory<JschConnectionInfo, Session> {
15+
16+
@Override
17+
public Session create(JschConnectionInfo connInfo) throws Exception {
18+
JSch jsch = new JSch();
19+
Session session = jsch.getSession(connInfo.getUserName(), connInfo.getHost(), connInfo.getPort());
20+
session.setPassword(connInfo.getPassword());
21+
22+
Properties config = new Properties();
23+
config.put("StrictHostKeyChecking", "no"); // 호스트 정보를 검사하지 않는다.
24+
config.put("PreferredAuthentications", "password");
25+
session.setConfig(config);
26+
27+
session.connect();
28+
return session;
29+
}
30+
31+
@Override
32+
public void destroyObject(JschConnectionInfo key, PooledObject<Session> p) throws Exception {
33+
p.getObject().disconnect();
34+
}
35+
36+
@Override
37+
public boolean validateObject(JschConnectionInfo key, PooledObject<Session> p) {
38+
return p.getObject().isConnected();
39+
}
40+
41+
@Override
42+
public PooledObject<Session> wrap(Session obj) {
43+
return new DefaultPooledObject<>(obj);
44+
}
45+
}

src/main/java/root/javafx/Service/ServerConnectService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected Task<Boolean> createTask() {
2525
@Override
2626
protected Boolean call() throws Exception {
2727
jschServer.init();
28-
boolean isConn = JschServer.validateConn(jschServer.getSession());
28+
boolean isConn = jschServer.validateConn(jschServer.getSession());
2929
if (!isConn) {
3030
throw new Exception("Server connection test Failed");
3131
}

0 commit comments

Comments
 (0)