geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r573933 - in /geronimo/sandbox/gshell/trunk: gshell-cli/src/main/java/org/apache/geronimo/gshell/cli/ gshell-command-api/ gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/ gshell-command-api/src/test/resources/ gshell-com...
Date Sun, 09 Sep 2007 00:22:17 GMT
Author: jdillon
Date: Sat Sep  8 17:22:15 2007
New Revision: 573933

URL: http://svn.apache.org/viewvc?rev=573933&view=rev
Log:
Hooking up IO's verbosity to cli flags
Added cli options to control ansi and the terminal
Added color error handling muck
Hook up the ANSI RenderWriters for commands IO out & err for now
Other minor cruft

Added:
    geronimo/sandbox/gshell/trunk/gshell-command-api/src/test/resources/
    geronimo/sandbox/gshell/trunk/gshell-command-api/src/test/resources/org/
    geronimo/sandbox/gshell/trunk/gshell-command-api/src/test/resources/org/apache/
    geronimo/sandbox/gshell/trunk/gshell-command-api/src/test/resources/org/apache/geronimo/
    geronimo/sandbox/gshell/trunk/gshell-command-api/src/test/resources/org/apache/geronimo/gshell/
    geronimo/sandbox/gshell/trunk/gshell-command-api/src/test/resources/org/apache/geronimo/gshell/command/
    geronimo/sandbox/gshell/trunk/gshell-command-api/src/test/resources/org/apache/geronimo/gshell/command/descriptor/
Modified:
    geronimo/sandbox/gshell/trunk/gshell-cli/src/main/java/org/apache/geronimo/gshell/cli/Main.java
    geronimo/sandbox/gshell/trunk/gshell-command-api/pom.xml
    geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/IO.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/CommandLineBuilder.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ErrorNotification.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExitNotification.java
    geronimo/sandbox/gshell/trunk/gshell-maven-plugin/pom.xml

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=573933&r1=573932&r2=573933&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
Sat Sep  8 17:22:15 2007
@@ -19,12 +19,17 @@
 
 package org.apache.geronimo.gshell.cli;
 
+import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
 
 import jline.Terminal;
+import org.apache.geronimo.gshell.ErrorNotification;
 import org.apache.geronimo.gshell.ExitNotification;
 import org.apache.geronimo.gshell.Shell;
+import org.apache.geronimo.gshell.ansi.ANSI;
 import org.apache.geronimo.gshell.ansi.Renderer;
 import org.apache.geronimo.gshell.clp.Argument;
 import org.apache.geronimo.gshell.clp.CommandLineProcessor;
@@ -63,6 +68,9 @@
 
     private final StopWatch watch = new StopWatch(true);
 
+    // Late initialized
+    private Logger log;
+
     public Main(final ClassWorld classWorld) {
         assert classWorld != null;
 
@@ -86,6 +94,7 @@
     private void setDebug(boolean flag) {
         if (flag) {
             setConsoleLogLevel("DEBUG");
+            io.setVerbosity(IO.Verbosity.DEBUG);
         }
     }
 
@@ -93,6 +102,7 @@
     private void setVerbose(boolean flag) {
         if (flag) {
             setConsoleLogLevel("INFO");
+            io.setVerbosity(IO.Verbosity.VERBOSE);
         }
     }
 
@@ -100,6 +110,7 @@
     private void setQuiet(boolean flag) {
         if (flag) {
             setConsoleLogLevel("ERROR");
+            io.setVerbosity(IO.Verbosity.QUIET);
         }
     }
 
@@ -129,52 +140,105 @@
         System.setProperty(name, value);
     }
 
-    public void run(final String[] args) throws Exception {
-        assert args != null;
+    @Option(name="-C", aliases={"--color"}, argumentRequired=false, description="Enable or
disable use of ANSI colors")
+    private void enableAnsiColors(final boolean flag) {
+        ANSI.setEnabled(flag);
+    }
 
-        // Default is to be quiet
-        setConsoleLogLevel("WARN");
-        
-        CommandLineProcessor clp = new CommandLineProcessor(this);
-        clp.setStopAtNonOption(true);
-        clp.process(args);
+    @Option(name="-T", aliases={"--terminal"}, metaVar="TYPE", argumentRequired=true, description="Specify
the terminal TYPE to use")
+    private void setTerminalType(String type) {
+        type = type.toLowerCase();
 
-        if (help) {
-            io.out.println(System.getProperty("program.name", "gshell") + " [options] <command>
[args]");
-            io.out.println();
+        if ("unix".equals(type)) {
+            type = jline.UnixTerminal.class.getName();
+        }
+        else if ("win".equals(type) || "windows".equals("type")) {
+            type = jline.WindowsTerminal.class.getName();
+        }
+        else if ("false".equals(type) || "off".equals(type) || "none".equals(type)) {
+            type = jline.UnsupportedTerminal.class.getName();
 
-            Printer printer = new Printer(clp);
-            printer.printUsage(io.out);
+            //
+            // HACK: Disable ANSI, for some reason UnsupportedTerminal reports ANSI as enabled,
when it shouldn't
+            //
+            ANSI.setEnabled(false);
+        }
 
-            io.out.println();
-            io.out.flush();
+        System.setProperty("jline.terminal", type);
+    }
 
-            System.exit(0);
+    private File getUserStateDirectory() {
+        File userHome = new File(System.getProperty("user.home"));
+        File dir = new File(userHome, ".gshell");
+
+        try {
+            return dir.getCanonicalFile();
+        }
+        catch (IOException e) {
+            throw new RuntimeException(e);
         }
+    }
 
-        if (version) {
-            io.out.println(Version.getInstance());
-            io.out.println();
-            io.out.flush();
+    private void loadUserScript(final String fileName) {
+        assert fileName != null;
 
-            System.exit(0);
-        }
+        File file = new File(getUserStateDirectory(), fileName);
 
-        int code;
-        
-        try {
-            code = execute(this.args.toArray(new String[this.args.size()]));
+        if (file.exists()) {
+            //
+            // TODO: Run the 'source' comamnd for the filename
+            //
+
+            log.debug("Loading user-script: {}", file);
         }
-        finally {
-            io.flush();
+    }
+
+    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() + "|");
         
-        System.exit(code);
-    }
+        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);
+            }
+        }
+    }
+    
     private int execute(final String[] args) throws Exception {
         // Its okay to use logging now
-        final Logger log = LoggerFactory.getLogger(Main.class);
+        log = LoggerFactory.getLogger(getClass());
 
         // Boot up the container
         ContainerConfiguration config = new DefaultContainerConfiguration();
@@ -199,71 +263,102 @@
         log.debug("  Echo: {}", term.getEcho());
         log.debug("  ANSI: {} ", term.isANSISupported());
 
+        if (term instanceof jline.WindowsTerminal) {
+            log.debug("  Direct: {}", ((jline.WindowsTerminal)term).getDirectConsole());
+        }
+        
         log.debug("Started in {}", watch);
 
-        Object result = null;
-
-        //
-        // TODO: Pass interactive flags (maybe as property) so gshell knows what modfooe
it is
-        //
+        int code = 0;
 
-        if (commands != null) {
-            shell.execute(commands);
-        }
-        else if (interactive) {
-            log.debug("Starting interactive console");
+        try {
+            //
+            // TODO: Load user profile here
+            //
+
+            //
+            // TODO: Pass interactive flags (maybe as property) so gshell knows what modfooe
it is
+            //
 
-            IO io = shell.getIO();
+            Object result = null;
             
-            Console.Executor executor = new Console.Executor() {
-                public Result execute(String line) throws Exception {
-                    try {
-                        /* Object result =*/ shell.execute(line);
-                    }
-                    catch (ExitNotification n) {
-                        return Result.STOP;
-                    }
+            if (commands != null) {
+                shell.execute(commands);
+            }
+            else if (interactive) {
+                log.debug("Starting interactive console");
 
-                    return Result.CONTINUE;
-                }
-            };
 
-            JLineConsole runner = new JLineConsole(executor, io);
+                //
+                // TODO: Load user rc here
+                //
+
+                IO io = shell.getIO();
+
+                Console.Executor executor = new Console.Executor() {
+                    public Result execute(String line) throws Exception {
+                        try {
+                            /* Object result =*/ shell.execute(line);
+                        }
+                        catch (ExitNotification n) {
+                            return Result.STOP;
+                        }
 
-            runner.setPrompter(new Console.Prompter() {
-                Renderer renderer = new Renderer();
+                        return Result.CONTINUE;
+                    }
+                };
 
-                public String prompt() {
-                    return renderer.render("@|bold gsh| > ");
-                }
-            });
+                JLineConsole runner = new JLineConsole(executor, io);
+
+                runner.setPrompter(new Console.Prompter() {
+                    Renderer renderer = new Renderer();
+
+                    public String prompt() {
+                        return renderer.render("@|bold gsh| > ");
+                    }
+                });
 
+                runner.setErrorHandler(new Console.ErrorHandler() {
+                    public Result handleError(final Throwable error) {
+                        displayError(error);
+                        return Result.CONTINUE;
+                    }
+                });
 
-            runner.setErrorHandler(new Console.ErrorHandler() {
-                public Result handleError(Throwable error) {
-                    // Must use string concat here to get the error passed in as a Throwable
for rendering
-                    log.error("Execution failed: " + error, error);
-                    
-                    return Result.CONTINUE;
+                //
+                // TODO: Setup history here
+                //
+
+                // Check if there are args, and run them and then enter interactive
+                if (args.length != 0) {
+                    shell.execute(args);
                 }
-            });
 
-            // Check if there are args, and run them and then enter interactive
-            if (args.length != 0) {
-                shell.execute(args);
+                //
+                // TODO: Display the banner here
+                //
+
+                runner.run();
+            }
+            else {
+                result = shell.execute(args);
             }
 
-            runner.run();
-        }
-        else {
-            result = shell.execute(args);
+            // If the result is a number, then pass that back to the calling shell
+            if (result instanceof Number) {
+                code = ((Number)result).intValue();
+            }
         }
+        catch (ExitNotification n) {
+            log.debug("Exiting w/code: {}", n.code);
 
-        // If the result is a number, then pass that back to the calling shell
-        int code = 0;
-        
-        if (result instanceof Number) {
-            code = ((Number)result).intValue();
+            code = n.code;
+        }
+        catch (Throwable t) {
+            io.err.println("FATAL: " + t);
+            t.printStackTrace(io.err);
+            
+            code = 1;
         }
 
         log.debug("Exiting with code: {}, after running for: {}", code, watch);
@@ -275,6 +370,63 @@
     // Bootstrap
     //
 
+    public void run(final String[] args) throws Exception {
+        assert args != null;
+
+        // Default is to be quiet
+        setConsoleLogLevel("WARN");
+
+        CommandLineProcessor clp = new CommandLineProcessor(this);
+        clp.setStopAtNonOption(true);
+        clp.process(args);
+
+        //
+        // TODO: Use methods to handle these...
+        //
+        
+        if (help) {
+            io.out.println(System.getProperty("program.name", "gshell") + " [options] <command>
[args]");
+            io.out.println();
+
+            Printer printer = new Printer(clp);
+            printer.printUsage(io.out);
+
+            io.out.println();
+            io.out.flush();
+
+            System.exit(0);
+        }
+
+        if (version) {
+            io.out.println(Version.getInstance());
+            io.out.println();
+            io.out.flush();
+
+            System.exit(0);
+        }
+
+        final AtomicReference<Integer> codeRef = new AtomicReference<Integer>();
+
+        Runtime.getRuntime().addShutdownHook(new Thread("GShell Shutdown Hook") {
+            public void run() {
+                if (codeRef.get() == null) {
+                    // Give the user a warning when the JVM shutdown abnormally, normal shutdown
+                    // will set an exit code through the proper channels
+
+                    io.err.println();
+                    io.err.println("WARNING: Abnormal JVM shutdown detected");
+                }
+
+                io.flush();
+            }
+        });
+
+        int code = execute(this.args.toArray(new String[this.args.size()]));
+        codeRef.set(code);
+
+        System.exit(code);
+    }
+    
     public static void main(final String[] args, final ClassWorld world) throws Exception
{
         Main main = new Main(world);
         main.run(args);

Modified: geronimo/sandbox/gshell/trunk/gshell-command-api/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-command-api/pom.xml?rev=573933&r1=573932&r2=573933&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-command-api/pom.xml (original)
+++ geronimo/sandbox/gshell/trunk/gshell-command-api/pom.xml Sat Sep  8 17:22:15 2007
@@ -46,6 +46,12 @@
 
         <dependency>
             <groupId>org.apache.geronimo.gshell.support</groupId>
+            <artifactId>gshell-ansi</artifactId>
+            <version>${pom.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.geronimo.gshell.support</groupId>
             <artifactId>gshell-i18n</artifactId>
             <version>${pom.version}</version>
         </dependency>

Modified: geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/IO.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/IO.java?rev=573933&r1=573932&r2=573933&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/IO.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-command-api/src/main/java/org/apache/geronimo/gshell/command/IO.java
Sat Sep  8 17:22:15 2007
@@ -26,6 +26,8 @@
 import java.io.PrintWriter;
 import java.io.Reader;
 
+import org.apache.geronimo.gshell.ansi.RenderWriter;
+
 /**
  * Container for input/output handles.
  *
@@ -92,8 +94,17 @@
         this.errorStream = err;
 
         this.in = new InputStreamReader(in);
-        this.out = new PrintWriter(out, true);
-        this.err = new PrintWriter(err, true);
+
+        //
+        // TODO: Once all user output is in i18n, then it would be more efficent to have
the MessageSource
+        //       be ANSI-aware instead of this...
+        //
+
+        this.out = new RenderWriter(outputStream, true);
+        this.err = new RenderWriter(errorStream, true);
+        
+        // this.out = new PrintWriter(out, true);
+        // this.err = new PrintWriter(err, true);
     }
 
     /**
@@ -122,6 +133,13 @@
         assert verbosity != null;
         
         this.verbosity = verbosity;
+    }
+
+    /**
+     * Returns the verbosity level.
+     */
+    public Verbosity getVerbosity() {
+        return verbosity;
     }
 
     /**

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/CommandLineBuilder.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/CommandLineBuilder.java?rev=573933&r1=573932&r2=573933&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/CommandLineBuilder.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/CommandLineBuilder.java
Sat Sep  8 17:22:15 2007
@@ -84,11 +84,6 @@
         
         return new CommandLine() {
             public Object execute() throws Exception {
-
-                //
-                // TODO: Handle ErrorNotification
-                //
-
                 return root.jjtAccept(visitor, null);
             }
         };

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ErrorNotification.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ErrorNotification.java?rev=573933&r1=573932&r2=573933&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ErrorNotification.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ErrorNotification.java
Sat Sep  8 17:22:15 2007
@@ -31,15 +31,15 @@
 {
     ///CLOVER:OFF
 
-    public ErrorNotification(String msg) {
+    public ErrorNotification(final String msg) {
         super(msg);
     }
 
-    public ErrorNotification(String msg, Throwable cause) {
+    public ErrorNotification(final String msg, final Throwable cause) {
         super(msg, cause);
     }
 
-    public ErrorNotification(Throwable cause) {
+    public ErrorNotification(final Throwable cause) {
         super(cause);
     }
 

Modified: 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=573933&r1=573932&r2=573933&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExitNotification.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExitNotification.java
Sat Sep  8 17:22:15 2007
@@ -33,7 +33,7 @@
 
     public static final int DEFAULT_CODE = 0;
 
-    private final int code;
+    public final int code;
 
     public ExitNotification(final int code) {
         this.code = code;
@@ -41,10 +41,6 @@
 
     public ExitNotification() {
         this(DEFAULT_CODE);
-    }
-
-    public int getCode() {
-        return code;
     }
 
     public static void exit(final int code) {

Modified: geronimo/sandbox/gshell/trunk/gshell-maven-plugin/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-maven-plugin/pom.xml?rev=573933&r1=573932&r2=573933&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-maven-plugin/pom.xml (original)
+++ geronimo/sandbox/gshell/trunk/gshell-maven-plugin/pom.xml Sat Sep  8 17:22:15 2007
@@ -33,7 +33,7 @@
     <packaging>maven-plugin</packaging>
 
     <description>
-        Provides build integration wtih GShell.
+        Provides build integration with GShell.
     </description>
 
     <dependencies>



Mime
View raw message