refactor and reformate code

This commit is contained in:
Niklas 2017-10-24 23:09:01 +02:00
parent b2b6a8cae3
commit aeac202b32
6 changed files with 159 additions and 161 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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"> 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> <modelVersion>4.0.0</modelVersion>

View File

@ -19,46 +19,6 @@ public class Database {
setStatement(); 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) { public static String escapeString(String x, boolean escapeDoubleQuotes) {
StringBuilder sBuilder = new StringBuilder(x.length() * 11 / 10); StringBuilder sBuilder = new StringBuilder(x.length() * 11 / 10);
@ -125,4 +85,43 @@ public class Database {
return sBuilder.toString(); 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) {
}
}
} }

View File

@ -35,46 +35,6 @@ public class MassCommand {
this(command, "", serverList, answer); 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) { public static void run(String command, MultiWindowTextGUI gui, List<Server> serverarray, String ssh_private) {
Label label = new Label("[ ] 00%"); Label label = new Label("[ ] 00%");
Button button = new Button("Abbruch"); Button button = new Button("Abbruch");
@ -125,4 +85,44 @@ public class MassCommand {
button.addListener(button1 -> mc.stop()); button.addListener(button1 -> mc.stop());
button.takeFocus(); 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();
}
} }

View File

@ -26,22 +26,6 @@ public class SSHManager {
private String errorMessage = null; private String errorMessage = null;
private int intTimeOut; 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, public SSHManager(String userName, String password,
String connectionIP, String knownHostsFileName) { String connectionIP, String knownHostsFileName) {
doCommonConstructorActions(userName, password, doCommonConstructorActions(userName, password,
@ -66,6 +50,22 @@ public class SSHManager {
intTimeOut = timeOutMilliseconds; 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() { public String connect() {
errorMessage = null; errorMessage = null;

View File

@ -34,6 +34,26 @@ public class Server {
new ServerStatus(null, null, 0, 0, 0, 0, 0, State.OFFLINE, 0)); 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() { public int getId() {
return id; return id;
} }
@ -87,24 +107,4 @@ public class Server {
} }
return instance; 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;
}
} }

View File

@ -32,10 +32,24 @@ public class ServerStatus {
this.lastpoll = lastpoll; 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() { public String getOS() {
return os; return os;
} }
public void setOS(String os) {
this.os = os;
}
public String getCPUModel() { public String getCPUModel() {
return cpu; return cpu;
} }
@ -44,66 +58,62 @@ public class ServerStatus {
return cpucount; 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) { public void setCPUcount(int cpucount) {
this.cpucount = cpucount; this.cpucount = cpucount;
} }
public int getMemTotal() {
return memTotal;
}
public void setMemTotal(int memTotal) { public void setMemTotal(int memTotal) {
this.memTotal = memTotal; this.memTotal = memTotal;
} }
public int getMemFree() {
return memFree;
}
public void setMemFree(int memFree) { public void setMemFree(int memFree) {
this.memFree = memFree; this.memFree = memFree;
} }
public float getLoad() {
return load;
}
public void setLoad(float load) { public void setLoad(float load) {
this.load = load; this.load = load;
} }
public long getUptime() {
return uptime;
}
public void setUptime(long uptime) { public void setUptime(long uptime) {
this.uptime = uptime; this.uptime = uptime;
} }
public State getState() {
return state == null ? State.OFFLINE : state;
}
public void setState(State state) { public void setState(State state) {
this.state = state; this.state = state;
} }
public long getLastpoll() {
return lastpoll;
}
public void setLastpoll(long lastpoll) { public void setLastpoll(long lastpoll) {
this.lastpoll = lastpoll; this.lastpoll = lastpoll;
} }
public void setCPU(String cpu) {
this.cpu = cpu;
}
public String getUptimeFormatted() { public String getUptimeFormatted() {
return DateFormatUtils.format(uptime * 1000, "HH:mm:ss"); return DateFormatUtils.format(uptime * 1000, "HH:mm:ss");
} }
@ -111,15 +121,4 @@ public class ServerStatus {
public String getLastPollFormatted() { public String getLastPollFormatted() {
return getDate(lastpoll, "dd.MM.yyyy HH:MM:ss"); 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());
}
} }