refactor and reformate code
This commit is contained in:
parent
b2b6a8cae3
commit
aeac202b32
4
pom.xml
4
pom.xml
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -19,46 +19,6 @@ public class Database {
|
||||
setStatement();
|
||||
}
|
||||
|
||||
private void setUrl(String sUrlVar) {
|
||||
sUrl = sUrlVar;
|
||||
}
|
||||
|
||||
private void setConnection() throws Exception {
|
||||
conn = DriverManager.getConnection("jdbc:sqlite:" + sUrl);
|
||||
}
|
||||
|
||||
|
||||
public Connection getConnection() {
|
||||
return conn;
|
||||
}
|
||||
|
||||
private void setStatement() throws Exception {
|
||||
if (conn == null) {
|
||||
setConnection();
|
||||
}
|
||||
statement = conn.createStatement();
|
||||
statement.setQueryTimeout(iTimeout); // set timeout to 30 sec.
|
||||
}
|
||||
|
||||
public PreparedStatement getPreparedStatement(String sql) throws SQLException {
|
||||
return conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||
}
|
||||
|
||||
public void executeUpdate(String instruction) throws SQLException {
|
||||
statement.executeUpdate(instruction);
|
||||
}
|
||||
|
||||
public ResultSet executeQuery(String instruction) throws SQLException {
|
||||
return statement.executeQuery(instruction);
|
||||
}
|
||||
|
||||
public void closeConnection() {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
public static String escapeString(String x, boolean escapeDoubleQuotes) {
|
||||
StringBuilder sBuilder = new StringBuilder(x.length() * 11 / 10);
|
||||
|
||||
@ -125,4 +85,43 @@ public class Database {
|
||||
|
||||
return sBuilder.toString();
|
||||
}
|
||||
|
||||
private void setUrl(String sUrlVar) {
|
||||
sUrl = sUrlVar;
|
||||
}
|
||||
|
||||
private void setConnection() throws Exception {
|
||||
conn = DriverManager.getConnection("jdbc:sqlite:" + sUrl);
|
||||
}
|
||||
|
||||
public Connection getConnection() {
|
||||
return conn;
|
||||
}
|
||||
|
||||
private void setStatement() throws Exception {
|
||||
if (conn == null) {
|
||||
setConnection();
|
||||
}
|
||||
statement = conn.createStatement();
|
||||
statement.setQueryTimeout(iTimeout); // set timeout to 30 sec.
|
||||
}
|
||||
|
||||
public PreparedStatement getPreparedStatement(String sql) throws SQLException {
|
||||
return conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||
}
|
||||
|
||||
public void executeUpdate(String instruction) throws SQLException {
|
||||
statement.executeUpdate(instruction);
|
||||
}
|
||||
|
||||
public ResultSet executeQuery(String instruction) throws SQLException {
|
||||
return statement.executeQuery(instruction);
|
||||
}
|
||||
|
||||
public void closeConnection() {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
}
|
@ -35,46 +35,6 @@ public class MassCommand {
|
||||
this(command, "", serverList, answer);
|
||||
}
|
||||
|
||||
public MassCommand setPrivateKey(String private_key) {
|
||||
this.private_key = private_key;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
t = new Thread(() -> {
|
||||
final boolean[] failed = {false};
|
||||
final int[] counter = {0};
|
||||
answer.onStart();
|
||||
serverList.forEach(server -> {
|
||||
if (stop) return;
|
||||
if (failed[0]) return;
|
||||
SSHManager manager = server.getSSHSession(private_key);
|
||||
if (manager.hasError()) {
|
||||
failed[0] = true;
|
||||
answer.onFail(manager.getErrorMessage());
|
||||
return;
|
||||
}
|
||||
if (failed[0]) return;
|
||||
|
||||
String result = manager.sendCommand(command);
|
||||
Logger.info(result);
|
||||
|
||||
manager.close();
|
||||
answer.onUpdate(counter[0] + 1, serverList.size());
|
||||
counter[0]++;
|
||||
});
|
||||
|
||||
if (!failed[0]) answer.onFinish();
|
||||
});
|
||||
|
||||
t.start();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
stop = true;
|
||||
t.interrupt();
|
||||
}
|
||||
|
||||
public static void run(String command, MultiWindowTextGUI gui, List<Server> serverarray, String ssh_private) {
|
||||
Label label = new Label("[ ] 00%");
|
||||
Button button = new Button("Abbruch");
|
||||
@ -125,4 +85,44 @@ public class MassCommand {
|
||||
button.addListener(button1 -> mc.stop());
|
||||
button.takeFocus();
|
||||
}
|
||||
|
||||
public MassCommand setPrivateKey(String private_key) {
|
||||
this.private_key = private_key;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
t = new Thread(() -> {
|
||||
final boolean[] failed = {false};
|
||||
final int[] counter = {0};
|
||||
answer.onStart();
|
||||
serverList.forEach(server -> {
|
||||
if (stop) return;
|
||||
if (failed[0]) return;
|
||||
SSHManager manager = server.getSSHSession(private_key);
|
||||
if (manager.hasError()) {
|
||||
failed[0] = true;
|
||||
answer.onFail(manager.getErrorMessage());
|
||||
return;
|
||||
}
|
||||
if (failed[0]) return;
|
||||
|
||||
String result = manager.sendCommand(command);
|
||||
Logger.info(result);
|
||||
|
||||
manager.close();
|
||||
answer.onUpdate(counter[0] + 1, serverList.size());
|
||||
counter[0]++;
|
||||
});
|
||||
|
||||
if (!failed[0]) answer.onFinish();
|
||||
});
|
||||
|
||||
t.start();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
stop = true;
|
||||
t.interrupt();
|
||||
}
|
||||
}
|
||||
|
@ -26,22 +26,6 @@ public class SSHManager {
|
||||
private String errorMessage = null;
|
||||
private int intTimeOut;
|
||||
|
||||
private void doCommonConstructorActions(String userName,
|
||||
String password, String connectionIP, String knownHostsFileName) {
|
||||
jschSSHChannel = new JSch();
|
||||
|
||||
try {
|
||||
jschSSHChannel.addIdentity(password);
|
||||
jschSSHChannel.setKnownHosts(knownHostsFileName);
|
||||
} catch (JSchException jschX) {
|
||||
logError(jschX.getMessage());
|
||||
}
|
||||
|
||||
strUserName = userName;
|
||||
strPassword = password;
|
||||
strConnectionIP = connectionIP;
|
||||
}
|
||||
|
||||
public SSHManager(String userName, String password,
|
||||
String connectionIP, String knownHostsFileName) {
|
||||
doCommonConstructorActions(userName, password,
|
||||
@ -66,6 +50,22 @@ public class SSHManager {
|
||||
intTimeOut = timeOutMilliseconds;
|
||||
}
|
||||
|
||||
private void doCommonConstructorActions(String userName,
|
||||
String password, String connectionIP, String knownHostsFileName) {
|
||||
jschSSHChannel = new JSch();
|
||||
|
||||
try {
|
||||
jschSSHChannel.addIdentity(password);
|
||||
jschSSHChannel.setKnownHosts(knownHostsFileName);
|
||||
} catch (JSchException jschX) {
|
||||
logError(jschX.getMessage());
|
||||
}
|
||||
|
||||
strUserName = userName;
|
||||
strPassword = password;
|
||||
strConnectionIP = connectionIP;
|
||||
}
|
||||
|
||||
public String connect() {
|
||||
errorMessage = null;
|
||||
|
||||
|
@ -34,6 +34,26 @@ public class Server {
|
||||
new ServerStatus(null, null, 0, 0, 0, 0, 0, State.OFFLINE, 0));
|
||||
}
|
||||
|
||||
public static HashMap<String, Server> getServerList(Database db) {
|
||||
HashMap<String, Server> serverlist = new HashMap<>();
|
||||
|
||||
try {
|
||||
ResultSet rs = db.executeQuery("SELECT * FROM server LEFT JOIN status ON server.id = status.server_id;");
|
||||
while (rs.next()) {
|
||||
ServerStatus status = new ServerStatus(rs.getString("os"),
|
||||
rs.getString("cpu"), rs.getInt("cpucount"), rs.getInt("max_ram"),
|
||||
rs.getInt("current_ram"), rs.getFloat("load"),
|
||||
rs.getLong("uptime"), State.getByName(rs.getString("state")), rs.getLong("lastpoll"));
|
||||
Server s = new Server(rs.getInt("id"), rs.getString("servername"), rs.getString("username"),
|
||||
rs.getString("ip"), rs.getInt("port"), rs.getLong("adddate"), status);
|
||||
serverlist.put(rs.getString("servername"), s);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
Logger.error(e);
|
||||
}
|
||||
return serverlist;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
@ -87,24 +107,4 @@ public class Server {
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static HashMap<String, Server> getServerList(Database db) {
|
||||
HashMap<String, Server> serverlist = new HashMap<>();
|
||||
|
||||
try {
|
||||
ResultSet rs = db.executeQuery("SELECT * FROM server LEFT JOIN status ON server.id = status.server_id;");
|
||||
while (rs.next()) {
|
||||
ServerStatus status = new ServerStatus(rs.getString("os"),
|
||||
rs.getString("cpu"), rs.getInt("cpucount"), rs.getInt("max_ram"),
|
||||
rs.getInt("current_ram"), rs.getFloat("load"),
|
||||
rs.getLong("uptime"), State.getByName(rs.getString("state")), rs.getLong("lastpoll"));
|
||||
Server s = new Server(rs.getInt("id"), rs.getString("servername"), rs.getString("username"),
|
||||
rs.getString("ip"), rs.getInt("port"), rs.getLong("adddate"), status);
|
||||
serverlist.put(rs.getString("servername"), s);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
Logger.error(e);
|
||||
}
|
||||
return serverlist;
|
||||
}
|
||||
}
|
||||
|
@ -32,10 +32,24 @@ public class ServerStatus {
|
||||
this.lastpoll = lastpoll;
|
||||
}
|
||||
|
||||
public static String getDate(long milliSeconds, String dateFormat) {
|
||||
// Create a DateFormatter object for displaying date in specified format.
|
||||
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
|
||||
|
||||
// Create a calendar object that will convert the date and time value in milliseconds to date.
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTimeInMillis(milliSeconds);
|
||||
return formatter.format(calendar.getTime());
|
||||
}
|
||||
|
||||
public String getOS() {
|
||||
return os;
|
||||
}
|
||||
|
||||
public void setOS(String os) {
|
||||
this.os = os;
|
||||
}
|
||||
|
||||
public String getCPUModel() {
|
||||
return cpu;
|
||||
}
|
||||
@ -44,66 +58,62 @@ public class ServerStatus {
|
||||
return cpucount;
|
||||
}
|
||||
|
||||
public int getMemTotal() {
|
||||
return memTotal;
|
||||
}
|
||||
|
||||
public int getMemFree() {
|
||||
return memFree;
|
||||
}
|
||||
|
||||
public float getLoad() {
|
||||
return load;
|
||||
}
|
||||
|
||||
public long getUptime() {
|
||||
return uptime;
|
||||
}
|
||||
|
||||
public State getState() {
|
||||
return state == null ? State.OFFLINE : state;
|
||||
}
|
||||
|
||||
public long getLastpoll() {
|
||||
return lastpoll;
|
||||
}
|
||||
|
||||
public void setOS(String os) {
|
||||
this.os = os;
|
||||
}
|
||||
|
||||
public void setCPU(String cpu) {
|
||||
this.cpu = cpu;
|
||||
}
|
||||
|
||||
public void setCPUcount(int cpucount) {
|
||||
this.cpucount = cpucount;
|
||||
}
|
||||
|
||||
public int getMemTotal() {
|
||||
return memTotal;
|
||||
}
|
||||
|
||||
public void setMemTotal(int memTotal) {
|
||||
this.memTotal = memTotal;
|
||||
}
|
||||
|
||||
public int getMemFree() {
|
||||
return memFree;
|
||||
}
|
||||
|
||||
public void setMemFree(int memFree) {
|
||||
this.memFree = memFree;
|
||||
}
|
||||
|
||||
public float getLoad() {
|
||||
return load;
|
||||
}
|
||||
|
||||
public void setLoad(float load) {
|
||||
this.load = load;
|
||||
}
|
||||
|
||||
public long getUptime() {
|
||||
return uptime;
|
||||
}
|
||||
|
||||
public void setUptime(long uptime) {
|
||||
this.uptime = uptime;
|
||||
}
|
||||
|
||||
public State getState() {
|
||||
return state == null ? State.OFFLINE : state;
|
||||
}
|
||||
|
||||
public void setState(State state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public long getLastpoll() {
|
||||
return lastpoll;
|
||||
}
|
||||
|
||||
public void setLastpoll(long lastpoll) {
|
||||
this.lastpoll = lastpoll;
|
||||
}
|
||||
|
||||
public void setCPU(String cpu) {
|
||||
this.cpu = cpu;
|
||||
}
|
||||
|
||||
public String getUptimeFormatted() {
|
||||
return DateFormatUtils.format(uptime * 1000, "HH:mm:ss");
|
||||
}
|
||||
@ -111,15 +121,4 @@ public class ServerStatus {
|
||||
public String getLastPollFormatted() {
|
||||
return getDate(lastpoll, "dd.MM.yyyy HH:MM:ss");
|
||||
}
|
||||
|
||||
|
||||
public static String getDate(long milliSeconds, String dateFormat) {
|
||||
// Create a DateFormatter object for displaying date in specified format.
|
||||
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
|
||||
|
||||
// Create a calendar object that will convert the date and time value in milliseconds to date.
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTimeInMillis(milliSeconds);
|
||||
return formatter.format(calendar.getTime());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user