Skip to content

Commit d3952a3

Browse files
committed
Refactoring: Move 'executeCommand' method to JschServer for seperation of concern
1 parent 53dab1f commit d3952a3

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package root.common.server.implement;
22

3+
import java.io.IOException;
34
import java.io.InputStream;
45
import java.util.Properties;
56

7+
import org.apache.commons.io.IOUtils;
8+
69
import com.jcraft.jsch.Channel;
710
import com.jcraft.jsch.ChannelExec;
811
import com.jcraft.jsch.JSch;
@@ -97,6 +100,10 @@ public Channel openExecChannel(Session session, String command) {
97100
}
98101
return channel;
99102
}
103+
104+
private Channel openExecChannel(String command) {
105+
return openExecChannel(session, command);
106+
}
100107

101108
public InputStream connectChannel(Channel channel) {
102109
InputStream in = null;
@@ -120,6 +127,15 @@ public String getServerName() {
120127
return this.jschConnectionInfo.getServerName();
121128
}
122129

130+
public String executeCommand(String command) throws JSchException, IOException {
131+
Channel channel = openExecChannel(command);
132+
InputStream in = connectChannel(channel);
133+
String result = IOUtils.toString(in, "UTF-8");
134+
disConnectChannel(channel);
135+
disConnect(session);
136+
return result.trim();
137+
}
138+
123139
public static boolean validateConn(Session session) {
124140
if (session == null) {
125141
log.error("JSch session is null");

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

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package root.core.repository.implement;
22

3-
import java.io.IOException;
43
import java.io.InputStream;
54
import java.time.LocalDate;
65
import java.util.ArrayList;
@@ -11,7 +10,6 @@
1110
import org.apache.commons.io.IOUtils;
1211

1312
import com.jcraft.jsch.Channel;
14-
import com.jcraft.jsch.JSchException;
1513
import com.jcraft.jsch.Session;
1614

1715
import lombok.extern.slf4j.Slf4j;
@@ -42,9 +40,9 @@ public String getServerName() {
4240
public int getAlertLogFileLineCount(AlertLogCommand alc) {
4341
int fileLineCnt = 0;
4442
try {
45-
String command = "cat " + alc.getReadFilePath() + " | wc -l";
46-
String executeResult = executeCommand(command);
47-
fileLineCnt = Integer.parseInt(executeResult.trim());
43+
String command = String.format("cat %s | wc -l", alc.getReadFilePath());
44+
String executeResult = jsch.executeCommand(command);
45+
fileLineCnt = Integer.parseInt(executeResult);
4846
} catch (Exception e) {
4947
log.error(e.getMessage());
5048
}
@@ -56,8 +54,8 @@ public int getAlertLogFileLineCount(AlertLogCommand alc) {
5654
public String checkAlertLog(AlertLogCommand alc) {
5755
String result = "";
5856
try {
59-
String command = "tail -" + alc.getReadLine() + " " + alc.getReadFilePath();
60-
result = executeCommand(command);
57+
String command = String.format("tail -%d %s", alc.getReadLine(), alc.getReadFilePath());
58+
result = jsch.executeCommand(command);
6159
} catch (Exception e) {
6260
log.error(e.getMessage());
6361
}
@@ -252,18 +250,4 @@ private String getAlertLogStringFromCertainDate(AlertLogCommand alc, String star
252250

253251
return fullAlertLogString;
254252
}
255-
256-
private String executeCommand(String command) throws JSchException, IOException {
257-
String result = null;
258-
259-
Session session = jsch.getSession();
260-
session.connect();
261-
Channel channel = jsch.openExecChannel(session, command);
262-
InputStream in = jsch.connectChannel(channel);
263-
result = IOUtils.toString(in, "UTF-8");
264-
jsch.disConnectChannel(channel);
265-
jsch.disConnect(session);
266-
267-
return result;
268-
}
269253
}

0 commit comments

Comments
 (0)