This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-daemon.git
The following commit(s) were added to refs/heads/master by this push: new c2901de Fix odd formatting new 0f24632 Merge branch 'master' of https://gitbox.apache.org/repos/asf/commons-daemon.git c2901de is described below commit c2901de575194855609ff4d41521243b16cdcf75 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Fri Jun 2 14:29:09 2023 -0400 Fix odd formatting --- .../org/apache/commons/daemon/SimpleDaemon.java | 226 ++++++++++----------- 1 file changed, 107 insertions(+), 119 deletions(-) diff --git a/src/test/java/org/apache/commons/daemon/SimpleDaemon.java b/src/test/java/org/apache/commons/daemon/SimpleDaemon.java index 411bcda..0740a1d 100644 --- a/src/test/java/org/apache/commons/daemon/SimpleDaemon.java +++ b/src/test/java/org/apache/commons/daemon/SimpleDaemon.java @@ -35,46 +35,42 @@ public class SimpleDaemon implements Daemon, Runnable, DaemonUserSignal { private boolean softReloadSignalled; public SimpleDaemon() { - System.err.println("SimpleDaemon: instance "+this.hashCode()+ - " created"); + System.err.println("SimpleDaemon: instance " + this.hashCode() + " created"); this.handlers = new Vector<>(); } @Override protected void finalize() { - System.err.println("SimpleDaemon: instance "+this.hashCode()+ - " garbage collected"); + System.err.println("SimpleDaemon: instance " + this.hashCode() + " garbage collected"); } /** * init and destroy were added in jakarta-tomcat-daemon. */ @Override - public void init(final DaemonContext context) - throws Exception { - System.err.println("SimpleDaemon: instance "+this.hashCode()+ - " init"); + public void init(final DaemonContext context) throws Exception { + System.err.println("SimpleDaemon: instance " + this.hashCode() + " init"); - int port=1200; + int port = 1200; final String[] a = context.getArguments(); - if (a.length>0) { - port=Integer.parseInt(a[0]); + if (a.length > 0) { + port = Integer.parseInt(a[0]); } - if (a.length>1) { - this.directory=a[1]; + if (a.length > 1) { + this.directory = a[1]; } else { - this.directory="/tmp"; + this.directory = "/tmp"; } /* Dump a message */ - System.err.println("SimpleDaemon: loading on port "+port); + System.err.println("SimpleDaemon: loading on port " + port); /* Set up this simple daemon */ - this.controller=context.getController(); - this.server=new ServerSocket(port); - this.thread=new Thread(this); + this.controller = context.getController(); + this.server = new ServerSocket(port); + this.thread = new Thread(this); } @Override @@ -87,13 +83,12 @@ public class SimpleDaemon implements Daemon, Runnable, DaemonUserSignal { } @Override - public void stop() - throws IOException, InterruptedException { + public void stop() throws IOException, InterruptedException { /* Dump a message */ System.err.println("SimpleDaemon: stopping"); /* Close the ServerSocket. This will make our thread to terminate */ - this.stopping=true; + this.stopping = true; this.server.close(); /* Wait for the main thread to exit and dump a message */ @@ -103,40 +98,39 @@ public class SimpleDaemon implements Daemon, Runnable, DaemonUserSignal { @Override public void destroy() { - System.err.println("SimpleDaemon: instance "+this.hashCode()+ - " destroy"); + System.err.println("SimpleDaemon: instance " + this.hashCode() + " destroy"); } @Override public void run() { - int number=0; + int number = 0; System.err.println("SimpleDaemon: started acceptor loop"); try { - while(!this.stopping) { + while (!this.stopping) { checkForReload(); - final Socket socket=this.server.accept(); + final Socket socket = this.server.accept(); checkForReload(); - final Handler handler=new Handler(socket,this,this.controller); + final Handler handler = new Handler(socket, this, this.controller); handler.setConnectionNumber(number++); handler.setDirectoryName(this.directory); new Thread(handler).start(); } } catch (final IOException e) { - /* Don't dump any error message if we are stopping. A IOException - is generated when the ServerSocket is closed in stop() */ + /* + * Don't dump any error message if we are stopping. A IOException is generated when the ServerSocket is closed in stop() + */ if (!this.stopping) { e.printStackTrace(System.err); } } /* Terminate all handlers that at this point are still open */ - final Enumeration<Handler> openhandlers=this.handlers.elements(); + final Enumeration<Handler> openhandlers = this.handlers.elements(); while (openhandlers.hasMoreElements()) { - final Handler handler=openhandlers.nextElement(); - System.err.println("SimpleDaemon: dropping connection "+ - handler.getConnectionNumber()); + final Handler handler = openhandlers.nextElement(); + System.err.println("SimpleDaemon: dropping connection " + handler.getConnectionNumber()); handler.close(); } @@ -145,8 +139,8 @@ public class SimpleDaemon implements Daemon, Runnable, DaemonUserSignal { @Override public void signal() { - /* In this example we are using soft reload on - * custom signal. + /* + * In this example we are using soft reload on custom signal. */ this.softReloadSignalled = true; } @@ -183,26 +177,24 @@ public class SimpleDaemon implements Daemon, Runnable, DaemonUserSignal { private int number; public Handler(final Socket s, final SimpleDaemon p, final DaemonController c) { - this.socket=s; - this.parent=p; - this.controller=c; + this.socket = s; + this.parent = p; + this.controller = c; } @Override public void run() { this.parent.addHandler(this); - System.err.println("SimpleDaemon: connection "+this.number+ - " opened from "+this.socket.getInetAddress()); + System.err.println("SimpleDaemon: connection " + this.number + " opened from " + this.socket.getInetAddress()); try { - final InputStream in=this.socket.getInputStream(); - final OutputStream out=this.socket.getOutputStream(); - handle(in,out); + final InputStream in = this.socket.getInputStream(); + final OutputStream out = this.socket.getOutputStream(); + handle(in, out); this.socket.close(); } catch (final IOException e) { e.printStackTrace(System.err); } - System.err.println("SimpleDaemon: connection "+this.number+ - " closed"); + System.err.println("SimpleDaemon: connection " + this.number + " closed"); this.parent.removeHandler(this); } @@ -215,7 +207,7 @@ public class SimpleDaemon implements Daemon, Runnable, DaemonUserSignal { } public void setConnectionNumber(final int number) { - this.number=number; + this.number = number; } public int getConnectionNumber() { @@ -223,7 +215,7 @@ public class SimpleDaemon implements Daemon, Runnable, DaemonUserSignal { } public void setDirectoryName(final String directory) { - this.directory=directory; + this.directory = directory; } public String getDirectoryName() { @@ -231,20 +223,21 @@ public class SimpleDaemon implements Daemon, Runnable, DaemonUserSignal { } public void log(final String name) throws IOException { - try (final OutputStream file = new FileOutputStream(name, true); - final PrintStream out = new PrintStream(file)) { + try (final OutputStream file = new FileOutputStream(name, true); + final PrintStream out = new PrintStream(file)) { out.println(new SimpleDateFormat().format(new Date())); } } public void handle(final InputStream in, final OutputStream os) { - final PrintStream out=new PrintStream(os); + final PrintStream out = new PrintStream(os); - while(true) { + while (true) { try { - /* If we don't have data in the System InputStream, we want - to ask to the user for an option. */ - if (in.available()==0) { + /* + * If we don't have data in the System InputStream, we want to ask to the user for an option. + */ + if (in.available() == 0) { out.println(); out.println("Please select one of the following:"); out.println(" 1) Shutdown"); @@ -256,77 +249,72 @@ public class SimpleDaemon implements Daemon, Runnable, DaemonUserSignal { } /* Read an option from the client */ - final int x=in.read(); + final int x = in.read(); switch (x) { - /* If the socket was closed, we simply return */ - case -1: - return; - - /* Attempt to shut down */ - case '1': - out.println("Attempting a shutdown..."); - try { - this.controller.shutdown(); - } catch (final IllegalStateException e) { - out.println(); - out.println("Can't shutdown now"); - e.printStackTrace(out); - } - break; - - /* Attempt to reload */ - case '2': - out.println("Attempting a reload..."); - try { - this.controller.reload(); - } catch (final IllegalStateException e) { - out.println(); - out.println("Can't reload now"); - e.printStackTrace(out); - } - break; - - /* Disconnect */ - case '3': - final String name=this.getDirectoryName()+ - "/SimpleDaemon."+ - this.getConnectionNumber()+ - ".tmp"; - try { - this.log(name); - out.println("File '"+name+"' created"); - } catch (final IOException e) { - e.printStackTrace(out); - } - break; - - /* Disconnect */ - case '4': - out.println("Disconnecting..."); - return; - case '5': - out.println("Reloading configuration..."); - this.parent.signal(); - return; - - /* Discard any carriage return / newline characters */ - case '\r': - case '\n': - break; - - /* We got something that we weren't supposed to get */ - default: - out.println("Unknown option '"+(char)x+"'"); - break; + /* If the socket was closed, we simply return */ + case -1: + return; + + /* Attempt to shut down */ + case '1': + out.println("Attempting a shutdown..."); + try { + this.controller.shutdown(); + } catch (final IllegalStateException e) { + out.println(); + out.println("Can't shutdown now"); + e.printStackTrace(out); + } + break; + + /* Attempt to reload */ + case '2': + out.println("Attempting a reload..."); + try { + this.controller.reload(); + } catch (final IllegalStateException e) { + out.println(); + out.println("Can't reload now"); + e.printStackTrace(out); + } + break; + + /* Disconnect */ + case '3': + final String name = this.getDirectoryName() + "/SimpleDaemon." + this.getConnectionNumber() + ".tmp"; + try { + this.log(name); + out.println("File '" + name + "' created"); + } catch (final IOException e) { + e.printStackTrace(out); + } + break; + + /* Disconnect */ + case '4': + out.println("Disconnecting..."); + return; + case '5': + out.println("Reloading configuration..."); + this.parent.signal(); + return; + + /* Discard any carriage return / newline characters */ + case '\r': + case '\n': + break; + + /* We got something that we weren't supposed to get */ + default: + out.println("Unknown option '" + (char) x + "'"); + break; } - /* If we get an IOException we return (disconnect) */ + /* If we get an IOException we return (disconnect) */ } catch (final IOException e) { - System.err.println("SimpleDaemon: IOException in "+ - "connection "+ - this.getConnectionNumber()); + System.err.println("SimpleDaemon: IOException in " + "connection " + this.getConnectionNumber()); return; } }