geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r410770 - in /geronimo/sandbox/gshell/trunk: gshell-cli/src/main/java/org/apache/geronimo/gshell/cli/ gshell-commands/standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/ gshell-core/src/main/java/org/apache/geronim...
Date Thu, 01 Jun 2006 05:53:43 GMT
Author: jdillon
Date: Wed May 31 22:53:41 2006
New Revision: 410770

URL: http://svn.apache.org/viewvc?rev=410770&view=rev
Log:
Use ExitNotification to signal the shell to exit instead of System.exit()
Generalized the socket server daemon

Added:
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExitNotification.java
  (with props)
    geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/SocketServerDaemon.java
  (contents, props changed)
      - copied, changed from r410734, geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/GShellDaemon.java
Removed:
    geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/GShellDaemon.java
Modified:
    geronimo/sandbox/gshell/trunk/gshell-cli/src/main/java/org/apache/geronimo/gshell/cli/Main.java
    geronimo/sandbox/gshell/trunk/gshell-commands/standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/ExitCommand.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/GShell.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/InteractiveGShell.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/util/Arguments.java
    geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/GShellServer.java
    geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/ServerCommand.java

Modified: geronimo/sandbox/gshell/trunk/gshell-cli/src/main/java/org/apache/geronimo/gshell/cli/Main.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-cli/src/main/java/org/apache/geronimo/gshell/cli/Main.java?rev=410770&r1=410769&r2=410770&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-cli/src/main/java/org/apache/geronimo/gshell/cli/Main.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-cli/src/main/java/org/apache/geronimo/gshell/cli/Main.java
Wed May 31 22:53:41 2006
@@ -74,15 +74,15 @@
 
     private void setPropertyFrom(final String namevalue) {
         String name, value;
-        int j = namevalue.indexOf("=");
+        int i = namevalue.indexOf("=");
 
-        if (j == -1) {
+        if (i == -1) {
             name = namevalue;
-            value = "true";
+            value = Boolean.TRUE.toString();
         }
         else {
-            name = namevalue.substring(0, j);
-            value = namevalue.substring(j + 1, namevalue.length());
+            name = namevalue.substring(0, i);
+            value = namevalue.substring(i + 1, namevalue.length());
         }
         name = name.trim();
 
@@ -138,7 +138,7 @@
             formatter.printHelp(
                 io.out,
                 80, // width
-                "gshell [options] <command> [args]",
+                System.getProperty("program.name", "gshell") + " [options] <command>
[args]",
                 "",
                 options,
                 4, // left pad
@@ -207,12 +207,6 @@
         // TEMP: Log some info about the terminal
         //
 
-        if (debug) {
-            log.debug("Using STDIN: " + System.in);
-            log.debug("Using STDOUT: " + System.out);
-            log.debug("Using STDERR: " + System.err);
-        }
-
         Terminal term = Terminal.getTerminal();
 
         if (debug) {
@@ -228,6 +222,8 @@
             log.debug("Started in " + watch);
         }
 
+        int status = 0;
+
         if (interactive) {
             InteractiveGShell interp = new InteractiveGShell(io, gshell);
 
@@ -239,19 +235,19 @@
             interp.run();
         }
         else {
-            int status = gshell.execute(args);
-
-            if (debug) {
-                log.debug("Ran for " + watch);
-            }
-
-            System.exit(status);
+            status = gshell.execute(args);
         }
 
         if (debug) {
             log.debug("Ran for " + watch);
         }
+
+        System.exit(status);
     }
+
+    //
+    // Bootstrap
+    //
 
     public static void main(final String[] args, final ClassWorld world) throws Exception
{
         assert args != null;

Modified: geronimo/sandbox/gshell/trunk/gshell-commands/standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/ExitCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-commands/standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/ExitCommand.java?rev=410770&r1=410769&r2=410770&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-commands/standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/ExitCommand.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-commands/standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/ExitCommand.java
Wed May 31 22:53:41 2006
@@ -25,9 +25,11 @@
 import org.apache.geronimo.gshell.command.Command;
 import org.apache.geronimo.gshell.command.CommandSupport;
 import org.apache.geronimo.gshell.console.IO;
+import org.apache.geronimo.gshell.ExitNotification;
+import org.apache.geronimo.gshell.util.Arguments;
 
 /**
- * Exit the virtual machine.
+ * Exit the current shell.
  *
  * @version $Id$
  */
@@ -53,23 +55,18 @@
             .withDescription("Display this help message")
             .create('h'));
 
-        options.addOption(OptionBuilder.withLongOpt("code")
-            .withDescription("Use the given exit code")
-            .hasArg()
-            .create('c'));
-
         CommandLineParser parser = new PosixParser();
         CommandLine line = parser.parse(options, args);
 
         if (line.hasOption('h')) {
-            io.out.println(getName() + " -- exit the virtual machine");
+            io.out.println(getName() + " -- exit the current shell");
             io.out.println();
 
             HelpFormatter formatter = new HelpFormatter();
             formatter.printHelp(
                 io.out,
                 80, // width (FIXME: Should pull from gshell.columns variable)
-                getName() + " [options]",
+                getName() + " [options] [code]",
                 "",
                 options,
                 4, // left pad
@@ -82,14 +79,7 @@
             return Command.SUCCESS;
         }
 
-        // Get the status code to exit with
-        int exitCode = 0;
-        if (line.hasOption('c')) {
-            String value = line.getOptionValue('c');
-            exitCode = Integer.parseInt(value);
-        }
-
-        exit(exitCode);
+        exit(line.getArgs());
 
         // Should never get this far
         assert false;
@@ -97,14 +87,26 @@
         return Command.FAILURE;
     }
 
+    private void exit(final String[] args) {
+        int exitCode = 0;
+
+        if (args.length > 1) {
+            getIO().err.println("Unexpected arguments: " + Arguments.asString(args));
+        }
+        if (args.length == 1) {
+            exitCode = Integer.parseInt(args[0]);
+        }
+
+        exit(exitCode);
+    }
+
     private void exit(final int exitCode) {
+        log.info("Exiting w/code: " + exitCode);
+
         //
-        // TODO: Need to implement this with an exception notification
-        //       To allow sub-shell or sever shells to function w/o
-        //       killing the entire JVM :-(
+        // DO NOT Call System.exit() !!!
         //
 
-        log.info("Exiting w/code: " + exitCode);
-        System.exit(exitCode);
+        throw new ExitNotification(exitCode);
     }
 }

Added: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExitNotification.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExitNotification.java?rev=410770&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExitNotification.java
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExitNotification.java
Wed May 31 22:53:41 2006
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.geronimo.gshell;
+
+import org.apache.geronimo.gshell.console.InteractiveConsole;
+import org.apache.geronimo.gshell.console.JLineConsole;
+import org.apache.geronimo.gshell.console.IO;
+
+import java.io.IOException;
+
+/**
+ * Thrown to indicate that the current shell should exit.
+ *
+ * <p>
+ * Commands should use this instead of {@link System#exit}.
+ *
+ * @version $Id$
+ */
+public class ExitNotification
+    extends Error
+{
+    private final int code;
+
+    public ExitNotification(final int code) {
+        this.code = code;
+    }
+
+    public ExitNotification() {
+        this(0);
+    }
+
+    public int getCode() {
+        return code;
+    }
+}

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExitNotification.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExitNotification.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExitNotification.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/GShell.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/GShell.java?rev=410770&r1=410769&r2=410770&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/GShell.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/GShell.java
Wed May 31 22:53:41 2006
@@ -26,6 +26,7 @@
 import org.apache.geronimo.gshell.command.CommandContext;
 import org.apache.geronimo.gshell.command.Variables;
 import org.apache.geronimo.gshell.command.VariablesMap;
+import org.apache.geronimo.gshell.command.CommandException;
 import org.apache.geronimo.gshell.commandline.CommandLineBuilder;
 import org.apache.geronimo.gshell.commandline.CommandLine;
 import org.apache.geronimo.gshell.util.Arguments;
@@ -42,15 +43,23 @@
 
     private final IO io;
 
-    public GShell(final IO io) {
+    private final CommandManager commandManager;
+
+    public GShell(final IO io) throws CommandException {
         if (io == null) {
             throw new IllegalArgumentException("IO is null");
         }
 
         this.io = io;
+        
+        //
+        // HACK: DI CommandManager...  Maybe need to setup the top-level container here
+        //
+
+        this.commandManager = new CommandManager();
     }
     
-    public GShell() {
+    public GShell() throws CommandException {
         this(new IO());
     }
 
@@ -81,17 +90,13 @@
 
         boolean debug = log.isDebugEnabled();
 
-        log.info("Executing (" + commandName + "): " + java.util.Arrays.asList(args));
+        log.info("Executing (" + commandName + "): " + Arguments.asString(args));
 
         //
         // HACK: Just get something working right now
         //
 
-        //
-        // HACK: DI CommandManager...
-        //
-
-        Command cmd = new CommandManager().getCommand(commandName);
+        Command cmd = commandManager.getCommand(commandName);
 
         cmd.init(new CommandContext() {
             Variables vars = new VariablesMap();
@@ -132,7 +137,7 @@
         assert args != null;
         assert args.length > 1;
 
-        log.info("Executing (String[]): " + java.util.Arrays.asList(args));
+        log.info("Executing (String[]): " + Arguments.asString(args));
 
         return execute(args[0], Arguments.shift(args));
     }

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/InteractiveGShell.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/InteractiveGShell.java?rev=410770&r1=410769&r2=410770&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/InteractiveGShell.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/InteractiveGShell.java
Wed May 31 22:53:41 2006
@@ -23,7 +23,7 @@
 import java.io.IOException;
 
 /**
- * ???
+ * Provides the user-interaction bits for GShell.
  *
  * @version $Id$
  */
@@ -39,7 +39,12 @@
 
                     // Execute unless the line is just blank
                     if (!line.trim().equals("")) {
-                        gshell.execute(line);
+                        try {
+                            gshell.execute(line);
+                        }
+                        catch (ExitNotification n) {
+                            return Result.STOP;
+                        }
                     }
 
                     return Result.CONTINUE;

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java?rev=410770&r1=410769&r2=410770&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandSupport.java
Wed May 31 22:53:41 2006
@@ -19,6 +19,7 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.geronimo.gshell.console.IO;
+import org.apache.geronimo.gshell.util.Arguments;
 
 /**
  * Provides support for {@link Command} implemenations.
@@ -29,19 +30,19 @@
     implements Command
 {
     protected Log log;
-    
+
     private String name;
 
     private CommandContext context;
-    
+
     protected CommandSupport(final String name) {
         setName(name);
     }
-    
+
     protected CommandSupport() {
         // Sub-class must call setName()
     }
-    
+
     public void setName(final String name) {
         if (name == null) {
             throw new IllegalArgumentException("Name is null");
@@ -49,10 +50,10 @@
         if (name.trim().length() == 0) {
             throw new IllegalArgumentException("Name is empty");
         }
-        
+
         this.name = name;
     }
-    
+
     public String getName() {
         if (name == null) {
             throw new IllegalStateException("Name was not set");
@@ -60,101 +61,105 @@
 
         return name;
     }
-    
+
     //
     // Life-cycle
     //
-    
+
     public final void init(final CommandContext context) {
         if (this.context != null) {
             throw new IllegalStateException("Command already initalized");
         }
-        
+
         // Initialize logging with command name
         log = LogFactory.getLog(this.getClass().getName() + "." + getName());
-        
+
         log.debug("Initializing");
-        
+
         this.context = context;
-        
+
         doInit();
     }
-    
+
     protected void doInit() {
         // Sub-class should override to provide custom initialization
     }
-    
+
     private void ensureInitialized() {
         if (context == null) {
             throw new IllegalStateException("Command has not been initialized");
         }
     }
-    
+
     public final void destroy() {
         if (this.context == null) {
             throw new IllegalStateException("Command already destroyed (or never initialized)");
         }
-        
+
         log.debug("Destroying");
-        
+
         doDestroy();
-        
+
         this.context = null;
     }
-    
+
     protected void doDestroy() {
         // Sub-class should override to provide custom cleanup
     }
-    
+
     public void abort() {
         // Sub-calss should override to allow for custom abort functionality
     }
-    
+
     //
     // Context Helpers
     //
-    
+
     protected CommandContext getCommandContext() {
         assert context != null;
-        
+
         return context;
     }
-    
+
     protected Variables getVariables() {
         return getCommandContext().getVariables();
     }
-    
+
     protected IO getIO() {
         return getCommandContext().getIO();
     }
-    
+
     //
     // Execute Helpers
     //
-    
+
     public int execute(final String... args) throws Exception {
         assert args != null;
-        
+
         // Make sure that we have been initialized before we go any further
         ensureInitialized();
 
-        log.info("Executing w/arguments: " + java.util.Arrays.asList(args));
+        log.info("Executing w/arguments: " + Arguments.asString(args));
 
         int status;
-        
+
         try {
             status = doExecute(args);
         }
         catch (Exception e) {
-            //
-            // TODO: Handle Errors here too
-            //
-            
             log.error(e.getMessage());
             if (log.isDebugEnabled()) {
-                log.debug("Failure details", e);
+                log.debug("Exception details", e);
             }
-            
+
+            status = Command.FAILURE;
+        }
+        catch (Error e) {
+            log.error(e.getMessage());
+            if (log.isDebugEnabled()) {
+                log.debug("Error details", e);
+            }
+
             status = Command.FAILURE;
         }
         finally {
@@ -166,10 +171,10 @@
 
         return status;
     }
-    
+
     protected int doExecute(final String[] args) throws Exception {
         // Sub-class should override to perform custom execution
-        
+
         return Command.FAILURE;
     }
 }

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/util/Arguments.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/util/Arguments.java?rev=410770&r1=410769&r2=410770&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/util/Arguments.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/util/Arguments.java
Wed May 31 22:53:41 2006
@@ -28,10 +28,26 @@
     }
 
     public static String[] shift(final String[] args, int pos) {
+        assert args != null;
         assert args.length >= pos;
 
         String[] _args = new String[args.length - pos];
         System.arraycopy(args, pos, _args, 0, _args.length);
         return _args;
+    }
+
+    public static String asString(final String[] args) {
+        assert args != null;
+
+        StringBuffer buff = new StringBuffer();
+
+        for (int i=0; i<args.length; i++ ) {
+            buff.append(args[i]);
+            if (i + 1 < args.length) {
+                buff.append(", ");
+            }
+        }
+
+        return buff.toString();
     }
 }

Modified: geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/GShellServer.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/GShellServer.java?rev=410770&r1=410769&r2=410770&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/GShellServer.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/GShellServer.java
Wed May 31 22:53:41 2006
@@ -82,6 +82,10 @@
             // TODO: Abstract Telnet specifics; support other protocols (ie. SSH)
             //
 
+            //
+            // TODO: Need access to the Terminal... NVT4J
+            //
+
             io = new IO(new TelnetInputStream(in, out), new TelnetPrintStream(out));
             GShell shell = new GShell(io);
 

Modified: geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/ServerCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/ServerCommand.java?rev=410770&r1=410769&r2=410770&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/ServerCommand.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/ServerCommand.java
Wed May 31 22:53:41 2006
@@ -25,6 +25,10 @@
 import org.apache.geronimo.gshell.command.Command;
 import org.apache.geronimo.gshell.command.CommandSupport;
 import org.apache.geronimo.gshell.console.IO;
+import org.apache.geronimo.gshell.server.SocketServerDaemon.SocketHandler;
+
+import java.net.Socket;
+import java.io.IOException;
 
 /**
  * Starts a GShell server.
@@ -34,9 +38,7 @@
 public class ServerCommand
     extends CommandSupport
 {
-    private int port = GShellDaemon.DEFAULT_PORT;
-
-    private boolean background = false;
+    private int port = 5057;
 
     public ServerCommand() {
         super("server");
@@ -62,10 +64,6 @@
             .hasArg()
             .create('p'));
 
-        options.addOption(OptionBuilder.withLongOpt("background")
-            .withDescription("Run as a daemon in the background")
-            .create('b'));
-
         CommandLineParser parser = new PosixParser();
         CommandLine line = parser.parse(options, args);
 
@@ -95,22 +93,23 @@
             port = Integer.parseInt(tmp);
         }
 
-        if (line.hasOption('b')) {
-            background = true;
-        }
-
         server();
 
         return Command.SUCCESS;
     }
 
     private void server() throws Exception {
-        GShellDaemon daemon = new GShellDaemon(port, background);
+        SocketHandler handler = new SocketHandler() {
+            GShellServer server = new GShellServer();
 
-        //
-        // NOTE: Spit this out before hand, since if not --background
-        //       start() will not return right away
-        //
+            public void handle(final Socket socket) throws IOException {
+                assert socket != null;
+
+                server.service(socket);
+            }
+        };
+
+        SocketServerDaemon daemon = new SocketServerDaemon(port, handler);
 
         IO io = getIO();
         io.out.println("Listening for connections on port: " + port);

Copied: geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/SocketServerDaemon.java
(from r410734, geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/GShellDaemon.java)
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/SocketServerDaemon.java?p2=geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/SocketServerDaemon.java&p1=geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/GShellDaemon.java&r1=410734&r2=410770&rev=410770&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/GShellDaemon.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/SocketServerDaemon.java
Wed May 31 22:53:41 2006
@@ -22,46 +22,39 @@
 import java.net.Socket;
 import java.net.ServerSocket;
 
+import java.io.IOException;
+
 //
 // NOTE: Some bits lifted from XBean Telnet module
 //
 
 /**
- * Daemon service which listens for socket connections and
- * spawns client shells via {@link GShellServer}.
+ * Daemon service which listens for socket connections and spawns client threads.
  *
  * @version $Id$
  */
-public class GShellDaemon
+public class SocketServerDaemon
     implements Runnable
 {
-    public static final int DEFAULT_PORT = 5057;
-
-    private static final Log log = LogFactory.getLog(GShellDaemon.class);
+    private static final Log log = LogFactory.getLog(SocketServerDaemon.class);
 
-    private final GShellServer server;
+    private final SocketHandler handler;
 
     private final int port;
 
-    private boolean background;
-
     private ServerSocket serverSocket;
 
-    private boolean running = false;
+    private ThreadGroup threads = new ThreadGroup("SocketServerDaemon");
 
-    public GShellDaemon(final int port, final boolean background) {
-        this.port = port;
-        this.background = background;
-
-        this.server = new GShellServer();
-    }
+    private boolean running;
 
-    public GShellDaemon(final int port) {
-        this(port, true);
-    }
+    public SocketServerDaemon(final int port, final SocketHandler handler) {
+        if (handler == null) {
+            throw new IllegalArgumentException("Socket handler is null");
+        }
 
-    public GShellDaemon() {
-        this(DEFAULT_PORT);
+        this.port = port;
+        this.handler = handler;
     }
 
     public void start() throws Exception {
@@ -75,26 +68,21 @@
 
         serverSocket = new ServerSocket(port, 20);
         Thread d = new Thread(this);
-        d.setName("GShell Daemon@" + d.hashCode());
+        d.setName("SocketServerDaemon@" + d.hashCode());
         d.setDaemon(true);
         d.start();
 
         log.info("Started");
-
-        //
-        // FIXME: This is broken... not what I had wanted at all :-(
-        //
-
-        // Wait for one client session and then return
-        if (!background) {
-            log.debug("Waiting for job to finish");
-
-            running = false;
-            d.join();
-        }
     }
 
     public void stop() throws Exception {
+        if (!running) {
+            throw new IllegalStateException("Not started");
+        }
+
+        serverSocket.close();
+        serverSocket = null;
+
         running = false;
     }
 
@@ -105,10 +93,14 @@
 
         log.info("Starting new thread for client: " + socket);
 
-        Thread d = new Thread(new Runnable() {
+        //
+        // TODO: Maybe use a thread-pool here?
+        //
+
+        Thread d = new Thread(threads, new Runnable() {
             public void run() {
                 try {
-                    server.service(socket);
+                    handler.handle(socket);
                 }
                 catch (Throwable e) {
                     log.error("Service failure", e);
@@ -126,11 +118,15 @@
             }
         });
 
-        d.setName("GShell@" + d.hashCode());
+        d.setName("SocketHandler@" + d.hashCode());
         d.setDaemon(true);
         d.start();
     }
 
+    //
+    // Runnable
+    //
+
     public void run() {
         log.info("Listening for connections on port: " + port);
 
@@ -147,5 +143,24 @@
                 log.error("Unexpected; ignoring", t);
             }
         }
+    }
+
+    //
+    // SocketHandler
+    //
+
+    /**
+     * Allows custom processing for client socket connections.
+     */
+    public static interface SocketHandler
+    {
+        /**
+         * Handle the client socket.
+         *
+         * @param socket    The client socket; never null
+         *
+         * @throws IOException
+         */
+        void handle(Socket socket) throws IOException;
     }
 }

Propchange: geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/SocketServerDaemon.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/SocketServerDaemon.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/SocketServerDaemon.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message