geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r702444 - in /geronimo/gshell/trunk: gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/ gshell-support/gshell-interpolation/src/main/java/org/apache/geronimo/gshell/interpolation/ gshell-wisdom/gshel...
Date Tue, 07 Oct 2008 11:54:28 GMT
Author: jdillon
Date: Tue Oct  7 04:54:27 2008
New Revision: 702444

URL: http://svn.apache.org/viewvc?rev=702444&view=rev
Log:
Implement (again finally) a variable configurable prompt, a few wee hacks to make it work
Use io.outputStream for set output to avoid any unwanted translation (like ansi stuff)

Added:
    geronimo/gshell/trunk/gshell-support/gshell-interpolation/src/main/java/org/apache/geronimo/gshell/interpolation/VariablesValueSource.java
  (with props)
Modified:
    geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/SetAction.java
    geronimo/gshell/trunk/gshell-support/gshell-interpolation/src/main/java/org/apache/geronimo/gshell/interpolation/VariableInterpolator.java
    geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/shell/ConsolePrompterImpl.java
    geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/shell/ShellImpl.java

Modified: geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/SetAction.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/SetAction.java?rev=702444&r1=702443&r2=702444&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/SetAction.java
(original)
+++ geronimo/gshell/trunk/gshell-commands/gshell-builtins/src/main/java/org/apache/geronimo/gshell/commands/builtins/SetAction.java
Tue Oct  7 04:54:27 2008
@@ -93,6 +93,8 @@
         assert context != null;
         IO io = context.getIo();
 
+        // NOTE: Using io.outputStream to display values to avoid any ANSI encoding or other
translation.
+        
         switch (mode) {
             case PROPERTY: {
                 Properties props = System.getProperties();
@@ -101,12 +103,14 @@
                     String name = (String) o;
                     String value = props.getProperty(name);
 
-                    io.out.print(name);
-                    io.out.print("=");
-                    io.out.print(value);
+                    io.outputStream.print(name);
+                    io.outputStream.print("='");
+                    io.outputStream.print(value);
+                    io.outputStream.print("'");
+
                     // Value is always a string, so no need to add muck here for --verbose
 
-                    io.out.println();
+                    io.outputStream.println();
                 }
                 break;
             }
@@ -117,25 +121,36 @@
 
                 while (iter.hasNext()) {
                     String name = iter.next();
+
+                    // HACK: Hide some internal muck for now
+                    if (name.startsWith("SHELL")) {
+                        continue;
+                    }
+
                     Object value = variables.get(name);
 
-                    io.out.print(name);
-                    io.out.print("=");
-                    io.out.print(value);
+                    io.outputStream.print(name);
+                    io.outputStream.print("='");
+                    io.outputStream.print(value);
+                    io.outputStream.flush();
+                    io.outputStream.print("'");
 
                     // When --verbose include the class details of the value
                     if (verbose && value != null) {
-                        io.out.print(" (");
-                        io.out.print(value.getClass());
-                        io.out.print(")");
+                        io.outputStream.print(" (");
+                        io.outputStream.print(value.getClass());
+                        io.outputStream.print(")");
                     }
 
-                    io.out.println();
+                    io.outputStream.println();
                 }
                 break;
             }
         }
 
+        // Manually flush the stream, normally framework only flushes io.out
+        io.outputStream.flush();
+
         return Result.SUCCESS;
     }
 }

Modified: geronimo/gshell/trunk/gshell-support/gshell-interpolation/src/main/java/org/apache/geronimo/gshell/interpolation/VariableInterpolator.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-support/gshell-interpolation/src/main/java/org/apache/geronimo/gshell/interpolation/VariableInterpolator.java?rev=702444&r1=702443&r2=702444&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-support/gshell-interpolation/src/main/java/org/apache/geronimo/gshell/interpolation/VariableInterpolator.java
(original)
+++ geronimo/gshell/trunk/gshell-support/gshell-interpolation/src/main/java/org/apache/geronimo/gshell/interpolation/VariableInterpolator.java
Tue Oct  7 04:54:27 2008
@@ -19,24 +19,14 @@
 
 package org.apache.geronimo.gshell.interpolation;
 
-import org.apache.commons.jexl.Expression;
-import org.apache.commons.jexl.ExpressionFactory;
-import org.apache.commons.jexl.JexlContext;
-import org.apache.commons.jexl.resolver.FlatResolver;
 import org.apache.geronimo.gshell.command.Variables;
 import org.apache.geronimo.gshell.notification.ErrorNotification;
 import org.codehaus.plexus.interpolation.InterpolationException;
 import org.codehaus.plexus.interpolation.Interpolator;
 import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
-import org.codehaus.plexus.interpolation.ValueSource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.Collection;
-import java.util.Map;
-import java.util.Set;
-import java.util.List;
-
 /**
  * Provides interpolation for shell variables using Jexl.
  *
@@ -49,8 +39,6 @@
 {
     private final Logger log = LoggerFactory.getLogger(getClass());
 
-    private final FlatResolver resolver = new FlatResolver(true);
-
     public String interpolate(final String input, final Variables vars) {
         assert input != null;
         assert vars != null;
@@ -78,109 +66,10 @@
     }
 
     private Interpolator createInterpolator(final Variables vars) {
+        assert vars != null;
+        
         Interpolator interp = new RegexBasedInterpolator();
-
-        // This complex crap here is to adapt our Variables to a JexlContext w/the least
overhead
-        interp.addValueSource(new ValueSource()
-        {
-            final Map map = new Map() {
-                private String key(final Object key) {
-                    return String.valueOf(key);
-                }
-
-                public Object get(final Object key) {
-                    return vars.get(key(key));
-                }
-
-                public Object put(final Object key, final Object value) {
-                    Object prev = vars.get(key(key));
-
-                    vars.set(key(key), value);
-
-                    return prev;
-                }
-
-                // Jexl does not use any of these Map methods
-
-                public int size() {
-                    throw new UnsupportedOperationException();
-                }
-
-                public boolean isEmpty() {
-                    throw new UnsupportedOperationException();
-                }
-
-                public boolean containsKey(Object key) {
-                    throw new UnsupportedOperationException();
-                }
-
-                public boolean containsValue(Object value) {
-                    throw new UnsupportedOperationException();
-                }
-
-                public Object remove(Object key) {
-                    throw new UnsupportedOperationException();
-                }
-
-                public void putAll(Map t) {
-                    throw new UnsupportedOperationException();
-                }
-
-                public void clear() {
-                    throw new UnsupportedOperationException();
-                }
-
-                public Set keySet() {
-                    throw new UnsupportedOperationException();
-                }
-
-                public Collection values() {
-                    throw new UnsupportedOperationException();
-                }
-
-                public Set entrySet() {
-                    throw new UnsupportedOperationException();
-                }
-            };
-
-            final JexlContext jc = new JexlContext()
-            {
-                public Map getVars() {
-                    return map;
-                }
-
-                // Jexl never calls setVars
-
-                public void setVars(Map map) {
-                    throw new UnsupportedOperationException();
-                }
-            };
-
-            public Object getValue(final String s) {
-                try {
-                    Expression expr = ExpressionFactory.createExpression(s);
-                    expr.addPreResolver(resolver);
-
-                    return expr.evaluate(jc);
-                }
-                catch (Exception e) {
-                    throw new ErrorNotification("Failed to evaluate expression: " + s, e);
-                }
-            }
-
-            //
-            // TODO: See what this is supposed to return...
-            //
-
-            public List getFeedback() {
-                return null;
-            }
-
-            public void clearFeedback() {
-                // ???
-            }
-        });
-
+        interp.addValueSource(new VariablesValueSource(vars));
         return interp;
     }
 }
\ No newline at end of file

Added: geronimo/gshell/trunk/gshell-support/gshell-interpolation/src/main/java/org/apache/geronimo/gshell/interpolation/VariablesValueSource.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-support/gshell-interpolation/src/main/java/org/apache/geronimo/gshell/interpolation/VariablesValueSource.java?rev=702444&view=auto
==============================================================================
--- geronimo/gshell/trunk/gshell-support/gshell-interpolation/src/main/java/org/apache/geronimo/gshell/interpolation/VariablesValueSource.java
(added)
+++ geronimo/gshell/trunk/gshell-support/gshell-interpolation/src/main/java/org/apache/geronimo/gshell/interpolation/VariablesValueSource.java
Tue Oct  7 04:54:27 2008
@@ -0,0 +1,163 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.interpolation;
+
+import org.apache.commons.jexl.Expression;
+import org.apache.commons.jexl.ExpressionFactory;
+import org.apache.commons.jexl.JexlContext;
+import org.apache.commons.jexl.resolver.FlatResolver;
+import org.apache.geronimo.gshell.command.Variables;
+import org.apache.geronimo.gshell.notification.ErrorNotification;
+import org.codehaus.plexus.interpolation.AbstractValueSource;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * {@link org.codehaus.plexus.interpolation.ValueSource} implementation pulling data from
a {@link Variables} instance.
+ *
+ * @version $Rev$ $Date$
+ */
+public class VariablesValueSource
+    extends AbstractValueSource
+{
+    private final FlatResolver resolver = new FlatResolver(true);
+
+    private final JexlContext jexlContext;
+
+    private Variables variables;
+
+    public VariablesValueSource(final Variables vars) {
+        super(false);
+
+        jexlContext = new JexlContext() {
+            private final Map map = new VariablesMapAdapter();
+
+            public Map getVars() {
+                return map;
+            }
+
+            public void setVars(Map map) {
+                throw new UnsupportedOperationException();
+            }
+        };
+
+        if (vars != null) {
+            setVariables(vars);
+        }
+    }
+
+    public VariablesValueSource() {
+        this(null);
+    }
+    
+    public Variables getVariables() {
+        return variables;
+    }
+
+    public void setVariables(final Variables variables) {
+        this.variables = variables;
+    }
+
+    public Object getValue(final String input) {
+        assert input != null;
+
+        if (variables == null) {
+            throw new IllegalStateException("Variables have not yet been set");
+        }
+        
+        try {
+            Expression expr = ExpressionFactory.createExpression(input);
+            expr.addPreResolver(resolver);
+
+            return expr.evaluate(jexlContext);
+        }
+        catch (Exception e) {
+            throw new ErrorNotification("Failed to evaluate expression: " + input, e);
+        }
+    }
+
+    //
+    // VariablesMapAdapter
+    //
+
+    private class VariablesMapAdapter
+        implements Map
+    {
+        private String key(final Object key) {
+            return String.valueOf(key);
+        }
+
+        public Object get(final Object key) {
+            return variables.get(key(key));
+        }
+
+        public Object put(final Object key, final Object value) {
+            Object prev = variables.get(key(key));
+
+            variables.set(key(key), value);
+
+            return prev;
+        }
+
+        // Jexl does not use any of these Map methods
+
+        public int size() {
+            throw new UnsupportedOperationException();
+        }
+
+        public boolean isEmpty() {
+            throw new UnsupportedOperationException();
+        }
+
+        public boolean containsKey(Object key) {
+            throw new UnsupportedOperationException();
+        }
+
+        public boolean containsValue(Object value) {
+            throw new UnsupportedOperationException();
+        }
+
+        public Object remove(Object key) {
+            throw new UnsupportedOperationException();
+        }
+
+        public void putAll(Map t) {
+            throw new UnsupportedOperationException();
+        }
+
+        public void clear() {
+            throw new UnsupportedOperationException();
+        }
+
+        public Set keySet() {
+            throw new UnsupportedOperationException();
+        }
+
+        public Collection values() {
+            throw new UnsupportedOperationException();
+        }
+
+        public Set entrySet() {
+            throw new UnsupportedOperationException();
+        }
+    }
+}
\ No newline at end of file

Propchange: geronimo/gshell/trunk/gshell-support/gshell-interpolation/src/main/java/org/apache/geronimo/gshell/interpolation/VariablesValueSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/gshell/trunk/gshell-support/gshell-interpolation/src/main/java/org/apache/geronimo/gshell/interpolation/VariablesValueSource.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/gshell/trunk/gshell-support/gshell-interpolation/src/main/java/org/apache/geronimo/gshell/interpolation/VariablesValueSource.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/shell/ConsolePrompterImpl.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/shell/ConsolePrompterImpl.java?rev=702444&r1=702443&r2=702444&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/shell/ConsolePrompterImpl.java
(original)
+++ geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/shell/ConsolePrompterImpl.java
Tue Oct  7 04:54:27 2008
@@ -19,15 +19,21 @@
 
 package org.apache.geronimo.gshell.wisdom.shell;
 
-import org.apache.geronimo.gshell.ansi.Code;
 import org.apache.geronimo.gshell.ansi.Renderer;
 import org.apache.geronimo.gshell.application.Application;
+import org.apache.geronimo.gshell.command.Variables;
 import org.apache.geronimo.gshell.console.Console;
-import org.apache.geronimo.gshell.model.application.Branding;
+import org.apache.geronimo.gshell.interpolation.VariablesValueSource;
+import org.codehaus.plexus.interpolation.InterpolationException;
+import org.codehaus.plexus.interpolation.Interpolator;
+import org.codehaus.plexus.interpolation.PrefixedObjectValueSource;
+import org.codehaus.plexus.interpolation.StringSearchInterpolator;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import javax.annotation.PostConstruct;
+
 /**
  * {@link Console.Prompter} component.
  *
@@ -41,27 +47,55 @@
     @Autowired
     private Application application;
 
-    private Renderer renderer = new Renderer();
+    private final Interpolator interp = new StringSearchInterpolator("%{", "}");
+
+    private final VariablesValueSource variablesValueSource = new VariablesValueSource();
+
+    private final Renderer renderer = new Renderer();
 
-    //
-    // TODO: Need to create a PatternPrompter, which can use interpolation of a variable
to render the prompt
-    //       so the following variable value would set the same prompt as we are hardcoding
here:
-    //
-    //    set gshell.prompt="@|bold ${application.username}|@${application.localHost.hostName}:@|bold
${application.branding.name}|> "
-    //
+    @PostConstruct
+    public void init() {
+        assert application != null;
+        interp.addValueSource(new PrefixedObjectValueSource("application", application));
+        interp.addValueSource(new PrefixedObjectValueSource("branding", application.getModel().getBranding()));
+        interp.addValueSource(variablesValueSource);
+    }
 
     public String prompt() {
+        String prompt = null;
+
         assert application != null;
-        Branding branding = application.getModel().getBranding();
+        Variables vars = application.getVariables();
+        String pattern = (String) vars.get("prompt");
+
+        if (pattern != null) {
+            assert variablesValueSource != null;
+            variablesValueSource.setVariables(vars);
+
+            try {
+                assert interp != null;
+                prompt = interp.interpolate(pattern);
+            }
+            catch (InterpolationException e) {
+                log.error("Failed to render prompt pattern: " + pattern, e);
+            }
+        }
+
+        // Use a default prompt if we don't have anything here
+        if (prompt == null) {
+            prompt = defaultPrompt();
+        }
+
+        // Encode ANSI muck if it looks like there are codes encoded
+        if (Renderer.test(prompt)) {
+            prompt = renderer.render(prompt);
+        }
 
-        StringBuilder buff = new StringBuilder();
-        buff.append(Renderer.encode(application.getUserName(), Code.BOLD));
-        buff.append("@");
-        buff.append(application.getLocalHost().getHostName());
-        buff.append(":");
-        buff.append(Renderer.encode(branding.getName(), Code.BOLD));
-        buff.append("> ");
+        return prompt;
+    }
 
-        return renderer.render(buff.toString());
+    private String defaultPrompt() {
+        assert application != null;
+        return application.getModel().getBranding().getName() + "> ";
     }
 }
\ No newline at end of file

Modified: geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/shell/ShellImpl.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/shell/ShellImpl.java?rev=702444&r1=702443&r2=702444&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/shell/ShellImpl.java
(original)
+++ geronimo/gshell/trunk/gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/shell/ShellImpl.java
Tue Oct  7 04:54:27 2008
@@ -123,6 +123,9 @@
         // HACK: Add history for the 'history' command, since its not part of the Shell interf
it can't really access it easy, resolve with ^^^
         vars.set("SHELL.HISTORY", getHistory(), true);
 
+        // HACK: Set the default prompt here for now, probably want to get this from branding
+        vars.set("prompt", "@|bold %{application.userName}|@%{application.localHost.hostName}:@|bold
%{branding.name}|> ");
+        
         branding = application.getModel().getBranding();
 
         opened = true;



Mime
View raw message