geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r576852 [2/2] - 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-com...
Date Tue, 18 Sep 2007 11:25:20 GMT
Copied: geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RemoteShell.java
(from r576639, geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultShell.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/RemoteShell.java?p2=geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RemoteShell.java&p1=geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultShell.java&r1=576639&r2=576852&rev=576852&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/DefaultShell.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RemoteShell.java
Tue Sep 18 04:25:18 2007
@@ -17,27 +17,20 @@
  * under the License.
  */
 
-package org.apache.geronimo.gshell;
+package org.apache.geronimo.gshell.remote.server;
 
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
-import java.util.concurrent.atomic.AtomicReference;
 
-import jline.History;
-import jline.Terminal;
-import org.apache.geronimo.gshell.ansi.Renderer;
 import org.apache.geronimo.gshell.branding.Branding;
 import org.apache.geronimo.gshell.command.CommandExecutor;
 import org.apache.geronimo.gshell.command.IO;
-import org.apache.geronimo.gshell.console.Console;
-import org.apache.geronimo.gshell.console.JLineConsole;
-import org.apache.geronimo.gshell.console.TerminalInfo;
 import org.apache.geronimo.gshell.shell.Environment;
-import org.apache.geronimo.gshell.shell.InteractiveShell;
 import org.apache.geronimo.gshell.shell.Shell;
 import org.apache.geronimo.gshell.shell.ShellInfo;
 import org.codehaus.plexus.component.annotations.Component;
+import org.codehaus.plexus.component.annotations.InstantiationStrategy;
 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;
@@ -46,14 +39,16 @@
 import org.slf4j.LoggerFactory;
 
 /**
- * This is the primary implementation of {@link Shell}.
+ * ???
  *
  * @version $Rev$ $Date$
  */
-@Component(role=InteractiveShell.class)
-public class DefaultShell
-    implements InteractiveShell, Initializable
+@Component(role=RemoteShell.class, instantiationStrategy=InstantiationStrategy.PER_LOOKUP)
+public class RemoteShell
+    implements Shell, Initializable
 {
+    public static final String SESSION_KEY = RemoteShell.class.getName();
+
     private Logger log = LoggerFactory.getLogger(getClass());
 
     @Requirement
@@ -66,22 +61,38 @@
     private CommandExecutor executor;
 
     @Requirement
-    private TerminalInfo termInfo;
-
-    @Requirement
-    private Terminal terminal;
-
-    @Requirement
     private Environment env;
 
     @Requirement
     private IO io;
 
+    private boolean opened;
+    
+    private void ensureOpened() {
+        if (!opened) {
+            throw new IllegalStateException("Remote shell has been closed");
+        }
+    }
+
+    public boolean isOpened() {
+        return opened;
+    }
+
+    public void close() {
+        opened = false;
+
+        log.info("Closed");
+    }
+
     public Environment getEnvironment() {
+        ensureOpened();
+
         return env;
     }
 
     public ShellInfo getShellInfo() {
+        ensureOpened();
+
         return shellInfo;
     }
 
@@ -92,7 +103,7 @@
         //
         //        This could be a warning sign that some of this class needs to be split
up into smaller bits...
         //
-        
+
         try {
             loadProfileScripts();
         }
@@ -104,155 +115,23 @@
     //
     // Command Execution (all delegates)
     //
-    
+
     public Object execute(final String line) throws Exception {
+        ensureOpened();
+
         return executor.execute(line);
     }
 
     public Object execute(final Object... args) throws Exception {
+        ensureOpened();
+
         return executor.execute((Object[])args);
     }
 
     public Object execute(final String path, final Object[] args) throws Exception {
-        return executor.execute(path, args);
-    }
+        ensureOpened();
 
-    //
-    // Interactive Shell
-    //
-
-    public void run(final Object... args) throws Exception {
-        assert args != null;
-
-        log.debug("Starting interactive console");
-
-        loadUserScript(branding.getInteractiveScriptName());
-
-        // Setup 2 final refs to allow our executor to pass stuff back to us
-        final AtomicReference<ExitNotification> exitNotifHolder = new AtomicReference<ExitNotification>();
-        final AtomicReference<Object> lastResultHolder = new AtomicReference<Object>();
-
-        // Whip up a tiny console executor that will execute shell command-lines
-        Console.Executor executor = new Console.Executor() {
-            public Result execute(final String line) throws Exception {
-                assert line != null;
-                
-                try {
-                    Object result = DefaultShell.this.execute(line);
-                    
-                    lastResultHolder.set(result);
-                }
-                catch (ExitNotification n) {
-                    exitNotifHolder.set(n);
-                    
-                    return Result.STOP;
-                }
-
-                return Result.CONTINUE;
-            }
-        };
-
-        // Ya, bust out the sexy JLine console baby!
-        JLineConsole console = new JLineConsole(executor, io, terminal);
-
-        // Setup the prompt
-        console.setPrompter(new Console.Prompter() {
-            Renderer renderer = new Renderer();
-
-            public String prompt() {
-                String userName = shellInfo.getUserName();
-                String hostName = shellInfo.getLocalHost().getHostName();
-
-                //
-                // HACK: There is no path... yet ;-)
-                //
-                String path = "/";
-
-                return renderer.render("@|bold " + userName + "|@" + hostName + ":@|bold
" + path + "|> ");
-            }
-        });
-
-        // Delegate errors for display and then continue
-        console.setErrorHandler(new Console.ErrorHandler() {
-            public Result handleError(final Throwable error) {
-                assert error != null;
-
-                displayError(error);
-                
-                return Result.CONTINUE;
-            }
-        });
-
-        // Hook up a nice history file (we gotta hold on to the history object at some point
so the 'history' command can get to it) 
-        History history = new History();
-        console.setHistory(history);
-        console.setHistoryFile(new File(branding.getUserDirectory(), branding.getHistoryFileName()));
-
-        // Unless the user wants us to shut up, then display a nice welcome banner
-        if (!io.isQuiet()) {
-            io.out.println(branding.getWelcomeBanner());
-        }
-
-        // Check if there are args, and run them and then enter interactive
-        if (args.length != 0) {
-            execute(args);
-        }
-
-        // And then spin up the console and go for a jog
-        console.run();
-
-        // If any exit notification occured while running, then puke it up
-        ExitNotification n = exitNotifHolder.get();
-        if (n != null) {
-            throw n;
-        }
-    }
-
-    //
-    // Error Display
-    //
-
-    private void displayError(final Throwable error) {
-        assert error != null;
-
-        // Decode any error notifications
-        Throwable cause = error;
-        if (error instanceof ErrorNotification) {
-            cause = error.getCause();
-        }
-
-        // Spit out the terse reason why we've failed
-        io.err.print("@|bold,red ERROR| ");
-        io.err.print(cause.getClass().getSimpleName());
-        io.err.println(": @|bold,red " + cause.getMessage() + "|");
-
-        if (io.isDebug()) {
-            // If we have debug enabled then skip the fancy bits below, and log the full
error, don't decode shit
-            log.debug(error.toString(), error);
-        }
-        else if (io.isVerbose()) {
-            // Render a fancy ansi colored stack trace
-            StackTraceElement[] trace = cause.getStackTrace();
-            StringBuffer buff = new StringBuffer();
-
-            for (StackTraceElement e : trace) {
-                buff.append("        @|bold at| ").
-                        append(e.getClassName()).
-                        append(".").
-                        append(e.getMethodName()).
-                        append(" (@|bold ");
-
-                buff.append(e.isNativeMethod() ? "Native Method" :
-                            (e.getFileName() != null && e.getLineNumber() != -1 ?
e.getFileName() + ":" + e.getLineNumber() :
-                                (e.getFileName() != null ? e.getFileName() : "Unknown Source")));
-
-                buff.append("|)");
-
-                io.err.println(buff);
-
-                buff.setLength(0);
-            }
-        }
+        return executor.execute(path, args);
     }
 
     //

Modified: geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RshServerCommand.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/RshServerCommand.java?rev=576852&r1=576851&r2=576852&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RshServerCommand.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RshServerCommand.java
Tue Sep 18 04:25:18 2007
@@ -35,7 +35,7 @@
 public class RshServerCommand
     extends CommandSupport
 {
-    @Argument(required=true)
+    @Argument(metaVar="LOCATION", required=true, index=0)
     private URI location;
 
     @Requirement
@@ -46,7 +46,14 @@
     protected Object doExecute() throws Exception {
         server = factory.connect(location);
 
-        io.out.println("Listening on: " + location);
+        io.info("Listening on: {}", location);
+
+        // For now just wait...
+        synchronized (this) {
+            wait();
+        }
+
+        server.close();
         
         return SUCCESS;
     }

Copied: geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RshServerMessageVisitor.java
(from r576671, geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/transport/tcp/TcpServerMessageVisitor.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/RshServerMessageVisitor.java?p2=geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RshServerMessageVisitor.java&p1=geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/transport/tcp/TcpServerMessageVisitor.java&r1=576671&r2=576852&rev=576852&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-common/src/main/java/org/apache/geronimo/gshell/remote/transport/tcp/TcpServerMessageVisitor.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RshServerMessageVisitor.java
Tue Sep 18 04:25:18 2007
@@ -17,113 +17,167 @@
  * under the License.
  */
 
-package org.apache.geronimo.gshell.remote.transport.tcp;
-
-import java.io.BufferedReader;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.io.PrintWriter;
-import java.util.Date;
+package org.apache.geronimo.gshell.remote.server;
 
+import org.apache.geronimo.gshell.DefaultEnvironment;
+import org.apache.geronimo.gshell.command.IO;
+import org.apache.geronimo.gshell.lookup.EnvironmentLookup;
+import org.apache.geronimo.gshell.lookup.IOLookup;
+import org.apache.geronimo.gshell.remote.message.CloseShellMessage;
 import org.apache.geronimo.gshell.remote.message.EchoMessage;
-import org.apache.geronimo.gshell.remote.message.HandShakeMessage;
 import org.apache.geronimo.gshell.remote.message.ExecuteMessage;
-import org.apache.geronimo.gshell.remote.transport.Transport;
+import org.apache.geronimo.gshell.remote.message.HandShakeMessage;
+import org.apache.geronimo.gshell.remote.message.MessageVisitor;
+import org.apache.geronimo.gshell.remote.message.MessageVisitorSupport;
+import org.apache.geronimo.gshell.remote.message.OpenShellMessage;
+import org.apache.geronimo.gshell.shell.Environment;
 import org.apache.mina.common.IoSession;
+import org.codehaus.plexus.PlexusContainer;
 import org.codehaus.plexus.component.annotations.Component;
+import org.codehaus.plexus.component.annotations.Requirement;
+import org.codehaus.plexus.component.factory.ComponentFactory;
 
 /**
  * Defines the logic for server-side message processing.
  *
  * @version $Rev$ $Date$
  */
-@Component(role=TcpServerMessageVisitor.class)
-public class TcpServerMessageVisitor
-    extends TcpMessageVisitorSupport
+@Component(role=MessageVisitor.class, hint="server")
+public class RshServerMessageVisitor
+    extends MessageVisitorSupport
 {
+    @Requirement
+    private PlexusContainer container;
+
+    //
+    // Remote Shell Access
+    //
+
+    private RemoteShell getRemoteShell(final IoSession session) {
+        assert session != null;
+
+        RemoteShell shell = (RemoteShell) session.getAttribute(RemoteShell.SESSION_KEY);
+
+        if (shell == null) {
+            throw new IllegalStateException("Remote shell not bound");
+        }
+
+        return shell;
+    }
+
+    private void setRemoteShell(final IoSession session, final RemoteShell shell) {
+        assert session != null;
+        assert shell != null;
+
+        // Make sure that no session already exists
+        Object obj = session.getAttribute(RemoteShell.SESSION_KEY);
+
+        if (obj != null) {
+            throw new IllegalStateException("Remote shell already bound");
+        }
+
+        session.setAttribute(RemoteShell.class.getName(), shell);
+    }
+
+    private void unsetRemoteShell(final IoSession session) {
+        assert session != null;
+
+        Object obj = session.getAttribute(RemoteShell.SESSION_KEY);
+
+        // Complain if no remote shell has been bound
+        if (obj != null) {
+            log.warn("Ignoring request to unset remote shell; no shell is bound");
+        }
+        else {
+            session.removeAttribute(RemoteShell.SESSION_KEY);
+        }
+    }
+
+    //
+    // MessageVisitor
+    //
+
     public void visitEcho(final EchoMessage msg) throws Exception {
         assert msg != null;
 
+        log.info("ECHO: {}", msg);
+
         String text = msg.getText();
 
+        msg.reply(new EchoMessage(text));
+    }
+
+    public void visitHandShake(final HandShakeMessage msg) throws Exception {
+        assert msg != null;
+
+        log.info("HANDSHAKE: {}", msg);
+
         //
-        // HACK: This is just here to test out the stream io muck
+        // TODO:
         //
+        
+        msg.reply(new EchoMessage("SUCCESS"));
+    }
 
-        if ("READ_STREAMS".equals(text)) {
-            final IoSession session = msg.getSession();
+    public void visitOpenShell(final OpenShellMessage msg) throws Exception {
+        assert msg != null;
 
-            OutputStream out = (OutputStream) session.getAttribute(Transport.OUTPUT_STREAM);
-            final PrintWriter writer = new PrintWriter(out);
+        log.info("OPEN SHELL: {}", msg);
 
-            InputStream in = (InputStream) session.getAttribute(Transport.INPUT_STREAM);
-            final BufferedReader reader = new BufferedReader(new InputStreamReader(in));
-
-            Thread t = new Thread("Stream Consumer") {
-                public void run() {
-                    try {
-                        log.debug("Consumer running...");
-
-                        String line;
-                        while ((line = reader.readLine()) != null) {
-                            System.err.println(line);
-                        }
-
-                        log.debug("Consumer stopped");
-                    }
-                    catch (Exception e) {
-                        log.error(e.getMessage(), e);
-                    }
-                }
-            };
-
-            t.start();
-
-            Thread t2 = new Thread("Noise Maker") {
-                public void run() {
-                    try {
-                        log.debug("Noise Maker...");
-
-                        while (true) {
-                            writer.println("FROM SERVER: " + new Date());
-                            writer.flush();
-
-                            Thread.sleep(1000 * 5);
-                        }
-
-                        // log.debug("Noise Maker stopped");
-                    }
-                    catch (Exception e) {
-                        log.error(e.getMessage(), e);
-                    }
-                }
-            };
+        IoSession session = msg.getSession();
 
-            t2.start();
-        }
-        else {
-            log.info("ECHOING: {}", text);
+        // Setup the I/O context
+        IO io = new IO(getInputStream(session), getOutputStream(session));
+        IOLookup ioLookup = (IOLookup) container.lookup(ComponentFactory.class, IOLookup.class.getSimpleName());
+        ioLookup.set(io);
 
-            msg.reply(new EchoMessage(text));
-        }
+        // Setup the shell environemnt
+        Environment env = new DefaultEnvironment(io);
+        EnvironmentLookup envLookup = (EnvironmentLookup) container.lookup(ComponentFactory.class,
EnvironmentLookup.class.getSimpleName());
+        envLookup.set(env);
+
+        // Create a new shell instance
+        RemoteShell shell = (RemoteShell) container.lookup(RemoteShell.class);
+
+        // Bind it to the session
+        setRemoteShell(session, shell);
+
+        //
+        // TODO: Send response
+        //
     }
 
-    public void visitHandShake(final HandShakeMessage msg) throws Exception {
+    public void visitCloseShell(final CloseShellMessage msg) throws Exception {
         assert msg != null;
 
-        log.info("HANDSHAKE");
+        log.info("CLOSE SHELL: {}", msg);
 
-        msg.reply(new EchoMessage("SUCCESS"));
+        IoSession session = msg.getSession();
+
+        RemoteShell shell = getRemoteShell(session);
+
+        shell.close();
+
+        unsetRemoteShell(session);
+
+        //
+        // TODO: Send response
+        //
     }
 
     public void visitExecute(final ExecuteMessage msg) throws Exception {
         assert msg != null;
 
-        log.info("EXECUTE: {}", msg.getLine());
+        log.info("EXECUTE: {}", msg);
+
+        IoSession session = msg.getSession();
+
+        RemoteShell shell = getRemoteShell(session);
+
+        Object result = msg.execute(shell);
 
         //
-        // TODO:
+        // TODO: Send response
         //
     }
 }

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

Propchange: geronimo/sandbox/gshell/trunk/gshell-remote/gshell-remote-server/src/main/java/org/apache/geronimo/gshell/remote/server/RshServerMessageVisitor.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/RshServerMessageVisitor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/sandbox/gshell/trunk/gshell-remote/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-remote/pom.xml?rev=576852&r1=576851&r2=576852&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-remote/pom.xml (original)
+++ geronimo/sandbox/gshell/trunk/gshell-remote/pom.xml Tue Sep 18 04:25:18 2007
@@ -41,8 +41,8 @@
     
     <dependencies>
         <dependency>
-            <groupId>org.apache.geronimo.gshell.support</groupId>
-            <artifactId>gshell-common</artifactId>
+            <groupId>org.apache.geronimo.gshell</groupId>
+            <artifactId>gshell-core</artifactId>
             <version>${version}</version>
         </dependency>
         



Mime
View raw message