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/03 16:51:17 UTC
svn commit: r662816 - in /geronimo/gshell/trunk: gshell-core/
gshell-core/src/main/java/org/apache/geronimo/gshell/ gshell-support/
gshell-support/gshell-expr/
Author: jdillon
Date: Tue Jun 3 07:51:16 2008
New Revision: 662816
URL: http://svn.apache.org/viewvc?rev=662816&view=rev
Log:
(GSHELL-112) Ripped out gshell-expr and replaced with VariableInterpolator
Added:
geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/VariableInterpolator.java (contents, props changed)
- copied, changed from r662782, geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExecutingVisitor.java
Removed:
geronimo/gshell/trunk/gshell-support/gshell-expr/
Modified:
geronimo/gshell/trunk/gshell-core/pom.xml
geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExecutingVisitor.java
geronimo/gshell/trunk/gshell-support/pom.xml
Modified: geronimo/gshell/trunk/gshell-core/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-core/pom.xml?rev=662816&r1=662815&r2=662816&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-core/pom.xml (original)
+++ geronimo/gshell/trunk/gshell-core/pom.xml Tue Jun 3 07:51:16 2008
@@ -44,11 +44,6 @@
</dependency>
<dependency>
- <groupId>org.apache.geronimo.gshell.support</groupId>
- <artifactId>gshell-expr</artifactId>
- </dependency>
-
- <dependency>
<groupId>org.apache.geronimo.gshell</groupId>
<artifactId>gshell-api</artifactId>
</dependency>
@@ -62,6 +57,22 @@
<groupId>org.apache.geronimo.gshell</groupId>
<artifactId>gshell-plugin</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-interpolation</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>commons-jexl</groupId>
+ <artifactId>commons-jexl</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>jcl104-over-slf4j</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
Modified: geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExecutingVisitor.java
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExecutingVisitor.java?rev=662816&r1=662815&r2=662816&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExecutingVisitor.java (original)
+++ geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExecutingVisitor.java Tue Jun 3 07:51:16 2008
@@ -20,10 +20,7 @@
package org.apache.geronimo.gshell;
import org.apache.geronimo.gshell.command.CommandExecutor;
-import org.apache.geronimo.gshell.command.Variables;
import org.apache.geronimo.gshell.common.Arguments;
-import org.apache.geronimo.gshell.expression.ExpressionEvaluator;
-import org.apache.geronimo.gshell.expression.JexlExpressionEvaluator;
import org.apache.geronimo.gshell.parser.ASTCommandLine;
import org.apache.geronimo.gshell.parser.ASTExpression;
import org.apache.geronimo.gshell.parser.ASTOpaqueString;
@@ -38,9 +35,6 @@
import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
-import java.util.HashMap;
-import java.util.Iterator;
/**
* Visitor which will execute command-lines as parsed.
@@ -56,6 +50,8 @@
private final CommandExecutor executor;
+ private final VariableInterpolator interp = new VariableInterpolator();
+
public ExecutingVisitor(final CommandExecutor executor, final Environment env) {
assert executor != null;
assert env != null;
@@ -119,47 +115,25 @@
return value;
}
+ public Object visit(final ASTQuotedString node, final Object data) {
+ assert node != null;
- private static Map convertToMap(final Variables vars) {
- assert vars != null;
-
- Map<String,Object> map = new HashMap<String,Object>();
- Iterator<String> iter = vars.names();
-
- while (iter.hasNext()) {
- String name = iter.next();
- map.put(name, vars.get(name));
- }
-
- return map;
- }
-
- private String evaluate(final String expr) {
- assert expr != null;
-
- ExpressionEvaluator evaluator = new JexlExpressionEvaluator(convertToMap(env.getVariables()));
+ String value = interp.interpolate(node.getValue(), env.getVariables());
- try {
- return evaluator.parse(expr);
- }
- catch (Exception e) {
- throw new ErrorNotification("Failed to evaluate expression: " + expr, e);
- }
+ return appendString(value, data);
}
- public Object visit(final ASTQuotedString node, final Object data) {
- String value = evaluate(node.getValue());
+ public Object visit(final ASTPlainString node, final Object data) {
+ assert node != null;
+ String value = interp.interpolate(node.getValue(), env.getVariables());
+
return appendString(value, data);
}
public Object visit(final ASTOpaqueString node, final Object data) {
- return appendString(node.getValue(), data);
- }
+ assert node != null;
- public Object visit(final ASTPlainString node, final Object data) {
- String value = evaluate(node.getValue());
-
- return appendString(value, data);
+ return appendString(node.getValue(), data);
}
}
Copied: geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/VariableInterpolator.java (from r662782, geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExecutingVisitor.java)
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/VariableInterpolator.java?p2=geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/VariableInterpolator.java&p1=geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExecutingVisitor.java&r1=662782&r2=662816&rev=662816&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/ExecutingVisitor.java (original)
+++ geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/VariableInterpolator.java Tue Jun 3 07:51:16 2008
@@ -19,147 +19,153 @@
package org.apache.geronimo.gshell;
-import org.apache.geronimo.gshell.command.CommandExecutor;
+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.common.Arguments;
-import org.apache.geronimo.gshell.expression.ExpressionEvaluator;
-import org.apache.geronimo.gshell.expression.JexlExpressionEvaluator;
-import org.apache.geronimo.gshell.parser.ASTCommandLine;
-import org.apache.geronimo.gshell.parser.ASTExpression;
-import org.apache.geronimo.gshell.parser.ASTOpaqueString;
-import org.apache.geronimo.gshell.parser.ASTPlainString;
-import org.apache.geronimo.gshell.parser.ASTProcess;
-import org.apache.geronimo.gshell.parser.ASTQuotedString;
-import org.apache.geronimo.gshell.parser.CommandLineParserVisitor;
-import org.apache.geronimo.gshell.parser.SimpleNode;
-import org.apache.geronimo.gshell.shell.Environment;
+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.ArrayList;
-import java.util.List;
+import java.util.Collection;
import java.util.Map;
-import java.util.HashMap;
-import java.util.Iterator;
+import java.util.Set;
/**
- * Visitor which will execute command-lines as parsed.
- *
+ * Provides interpolation for shell varibles.
+ *
* @version $Rev$ $Date$
*/
-public class ExecutingVisitor
- implements CommandLineParserVisitor
+public class VariableInterpolator
{
private final Logger log = LoggerFactory.getLogger(getClass());
- private final Environment env;
-
- private final CommandExecutor executor;
-
- public ExecutingVisitor(final CommandExecutor executor, final Environment env) {
- assert executor != null;
- assert env != null;
+ private final FlatResolver resolver = new FlatResolver(true);
- this.executor = executor;
- this.env = env;
- }
-
- public Object visit(final SimpleNode node, final Object data) {
- assert node != null;
-
- // It is an error if we forgot to implement a node handler
- throw new Error("Unhandled node type: " + node.getClass().getName());
- }
-
- public Object visit(final ASTCommandLine node, final Object data) {
- assert node != null;
+ public String interpolate(final String input, final Variables vars) {
+ assert input != null;
+ assert vars != null;
- //
- // NOTE: Visiting children will execute seperate commands in serial
- //
+ // If there is no $ in the expression, then skip the interpolation muck to speed things up
+ if (input.indexOf('$') == -1) {
+ return input;
+ }
- return node.childrenAccept(this, data);
- }
+ Interpolator interp = createInterpolator(vars);
- public Object visit(final ASTExpression node, final Object data) {
- assert node != null;
+ log.trace("Interpolating: {}", input);
- Object[][] commands = new Object[node.jjtGetNumChildren()][];
- for (int i = 0; i < node.jjtGetNumChildren(); i++) {
- ASTProcess proc = (ASTProcess) node.jjtGetChild(i);
- List<Object> list = new ArrayList<Object>(proc.jjtGetNumChildren());
- proc.childrenAccept(this, list);
- commands[i] = list.toArray(new Object[list.size()]);
- assert list.size() >= 1;
- }
+ String result;
try {
- return executor.execute(commands);
+ result = interp.interpolate(input);
}
- catch (Exception e) {
- String s = Arguments.asString(commands[0]);
- for (int i = 1; i < commands.length; i++) {
- s += " | " + Arguments.asString(commands[i]);
- }
- throw new ErrorNotification("Shell execution failed; commands=" + s, e);
+ catch (InterpolationException e) {
+ throw new ErrorNotification("Failed to interpolate expression: " + input, e);
}
- }
- public Object visit(ASTProcess node, Object data) {
- return null;
+ log.trace("Iterpolated result: {}", result);
+
+ return result;
}
- @SuppressWarnings({"unchecked"})
- private Object appendString(final String value, final Object data) {
- assert data != null;
- assert data instanceof List;
+ private Interpolator createInterpolator(final Variables vars) {
+ Interpolator interp = new RegexBasedInterpolator();
- List<Object> args = (List<Object>)data;
- args.add(value);
+ // 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);
+ }
- return value;
- }
+ 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));
- private static Map convertToMap(final Variables vars) {
- assert vars != null;
+ vars.set(key(key), value);
- Map<String,Object> map = new HashMap<String,Object>();
- Iterator<String> iter = vars.names();
+ return prev;
+ }
- while (iter.hasNext()) {
- String name = iter.next();
- map.put(name, vars.get(name));
- }
+ // Jexl does not use any of these Map methods
- return map;
- }
+ public int size() {
+ throw new UnsupportedOperationException();
+ }
- private String evaluate(final String expr) {
- assert expr != null;
-
- ExpressionEvaluator evaluator = new JexlExpressionEvaluator(convertToMap(env.getVariables()));
+ public boolean isEmpty() {
+ throw new UnsupportedOperationException();
+ }
- try {
- return evaluator.parse(expr);
- }
- catch (Exception e) {
- throw new ErrorNotification("Failed to evaluate expression: " + expr, e);
- }
- }
+ public boolean containsKey(Object key) {
+ throw new UnsupportedOperationException();
+ }
- public Object visit(final ASTQuotedString node, final Object data) {
- String value = evaluate(node.getValue());
+ public boolean containsValue(Object value) {
+ throw new UnsupportedOperationException();
+ }
- return appendString(value, data);
- }
+ public Object remove(Object key) {
+ throw new UnsupportedOperationException();
+ }
- public Object visit(final ASTOpaqueString node, final Object data) {
- return appendString(node.getValue(), data);
- }
+ 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 {
+ // Still using Jexl here for now, since it can handle expression like ${env.TERM}
+ // (where "env" is a variable bound to a map, ...)
+ Expression expr = ExpressionFactory.createExpression(s);
+ expr.addPreResolver(resolver);
+
+ return expr.evaluate(jc);
+ }
+ catch (Exception e) {
+ throw new ErrorNotification("Failed to evaluate expression: " + s, e);
+ }
+ }
+ });
- public Object visit(final ASTPlainString node, final Object data) {
- String value = evaluate(node.getValue());
-
- return appendString(value, data);
+ return interp;
}
-}
+}
\ No newline at end of file
Propchange: geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/VariableInterpolator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/VariableInterpolator.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: geronimo/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/VariableInterpolator.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: geronimo/gshell/trunk/gshell-support/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/gshell/trunk/gshell-support/pom.xml?rev=662816&r1=662815&r2=662816&view=diff
==============================================================================
--- geronimo/gshell/trunk/gshell-support/pom.xml (original)
+++ geronimo/gshell/trunk/gshell-support/pom.xml Tue Jun 3 07:51:16 2008
@@ -48,7 +48,6 @@
<module>gshell-io</module>
<module>gshell-prefs</module>
<module>gshell-plexus</module>
- <module>gshell-expr</module>
<module>gshell-url</module>
</modules>