Return-Path: Delivered-To: apmail-geronimo-scm-archive@www.apache.org Received: (qmail 6837 invoked from network); 11 Jun 2008 19:17:23 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 11 Jun 2008 19:17:23 -0000 Received: (qmail 12287 invoked by uid 500); 11 Jun 2008 19:17:26 -0000 Delivered-To: apmail-geronimo-scm-archive@geronimo.apache.org Received: (qmail 12241 invoked by uid 500); 11 Jun 2008 19:17:25 -0000 Mailing-List: contact scm-help@geronimo.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: dev@geronimo.apache.org List-Id: Delivered-To: mailing list scm@geronimo.apache.org Received: (qmail 12231 invoked by uid 99); 11 Jun 2008 19:17:25 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 11 Jun 2008 12:17:25 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 11 Jun 2008 19:16:44 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 807702388A4C; Wed, 11 Jun 2008 12:17:02 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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... Date: Wed, 11 Jun 2008 19:17:01 -0000 To: scm@geronimo.apache.org From: jdillon@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080611191702.807702388A4C@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.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 ${env.TERM} + * (where env 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 @@ org.apache.geronimo.gshell - gshell-core - 1.0-alpha-2-SNAPSHOT - - - - org.apache.geronimo.gshell gshell-bootstrap 1.0-alpha-2-SNAPSHOT @@ -459,7 +453,13 @@ gshell-vfs 1.0-alpha-2-SNAPSHOT - + + + org.apache.geronimo.gshell + gshell-rapture + 1.0-alpha-2-SNAPSHOT + + org.apache.geronimo.gshell gshell-diet-log4j @@ -491,7 +491,7 @@ gshell-plugin gshell-parser gshell-layout - gshell-core + gshell-rapture gshell-cli gshell-commands gshell-remote