geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r462631 - in /geronimo/genesis/trunk/plugins/script-maven-plugin: pom.xml src/main/java/org/apache/geronimo/genesis/plugins/script/GroovyMojo.java src/main/java/org/apache/geronimo/genesis/plugins/script/StringValueParser.java
Date Tue, 10 Oct 2006 23:07:19 GMT
Author: jdillon
Date: Tue Oct 10 16:07:18 2006
New Revision: 462631

URL: http://svn.apache.org/viewvc?view=rev&rev=462631
Log:
Added new default variable 'properties' which contains pre-resolved bits from project.getProperties()

Added:
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/StringValueParser.java
  (with props)
Modified:
    geronimo/genesis/trunk/plugins/script-maven-plugin/pom.xml
    geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/GroovyMojo.java

Modified: geronimo/genesis/trunk/plugins/script-maven-plugin/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/genesis/trunk/plugins/script-maven-plugin/pom.xml?view=diff&rev=462631&r1=462630&r2=462631
==============================================================================
--- geronimo/genesis/trunk/plugins/script-maven-plugin/pom.xml (original)
+++ geronimo/genesis/trunk/plugins/script-maven-plugin/pom.xml Tue Oct 10 16:07:18 2006
@@ -53,6 +53,12 @@
             <version>06</version>
         </dependency>
 
+        <dependency>
+            <groupId>commons-jexl</groupId>
+            <artifactId>commons-jexl</artifactId>
+            <version>1.1</version>
+        </dependency>
+
         <!--
         NOTE: Included for AntBuilder support.  Needs to be in the same classloader as groovy-all-1.0-jsr
         -->

Modified: geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/GroovyMojo.java
URL: http://svn.apache.org/viewvc/geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/GroovyMojo.java?view=diff&rev=462631&r1=462630&r2=462631
==============================================================================
--- geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/GroovyMojo.java
(original)
+++ geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/GroovyMojo.java
Tue Oct 10 16:07:18 2006
@@ -29,6 +29,10 @@
 import java.net.URLClassLoader;
 import java.util.List;
 import java.util.ArrayList;
+import java.util.Properties;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Iterator;
 
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.Artifact;
@@ -153,7 +157,25 @@
         
         // Set some properties
         groovyObject.setProperty("project", project);
-        
+        groovyObject.setProperty("properties", resolveProperties(project.getProperties()));
// Force all properties to resolve
+
         groovyObject.invokeMethod("run", new Object[0]);
+    }
+
+    private Properties resolveProperties(final Properties source) {
+        Properties props = new Properties();
+
+        Map vars = new HashMap();
+        vars.put("project", project);
+
+        StringValueParser parser = new StringValueParser(vars);
+
+        Iterator iter = source.keySet().iterator();
+        while (iter.hasNext()) {
+            String name = (String)iter.next();
+            props.put(name, parser.parse(source.getProperty(name)));
+        }
+
+        return props;
     }
 }

Added: geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/StringValueParser.java
URL: http://svn.apache.org/viewvc/geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/StringValueParser.java?view=auto&rev=462631
==============================================================================
--- geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/StringValueParser.java
(added)
+++ geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/StringValueParser.java
Tue Oct 10 16:07:18 2006
@@ -0,0 +1,184 @@
+/*
+ * 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.genesis.plugins.script;
+
+import java.util.Map;
+
+import org.apache.commons.jexl.Expression;
+import org.apache.commons.jexl.ExpressionFactory;
+import org.apache.commons.jexl.JexlContext;
+import org.apache.commons.jexl.JexlHelper;
+import org.apache.commons.jexl.resolver.FlatResolver;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Handles pasring expressions from a string.
+ *
+ * @version $Rev$ $Date$
+ */
+public class StringValueParser
+{
+    private static final Log log = LogFactory.getLog(StringValueParser.class);
+
+    protected JexlContext context;
+
+    public StringValueParser(final Map vars) {
+        if (vars == null) {
+            throw new IllegalArgumentException("vars");
+        }
+
+        context = JexlHelper.createContext();
+        context.setVars(vars);
+
+        if (log.isTraceEnabled()) {
+            log.trace("Using variables: " + context.getVars());
+        }
+    }
+
+    public StringValueParser() {
+        this(System.getProperties());
+    }
+
+    public Map getVariables() {
+        return context.getVars();
+    }
+
+    public Object getVariable(final Object name) {
+        if (name == null) {
+            throw new IllegalArgumentException("name");
+        }
+
+        return getVariables().get(name);
+    }
+
+    public Object setVariable(final Object name, final Object value) {
+        if (name == null) {
+            throw new IllegalArgumentException("name");
+        }
+
+        return getVariables().put(name, value);
+    }
+
+    public Object unsetVariable(final Object name) {
+        if (name == null) {
+            throw new IllegalArgumentException("name");
+        }
+
+        return getVariables().remove(name);
+    }
+
+    public void addVariables(final Map map) {
+        if (map == null) {
+            throw new IllegalArgumentException("map");
+        }
+
+        getVariables().putAll(map);
+    }
+
+    private FlatResolver resolver = new FlatResolver(true);
+
+    protected Expression createExpression(final String expression) throws Exception {
+        // assert expression != null;
+
+        Expression expr = ExpressionFactory.createExpression(expression);
+        expr.addPreResolver(resolver);
+
+        return expr;
+    }
+
+    public Object evaluate(final String expression) throws Exception {
+        if (expression == null) {
+            throw new IllegalArgumentException("expression");
+        }
+
+        boolean trace = log.isTraceEnabled();
+        if (trace) {
+            log.trace("Evaluating expression: " + expression);
+        }
+
+        Expression expr = createExpression(expression);
+        Object obj = expr.evaluate(context);
+        if (trace) {
+            log.trace("Result: " + obj);
+        }
+
+        return obj;
+    }
+
+    public String parse(final String input) {
+        if (input == null) {
+            throw new IllegalArgumentException("input");
+        }
+
+        boolean trace = log.isTraceEnabled();
+        if (trace) {
+            log.trace("Parsing input: " + input);
+        }
+
+        StringBuffer buff = new StringBuffer();
+
+        int cur = 0;
+        int prefixLoc;
+        int suffixLoc;
+
+        while (cur < input.length()) {
+            prefixLoc = input.indexOf("${", cur);
+
+            if (prefixLoc < 0) {
+                break;
+            }
+
+            suffixLoc = input.indexOf("}", prefixLoc);
+            if (suffixLoc < 0) {
+                throw new RuntimeException("Missing '}': " + input);
+            }
+
+            String expr = input.substring(prefixLoc + 2, suffixLoc);
+            buff.append(input.substring(cur, prefixLoc));
+
+            try {
+                buff.append(evaluate(expr));
+            }
+            catch (Exception e) {
+                throw new RuntimeException("Failed to evaluate: " + expr, e);
+            }
+
+            cur = suffixLoc + 1;
+        }
+
+        buff.append(input.substring(cur));
+
+        if (trace) {
+            log.trace("Parsed result: " + buff);
+        }
+
+        return buff.toString();
+    }
+
+    public String parse(final String input, final boolean trim) {
+        String output = parse(input);
+        if (trim && output != null) {
+            output = output.trim();
+        }
+
+        return output;
+    }
+}

Propchange: geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/StringValueParser.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/StringValueParser.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/genesis/trunk/plugins/script-maven-plugin/src/main/java/org/apache/geronimo/genesis/plugins/script/StringValueParser.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message