You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2006/05/31 10:03:28 UTC

svn commit: r410452 - in /geronimo/sandbox/gshell/trunk: ./ gshell-core/src/main/java/org/apache/geronimo/gshell/command/ gshell-core/src/main/java/org/apache/geronimo/gshell/console/ gshell-server/ gshell-server/src/main/java/org/apache/geronimo/gshel...

Author: jdillon
Date: Wed May 31 01:03:27 2006
New Revision: 410452

URL: http://svn.apache.org/viewvc?rev=410452&view=rev
Log:
Mostly-functional server bits for GShell
Some bits crafted after from XBean Telnet; currently using the Telnet*Stream impl
Crude 'server' command to start up the daemon
Only supports Telnet at the moment... but it does work... um, sorta :-]

Added:
    geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/GShellDaemon.java   (with props)
    geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/ServerCommand.java   (with props)
    geronimo/sandbox/gshell/trunk/gshell-server/src/main/resources/META-INF/
    geronimo/sandbox/gshell/trunk/gshell-server/src/main/resources/META-INF/org.apache.geronimo.gshell.command/
    geronimo/sandbox/gshell/trunk/gshell-server/src/main/resources/META-INF/org.apache.geronimo.gshell.command/server.properties
Modified:
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandManager.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/IO.java
    geronimo/sandbox/gshell/trunk/gshell-server/pom.xml
    geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/GShellServer.java
    geronimo/sandbox/gshell/trunk/pom.xml

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandManager.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandManager.java?rev=410452&r1=410451&r2=410452&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandManager.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/CommandManager.java Wed May 31 01:03:27 2006
@@ -22,7 +22,6 @@
 import java.util.Map;
 import java.util.Properties;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Set;
 import java.util.Collections;
 

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/IO.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/IO.java?rev=410452&r1=410451&r2=410452&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/IO.java (original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/IO.java Wed May 31 01:03:27 2006
@@ -21,6 +21,7 @@
 import java.io.PrintStream;
 import java.io.PrintWriter;
 import java.io.Reader;
+import java.io.IOException;
 
 /**
  * Container for input/output handles.
@@ -93,6 +94,16 @@
     }
 
     /**
+     * Construct a new IO container.
+     *
+     * @param in    The input steam; must not be null
+     * @param out   The output stream and error stream; must not be null
+     */
+    public IO(final InputStream in, final PrintStream out) {
+        this(in, out, out);
+    }
+
+    /**
      * Helper which uses current values from {@link System}.
      */
     public IO() {
@@ -105,5 +116,11 @@
     public void flush() {
         out.flush();
         err.flush();
+    }
+
+    public void close() throws IOException {
+        in.close();
+        out.close();
+        err.close();
     }
 }

Modified: geronimo/sandbox/gshell/trunk/gshell-server/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-server/pom.xml?rev=410452&r1=410451&r2=410452&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-server/pom.xml (original)
+++ geronimo/sandbox/gshell/trunk/gshell-server/pom.xml Wed May 31 01:03:27 2006
@@ -26,6 +26,11 @@
             <artifactId>gshell-core</artifactId>
             <version>${pom.version}</version>
         </dependency>
+
+        <dependency>
+            <groupId>org.apache.xbean</groupId>
+            <artifactId>xbean-telnet</artifactId>
+        </dependency>
     </dependencies>
     
 </project>

Added: 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/GShellDaemon.java?rev=410452&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/GShellDaemon.java (added)
+++ geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/GShellDaemon.java Wed May 31 01:03:27 2006
@@ -0,0 +1,151 @@
+/*
+ * 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.server;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.net.Socket;
+import java.net.ServerSocket;
+
+//
+// NOTE: Some bits lifted from XBean Telnet module
+//
+
+/**
+ * Daemon service which listens for socket connections and
+ * spawns client shells via {@link GShellServer}.
+ *
+ * @version $Id$
+ */
+public class GShellDaemon
+    implements Runnable
+{
+    public static final int DEFAULT_PORT = 5057;
+
+    private static final Log log = LogFactory.getLog(GShellDaemon.class);
+
+    private final GShellServer server;
+
+    private final int port;
+
+    private boolean background;
+
+    private ServerSocket serverSocket;
+
+    private boolean running = false;
+
+    public GShellDaemon(final int port, final boolean background) {
+        this.port = port;
+        this.background = background;
+
+        this.server = new GShellServer();
+    }
+
+    public GShellDaemon(final int port) {
+        this(port, true);
+    }
+
+    public GShellDaemon() {
+        this(DEFAULT_PORT);
+    }
+
+    public void start() throws Exception {
+        if (running) {
+            throw new IllegalStateException("Already started");
+        }
+
+        log.info("Starting...");
+
+        running = true;
+
+        serverSocket = new ServerSocket(port, 20);
+        Thread d = new Thread(this);
+        d.setName("GShell Daemon@" + 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 {
+        running = false;
+    }
+
+    public void service(final Socket socket) {
+        if (socket == null) {
+            throw new IllegalArgumentException("Socket is null");
+        }
+
+        log.info("Starting new thread for client: " + socket);
+
+        Thread d = new Thread(new Runnable() {
+            public void run() {
+                try {
+                    server.service(socket);
+                }
+                catch (Throwable e) {
+                    log.error("Service failure", e);
+                }
+                finally {
+                    try {
+                        if (socket != null) {
+                            socket.close();
+                        }
+                    }
+                    catch (Throwable t) {
+                        log.error("Failure while closing socket; ignoring", t);
+                    }
+                }
+            }
+        });
+
+        d.setName("GShell@" + d.hashCode());
+        d.setDaemon(true);
+        d.start();
+    }
+
+    public void run() {
+        log.info("Listening for connections on port: " + port);
+
+        while (running) {
+            try {
+                Socket socket = serverSocket.accept();
+                socket.setTcpNoDelay(true);
+
+                if (running) {
+                    service(socket);
+                }
+            }
+            catch (Throwable t) {
+                log.error("Unexpected; ignoring", t);
+            }
+        }
+    }
+}
\ No newline at end of file

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

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

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

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=410452&r1=410451&r2=410452&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 01:03:27 2006
@@ -18,6 +18,21 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.geronimo.gshell.GShell;
+import org.apache.geronimo.gshell.InteractiveGShell;
+import org.apache.geronimo.gshell.console.IO;
+
+import org.apache.xbean.terminal.telnet.TelnetInputStream;
+import org.apache.xbean.terminal.telnet.TelnetPrintStream;
+
+import java.net.Socket;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+//
+// NOTE: Some bits lifted from XBean Telnet module
+//
 
 /**
  * ???
@@ -28,7 +43,51 @@
 {
     private static final Log log = LogFactory.getLog(GShellServer.class);
 
-    //
-    // TODO: ???
-    //
+    public void service(final Socket socket) throws IOException {
+        if (socket == null) {
+            throw new IllegalArgumentException("Socket is null");
+        }
+
+        if (log.isDebugEnabled()) {
+            log.debug("Servicing socket: " + socket);
+        }
+
+        try {
+            service(socket.getInputStream(), socket.getOutputStream());
+        }
+        finally {
+            if (socket != null) {
+                socket.close();
+            }
+        }
+    }
+
+    public void service(final InputStream in, final OutputStream out) throws IOException {
+        if (in == null) {
+            throw new IllegalArgumentException("Input is null");
+        }
+        if (out == null) {
+            throw new IllegalArgumentException("Output is null");
+        }
+
+        IO io = null;
+
+        //
+        // TODO: Need to figure out how to get the logging stream for this GShell to use
+        //       the given IO streams
+        //
+
+        try {
+            io = new IO(new TelnetInputStream(in, out), new TelnetPrintStream(out));
+            GShell shell = new GShell(io);
+
+            InteractiveGShell interp = new InteractiveGShell(io, shell);
+            interp.run();
+        }
+        finally {
+            if (io != null) {
+                io.close();
+            }
+        }
+    }
 }

Added: 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=410452&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/ServerCommand.java (added)
+++ geronimo/sandbox/gshell/trunk/gshell-server/src/main/java/org/apache/geronimo/gshell/server/ServerCommand.java Wed May 31 01:03:27 2006
@@ -0,0 +1,121 @@
+/*
+ * 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.server;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.OptionBuilder;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.PosixParser;
+import org.apache.geronimo.gshell.command.Command;
+import org.apache.geronimo.gshell.command.CommandSupport;
+import org.apache.geronimo.gshell.console.IO;
+
+/**
+ * Starts a GShell server.
+ *
+ * @version $Id$
+ */
+public class ServerCommand
+    extends CommandSupport
+{
+    private int port = GShellDaemon.DEFAULT_PORT;
+
+    private boolean background = false;
+
+    public ServerCommand() {
+        super("server");
+    }
+
+    protected int doExecute(final String[] args) throws Exception {
+        assert args != null;
+
+        //
+        // TODO: Optimize, move common code to CommandSupport
+        //
+
+        IO io = getIO();
+
+        Options options = new Options();
+
+        options.addOption(OptionBuilder.withLongOpt("help")
+            .withDescription("Display this help message")
+            .create('h'));
+
+        options.addOption(OptionBuilder.withLongOpt("port")
+            .withDescription("Use a specified port number")
+            .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);
+
+        if (line.hasOption('h')) {
+            io.out.println(getName() + " -- starts a GShell server");
+            io.out.println();
+
+            HelpFormatter formatter = new HelpFormatter();
+            formatter.printHelp(
+                io.out,
+                80, // width (FIXME: Should pull from gshell.columns variable)
+                getName() + " [options]",
+                "",
+                options,
+                4, // left pad
+                4, // desc pad
+                "",
+                false); // auto usage
+
+            io.out.println();
+
+            return Command.SUCCESS;
+        }
+
+        if (line.hasOption('p')) {
+            String tmp = line.getOptionValue('p');
+            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);
+
+        //
+        // NOTE: Spit this out before hand, since if not --background
+        //       start() will not return right away
+        //
+
+        IO io = getIO();
+        io.out.println("Listening for connections on port: " + port);
+        io.flush();
+
+        daemon.start();
+    }
+}

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

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

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

Added: geronimo/sandbox/gshell/trunk/gshell-server/src/main/resources/META-INF/org.apache.geronimo.gshell.command/server.properties
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-server/src/main/resources/META-INF/org.apache.geronimo.gshell.command/server.properties?rev=410452&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-server/src/main/resources/META-INF/org.apache.geronimo.gshell.command/server.properties (added)
+++ geronimo/sandbox/gshell/trunk/gshell-server/src/main/resources/META-INF/org.apache.geronimo.gshell.command/server.properties Wed May 31 01:03:27 2006
@@ -0,0 +1,11 @@
+##
+## $Id: cat.properties 409735 2006-05-26 20:28:27Z jdillon $
+##
+
+class=org.apache.geronimo.gshell.server.ServerCommand
+
+name=server
+
+# aliases=
+
+category=/gshell/server

Modified: geronimo/sandbox/gshell/trunk/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/pom.xml?rev=410452&r1=410451&r2=410452&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/pom.xml (original)
+++ geronimo/sandbox/gshell/trunk/pom.xml Wed May 31 01:03:27 2006
@@ -147,6 +147,14 @@
                 <type>jar</type>
                 <scope>compile</scope>
             </dependency>
+
+            <dependency>
+                <groupId>org.apache.xbean</groupId>
+                <artifactId>xbean-telnet</artifactId>
+                <version>2.4-SNAPSHOT</version>
+                <type>jar</type>
+                <scope>compile</scope>
+            </dependency>
         </dependencies>
     </dependencyManagement>