geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r410204 - in /geronimo/sandbox/gshell/trunk: gshell-cli/src/main/java/org/apache/geronimo/gshell/cli/ gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/scripting/ gshell-core/src/main/java/org/apache/geron...
Date Tue, 30 May 2006 07:26:36 GMT
Author: jdillon
Date: Tue May 30 00:26:34 2006
New Revision: 410204

URL: http://svn.apache.org/viewvc?rev=410204&view=rev
Log:
Refactored InteractiveConsole for all your interactive input needs, its generic and not specific
to GShell anymore
Script command's interpreter can now be exited with a CTRL-D, which brings you back to gshell

Removed:
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/scripting/InteractiveInterpreter.java
Modified:
    geronimo/sandbox/gshell/trunk/gshell-cli/src/main/java/org/apache/geronimo/gshell/cli/Main.java
    geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/scripting/ScriptCommand.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/InteractiveConsole.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/JLineConsole.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/SimpleConsole.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/console/InteractiveConsoleTest.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=410204&r1=410203&r2=410204&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
Tue May 30 00:26:34 2006
@@ -26,10 +26,8 @@
 import org.apache.commons.cli.HelpFormatter;
 
 import org.apache.geronimo.gshell.GShell;
-import org.apache.geronimo.gshell.command.CommandManager;
 import org.apache.geronimo.gshell.console.IO;
 import org.apache.geronimo.gshell.console.InteractiveConsole;
-import org.apache.geronimo.gshell.console.SimpleConsole;
 import org.apache.geronimo.gshell.console.JLineConsole;
 
 import org.apache.geronimo.gshell.util.Version;
@@ -180,7 +178,7 @@
         //
 
         // Startup the shell
-        GShell gshell = new GShell(io);
+        final GShell gshell = new GShell(io);
         String[] _args = line.getArgs();
 
         // Force interactive if there are no args
@@ -189,7 +187,33 @@
         }
 
         if (interactive) {
-            InteractiveConsole console = new InteractiveConsole(new JLineConsole(io), gshell);
+            //
+            // TODO: May want to create a simple class in core to hide these details
+            //
+
+            InteractiveConsole console = new InteractiveConsole(
+                new JLineConsole(io),
+                new InteractiveConsole.Executor() {
+                    public Result execute(final String line) throws Exception {
+                        assert line != null;
+
+                        // Execute unless the line is just blank
+                        if (!line.trim().equals("")) {
+                            gshell.execute(line);
+                        }
+
+                        return Result.CONTINUE;
+                    }
+                },
+                new InteractiveConsole.Prompter() {
+                    public String getPrompt() {
+                        //
+                        // TODO: Need to hook this up to allow it to change
+                        //
+                        
+                        return "> ";
+                    }
+                });
 
             // Check if there are args, and run them and then enter interactive
             if (_args.length != 0) {

Modified: geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/scripting/ScriptCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/scripting/ScriptCommand.java?rev=410204&r1=410203&r2=410204&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/scripting/ScriptCommand.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-commands/scripting-commands/src/main/java/org/apache/geronimo/gshell/commands/scripting/ScriptCommand.java
Tue May 30 00:26:34 2006
@@ -27,6 +27,8 @@
 import org.apache.geronimo.gshell.command.CommandSupport;
 import org.apache.geronimo.gshell.console.IO;
 import org.apache.geronimo.gshell.console.JLineConsole;
+import org.apache.geronimo.gshell.console.InteractiveConsole;
+
 import org.apache.bsf.BSFManager;
 import org.apache.bsf.BSFEngine;
 
@@ -126,7 +128,7 @@
         }
 
         BSFManager manager = new BSFManager();
-        BSFEngine engine = manager.loadScriptingEngine(language);
+        final BSFEngine engine = manager.loadScriptingEngine(language);
 
         if (this.expression != null) {
             log.info("Evaluating expression: " + expression);
@@ -135,11 +137,37 @@
 
             log.info("Expression result: " + obj);
         }
+        else {
+            // No expression, assume interactive (else we don't do anything)
+            interactive = true;
+
+            //
+            // TODO: This will change when file/URL processing is added
+            //
+        }
 
         if (this.interactive) {
-            String prompt = "script:" + language + ">";
-            InteractiveInterpreter interp = new InteractiveInterpreter(new JLineConsole(getIO()),
engine, prompt);
-            interp.run();
+            InteractiveConsole console = new InteractiveConsole(
+                new JLineConsole(getIO()),
+                new InteractiveConsole.Executor() {
+                    public Result execute(final String line) throws Exception {
+                        // Execute unless the line is just blank
+                        if (!line.trim().equals("")) {
+                            engine.exec("<unknown>", 1, 1, line);
+                        }
+
+                        return Result.CONTINUE;
+                    }
+                },
+                new InteractiveConsole.Prompter() {
+                    public String getPrompt() {
+                        return "script:" + language + "> ";
+                    }
+                });
+
+            // Allow CTRL-D to exit :-)
+            console.setShutdownOnNull(true);
+            console.run();
         }
 
         return Command.SUCCESS;

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/InteractiveConsole.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/InteractiveConsole.java?rev=410204&r1=410203&r2=410204&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/InteractiveConsole.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/InteractiveConsole.java
Tue May 30 00:26:34 2006
@@ -18,10 +18,10 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.geronimo.gshell.GShell;
 
 /**
- * ???
+ * Provides the framework to interactivly get input from a console
+ * and "do something" with the line that was read.
  *
  * @version $Id$
  */
@@ -34,60 +34,113 @@
 
     private static final Log log = LogFactory.getLog(InteractiveConsole.class);
 
-    private final GShell gshell;
-
     private final Console console;
 
-    public InteractiveConsole(final Console console, final GShell gshell) {
+    private Executor executor;
+
+    private Prompter prompter;
+
+    private boolean shutdownOnNull = false;
+
+    public InteractiveConsole(final Console console, final Executor executor, final Prompter
prompter) {
         if (console == null) {
             throw new IllegalArgumentException("Console is null");
         }
-        if (gshell == null) {
-            throw new IllegalArgumentException("GShell is null");
+        if (executor == null) {
+            throw new IllegalArgumentException("Executor is null");
+        }
+        if (prompter == null) {
+            throw new IllegalArgumentException("Prompter is null");
         }
-
-        //
-        // TODO: Can probaby abstract the GShell bits to just some kind of String executor
-        //
 
         this.console = console;
-        this.gshell = gshell;
+        this.executor = executor;
+        this.prompter = prompter;
+    }
+
+    /**
+     * Enable or disable shutting down the interactive loop when
+     * a null value is read from the given console.
+     *
+     * @param flag  True to shutdown when a null is received; else false
+     */
+    public void setShutdownOnNull(final boolean flag) {
+        this.shutdownOnNull = flag;
+    }
+
+    /**
+     * @see #setShutdownOnNull
+     */
+    public boolean isShutdownOnNull() {
+        return shutdownOnNull;
     }
 
     public void run() {
         log.info("Running...");
 
         boolean debug = log.isDebugEnabled();
+        boolean running = true;
 
-        while (true) {
+        while (running) {
             try {
-                //
-                // TODO: Need to resolve how to allow the prompt to be changed
-                //
-
-                String prompt = "> ";
                 String line;
 
-                while ((line = console.readLine(prompt)) != null) {
+                while ((line = console.readLine(prompter.getPrompt())) != null) {
                     if (debug) {
                         log.debug("Read line: " + line);
                     }
 
-                    // Just ignore blank lines
-                    if (line.trim().equals("")) {
-                        continue;
+                    Executor.Result result = executor.execute(line);
+
+                    // Allow executor to request that the loop stop
+                    if (result == Executor.Result.STOP) {
+                        log.debug("Executor requested STOP");
+                        running = false;
+                        break;
                     }
+                }
 
-                    int result = gshell.execute(line);
+                //
+                // TODO: Probably need to expose more configurability for handing/rejecting
shutdown
+                //
 
-                    if (debug) {
-                        log.debug("Command result: " + result);
-                    }
+                // Line was null, maybe shutdown
+                if (shutdownOnNull) {
+                    log.debug("Input was null; which will cause shutdown");
+                    running = false;
                 }
             }
             catch (Exception e) {
-                log.error("Unhandled failure", e);
+                log.error("Exception", e);
             }
+            catch (Error e) {
+                log.error("Error", e);
+            }
+        }
+
+        log.info("Stopped");
+    }
+
+    //
+    // Executor
+    //
+
+    public static interface Executor
+    {
+        enum Result {
+            CONTINUE,
+            STOP
         }
+
+        Result execute(String line) throws Exception;
+    }
+
+    //
+    // Prompter
+    //
+
+    public static interface Prompter
+    {
+        String getPrompt();
     }
 }

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/JLineConsole.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/JLineConsole.java?rev=410204&r1=410203&r2=410204&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/JLineConsole.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/JLineConsole.java
Tue May 30 00:26:34 2006
@@ -33,6 +33,7 @@
     private static final Log log = LogFactory.getLog(SimpleConsole.class);
 
     private final IO io;
+    
     private final ConsoleReader reader;
 
     public JLineConsole(final IO io) throws IOException {

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/SimpleConsole.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/SimpleConsole.java?rev=410204&r1=410203&r2=410204&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/SimpleConsole.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/console/SimpleConsole.java
Tue May 30 00:26:34 2006
@@ -33,6 +33,7 @@
     private static final Log log = LogFactory.getLog(SimpleConsole.class);
 
     private final IO io;
+    
     private final BufferedReader reader;
 
     public SimpleConsole(final IO io) {

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/console/InteractiveConsoleTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/console/InteractiveConsoleTest.java?rev=410204&r1=410203&r2=410204&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/console/InteractiveConsoleTest.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/console/InteractiveConsoleTest.java
Tue May 30 00:26:34 2006
@@ -28,7 +28,7 @@
 {
     public void testConstructorArgs() throws Exception {
         try {
-            new InteractiveConsole(null, null);
+            new InteractiveConsole(null, null, null);
             fail("Accepted null value");
         }
         catch (IllegalArgumentException expected) {
@@ -36,15 +36,25 @@
         }
 
         try {
-            new InteractiveConsole(new SimpleConsole(new IO()), null);
+            new InteractiveConsole(new SimpleConsole(new IO()), null, null);
             fail("Accepted null value");
         }
         catch (IllegalArgumentException expected) {
             // ignore
         }
 
-        //
-        // TODO: Check happy day
-        //
+        // Happy day
+        new InteractiveConsole(
+            new SimpleConsole(new IO()),
+            new InteractiveConsole.Executor() {
+                public Result execute(String line) throws Exception {
+                    return null;
+                }
+            },
+            new InteractiveConsole.Prompter() {
+                public String getPrompt() {
+                    return null;
+                }
+            });
     }
 }



Mime
View raw message