You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2021/02/06 11:13:28 UTC

[maven-scripting-plugin] branch refactor created (now 120b60b)

This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a change to branch refactor
in repository https://gitbox.apache.org/repos/asf/maven-scripting-plugin.git.


      at 120b60b  Refactor code, using more the terms of JSR223

This branch includes the following new commits:

     new 120b60b  Refactor code, using more the terms of JSR223

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[maven-scripting-plugin] 01/01: Refactor code, using more the terms of JSR223

Posted by rf...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a commit to branch refactor
in repository https://gitbox.apache.org/repos/asf/maven-scripting-plugin.git

commit 120b60b888aaf49b73348a06158f257c98e8ee7e
Author: rfscholte <rf...@apache.org>
AuthorDate: Sat Feb 6 12:13:17 2021 +0100

    Refactor code, using more the terms of JSR223
---
 .../{Execute.java => AbstractScriptEvaluator.java} | 25 +++++++------
 .../apache/maven/plugins/scripting/EvalMojo.java   | 19 +++++-----
 .../{ExecuteFile.java => FileScriptEvaluator.java} | 43 ++++++++++------------
 ...ecuteString.java => StringScriptEvaluator.java} | 30 ++++++++-------
 .../UnsupportedScriptEngineException.java          | 34 +++++++++++++++++
 5 files changed, 94 insertions(+), 57 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/scripting/Execute.java b/src/main/java/org/apache/maven/plugins/scripting/AbstractScriptEvaluator.java
similarity index 63%
rename from src/main/java/org/apache/maven/plugins/scripting/Execute.java
rename to src/main/java/org/apache/maven/plugins/scripting/AbstractScriptEvaluator.java
index 74752d0..7605680 100644
--- a/src/main/java/org/apache/maven/plugins/scripting/Execute.java
+++ b/src/main/java/org/apache/maven/plugins/scripting/AbstractScriptEvaluator.java
@@ -26,42 +26,43 @@ import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
 
 /**
- * Execute a script in the appropriate context and return its possibly null result
+ * Evaluates a script in the appropriate context and return its possibly null result
  * @author Rusi Popov
  */
-abstract class Execute
+abstract class AbstractScriptEvaluator
 {
 
   /**
    * @param bindings not null bindings to provide to the script to execute
    * @return the possibly null result the script produced
-   * @throws IllegalArgumentException when the engine is not configured correctly
-   * @throws ScriptException
+   * @throws UnsupportedScriptEngineException when the engine is not configured correctly
+   * @throws ScriptException  if an error occurs in script.
    */
-  public final Object run( Bindings bindings ) throws IllegalArgumentException, ScriptException
+  public final Object eval( Bindings bindings ) throws ScriptException, UnsupportedScriptEngineException
   {
     ScriptEngineManager manager = new ScriptEngineManager();
-    ScriptEngine engine = constructEngine( manager );
+    ScriptEngine engine = getEngine( manager );
     ScriptContext context = engine.getContext();
 
     context.setBindings( bindings, ScriptContext.GLOBAL_SCOPE );
 
-    return execute( engine, context );
+    return eval( engine, context );
   }
 
   /**
-   * Execute the script
+   * AbstractScriptEvaluator the script
    * @param engine not null
    * @param context not null, initialized
    * @return possibly null result of the script
-   * @throws ScriptException
+   * @throws ScriptException  if an error occurs in script.
    */
-  protected abstract Object execute( ScriptEngine engine, ScriptContext context ) throws ScriptException;
+  protected abstract Object eval( ScriptEngine engine, ScriptContext context ) throws ScriptException;
 
   /**
    * @param manager not null
    * @return non-null engine to execute the script
-   * @throws IllegalArgumentException when no engine could be identified
+   * @throws UnsupportedScriptEngineException when no engine could be identified
    */
-  protected abstract ScriptEngine constructEngine( ScriptEngineManager manager ) throws IllegalArgumentException;
+  protected abstract ScriptEngine getEngine( ScriptEngineManager manager )
+      throws UnsupportedScriptEngineException;
 }
diff --git a/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java b/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java
index ce04b2d..4a6dbbc 100644
--- a/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java
+++ b/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java
@@ -22,6 +22,7 @@ package org.apache.maven.plugins.scripting;
 import java.io.File;
 
 import javax.script.Bindings;
+import javax.script.ScriptException;
 import javax.script.SimpleBindings;
 
 import org.apache.maven.plugin.AbstractMojo;
@@ -32,7 +33,7 @@ import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.project.MavenProject;
 
 /**
- * Evaluate the specified script
+ * Evaluate the specified script or scriptFile
  *
  * @author Robert Scholte
  * @since 3.0.0
@@ -69,13 +70,13 @@ public class EvalMojo
     {
        try
        {
-         Execute execute = constructExecute();
+         AbstractScriptEvaluator execute = constructExecute();
 
          Bindings bindings = new SimpleBindings();
          bindings.put( "project", project );
          bindings.put( "log", getLog() );
 
-         Object result = execute.run( bindings );
+         Object result = execute.eval( bindings );
 
          getLog().info( "Result:" );
          if ( result != null )
@@ -83,28 +84,28 @@ public class EvalMojo
            getLog().info( result.toString() );
          }
        }
-       catch ( IllegalArgumentException e ) // configuring the plugin failed
+       catch ( ScriptException e ) // configuring the plugin failed
        {
          throw new MojoExecutionException( e.getMessage(), e );
        }
-       catch ( Exception e ) // execution failure
+       catch ( UnsupportedScriptEngineException e ) // execution failure
        {
            throw new MojoFailureException( e.getMessage(), e );
        }
     }
 
-    private Execute constructExecute() throws IllegalArgumentException
+    private AbstractScriptEvaluator constructExecute() throws IllegalArgumentException
     {
-      Execute execute;
+      AbstractScriptEvaluator execute;
 
       if ( scriptFile != null )
       {
-         execute = new ExecuteFile( engineName, scriptFile );
+         execute = new FileScriptEvaluator( engineName, scriptFile );
 
       }
       else if ( script != null )
       {
-         execute = new ExecuteString( engineName, script );
+         execute = new StringScriptEvaluator( engineName, script );
 
       }
       else
diff --git a/src/main/java/org/apache/maven/plugins/scripting/ExecuteFile.java b/src/main/java/org/apache/maven/plugins/scripting/FileScriptEvaluator.java
similarity index 58%
rename from src/main/java/org/apache/maven/plugins/scripting/ExecuteFile.java
rename to src/main/java/org/apache/maven/plugins/scripting/FileScriptEvaluator.java
index 198b418..9f7abd7 100644
--- a/src/main/java/org/apache/maven/plugins/scripting/ExecuteFile.java
+++ b/src/main/java/org/apache/maven/plugins/scripting/FileScriptEvaluator.java
@@ -22,6 +22,7 @@ package org.apache.maven.plugins.scripting;
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
+import java.io.UncheckedIOException;
 
 import javax.script.ScriptContext;
 import javax.script.ScriptEngine;
@@ -29,11 +30,11 @@ import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
 
 /**
- * Execute a script held in a file. Use the engine name to override the engine if the file name does not refer/decode
+ * Evaluates a script held in a file. Use the engine name to override the engine if the file name does not refer/decode
  * to a valid engine name or not define any at all.
  * @author Rusi Popov
  */
-public class ExecuteFile extends Execute
+public class FileScriptEvaluator extends AbstractScriptEvaluator
 {
 
   /**
@@ -49,30 +50,22 @@ public class ExecuteFile extends Execute
   /**
    * @param engineName optional engine name, used to override the engine selection from the file extension
    * @param scriptFile not null
-   * @throws IllegalArgumentException when the combination of parameters is incorrect
    */
-  public ExecuteFile( String engineName, File scriptFile ) throws IllegalArgumentException
+  public FileScriptEvaluator( String engineName, File scriptFile )
   {
-    if ( scriptFile == null
-         || !scriptFile.isFile()
-         || !scriptFile.exists()
-         || !scriptFile.canRead() )
-    {
-      throw new IllegalArgumentException( "Expected an existing readable file \"" + scriptFile + "\" provided" );
-    }
     this.scriptFile = scriptFile;
 
     this.engineName = engineName;
   }
 
   /**
-   * @param engine
-   * @param context
-   * @return
-   * @throws ScriptException
-   * @see org.apache.maven.plugins.scripting.Execute#execute(javax.script.ScriptEngine, javax.script.ScriptContext)
+   * @param engine the script engine.
+   * @param context the script context.
+   * @return the result of the scriptFile.
+   * @throws ScriptException if an error occurs in script.
+   * @see org.apache.maven.plugins.scripting.AbstractScriptEvaluator#eval(javax.script.ScriptEngine, javax.script.ScriptContext)
    */
-  protected Object execute( ScriptEngine engine, ScriptContext context ) throws ScriptException
+  protected Object eval( ScriptEngine engine, ScriptContext context ) throws ScriptException
   {
     try ( FileReader reader = new FileReader( scriptFile ) )
     {
@@ -80,24 +73,28 @@ public class ExecuteFile extends Execute
     }
     catch ( IOException ex )
     {
-      throw new IllegalArgumentException( scriptFile + " caused:", ex );
+      throw new UncheckedIOException( scriptFile + " caused:", ex );
     }
   }
 
   /**
-   * @see org.apache.maven.plugins.scripting.Execute#constructEngine(javax.script.ScriptEngineManager)
+   * Gets the script engine by engineName, otherwise by extension of the sciptFile 
+   * 
+   * @param manager the script engine manager
+   * @throws UnsupportedScriptEngineException if specified engine is not available 
+   * @see org.apache.maven.plugins.scripting.AbstractScriptEvaluator#getEngine(javax.script.ScriptEngineManager)
    */
-  protected ScriptEngine constructEngine( ScriptEngineManager manager ) throws IllegalArgumentException
+  protected ScriptEngine getEngine( ScriptEngineManager manager ) throws UnsupportedScriptEngineException
   {
     ScriptEngine result;
 
-    if ( engineName != null && !engineName.trim().isEmpty() )
+    if ( engineName != null && !engineName.isEmpty() )
     {
       result = manager.getEngineByName( engineName );
 
       if ( result == null )
       {
-        throw new IllegalArgumentException( "No engine found by name \"" + engineName + "\n" );
+        throw new UnsupportedScriptEngineException( "No engine found by name \"" + engineName + "\n" );
       }
     }
     else
@@ -113,7 +110,7 @@ public class ExecuteFile extends Execute
 
       if ( result == null )
       {
-        throw new IllegalArgumentException( "No engine found by extension \"" + extension + "\n" );
+        throw new UnsupportedScriptEngineException( "No engine found by extension \"" + extension + "\n" );
       }
     }
     return result;
diff --git a/src/main/java/org/apache/maven/plugins/scripting/ExecuteString.java b/src/main/java/org/apache/maven/plugins/scripting/StringScriptEvaluator.java
similarity index 57%
rename from src/main/java/org/apache/maven/plugins/scripting/ExecuteString.java
rename to src/main/java/org/apache/maven/plugins/scripting/StringScriptEvaluator.java
index 38340f0..5a31c85 100644
--- a/src/main/java/org/apache/maven/plugins/scripting/ExecuteString.java
+++ b/src/main/java/org/apache/maven/plugins/scripting/StringScriptEvaluator.java
@@ -25,10 +25,10 @@ import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
 
 /**
- * Execute a script held in a string
+ * Evaluates a script held in a string
  * @author Rusi Popov
  */
-public class ExecuteString extends Execute
+public class StringScriptEvaluator extends AbstractScriptEvaluator
 {
 
   /**
@@ -42,13 +42,13 @@ public class ExecuteString extends Execute
   private final String script;
 
   /**
-   * @param engineName
-   * @param script
-   * @throws IllegalArgumentException
+   * @param engineName the engine name
+   * @param script the script
+   * @throws IllegalArgumentException if either engineName or script is null
    */
-  public ExecuteString( String engineName, String script ) throws IllegalArgumentException
+  public StringScriptEvaluator( String engineName, String script )
   {
-    if ( engineName == null || engineName.trim().isEmpty() )
+    if ( engineName == null || engineName.isEmpty() )
     {
       throw new IllegalArgumentException( "Expected a non-empty engine name provided" );
     }
@@ -62,24 +62,28 @@ public class ExecuteString extends Execute
   }
 
   /**
-   * @throws IllegalArgumentException
-   * @see org.apache.maven.plugins.scripting.Execute#constructEngine(javax.script.ScriptEngineManager)
+   * @param manager the script engine manager.
+   * @throws UnsupportedScriptEngineException if the engineName is not supported
+   * @see org.apache.maven.plugins.scripting.AbstractScriptEvaluator#getEngine(javax.script.ScriptEngineManager)
    */
-  protected ScriptEngine constructEngine( ScriptEngineManager manager ) throws IllegalArgumentException
+  protected ScriptEngine getEngine( ScriptEngineManager manager ) throws UnsupportedScriptEngineException
   {
     ScriptEngine result = manager.getEngineByName( engineName );
 
     if ( result == null )
     {
-      throw new IllegalArgumentException( "Unknown engine specified with name \"" + engineName + "\"" );
+      throw new UnsupportedScriptEngineException( "Unknown engine specified with name \"" + engineName + "\"" );
     }
     return result;
   }
 
   /**
-   * @see org.apache.maven.plugins.scripting.Execute#execute(javax.script.ScriptEngine, javax.script.ScriptContext)
+   * @param engine the script engine.
+   * @param context the script context.
+   * @throws ScriptException if an error occurs in script.
+   * @see org.apache.maven.plugins.scripting.AbstractScriptEvaluator#eval(javax.script.ScriptEngine, javax.script.ScriptContext)
    */
-  protected Object execute( ScriptEngine engine, ScriptContext context ) throws ScriptException
+  protected Object eval( ScriptEngine engine, ScriptContext context ) throws ScriptException
   {
     return engine.eval( script, context );
   }
diff --git a/src/main/java/org/apache/maven/plugins/scripting/UnsupportedScriptEngineException.java b/src/main/java/org/apache/maven/plugins/scripting/UnsupportedScriptEngineException.java
new file mode 100644
index 0000000..6f9975d
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/scripting/UnsupportedScriptEngineException.java
@@ -0,0 +1,34 @@
+package org.apache.maven.plugins.scripting;
+
+/*
+ * 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.
+ */
+
+/**
+ * Exception
+ * 
+ * @author Robert Scholte
+ *
+ */
+public class UnsupportedScriptEngineException extends Exception
+{
+    public UnsupportedScriptEngineException( String message )
+    {
+        super( message );
+    }
+}