You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2008/06/11 21:17:01 UTC

svn commit: r666796 [2/2] - in /geronimo/gshell/trunk: ./ gshell-assembly/src/main/underlay/etc/ gshell-core/ gshell-plugin/src/main/java/org/apache/geronimo/gshell/plugin/descriptor/ gshell-rapture/ gshell-rapture/src/ gshell-rapture/src/main/java/org...

Added: geronimo/gshell/trunk/gshell-rapture/src/main/java/org/apache/geronimo/gshell/rapture/VariableInterpolator.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-rapture/src/main/java/org/apache/geronimo/gshell/rapture/VariableInterpolator.java?rev=666796&view=auto
==============================================================================
--- geronimo/gshell/trunk/gshell-rapture/src/main/java/org/apache/geronimo/gshell/rapture/VariableInterpolator.java (added)
+++ geronimo/gshell/trunk/gshell-rapture/src/main/java/org/apache/geronimo/gshell/rapture/VariableInterpolator.java Wed Jun 11 12:17:00 2008
@@ -0,0 +1,173 @@
+/*
+ * 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.rapture;
+
+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.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;
+
+/**
+ * Provides interpolation for shell variables using Jexl.
+ * 
+ * Still using Jexl here for now, since it can handle expression like <tt>${env.TERM}</tt>
+ * (where <tt>env</tt> is a variable bound to a map, ...).
+ * 
+ * @version $Rev$ $Date$
+ */
+public class VariableInterpolator
+{
+    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;
+
+        // If there is no $ in the expression, then skip the interpolation muck to speed things up
+        if (input.indexOf('$') == -1) {
+            return input;
+        }
+
+        Interpolator interp = createInterpolator(vars);
+
+        log.trace("Interpolating: {}", input);
+
+        String result;
+        try {
+            result = interp.interpolate(input);
+        }
+        catch (InterpolationException e) {
+            throw new ErrorNotification("Failed to interpolate expression: " + input, e);
+        }
+
+        log.trace("Iterpolated result: {}", result);
+
+        return result;
+    }
+
+    private Interpolator createInterpolator(final Variables vars) {
+        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);
+                }
+            }
+        });
+
+        return interp;
+    }
+}
\ No newline at end of file

Propchange: geronimo/gshell/trunk/gshell-rapture/src/main/java/org/apache/geronimo/gshell/rapture/VariableInterpolator.java
------------------------------------------------------------------------------
    svn:eol-style = native

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

Propchange: geronimo/gshell/trunk/gshell-rapture/src/main/java/org/apache/geronimo/gshell/rapture/VariableInterpolator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/gshell/trunk/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/pom.xml?rev=666796&r1=666795&r2=666796&view=diff
==============================================================================
--- geronimo/gshell/trunk/pom.xml (original)
+++ geronimo/gshell/trunk/pom.xml Wed Jun 11 12:17:00 2008
@@ -372,12 +372,6 @@
 
             <dependency>
                 <groupId>org.apache.geronimo.gshell</groupId>
-                <artifactId>gshell-core</artifactId>
-                <version>1.0-alpha-2-SNAPSHOT</version>
-            </dependency>
-
-            <dependency>
-                <groupId>org.apache.geronimo.gshell</groupId>
                 <artifactId>gshell-bootstrap</artifactId>
                 <version>1.0-alpha-2-SNAPSHOT</version>
             </dependency>
@@ -459,7 +453,13 @@
                 <artifactId>gshell-vfs</artifactId>
                 <version>1.0-alpha-2-SNAPSHOT</version>
             </dependency>
-
+            
+            <dependency>
+                <groupId>org.apache.geronimo.gshell</groupId>
+                <artifactId>gshell-rapture</artifactId>
+                <version>1.0-alpha-2-SNAPSHOT</version>
+            </dependency>
+            
             <dependency>
                 <groupId>org.apache.geronimo.gshell</groupId>
                 <artifactId>gshell-diet-log4j</artifactId>
@@ -491,7 +491,7 @@
         <module>gshell-plugin</module>
         <module>gshell-parser</module>
         <module>gshell-layout</module>
-        <module>gshell-core</module>
+        <module>gshell-rapture</module>
         <module>gshell-cli</module>
         <module>gshell-commands</module>
         <module>gshell-remote</module>