You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ol...@apache.org on 2011/10/29 18:33:53 UTC

svn commit: r1194924 - in /maven/shared/trunk: ./ maven-script-interpreter/ maven-script-interpreter/src/ maven-script-interpreter/src/main/ maven-script-interpreter/src/main/java/ maven-script-interpreter/src/main/java/org/ maven-script-interpreter/sr...

Author: olamy
Date: Sat Oct 29 16:33:52 2011
New Revision: 1194924

URL: http://svn.apache.org/viewvc?rev=1194924&view=rev
Log:
move script interpreters classes to a new component

Added:
    maven/shared/trunk/maven-script-interpreter/
    maven/shared/trunk/maven-script-interpreter/pom.xml   (with props)
    maven/shared/trunk/maven-script-interpreter/src/
    maven/shared/trunk/maven-script-interpreter/src/main/
    maven/shared/trunk/maven-script-interpreter/src/main/java/
    maven/shared/trunk/maven-script-interpreter/src/main/java/org/
    maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/
    maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/
    maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/
    maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/
    maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreter.java   (with props)
    maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java   (with props)
    maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java   (with props)
    maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptInterpreter.java   (with props)
Modified:
    maven/shared/trunk/pom.xml

Added: maven/shared/trunk/maven-script-interpreter/pom.xml
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-script-interpreter/pom.xml?rev=1194924&view=auto
==============================================================================
--- maven/shared/trunk/maven-script-interpreter/pom.xml (added)
+++ maven/shared/trunk/maven-script-interpreter/pom.xml Sat Oct 29 16:33:52 2011
@@ -0,0 +1,56 @@
+<?xml version="1.0"?>
+<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.apache.maven.shared</groupId>
+    <artifactId>maven-shared-components</artifactId>
+    <version>16</version>
+    <relativePath>../maven-shared-components/pom.xml</relativePath>
+  </parent>
+  <artifactId>maven-script-interpreter</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <name>Maven Script Interpreter</name>
+  <dependencies>
+    <dependency>
+      <groupId>org.codehaus.groovy</groupId>
+      <artifactId>groovy</artifactId>
+      <version>1.8.3</version>
+    </dependency>
+    <dependency>
+      <groupId>org.beanshell</groupId>
+      <artifactId>bsh</artifactId>
+      <version>2.0b4</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.ant</groupId>
+      <artifactId>ant</artifactId>
+      <version>1.8.1</version>
+      <exclusions>
+        <exclusion>
+          <groupId>org.apache.ant</groupId>
+          <artifactId>ant-launcher</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.8.2</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <source>1.5</source>
+          <target>1.5</target>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Propchange: maven/shared/trunk/maven-script-interpreter/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/shared/trunk/maven-script-interpreter/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreter.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreter.java?rev=1194924&view=auto
==============================================================================
--- maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreter.java (added)
+++ maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreter.java Sat Oct 29 16:33:52 2011
@@ -0,0 +1,134 @@
+package org.apache.maven.shared.scriptinterpreter;
+
+/*
+ * 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.
+ */
+
+import bsh.Capabilities;
+import bsh.EvalError;
+import bsh.Interpreter;
+import bsh.TargetError;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Provides a facade to evaluate BeanShell scripts.
+ * 
+ * @author Benjamin Bentmann
+ * @version $Id$
+ */
+class BeanShellScriptInterpreter
+    implements ScriptInterpreter
+{
+
+    /**
+     * {@inheritDoc}
+     */
+    public Object evaluateScript( String script, List<String> classPath, Map<String, ? extends Object> globalVariables, PrintStream scriptOutput )
+        throws ScriptEvaluationException
+    {
+        PrintStream origOut = System.out;
+        PrintStream origErr = System.err;
+
+        try
+        {
+            Interpreter engine = new Interpreter();
+
+            if ( scriptOutput != null )
+            {
+                System.setErr( scriptOutput );
+                System.setOut( scriptOutput );
+                engine.setErr( scriptOutput );
+                engine.setOut( scriptOutput );
+            }
+
+            if ( !Capabilities.haveAccessibility() )
+            {
+                try
+                {
+                    Capabilities.setAccessibility( true );
+                }
+                catch ( Exception e )
+                {
+                    if ( scriptOutput != null )
+                    {
+                        e.printStackTrace( scriptOutput );
+                    }
+                }
+            }
+
+            if ( classPath != null && !classPath.isEmpty() )
+            {
+                for ( String path : classPath)
+                {
+                    try
+                    {
+                        engine.getClassManager().addClassPath( new File( path ).toURI().toURL() );
+                    }
+                    catch ( IOException e )
+                    {
+                        throw new RuntimeException( "bad class path: " + path, e );
+                    }
+                }
+            }
+
+            if ( globalVariables != null )
+            {
+                for ( String variable : globalVariables.keySet() )
+                {
+                    Object value = globalVariables.get( variable );
+                    try
+                    {
+                        engine.set( variable, value );
+                    }
+                    catch ( EvalError e )
+                    {
+                        throw new RuntimeException( e );
+                    }
+                }
+            }
+
+            try
+            {
+                return engine.eval( script );
+            }
+            catch ( TargetError e )
+            {
+                throw new ScriptEvaluationException( e.getTarget() );
+            }
+            catch ( ThreadDeath e )
+            {
+                throw e;
+            }
+            catch ( Throwable e )
+            {
+                throw new ScriptEvaluationException( e );
+            }
+        }
+        finally
+        {
+            System.setErr( origErr );
+            System.setOut( origOut );
+        }
+    }
+
+}

Propchange: maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/BeanShellScriptInterpreter.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java?rev=1194924&view=auto
==============================================================================
--- maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java (added)
+++ maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java Sat Oct 29 16:33:52 2011
@@ -0,0 +1,98 @@
+package org.apache.maven.shared.scriptinterpreter;
+
+/*
+ * 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.
+ */
+
+import groovy.lang.Binding;
+import groovy.lang.GroovyShell;
+import org.apache.tools.ant.AntClassLoader;
+import org.codehaus.groovy.control.CompilerConfiguration;
+
+import java.io.File;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Provides a facade to evaluate Groovy scripts.
+ * 
+ * @author Benjamin Bentmann
+ * @version $Id$
+ */
+class GroovyScriptInterpreter
+    implements ScriptInterpreter
+{
+
+    /**
+     * {@inheritDoc}
+     */
+    public Object evaluateScript( String script, List<String> classPath, Map<String, ? extends Object> globalVariables, PrintStream scriptOutput )
+        throws ScriptEvaluationException
+    {
+        PrintStream origOut = System.out;
+        PrintStream origErr = System.err;
+
+        try
+        {
+            CompilerConfiguration config = new CompilerConfiguration( CompilerConfiguration.DEFAULT );
+
+            if ( scriptOutput != null )
+            {
+                System.setErr( scriptOutput );
+                System.setOut( scriptOutput );
+                config.setOutput( new PrintWriter( scriptOutput ) );
+            }
+
+            ClassLoader loader = null;
+            if ( classPath != null && !classPath.isEmpty() )
+            {
+                AntClassLoader childFirstLoader = new AntClassLoader( getClass().getClassLoader(), false );
+                for ( String path : classPath )
+                {
+                    childFirstLoader.addPathComponent( new File( path ) );
+                }
+                loader = childFirstLoader;
+            }
+
+            Binding binding = new Binding( globalVariables );
+
+            GroovyShell interpreter = new GroovyShell( loader, binding, config );
+
+            try
+            {
+                return interpreter.evaluate( script );
+            }
+            catch ( ThreadDeath e )
+            {
+                throw e;
+            }
+            catch ( Throwable e )
+            {
+                throw new ScriptEvaluationException( e );
+            }
+        }
+        finally
+        {
+            System.setErr( origErr );
+            System.setOut( origOut );
+        }
+    }
+
+}

Propchange: maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/GroovyScriptInterpreter.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java?rev=1194924&view=auto
==============================================================================
--- maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java (added)
+++ maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java Sat Oct 29 16:33:52 2011
@@ -0,0 +1,48 @@
+package org.apache.maven.shared.scriptinterpreter;
+
+/*
+ * 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.
+ */
+
+/**
+ * Signals an error during parsing/evaluation of a script. This can either be a syntax error in the script itself or an
+ * exception triggered by the methods it invoked.
+ * 
+ * @author Benjamin Bentmann
+ * @version $Id$
+ */
+class ScriptEvaluationException
+    extends Exception
+{
+
+    /**
+     * The serial version identifier for this class.
+     */
+    private static final long serialVersionUID = 199336743291078393L;
+
+    /**
+     * Creates a new exception with the specified cause.
+     * 
+     * @param cause The cause, may be <code>null</code>.
+     */
+    public ScriptEvaluationException( Throwable cause )
+    {
+        super( cause );
+    }
+
+}

Propchange: maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptEvaluationException.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptInterpreter.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptInterpreter.java?rev=1194924&view=auto
==============================================================================
--- maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptInterpreter.java (added)
+++ maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptInterpreter.java Sat Oct 29 16:33:52 2011
@@ -0,0 +1,54 @@
+package org.apache.maven.shared.scriptinterpreter;
+
+/*
+ * 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.
+ */
+
+import java.io.PrintStream;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Defines a simple abstraction used to plug-in several script interpreters for the pre-/post-build-hooks. Each
+ * interpretator implementation should be stateless and support reuse.
+ * 
+ * @author Benjamin Bentmann
+ * @version $Id$
+ */
+interface ScriptInterpreter
+{
+
+    /**
+     * Evaluates the specified script.
+     * 
+     * @param script The script contents to evalute, must not be <code>null</code>.
+     * @param classPath The additional class path for the script interpreter, may be <code>null</code> or empty if only
+     *            the plugin realm should be used for the script evaluation. If specified, this class path will precede
+     *            the artifacts from the plugin class path.
+     * @param globalVariables The global variables (as a mapping from variable name to value) to define for the script,
+     *            may be <code>null</code> if not used.
+     * @param scriptOutput A print stream to redirect any output from the script to, may be <code>null</code> to use
+     *            stdout/stderr.
+     * @return The return value from the script, can be <code>null</code>
+     * @throws ScriptEvaluationException If the script evaluation produced an error.
+     */
+    Object evaluateScript( String script, List<String> classPath, Map<String, ? extends Object> globalVariables,
+                           PrintStream scriptOutput )
+        throws ScriptEvaluationException;
+
+}

Propchange: maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptInterpreter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/shared/trunk/maven-script-interpreter/src/main/java/org/apache/maven/shared/scriptinterpreter/ScriptInterpreter.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/shared/trunk/pom.xml
URL: http://svn.apache.org/viewvc/maven/shared/trunk/pom.xml?rev=1194924&r1=1194923&r2=1194924&view=diff
==============================================================================
--- maven/shared/trunk/pom.xml (original)
+++ maven/shared/trunk/pom.xml Sat Oct 29 16:33:52 2011
@@ -61,6 +61,7 @@ under the License.
     <module>maven-shared-resources</module>
     <module>maven-verifier</module>
     <module>maven-jarsigner</module>
+    <module>maven-script-interpreter</module>
   </modules>
 
   <profiles>