Skip to content

Commit 24ef747

Browse files
committed
Refactoring: Separate the concerns of JSchServer and Server Repository
1 parent 0d5bb03 commit 24ef747

File tree

3 files changed

+12
-40
lines changed

3 files changed

+12
-40
lines changed

src/main/java/root/core/repository/constracts/ServerMonitoringRepository.java

Lines changed: 0 additions & 8 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

119
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);

src/main/java/root/core/repository/implement/LinuxServerMonitoringRepository.java

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.apache.commons.io.IOUtils;
1111

1212
import com.jcraft.jsch.Channel;
13-
import com.jcraft.jsch.JSchException;
1413
import com.jcraft.jsch.Session;
1514

1615
import lombok.extern.slf4j.Slf4j;
@@ -37,39 +36,18 @@ public String getServerName() {
3736
return jsch.getServerName();
3837
}
3938

40-
@Override
41-
public Session getSession() {
42-
return jsch.getSession();
43-
}
44-
45-
@Override
46-
public Session connectSession(Session session) {
47-
try {
48-
session.connect();
49-
} catch (JSchException e) {
50-
log.error(e.getMessage());
51-
}
52-
return session;
53-
}
54-
55-
@Override
56-
public void disConnectSession(Session session) {
57-
if (session.isConnected() == true && session != null) {
58-
session.disconnect();
59-
}
60-
}
61-
6239
@Override
6340
public int getAlertLogFileLineCount(AlertLogCommand alc) {
6441
int fileLineCnt = 0;
6542
try {
66-
Session session = this.getSession();
67-
session = this.connectSession(session);
43+
Session session = jsch.getSession();
44+
session.connect();
6845
Channel channel = jsch.openExecChannel(session, "cat " + alc.getReadFilePath() + " | wc -l");
6946
InputStream in = jsch.connectChannel(channel);
7047
String result = IOUtils.toString(in, "UTF-8");
7148
fileLineCnt = Integer.parseInt(result.trim());
7249
jsch.disConnectChannel(channel);
50+
jsch.disConnect(session);
7351
} catch (Exception e) {
7452
log.error(e.getMessage());
7553
}
@@ -81,13 +59,14 @@ public int getAlertLogFileLineCount(AlertLogCommand alc) {
8159
public String checkAlertLog(AlertLogCommand alc) {
8260
String result = "";
8361
try {
84-
Session session = this.getSession();
85-
session = this.connectSession(session);
62+
Session session = jsch.getSession();
63+
session.connect();
8664
String command = "tail -" + alc.getReadLine() + " " + alc.getReadFilePath();
8765
Channel channel = jsch.openExecChannel(session, command);
8866
InputStream in = jsch.connectChannel(channel);
8967
result = IOUtils.toString(in, "UTF-8");
9068
jsch.disConnectChannel(channel);
69+
jsch.disConnect(session);
9170
} catch (Exception e) {
9271
log.error(e.getMessage());
9372
}
@@ -176,13 +155,14 @@ public AlertLog checkAlertLogDuringPeriod(AlertLogCommand alc, String startDate,
176155
public List<OSDiskUsage> checkOSDiskUsage() {
177156
List<OSDiskUsage> list = new ArrayList<>();
178157
try {
179-
Session session = this.getSession();
180-
session = this.connectSession(session);
158+
Session session = jsch.getSession();
159+
session.connect();
181160
Channel channel = jsch.openExecChannel(session, "df --block-size=K -P");
182161
InputStream in = jsch.connectChannel(channel);
183162
String result = IOUtils.toString(in, "UTF-8");
184163
list = stringToOsDiskUsageList(result);
185164
jsch.disConnectChannel(channel);
165+
jsch.disConnect(session);
186166
} catch (Exception e) {
187167
log.error(e.getMessage());
188168
}

src/test/java/root/core/repository/implement/LinuxServerMonitoringRepositoryTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void checkAlertLogTest() {
7171
AlertLogCommand alc = mock(AlertLogCommand.class);
7272
InputStream in = new ByteArrayInputStream(alertLogString.getBytes());
7373

74-
when(repo.getSession()).thenReturn(session);
74+
when(jsch.getSession()).thenReturn(session);
7575
String command = "tail -" + alc.getReadLine() + " " + alc.getReadFilePath();
7676
when(jsch.openExecChannel(session, command)).thenReturn(channel);
7777
when(jsch.connectChannel(channel)).thenReturn(in);
@@ -89,7 +89,7 @@ public void getAlertLogFileLineCountTest() {
8989
AlertLogCommand alc = mock(AlertLogCommand.class);
9090
InputStream in = new ByteArrayInputStream("26".getBytes());
9191

92-
when(repo.getSession()).thenReturn(session);
92+
when(jsch.getSession()).thenReturn(session);
9393
when(jsch.openExecChannel(session, "cat " + alc.getReadFilePath() + " | wc -l")).thenReturn(channel);
9494
when(jsch.connectChannel(channel)).thenReturn(in);
9595
doNothing().when(jsch).disConnectChannel(channel);
@@ -113,7 +113,7 @@ public void checkAlertLogDuringPeriod() {
113113
InputStream in = new ByteArrayInputStream(alertLogString.getBytes());
114114
InputStream in2 = new ByteArrayInputStream("26".getBytes());
115115

116-
when(repo.getSession()).thenReturn(session);
116+
when(jsch.getSession()).thenReturn(session);
117117
when(jsch.openExecChannel(session, "cat " + alc.getReadFilePath() + " | wc -l")).thenReturn(channel2);
118118
when(jsch.connectChannel(channel2)).thenReturn(in2);
119119
doNothing().when(jsch).disConnectChannel(channel2);

0 commit comments

Comments
 (0)