geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r578517 - in /geronimo/sandbox/gshell/trunk/gshell-remote: gshell-remote-client/src/main/java/org/apache/geronimo/gshell/remote/client/ gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/message/ gshell-remote-server/src/m...
Date Sat, 22 Sep 2007 22:26:18 GMT
Author: jdillon
Date: Sat Sep 22 15:26:14 2007
New Revision: 578517

URL: http://svn.apache.org/viewvc?rev=578517&view=rev
Log:
Move the visitor impls into nested classes of the handler to allow them to share more state

Added:
    geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-client/src/main/java/org/apache/geronimo/gshell/remote/client/RshClientHandler.java
  (contents, props changed)
      - copied, changed from r578511, geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-client/src/main/java/org/apache/geronimo/gshell/remote/client/RshClientMessageHandler.java
    geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RshServerHandler.java
  (contents, props changed)
      - copied, changed from r578511, geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RshServerMessageHandler.java
Removed:
    geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-client/src/main/java/org/apache/geronimo/gshell/remote/client/RshClientMessageHandler.java
    geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-client/src/main/java/org/apache/geronimo/gshell/remote/client/RshClientMessageVisitor.java
    geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RshServerMessageHandler.java
    geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RshServerMessageVisitor.java
Modified:
    geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/message/MessageHandler.java

Copied: geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-client/src/main/java/org/apache/geronimo/gshell/remote/client/RshClientHandler.java
(from r578511, geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-client/src/main/java/org/apache/geronimo/gshell/remote/client/RshClientMessageHandler.java)
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-client/src/main/java/org/apache/geronimo/gshell/remote/client/RshClientHandler.java?p2=geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-client/src/main/java/org/apache/geronimo/gshell/remote/client/RshClientHandler.java&p1=geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-client/src/main/java/org/apache/geronimo/gshell/remote/client/RshClientMessageHandler.java&r1=578511&r2=578517&rev=578517&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-client/src/main/java/org/apache/geronimo/gshell/remote/client/RshClientMessageHandler.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-client/src/main/java/org/apache/geronimo/gshell/remote/client/RshClientHandler.java
Sat Sep 22 15:26:14 2007
@@ -20,9 +20,11 @@
 package org.apache.geronimo.gshell.remote.client;
 
 import org.apache.geronimo.gshell.remote.message.MessageHandler;
-import org.apache.geronimo.gshell.remote.message.MessageVisitor;
+import org.apache.geronimo.gshell.remote.message.MessageVisitorSupport;
+import org.apache.geronimo.gshell.remote.message.rsh.EchoMessage;
 import org.codehaus.plexus.component.annotations.Component;
-import org.codehaus.plexus.component.annotations.Requirement;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
 
 /**
  * ???
@@ -30,14 +32,27 @@
  * @version $Rev$ $Date$
  */
 @Component(role=MessageHandler.class, hint="client")
-public class RshClientMessageHandler
+public class RshClientHandler
     extends MessageHandler
+    implements Initializable
 {
-    @Requirement
-    private RshClientMessageVisitor clientVisitor;
+    public RshClientHandler() {}
 
-    @Override
-    protected MessageVisitor getVisitor() {
-        return clientVisitor;
+    public void initialize() throws InitializationException {
+        setVisitor(new Visitor());
+    }
+
+    //
+    // MessageVisitor
+    //
+
+    private class Visitor
+        extends MessageVisitorSupport
+    {
+        public void visitEcho(final EchoMessage msg) throws Exception {
+            assert msg != null;
+
+            log.info("ECHO: {}", msg.getText());
+        }
     }
 }

Propchange: geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-client/src/main/java/org/apache/geronimo/gshell/remote/client/RshClientHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

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

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

Modified: geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/message/MessageHandler.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/message/MessageHandler.java?rev=578517&r1=578516&r2=578517&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/message/MessageHandler.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/message/MessageHandler.java
Sat Sep 22 15:26:14 2007
@@ -42,6 +42,12 @@
         this.visitor = visitor;
     }
 
+    protected void setVisitor(final MessageVisitor visitor) {
+        assert visitor != null;
+        
+        this.visitor = visitor;
+    }
+
     protected MessageVisitor getVisitor() {
         if (visitor == null) {
             throw new IllegalStateException("Message visitor not bound");

Copied: geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RshServerHandler.java
(from r578511, geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RshServerMessageHandler.java)
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RshServerHandler.java?p2=geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RshServerHandler.java&p1=geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RshServerMessageHandler.java&r1=578511&r2=578517&rev=578517&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RshServerMessageHandler.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RshServerHandler.java
Sat Sep 22 15:26:14 2007
@@ -19,10 +19,35 @@
 
 package org.apache.geronimo.gshell.remote.server;
 
+import java.io.PrintWriter;
+import java.util.Date;
+import java.util.UUID;
+
+import org.apache.geronimo.gshell.DefaultEnvironment;
+import org.apache.geronimo.gshell.command.IO;
+import org.apache.geronimo.gshell.common.Notification;
+import org.apache.geronimo.gshell.lookup.EnvironmentLookup;
+import org.apache.geronimo.gshell.lookup.IOLookup;
+import org.apache.geronimo.gshell.remote.RemoteShell;
 import org.apache.geronimo.gshell.remote.message.MessageHandler;
-import org.apache.geronimo.gshell.remote.message.MessageVisitor;
+import org.apache.geronimo.gshell.remote.message.MessageVisitorSupport;
+import org.apache.geronimo.gshell.remote.message.rsh.CloseShellMessage;
+import org.apache.geronimo.gshell.remote.message.rsh.EchoMessage;
+import org.apache.geronimo.gshell.remote.message.rsh.ExecuteMessage;
+import org.apache.geronimo.gshell.remote.message.rsh.OpenShellMessage;
+import org.apache.geronimo.gshell.remote.session.SessionAttributeBinder;
+import org.apache.geronimo.gshell.remote.stream.SessionInputStream;
+import org.apache.geronimo.gshell.remote.stream.SessionOutputStream;
+import org.apache.geronimo.gshell.shell.Environment;
+import org.apache.mina.common.IoSession;
+import org.codehaus.plexus.ContainerConfiguration;
+import org.codehaus.plexus.DefaultContainerConfiguration;
+import org.codehaus.plexus.PlexusContainer;
+import org.codehaus.plexus.PlexusContainerException;
 import org.codehaus.plexus.component.annotations.Component;
 import org.codehaus.plexus.component.annotations.Requirement;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
 
 /**
  * ???
@@ -30,14 +55,193 @@
  * @version $Rev$ $Date$
  */
 @Component(role=MessageHandler.class, hint="server")
-public class RshServerMessageHandler
+public class RshServerHandler
     extends MessageHandler
+    implements Initializable
 {
     @Requirement
-    private RshServerMessageVisitor serverVisitor;
+    private PlexusContainer parentContainer;
+
+    public void initialize() throws InitializationException {
+        setVisitor(new Visitor());
+    }
+
+    //
+    // TODO: Move the security handling (bits from SecurityFilter) into here
+    //
+
+    //
+    // MessageVisitor
+    //
+
+    private static final SessionAttributeBinder<IO> IO_BINDER = new SessionAttributeBinder<IO>(IO.class);
+
+    private static final SessionAttributeBinder<Environment> ENV_BINDER = new SessionAttributeBinder<Environment>(Environment.class);
+
+    private static final SessionAttributeBinder<RemoteShell> SHELL_BINDER = new SessionAttributeBinder<RemoteShell>(RemoteShell.class);
+
+    private class Visitor
+        extends MessageVisitorSupport
+    {
+        public void visitEcho(final EchoMessage msg) throws Exception {
+            assert msg != null;
+
+            log.info("ECHO: {}", msg);
+
+            String text = msg.getText();
+
+            //
+            // HACK:
+            //
+
+            if ("NOISE MAKER".equals(text)) {
+                log.info("Making noise...");
+
+                final IoSession session = msg.getSession();
+                final PrintWriter out = new PrintWriter(SessionOutputStream.BINDER.lookup(session),
false);
+
+                new Thread("NOISE MAKER") {
+                    public void run() {
+                        while (true) {
+                            out.println("FROM SERVER: " + new Date());
+                            out.flush();
+
+                            try {
+                                Thread.sleep(5000);
+                            }
+                            catch (InterruptedException e) {
+                                e.printStackTrace();
+                            }
+                        }
+                    }
+                }.start();
+            }
+            else if ("NO REPLY".equals(text)) {
+                // ignore
+            }
+            else {
+                msg.reply(new EchoMessage(text));
+            }
+        }
+
+        private RemoteShellContainer createContainer() throws PlexusContainerException {
+            // Create a new container which will be the parent for our remote shells
+            ContainerConfiguration config = new DefaultContainerConfiguration();
+
+            String name = "gshell.remote-shell:" + UUID.randomUUID();
+
+            config.setName(name);
+
+            config.setClassWorld(parentContainer.getContainerRealm().getWorld());
+
+            return new RemoteShellContainer(config);
+        }
+
+        public void visitOpenShell(final OpenShellMessage msg) throws Exception {
+            assert msg != null;
+
+            log.info("OPEN SHELL: {}", msg);
+
+            IoSession session = msg.getSession();
+
+            RemoteShellContainer shellContainer = createContainer();
+            RemoteShellContainer.BINDER.bind(session, shellContainer);
+
+            // Setup the I/O context (w/o auto-flushing)
+            IO io = new IO(SessionInputStream.BINDER.lookup(session), SessionOutputStream.BINDER.lookup(session),
false);
+
+            //
+            // FIXME: We need to set the verbosity of this I/O context as specified by the
client
+            //
+
+            IOLookup.set(shellContainer, io);
+            IO_BINDER.bind(session, io);
+
+            // Setup shell environemnt
+            Environment env = new DefaultEnvironment(io);
+            EnvironmentLookup.set(shellContainer, env);
+            ENV_BINDER.bind(session, env);
+
+            // Create a new shell instance
+            RemoteShell shell = (RemoteShell) shellContainer.lookup(RemoteShell.class);
+            SHELL_BINDER.bind(session, shell);
+
+            //
+            // TODO: Send a meaningful response
+            //
+
+            msg.reply(new EchoMessage("OPEN SHELL SUCCESS"));
+        }
+
+        public void visitCloseShell(final CloseShellMessage msg) throws Exception {
+            assert msg != null;
+
+            log.info("CLOSE SHELL: {}", msg);
+
+            IoSession session = msg.getSession();
+
+            log.info("Closing shell");
+
+            RemoteShell shell = SHELL_BINDER.unbind(session);
+
+            shell.close();
+
+            log.info("Unbinding resources");
+
+            IO_BINDER.unbind(session);
+
+            ENV_BINDER.unbind(session);
+
+            log.info("Destroying container");
+
+            RemoteShellContainer shellContainer = RemoteShellContainer.BINDER.unbind(session);
+            shellContainer.disposeAllComponents();
+
+            //
+            // TODO: Send a meaningful response
+            //
+
+            msg.reply(new EchoMessage("CLOSE SHELL SUCCESS"));
+        }
+
+        public void visitExecute(final ExecuteMessage msg) throws Exception {
+            assert msg != null;
+
+            log.info("EXECUTE: {}", msg);
+
+            IoSession session = msg.getSession();
+
+            RemoteShell shell = SHELL_BINDER.lookup(session);
+
+            try {
+                //
+                // TODO: Need to find a better place to stash this me thinks...
+                //
+
+                RemoteShellContainer shellContainer = RemoteShellContainer.BINDER.lookup(session);
+
+                IO io = IO_BINDER.lookup(session);
+                IOLookup.set(shellContainer, io);
+
+                Environment env = ENV_BINDER.lookup(session);
+                EnvironmentLookup.set(shellContainer, env);
+
+                Object result = msg.execute(shell);
+
+                log.debug("Result: {}", result);
+
+                msg.reply(new ExecuteMessage.Result(result));
+            }
+            catch (Notification n) {
+                log.debug("Notification: " + n);
+
+                msg.reply(new ExecuteMessage.Notification(n));
+            }
+            catch (Throwable t) {
+                log.debug("Fault: " + t);
 
-    @Override
-    protected MessageVisitor getVisitor() {
-        return serverVisitor;
+                msg.reply(new ExecuteMessage.Fault(t));
+            }
+        }
     }
 }

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

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

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



Mime
View raw message