Skip to content

Commit 7d5ee81

Browse files
authored
Merge pull request #202 from Dokyeongyun/ft-220329-serverConnectionTest
Ft 220329 server connection test
2 parents 0c5c4d8 + d6ef75e commit 7d5ee81

File tree

9 files changed

+81
-58
lines changed

9 files changed

+81
-58
lines changed

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import com.jcraft.jsch.JSchException;
1010
import com.jcraft.jsch.Session;
1111

12+
import lombok.extern.slf4j.Slf4j;
1213
import root.core.domain.JschConnectionInfo;
1314

15+
@Slf4j
1416
public class JschServer {
1517
private JSch jsch;
1618
private Session session;
@@ -23,19 +25,19 @@ public JschServer(JschConnectionInfo jschConnectionInfo) {
2325
public void init() {
2426
jsch = new JSch();
2527
session = null;
26-
28+
2729
try {
2830
session = jsch.getSession(jschConnectionInfo.getUserName(), jschConnectionInfo.getHost(),
2931
Integer.valueOf(jschConnectionInfo.getPort()));
3032
session.setPassword(jschConnectionInfo.getPassword());
31-
33+
3234
Properties config = new Properties();
3335
config.put("StrictHostKeyChecking", "no"); // 호스트 정보를 검사하지 않는다.
36+
config.put("PreferredAuthentications", "password");
3437
session.setConfig(config);
35-
38+
3639
} catch (JSchException e) {
37-
System.out.println("JSch Session Creation Faild!");
38-
e.printStackTrace();
40+
log.error(e.getMessage());
3941
}
4042
}
4143

@@ -50,8 +52,7 @@ public Session connect(Session session) {
5052
try {
5153
session.connect();
5254
} catch (JSchException e) {
53-
System.out.println("JSch Connection Faild!");
54-
e.printStackTrace();
55+
log.error(e.getMessage());
5556
}
5657
return session;
5758
}
@@ -69,8 +70,7 @@ public Channel openExecChannel(Session session, String command) {
6970
channelExec.setPty(true);
7071
channelExec.setCommand(command);
7172
} catch (JSchException e) {
72-
System.out.println("Channel Open Faild!");
73-
// e.printStackTrace();
73+
log.error(e.getMessage());
7474
}
7575
return channel;
7676
}
@@ -84,7 +84,7 @@ public InputStream connectChannel(Channel channel) {
8484

8585
channel.connect();
8686
} catch (Exception e) {
87-
System.out.println("Channel Connect Failed!");
87+
log.error(e.getMessage());
8888
}
8989
return in;
9090
}
@@ -96,4 +96,20 @@ public void disConnectChannel(Channel channel) {
9696
public String getServerName() {
9797
return this.jschConnectionInfo.getServerName();
9898
}
99+
100+
public static boolean validateConn(Session session) {
101+
if (session == null) {
102+
log.error("JSch session is null");
103+
return false;
104+
}
105+
106+
try {
107+
session.connect(15);
108+
} catch (JSchException e) {
109+
log.error(e.getMessage());
110+
return false;
111+
}
112+
113+
return session.isConnected();
114+
}
99115
}

src/main/java/root/javafx/CustomView/ConnectionInfoAP.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import javafx.scene.layout.AnchorPane;
44

5-
public class ConnectionInfoAP extends AnchorPane {
5+
public abstract class ConnectionInfoAP extends AnchorPane {
66

7+
abstract boolean isAnyEmptyInputForConnectionTest();
78
}

src/main/java/root/javafx/CustomView/ConnectionInfoVBox.java

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import root.javafx.DI.DependencyInjection;
2929
import root.javafx.Service.ConnectionTestService;
3030
import root.utils.AlertUtils;
31-
import root.utils.SceneUtils;
3231

3332
public class ConnectionInfoVBox<T> extends VBox {
3433

@@ -47,6 +46,9 @@ public class ConnectionInfoVBox<T> extends VBox {
4746
@FXML
4847
Text connInfoText; // 접속정보 인덱스 텍스트
4948

49+
@FXML
50+
FontAwesomeIconView connTestIcon;
51+
5052
@FXML
5153
JFXButton connTestBtn;
5254

@@ -215,9 +217,6 @@ private void bringFrontConnInfoAnchorPane(long index) {
215217
prevConnInfoBtn.setDisable(false);
216218
nextConnInfoBtn.setDisable(false);
217219
}
218-
219-
// Index logging
220-
// this.connInfoAPMap.print(connInfoIdx);
221220
}
222221

223222
private void setConnInfoIndexText() {
@@ -234,24 +233,23 @@ private void setConnInfoIndexText() {
234233

235234
// TODO Convert to Enum class
236235
private void setConnectionBtnIcon(int type) {
237-
FontAwesomeIconView icon = (FontAwesomeIconView) connTestBtn.lookup("#icon");
238236
switch (type) {
239237
case 1:
240-
icon.setIcon(FontAwesomeIcon.PLUG);
241-
icon.setFill(Paint.valueOf("#000000"));
238+
connTestIcon.setIcon(FontAwesomeIcon.PLUG);
239+
connTestIcon.setFill(Paint.valueOf("#000000"));
242240
break;
243241
case 2:
244-
icon.setIcon(FontAwesomeIcon.CHECK);
245-
icon.setFill(Paint.valueOf("#49a157"));
242+
connTestIcon.setIcon(FontAwesomeIcon.CHECK);
243+
connTestIcon.setFill(Paint.valueOf("#49a157"));
246244
break;
247245
case 3:
248-
icon.setIcon(FontAwesomeIcon.TIMES);
249-
icon.setFill(Paint.valueOf("#c40a0a"));
246+
connTestIcon.setIcon(FontAwesomeIcon.TIMES);
247+
connTestIcon.setFill(Paint.valueOf("#c40a0a"));
250248
break;
251249
case 4:
252-
icon.setIcon(FontAwesomeIcon.SPINNER);
253-
icon.setFill(Paint.valueOf("#484989"));
254-
icon.getStyleClass().add("fa-spin");
250+
connTestIcon.setIcon(FontAwesomeIcon.SPINNER);
251+
connTestIcon.setFill(Paint.valueOf("#484989"));
252+
connTestIcon.getStyleClass().add("fa-spin");
255253
break;
256254
}
257255
}
@@ -357,18 +355,5 @@ public long getNextActiveIdx(long connInfoIdx) {
357355
.findFirst()
358356
.orElse(getLastActiveIdx());
359357
}
360-
361-
public void print(long index) {
362-
System.out.println("Current Index: " + index);
363-
for (Long key : map.keySet()) {
364-
if(key == index) {
365-
System.out.print(key + "[:" + map.get(key).getStatus() + ":], ");
366-
} else {
367-
System.out.print(key + "[" + map.get(key).getStatus() + "], ");
368-
}
369-
370-
}
371-
System.out.println();
372-
}
373358
}
374359
}

src/main/java/root/javafx/CustomView/DBConnInfoControl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public boolean save(String configFilePath, Collection<StatefulAP> statefulAP) {
4242

4343
@Override
4444
public boolean canConnectionTest(ConnectionInfoAP curAP) {
45-
return ((DBConnectionInfoAnchorPane) curAP).isAnyEmptyInputForDBConnectionTest();
45+
return curAP.isAnyEmptyInputForConnectionTest();
4646
}
4747

4848
@Override

src/main/java/root/javafx/CustomView/DBConnectionInfoAnchorPane.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ public DBConnectionInfoAnchorPane() {
6161
e.printStackTrace();
6262
}
6363
}
64+
65+
@Override
66+
public boolean isAnyEmptyInputForConnectionTest() {
67+
return StringUtils.isAnyEmpty(hostTF.getText(), portTF.getText(), sidTF.getText(), userTF.getText(),
68+
passwordPF.getText(), driverCB.getSelectionModel().getSelectedItem());
69+
}
6470

6571
public void init() {
6672
// DB Url Generate Event Setting
@@ -123,11 +129,6 @@ public JdbcConnectionInfo getInputValues() {
123129
return jdbc;
124130
}
125131

126-
public boolean isAnyEmptyInputForDBConnectionTest() {
127-
return StringUtils.isAnyEmpty(hostTF.getText(), portTF.getText(), sidTF.getText(), userTF.getText(),
128-
passwordPF.getText(), driverCB.getSelectionModel().getSelectedItem());
129-
}
130-
131132
public boolean isAnyEmptyInput() {
132133
return StringUtils.isAnyEmpty(hostTF.getText(), portTF.getText(), sidTF.getText(), userTF.getText(),
133134
passwordPF.getText(), aliasTF.getText(), driverCB.getSelectionModel().getSelectedItem());
@@ -162,7 +163,7 @@ private void setDBConnTestBtnDisable(Node node) {
162163
}
163164

164165
// DB Connection test button lookup and setDisable
165-
topParent.lookup("#connTestBtn").setDisable(isAnyEmptyInputForDBConnectionTest());
166+
topParent.lookup("#connTestBtn").setDisable(isAnyEmptyInputForConnectionTest());
166167
} catch (Exception e) {
167168
e.printStackTrace();
168169
}

src/main/java/root/javafx/CustomView/ServerConnInfoControl.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
import java.util.Map;
66

77
import javafx.scene.control.Alert.AlertType;
8+
import javafx.scene.control.PasswordField;
9+
import javafx.scene.control.TextField;
810
import root.core.domain.JschConnectionInfo;
911
import root.core.repository.constracts.PropertyRepository;
1012
import root.core.repository.implement.PropertyRepositoryImpl;
1113
import root.javafx.CustomView.ConnectionInfoVBox.StatefulAP;
1214
import root.javafx.Service.ConnectionTestService;
15+
import root.javafx.Service.ServerConnectService;
1316
import root.utils.AlertUtils;
1417

1518
public class ServerConnInfoControl implements ConnInfoControl<JschConnectionInfo> {
@@ -38,15 +41,17 @@ public boolean save(String configFilePath, Collection<StatefulAP> statefulAP) {
3841

3942
@Override
4043
public boolean canConnectionTest(ConnectionInfoAP curAP) {
41-
// TODO Auto-generated method stub
42-
return true;
44+
return curAP.isAnyEmptyInputForConnectionTest();
4345
}
4446

4547
@Override
4648
public ConnectionTestService getConnectionTestService(ConnectionInfoAP curAP) {
47-
// TODO Auto-generated method stub
48-
System.out.println("Server test()");
49-
return null;
49+
String host = ((TextField) curAP.lookup("#hostTF")).getText();
50+
String port = ((TextField) curAP.lookup("#portTF")).getText();
51+
String id = ((TextField) curAP.lookup("#userTF")).getText();
52+
String pw = ((PasswordField) curAP.lookup("#passwordPF")).getText();
53+
54+
return new ServerConnectService(new JschConnectionInfo("", host, port, id, pw));
5055
}
5156

5257
@Override

src/main/java/root/javafx/CustomView/ServerConnectionInfoAnchorPane.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public ServerConnectionInfoAnchorPane() {
5757
e.printStackTrace();
5858
}
5959
}
60+
61+
@Override
62+
boolean isAnyEmptyInputForConnectionTest() {
63+
return StringUtils.isAnyEmpty(hostTF.getText(), portTF.getText(), userTF.getText(), passwordPF.getText());
64+
}
6065

6166
public void init() {
6267
// Set textFormatter
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
package root.javafx.Service;
22

33
import javafx.concurrent.Task;
4+
import javafx.scene.control.Alert.AlertType;
5+
import root.common.server.implement.JschServer;
46
import root.core.domain.JschConnectionInfo;
7+
import root.utils.AlertUtils;
58

69
public class ServerConnectService extends ConnectionTestService {
710

8-
public static final String SUCCESS_MSG = "원격서버가 성공적으로 연동되었습니다.";
11+
public static final String SUCCESS_MSG = "원격서버에 성공적으로 연동되었습니다.\n Host: %s:%s";
912
public static final String FAIL_MSG = "원격서버 연동에 실패했습니다.";
1013

1114
private JschConnectionInfo jsch;
15+
private JschServer jschServer;
1216

1317
public ServerConnectService(JschConnectionInfo jsch) {
1418
this.jsch = jsch;
19+
this.jschServer = new JschServer(jsch);
1520
}
1621

1722
@Override
1823
protected Task<Boolean> createTask() {
1924
return new Task<Boolean>() {
2025
@Override
2126
protected Boolean call() throws Exception {
22-
return true;
27+
jschServer.init();
28+
boolean isConn = JschServer.validateConn(jschServer.getSession());
29+
if (!isConn) {
30+
throw new Exception("Server connection test Failed");
31+
}
32+
return isConn;
2333
}
2434
};
2535
}
2636

2737
@Override
2838
public void alertSucceed() {
29-
// TODO Auto-generated method stub
30-
39+
AlertUtils.showAlert(AlertType.INFORMATION, "Server 연동테스트",
40+
String.format(ServerConnectService.SUCCESS_MSG, jsch.getHost(), jsch.getPort()));
3141
}
3242

3343
@Override
3444
public void alertFailed() {
35-
// TODO Auto-generated method stub
36-
45+
AlertUtils.showAlert(AlertType.ERROR, "Server 연동테스트",
46+
String.format(ServerConnectService.FAIL_MSG, jsch.getHost(), jsch.getPort()));
3747
}
3848
}

src/main/resources/fxml/ConnectionInfoVBox.fxml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<String fx:value="font-black" />
3030
</styleClass>
3131
<graphic>
32-
<FontAwesomeIconView fx:id="menuIconIV" fill="#0132ac" glyphName="" selectionFill="BLACK" size="20" />
32+
<FontAwesomeIconView fx:id="menuIconIV" fill="#0132ac" glyphName="" selectionFill="BLACK" size="20" text="" />
3333
</graphic>
3434
</Label>
3535
<ToolBar style="-fx-background-color: #ffffff00;" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="0.0">
@@ -41,7 +41,7 @@
4141
<Cursor fx:constant="HAND" />
4242
</cursor>
4343
<graphic>
44-
<FontAwesomeIconView fx:id="icon" glyphName="PLUG" />
44+
<FontAwesomeIconView fx:id="connTestIcon" glyphName="PLUG" />
4545
</graphic>
4646
<HBox.margin>
4747
<Insets right="5.0" />

0 commit comments

Comments
 (0)