Skip to content

Commit 53dab1f

Browse files
authored
Merge pull request #209 from Dokyeongyun/ft-220405-jschServerRefactoring
Ft 220405 jsch server refactoring
2 parents 4132e9d + c05b28b commit 53dab1f

File tree

16 files changed

+319
-132
lines changed

16 files changed

+319
-132
lines changed

src/main/java/root/applications/ConsoleApp.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919
import root.core.domain.JschConnectionInfo;
2020
import root.core.repository.constracts.DBCheckRepository;
2121
import root.core.repository.constracts.PropertyRepository;
22-
import root.core.repository.constracts.ServerCheckRepository;
22+
import root.core.repository.constracts.ServerMonitoringRepository;
2323
import root.core.repository.implement.DBCheckRepositoryImpl;
24+
import root.core.repository.implement.LinuxServerMonitoringRepository;
2425
import root.core.repository.implement.PropertyRepositoryImpl;
2526
import root.core.repository.implement.ReportFileRepo;
26-
import root.core.repository.implement.ServerCheckRepositoryImpl;
2727
import root.core.service.contracts.PropertyService;
2828
import root.core.service.implement.FilePropertyService;
2929
import root.core.usecase.constracts.DBCheckUsecase;
30-
import root.core.usecase.constracts.ServerCheckUsecase;
30+
import root.core.usecase.constracts.ServerMonitoringUsecase;
3131
import root.core.usecase.implement.DBCheckUsecaseImpl;
32-
import root.core.usecase.implement.ServerCheckUsecaseImpl;
32+
import root.core.usecase.implement.ServerMonitoringUsecaseImpl;
3333
import root.utils.CsvUtils;
3434
import root.utils.DateUtils;
3535
import root.utils.PatternUtils;
@@ -162,8 +162,8 @@ public static void main(String[] args) throws IOException {
162162
System.out.println("■ [ " + jsch.getServerName() + " Monitoring Start ]\n");
163163
JschServer server = new JschServer(jsch);
164164
server.init();
165-
ServerCheckRepository repo = new ServerCheckRepositoryImpl(server);
166-
ServerCheckUsecase usecase = new ServerCheckUsecaseImpl(repo, ReportFileRepo.getInstance());
165+
ServerMonitoringRepository repo = new LinuxServerMonitoringRepository(server);
166+
ServerMonitoringUsecase usecase = new ServerMonitoringUsecaseImpl(repo, ReportFileRepo.getInstance());
167167
ServerCheckBatch serverBatch = new ServerCheckBatch(usecase);
168168

169169
String startDate = DateUtils.addDate(DateUtils.getToday("yyyy-MM-dd"), 0, 0, -1);

src/main/java/root/common/server/implement/JschServer.java

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,59 +40,82 @@ public void init() {
4040
log.error(e.getMessage());
4141
}
4242
}
43-
43+
4444
public Session getSession() {
45-
if(session == null) {
45+
if (session == null) {
4646
return null;
4747
}
4848
return session;
4949
}
50-
51-
public Session connect(Session session) {
50+
51+
public Session connect(Session session) throws JSchException {
52+
if(session == null) {
53+
throw new NullPointerException("Session is null");
54+
}
55+
56+
if(session.isConnected()) {
57+
return session;
58+
}
59+
5260
try {
5361
session.connect();
5462
} catch (JSchException e) {
5563
log.error(e.getMessage());
64+
throw e;
5665
}
66+
5767
return session;
5868
}
59-
69+
6070
public void disConnect(Session session) {
71+
if(session == null) {
72+
throw new NullPointerException("Session is null");
73+
}
74+
6175
session.disconnect();
6276
}
63-
77+
6478
public Channel openExecChannel(Session session, String command) {
79+
if(session == null) {
80+
init();
81+
try {
82+
session = this.connect(this.getSession());
83+
} catch (JSchException e) {
84+
log.error(e.getMessage());
85+
}
86+
}
87+
6588
Channel channel = null;
6689
try {
6790
channel = session.openChannel("exec");
68-
//채널접속
69-
ChannelExec channelExec = (ChannelExec) channel; //명령 전송 채널사용
70-
channelExec.setPty(true);
71-
channelExec.setCommand(command);
91+
// 채널접속
92+
ChannelExec channelExec = (ChannelExec) channel; // 명령 전송 채널사용
93+
// channelExec.setPty(true);
94+
channelExec.setCommand(command);
7295
} catch (JSchException e) {
7396
log.error(e.getMessage());
7497
}
7598
return channel;
7699
}
77-
100+
78101
public InputStream connectChannel(Channel channel) {
79102
InputStream in = null;
80103
try {
81104
// CallBack
82105
in = channel.getInputStream();
83-
((ChannelExec) channel).setErrStream(System.err);
84-
106+
((ChannelExec) channel).setErrStream(System.err);
107+
85108
channel.connect();
86109
} catch (Exception e) {
87110
log.error(e.getMessage());
88111
}
89112
return in;
90113
}
91-
114+
92115
public void disConnectChannel(Channel channel) {
93-
channel.disconnect();
116+
channel.disconnect();
94117
}
95-
118+
96119
public String getServerName() {
97120
return this.jschConnectionInfo.getServerName();
98121
}
@@ -104,12 +127,12 @@ public static boolean validateConn(Session session) {
104127
}
105128

106129
try {
107-
session.connect(15);
130+
session.connect(3000);
108131
} catch (JSchException e) {
109132
log.error(e.getMessage());
110133
return false;
111134
}
112-
135+
113136
return session.isConnected();
114137
}
115138
}

src/main/java/root/core/batch/ServerCheckBatch.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package root.core.batch;
22

33
import root.core.domain.AlertLogCommand;
4-
import root.core.usecase.constracts.ServerCheckUsecase;
4+
import root.core.usecase.constracts.ServerMonitoringUsecase;
55

66
public class ServerCheckBatch {
7-
private ServerCheckUsecase serverCheckUsecase;
7+
private ServerMonitoringUsecase serverCheckUsecase;
88

9-
public ServerCheckBatch(ServerCheckUsecase serverCheckUsecase) {
9+
public ServerCheckBatch(ServerMonitoringUsecase serverCheckUsecase) {
1010
this.serverCheckUsecase = serverCheckUsecase;
1111
}
1212

src/main/java/root/core/domain/AlertLogCommand.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,27 @@
66
@NoArgsConstructor
77
@Data
88
public class AlertLogCommand {
9-
private String readCommand;
109
private String readLine;
1110
private String readFilePath;
1211
private String dateFormat;
1312
private String dateFormatRegex;
1413
private String[] catchErrorMsg;
1514

16-
public AlertLogCommand(String readCommand, String readLine, String readFilePath) {
17-
this.readCommand = readCommand;
15+
public AlertLogCommand(String readLine, String readFilePath) {
1816
this.readLine = readLine;
1917
this.readFilePath = readFilePath;
2018
}
2119

22-
public AlertLogCommand(String readCommand, String readLine, String readFilePath, String dateFormat, String dateFormatRegex) {
23-
this.readCommand = readCommand;
20+
public AlertLogCommand(String readLine, String readFilePath, String dateFormat, String dateFormatRegex) {
2421
this.readLine = readLine;
2522
this.readFilePath = readFilePath;
2623
this.dateFormat = dateFormat;
2724
this.dateFormatRegex = dateFormatRegex;
2825
}
2926

30-
public AlertLogCommand(String readCommand, String readLine, String readFilePath, String... catchErrorMsg) {
31-
this.readCommand = readCommand;
27+
public AlertLogCommand(String readLine, String readFilePath, String... catchErrorMsg) {
3228
this.readLine = readLine;
3329
this.readFilePath = readFilePath;
3430
this.catchErrorMsg = catchErrorMsg;
3531
}
36-
37-
public String getCommand() {
38-
return this.getReadCommand() + " -" + this.getReadLine() + " " + this.getReadFilePath();
39-
}
4032
}

src/main/java/root/core/domain/JschConnectionInfo.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
public class JschConnectionInfo {
77
private String serverName;
88
private String host;
9-
private String port;
9+
private int port;
1010
private String userName;
1111
private String password;
1212
private AlertLogCommand alc;
13-
13+
1414
public JschConnectionInfo() {
1515
this.alc = new AlertLogCommand();
1616
}
1717

18-
public JschConnectionInfo(String serverName, String host, String port, String userName, String password) {
18+
public JschConnectionInfo(String serverName, String host, int port, String userName, String password) {
1919
this.serverName = serverName;
2020
this.host = host;
2121
this.port = port;
@@ -24,13 +24,32 @@ public JschConnectionInfo(String serverName, String host, String port, String us
2424
this.alc = new AlertLogCommand();
2525
}
2626

27-
public JschConnectionInfo(String serverName, String host, String port, String userName, String password,
27+
public JschConnectionInfo(String serverName, String host, String port, String userName, String password) {
28+
this(serverName, host, 22, userName, password);
29+
this.setPort(port);
30+
}
31+
32+
public JschConnectionInfo(String serverName, String host, int port, String userName, String password,
2833
AlertLogCommand alc) {
29-
this.serverName = serverName;
30-
this.host = host;
31-
this.port = port;
32-
this.userName = userName;
33-
this.password = password;
34+
this(serverName, host, port, userName, password);
3435
this.alc = alc;
3536
}
37+
38+
public JschConnectionInfo(String serverName, String host, String port, String userName, String password,
39+
AlertLogCommand alc) {
40+
this(serverName, host, 22, userName, password, alc);
41+
this.setPort(port);
42+
}
43+
44+
public int getPort() {
45+
return this.port == 0 ? 22 : this.port;
46+
}
47+
48+
public void setPort(String portString) {
49+
try {
50+
this.port = Integer.parseInt(portString);
51+
} catch (NumberFormatException e) {
52+
this.port = 22; // ±âº»°ª
53+
}
54+
}
3655
}

src/main/java/root/core/repository/constracts/ServerCheckRepository.java renamed to src/main/java/root/core/repository/constracts/ServerMonitoringRepository.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,13 @@
22

33
import java.util.List;
44

5-
import com.jcraft.jsch.Session;
6-
75
import root.core.domain.AlertLog;
86
import root.core.domain.AlertLogCommand;
97
import root.core.domain.OSDiskUsage;
108

11-
public interface ServerCheckRepository {
9+
public interface ServerMonitoringRepository {
1210
String getServerName();
1311

14-
Session getSession();
15-
16-
Session connectSession(Session session);
17-
18-
void disConnectSession(Session session);
19-
2012
int getAlertLogFileLineCount(AlertLogCommand alc);
2113

2214
String checkAlertLog(AlertLogCommand alc);

0 commit comments

Comments
 (0)