|
1 | 1 | package root.common.server.implement; |
2 | 2 |
|
3 | | -import java.io.IOException; |
4 | 3 | import java.io.InputStream; |
5 | | -import java.util.Properties; |
6 | 4 |
|
7 | 5 | import org.apache.commons.io.IOUtils; |
8 | 6 |
|
9 | 7 | import com.jcraft.jsch.Channel; |
10 | 8 | import com.jcraft.jsch.ChannelExec; |
11 | | -import com.jcraft.jsch.JSch; |
12 | 9 | import com.jcraft.jsch.JSchException; |
13 | 10 | import com.jcraft.jsch.Session; |
14 | 11 |
|
|
17 | 14 |
|
18 | 15 | @Slf4j |
19 | 16 | public class JschServer { |
20 | | - private JSch jsch; |
21 | | - private Session session; |
22 | 17 | private JschConnectionInfo jschConnectionInfo; |
| 18 | + private ServerSessionConnectionPoolFactory connPool = null; |
23 | 19 |
|
24 | 20 | public JschServer(JschConnectionInfo jschConnectionInfo) { |
25 | 21 | this.jschConnectionInfo = jschConnectionInfo; |
26 | 22 | } |
27 | 23 |
|
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(); |
45 | 26 | } |
46 | 27 |
|
47 | | - public Session getSession() { |
48 | | - if (session == null) { |
49 | | - return null; |
50 | | - } |
51 | | - return session; |
| 28 | + public void init() { |
| 29 | + connPool = new ServerSessionConnectionPoolFactory(); |
52 | 30 | } |
53 | 31 |
|
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 { |
63 | 33 | try { |
64 | | - session.connect(); |
65 | | - } catch (JSchException e) { |
| 34 | + return connPool.makeObject(jschConnectionInfo).getObject(); |
| 35 | + } catch (Exception e) { |
66 | 36 | log.error(e.getMessage()); |
67 | | - throw e; |
| 37 | + return null; |
68 | 38 | } |
69 | | - |
70 | | - return session; |
71 | 39 | } |
72 | 40 |
|
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)); |
76 | 44 | } |
77 | | - |
78 | | - session.disconnect(); |
79 | 45 | } |
80 | 46 |
|
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"); |
100 | 50 | } |
| 51 | + |
| 52 | + Channel channel = session.openChannel("exec"); |
| 53 | + ChannelExec channelExec = (ChannelExec) channel; |
| 54 | +// channelExec.setPty(true); |
| 55 | + channelExec.setCommand(command); |
101 | 56 | return channel; |
102 | 57 | } |
103 | | - |
104 | | - private Channel openExecChannel(String command) { |
105 | | - return openExecChannel(session, command); |
106 | | - } |
107 | 58 |
|
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(); |
119 | 63 | return in; |
120 | 64 | } |
121 | 65 |
|
122 | 66 | 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 | + } |
128 | 70 | } |
129 | 71 |
|
130 | | - public String executeCommand(String command) throws JSchException, IOException { |
| 72 | + public String executeCommand(Session session, String command) throws Exception { |
131 | 73 | log.debug(command); |
132 | | - Channel channel = openExecChannel(command); |
| 74 | + Channel channel = openExecChannel(session, command); |
133 | 75 | InputStream in = connectChannel(channel); |
134 | 76 | String result = IOUtils.toString(in, "UTF-8"); |
135 | 77 | disConnectChannel(channel); |
136 | 78 | disConnect(session); |
137 | | - return result.trim(); |
| 79 | + return result; |
138 | 80 | } |
139 | | - |
140 | | - public static boolean validateConn(Session session) { |
141 | | - if (session == null) { |
142 | | - log.error("JSch session is null"); |
143 | | - return false; |
144 | | - } |
145 | 81 |
|
| 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) { |
146 | 88 | try { |
147 | | - session.connect(3000); |
148 | | - } catch (JSchException e) { |
| 89 | + return connPool.validateObject(jschConnectionInfo, connPool.wrap(session)); |
| 90 | + } catch (Exception e) { |
149 | 91 | log.error(e.getMessage()); |
150 | 92 | return false; |
151 | 93 | } |
152 | | - |
153 | | - return session.isConnected(); |
154 | 94 | } |
155 | 95 | } |
0 commit comments