-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerTask.java
More file actions
72 lines (60 loc) · 1.69 KB
/
Copy pathServerTask.java
File metadata and controls
72 lines (60 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package uploadServerTool;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public abstract class ServerTask implements Runnable {
public static final int code_error = 1;
public static final int code_normal = 0;
List<ServerTaskListener> listeners;
final Server server;
public ServerTask(Server server) {
this.server = server;
}
public void run() {
JSch jsch = new JSch();
try {
Session session = jsch.getSession(this.server.username, this.server.host, this.server.port);
String keyFile = this.server.getKeyFile();
if ((keyFile == null) || (keyFile.length() <= 0)) {
session.setPassword(this.server.password);
} else {
jsch.addIdentity(keyFile);
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect(10000);
_run(session);
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
}
}
protected abstract void _run(Session paramSession);
public void upMsg(int code, String msg) {
if (this.listeners == null) {
return;
}
for (ServerTaskListener l : this.listeners) {
l.update(code, msg);
}
}
public void upMsgln(int code, String msg) {
upMsg(code, msg + "\n");
}
public void upMsgFinish() {
upMsgln(0, " ok.");
}
public void upMsgFailure() {
upMsgln(1, " failure!");
}
public void addListener(ServerTaskListener listener) {
if (this.listeners == null) {
this.listeners = new ArrayList();
}
this.listeners.add(listener);
}
}