From 2ca2d93cdb3c50142e3964a307ce01a0579cc7a8 Mon Sep 17 00:00:00 2001 From: Sergio Brighenti Date: Mon, 26 Sep 2016 17:25:07 +0200 Subject: [PATCH] enhanced console added possibility to disable or enable socket upload and webserver --- .../it/ksuploader/main/KSUploaderServer.java | 125 ++++++++++++------ .../main/sockets/SocketListener.java | 57 +++++--- .../{UndertowServer.java => WebListener.java} | 22 +-- .../it/ksuploader/utils/Configuration.java | 32 ++++- 4 files changed, 166 insertions(+), 70 deletions(-) rename src/main/java/it/ksuploader/main/web/{UndertowServer.java => WebListener.java} (81%) diff --git a/src/main/java/it/ksuploader/main/KSUploaderServer.java b/src/main/java/it/ksuploader/main/KSUploaderServer.java index 6f43be9..c074689 100644 --- a/src/main/java/it/ksuploader/main/KSUploaderServer.java +++ b/src/main/java/it/ksuploader/main/KSUploaderServer.java @@ -2,13 +2,12 @@ import it.ksuploader.main.sockets.SocketListener; -import it.ksuploader.main.web.UndertowServer; +import it.ksuploader.main.web.WebListener; import it.ksuploader.utils.Configuration; import org.apache.log4j.*; import java.io.BufferedReader; import java.io.File; -import java.io.IOException; import java.io.InputStreamReader; import java.text.MessageFormat; import java.text.SimpleDateFormat; @@ -20,7 +19,7 @@ public class KSUploaderServer { public static Logger logger = Logger.getLogger("KSULogger"); public static SocketListener socketListener; - public static UndertowServer webServer; + public static WebListener webServer; public static void main(String[] args) { @@ -42,22 +41,19 @@ public static void main(String[] args) { } System.out.println("--------------------------------------------------------------------------------"); - // listen connections - try { - socketListener = new SocketListener(); - socketListener.start(); - - webServer = new UndertowServer(); - webServer.start(); - } catch (IOException e) { - logger.log(Level.FATAL, "Error setting up the listener", e); - System.exit(2); + socketListener = new SocketListener(); + webServer = new WebListener(); + + if (config.isSocketEnabled()) { + socketListener.startListen(); } - // open console - console(); + if (config.isWebserverEnabled()) { + webServer.startListen(); + } - System.exit(0); + // open consoleLoop + consoleLoop(); } private static void bootstrap(String[] args) throws Exception { @@ -65,14 +61,15 @@ private static void bootstrap(String[] args) throws Exception { if (args.length != 0) { if (args.length == 6) { KSUploaderServer.config.setPort(args[0]); - KSUploaderServer.config.setPass(args[1]); - KSUploaderServer.config.setFolder(args[2]); - KSUploaderServer.config.setFolderSize(args[3]); - KSUploaderServer.config.setMaxFileSize(args[4]); - KSUploaderServer.config.setWeb_url(args[5]); + KSUploaderServer.config.setPort(args[2]); + KSUploaderServer.config.setPass(args[3]); + KSUploaderServer.config.setFolder(args[4]); + KSUploaderServer.config.setFolderSize(args[5]); + KSUploaderServer.config.setMaxFileSize(args[6]); + KSUploaderServer.config.setWeb_url(args[7]); logger.log(Level.INFO, "Configuration created!"); } else { - throw new IllegalArgumentException("Correct args are: port, password, folder, folder size, max file size, web url"); + throw new IllegalArgumentException("Correct args are: port, web_port, password, folder, folder size, max file size, web url"); } } else { if ( @@ -84,6 +81,7 @@ private static void bootstrap(String[] args) throws Exception { } else { System.out.println("Password: " + KSUploaderServer.config.getPass()); System.out.println("Port: " + KSUploaderServer.config.getPort()); + System.out.println("Web Port: " + KSUploaderServer.config.getWebPort()); System.out.println("Folder: " + KSUploaderServer.config.getFolder()); System.out.println("Folder Max size: " + KSUploaderServer.config.getFolderSize()); System.out.println("File Max size: " + KSUploaderServer.config.getMaxFileSize()); @@ -111,38 +109,87 @@ public static void setupLogger() { Logger.getRootLogger().addAppender(logFile); } - private static void console() { - String cmd = ""; + private static void consoleLoop() { + String[] cmd; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); - while (!cmd.equals("shutdown")) { + while (true) { try { - cmd = in.readLine(); - switch (cmd.split(" ")[0]) { - case "change-password": - if (config.setPass(cmd.split(" ")[1])) { - System.out.println("Password changed."); - } else { - System.out.println("Error."); + cmd = in.readLine().split(" "); + switch (cmd[0]) { + case "password": + switch (cmd[1]) { + case "get": + System.out.println(config.getPass()); + break; + case "change": + if (cmd.length == 3 && config.setPass(cmd[2])) { + System.out.println("Password changed."); + } else { + System.out.println("Error. Usage: 'password change pas'"); + } + break; + } + break; + case "socket": + switch (cmd[1]) { + case "stop": + if (socketListener.isListening()) { + socketListener.stopListen(); + } + break; + case "start": + if (socketListener.isListening()) { + socketListener.stopListen(); + } + socketListener = new SocketListener(); + socketListener.startListen(); + break; + case "status": + System.out.println("Socket listening: " + socketListener.isListening()); + break; } break; - case "get-password": - System.out.println(config.getPass()); + case "web": + switch (cmd[1]) { + case "start": + if (webServer.isListening()) { + webServer.stopListen(); + } + webServer.startListen(); + break; + case "stop": + if (webServer.isListening()) { + webServer.stopListen(); + } + break; + case "status": + System.out.println("Webserver listening: " + webServer.isListening()); + break; + } break; case "help": case "?": - System.out.println("change-password - Change the server password"); - System.out.println("get-password - Print current password"); + System.out.println("password [get|change] - Print or change current password."); + System.out.println("socket [start|stop|status] - SocketListener operations."); + System.out.println("web [start|stop|status] - Webserver operations."); System.out.println("shutdown - Stop the server"); break; case "shutdown": - System.out.println("Shutting down..."); + if (socketListener.isListening()) { + socketListener.stopListen(); + } + if (webServer.isListening()) { + webServer.stopListen(); + } + System.out.println("Bye!"); + System.exit(0); break; default: System.out.println("Invalid command."); break; } - } catch (IOException e) { - logger.log(Level.WARN, "Invalid command."); + } catch (Exception e) { + logger.log(Level.WARN, "Command error: Invalid.", e); } } } diff --git a/src/main/java/it/ksuploader/main/sockets/SocketListener.java b/src/main/java/it/ksuploader/main/sockets/SocketListener.java index 59aa08b..0172985 100644 --- a/src/main/java/it/ksuploader/main/sockets/SocketListener.java +++ b/src/main/java/it/ksuploader/main/sockets/SocketListener.java @@ -4,47 +4,64 @@ import org.apache.log4j.Level; import org.apache.log4j.Logger; -import java.io.File; import java.io.IOException; import java.net.InetSocketAddress; +import java.nio.channels.ClosedChannelException; import java.nio.channels.ServerSocketChannel; public class SocketListener extends Thread { - private boolean listen; + + private boolean listening; private Logger logger = Logger.getLogger(this.getClass()); private ServerSocketChannel serverSocketChannel; - - public SocketListener() throws IOException { - - File uploadFolder = new File(KSUploaderServer.config.getFolder()); - - if (!uploadFolder.exists()) { - uploadFolder.mkdirs(); - } - this.listen = true; - this.buildSocket(); + + public SocketListener() { } - + private void buildSocket() throws IOException { try { this.serverSocketChannel = ServerSocketChannel.open(); this.serverSocketChannel.socket().bind(new InetSocketAddress(KSUploaderServer.config.getPort())); - + this.logger.log(Level.INFO, "Socket listening on port " + KSUploaderServer.config.getPort() + "."); - + } catch (IOException exc) { throw new IOException("Can't init the listening socket!"); } } - - public void setListen(boolean listen) { - this.listen = listen; + + public void stopListen() { + try { + this.serverSocketChannel.close(); + } catch (IOException e) { + this.logger.log(Level.WARN, "Cannot closing socket.", e); + } + this.interrupt(); + this.listening = false; } - + + public void startListen() { + try { + this.buildSocket(); + } catch (IOException e) { + this.logger.log(Level.ERROR, "Cannot build socket.", e); + return; + } + this.start(); + this.listening = true; + } + + public boolean isListening() { + return this.listening; + } + public void run() { - while (this.listen) { + while (true) { try { new SocketRequestHandler(this.serverSocketChannel.accept()).start(); + } catch (ClosedChannelException e) { + this.logger.log(Level.WARN, "Stopping listening..."); + break; } catch (IOException e) { this.logger.log(Level.ERROR, "Error during handling request", e); } diff --git a/src/main/java/it/ksuploader/main/web/UndertowServer.java b/src/main/java/it/ksuploader/main/web/WebListener.java similarity index 81% rename from src/main/java/it/ksuploader/main/web/UndertowServer.java rename to src/main/java/it/ksuploader/main/web/WebListener.java index 04a88d0..4c89e34 100644 --- a/src/main/java/it/ksuploader/main/web/UndertowServer.java +++ b/src/main/java/it/ksuploader/main/web/WebListener.java @@ -22,16 +22,17 @@ /** * Created by Sergio on 25/09/2016. */ -public class UndertowServer { +public class WebListener { private Logger logger = Logger.getLogger(this.getClass()); + private boolean listening; private Undertow server; - public UndertowServer() { + public WebListener() { try { DeploymentInfo servletBuilder = Servlets.deployment() - .setClassLoader(UndertowServer.class.getClassLoader()) + .setClassLoader(WebListener.class.getClassLoader()) .setDeploymentName("HttpRequestHandler") .setContextPath("/") .addServlets( @@ -60,22 +61,27 @@ public UndertowServer() { } } - public void start() { + public void startListen() { try { this.server.start(); logger.log(Level.INFO, "Webserver listening on port " + config.getWebPort() + "."); + this.listening = true; } catch (Exception e) { - logger.log(Level.ERROR, "Cannot start webserver."); + logger.log(Level.ERROR, "Cannot start webserver.", e); } } - public void stop() { + public void stopListen() { try { this.server.stop(); - logger.log(Level.INFO, "Webserver listening on port " + config.getWebPort() + "."); + this.logger.log(Level.WARN, "Stopping listening..."); + this.listening = false; } catch (Exception e) { - logger.log(Level.ERROR, "Cannot start webserver."); + logger.log(Level.ERROR, "Cannot stop webserver.", e); } } + public boolean isListening() { + return this.listening; + } } diff --git a/src/main/java/it/ksuploader/utils/Configuration.java b/src/main/java/it/ksuploader/utils/Configuration.java index 6429e32..d649c44 100644 --- a/src/main/java/it/ksuploader/utils/Configuration.java +++ b/src/main/java/it/ksuploader/utils/Configuration.java @@ -17,6 +17,8 @@ public class Configuration extends Properties { private long maxFileSize; private int port; private int web_port; + private boolean socketEnabled; + private boolean webserverEnabled; public Configuration() { InputStream inputStream; @@ -40,7 +42,7 @@ public Configuration() { } this.folder = this.getProperty("folder"); - // UndertowServer url + // web url if (this.getProperty("web_url") == null || this.getProperty("web_url").isEmpty()) { this.setProperty("web_url", "http://domain.com/"); correct_config = true; @@ -87,7 +89,23 @@ public Configuration() { logger.log(Level.INFO, "[Configuration] Setting default max_file_size(MB)"); } this.maxFileSize = Long.parseLong(this.getProperty("max_file_size(MB)")) * 1048576; - + + // socket enabled + if (this.getProperty("socket_enabled") == null || this.getProperty("socket_enabled").isEmpty()) { + this.setProperty("socket_enabled", "true"); + correct_config = true; + System.out.println("[LoadConfig] Setting default socket_enabled"); + } + this.socketEnabled = Boolean.valueOf(this.getProperty("socket_enabled")); + + // webserver enabled + if (this.getProperty("webserver_enabled") == null || this.getProperty("webserver_enabled").isEmpty()) { + this.setProperty("webserver_enabled", "true"); + correct_config = true; + System.out.println("[LoadConfig] Setting default webserver_enabled"); + } + this.webserverEnabled = Boolean.valueOf(this.getProperty("webserver_enabled")); + if (correct_config) this.store(new FileOutputStream("server.properties"), null); @@ -123,7 +141,15 @@ public long getFolderSize() { public long getMaxFileSize() { return maxFileSize; } - + + public boolean isSocketEnabled() { + return socketEnabled; + } + + public boolean isWebserverEnabled() { + return webserverEnabled; + } + private boolean save() { try { this.store(new FileOutputStream("server.properties"), null);