You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by bm...@apache.org on 2024/03/03 12:09:23 UTC

(maven-scripting-plugin) 02/02: (MOCKUP) use context's logger instead

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

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

commit 8b2e016c2beab616a8c937430a0bf996fe8ad11a
Author: Benjamin Marwell <bm...@apache.org>
AuthorDate: Sun Mar 3 13:07:51 2024 +0100

    (MOCKUP) use context's logger instead
---
 .../plugins/scripting/AbstractScriptEvaluator.java |  9 +---
 .../apache/maven/plugins/scripting/EvalMojo.java   |  2 +-
 .../scripting/engine/ContextAwareEngine.java       | 49 ----------------------
 .../maven/plugins/scripting/engine/JavaEngine.java | 29 ++++++-------
 4 files changed, 15 insertions(+), 74 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/scripting/AbstractScriptEvaluator.java b/src/main/java/org/apache/maven/plugins/scripting/AbstractScriptEvaluator.java
index 80d6d7e..a8cdb9b 100644
--- a/src/main/java/org/apache/maven/plugins/scripting/AbstractScriptEvaluator.java
+++ b/src/main/java/org/apache/maven/plugins/scripting/AbstractScriptEvaluator.java
@@ -24,9 +24,6 @@ import javax.script.ScriptEngine;
 import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
 
-import org.apache.maven.plugin.logging.Log;
-import org.apache.maven.plugins.scripting.engine.ContextAwareEngine;
-
 /**
  * Evaluates a script in the appropriate context and return its possibly null result
  * @author Rusi Popov
@@ -35,17 +32,13 @@ abstract class AbstractScriptEvaluator {
 
     /**
      * @param bindings not null bindings to provide to the script to execute
-     * @param log engine logger if context aware.
      * @return the possibly null result the script produced
      * @throws UnsupportedScriptEngineException when the engine is not configured correctly
      * @throws ScriptException  if an error occurs in script.
      */
-    public final Object eval(Bindings bindings, Log log) throws ScriptException, UnsupportedScriptEngineException {
+    public final Object eval(Bindings bindings) throws ScriptException, UnsupportedScriptEngineException {
         ScriptEngineManager manager = new ScriptEngineManager();
         ScriptEngine engine = getEngine(manager);
-        if (ContextAwareEngine.class.isInstance(engine)) {
-            ContextAwareEngine.class.cast(engine).setLog(log);
-        }
         ScriptContext context = engine.getContext();
 
         context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
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 63fec56..d166090 100644
--- a/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java
+++ b/src/main/java/org/apache/maven/plugins/scripting/EvalMojo.java
@@ -93,7 +93,7 @@ public class EvalMojo extends AbstractMojo {
             bindings.put("mojoExecution", mojoExecution);
             bindings.put("settings", settings);
 
-            Object result = execute.eval(bindings, getLog());
+            Object result = execute.eval(bindings);
 
             getLog().info("Result:");
             if (result != null) {
diff --git a/src/main/java/org/apache/maven/plugins/scripting/engine/ContextAwareEngine.java b/src/main/java/org/apache/maven/plugins/scripting/engine/ContextAwareEngine.java
deleted file mode 100644
index 926431d..0000000
--- a/src/main/java/org/apache/maven/plugins/scripting/engine/ContextAwareEngine.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * 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.maven.plugins.scripting.engine;
-
-/*
- * 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 org.apache.maven.plugin.logging.Log;
-
-/**
- * Enables a custom engine to get some mojo context.
- */
-public interface ContextAwareEngine {
-    default void setLog(Log log) {
-        // no-op
-    }
-}
diff --git a/src/main/java/org/apache/maven/plugins/scripting/engine/JavaEngine.java b/src/main/java/org/apache/maven/plugins/scripting/engine/JavaEngine.java
index e9d06af..fc26af0 100644
--- a/src/main/java/org/apache/maven/plugins/scripting/engine/JavaEngine.java
+++ b/src/main/java/org/apache/maven/plugins/scripting/engine/JavaEngine.java
@@ -73,20 +73,13 @@ import static java.util.stream.Collectors.joining;
 /**
  * The java engine implementation.
  */
-public class JavaEngine extends AbstractScriptEngine implements Compilable, ContextAwareEngine {
+public class JavaEngine extends AbstractScriptEngine implements Compilable {
     private final ScriptEngineFactory factory;
 
-    private Log log;
-
     public JavaEngine(ScriptEngineFactory factory) {
         this.factory = factory;
     }
 
-    @Override
-    public void setLog(Log log) {
-        this.log = log;
-    }
-
     @Override
     public CompiledScript compile(String script) throws ScriptException {
         // plexus compiler is great but overkill there so don't bring it just for that
@@ -159,8 +152,8 @@ public class JavaEngine extends AbstractScriptEngine implements Compilable, Cont
                         }
                     });
                 } catch (IOException e) {
-                    if (log != null) {
-                        log.debug(e);
+                    if (getLog() != null) {
+                        getLog().debug(e);
                     }
                 }
             }
@@ -170,8 +163,8 @@ public class JavaEngine extends AbstractScriptEngine implements Compilable, Cont
     private String mavenClasspathPrefix() {
         final String home = System.getProperty("maven.home");
         if (home == null) {
-            if (log != null) {
-                log.debug("No maven.home set");
+            if (getLog() != null) {
+                getLog().debug("No maven.home set");
             }
             return "";
         }
@@ -183,8 +176,8 @@ public class JavaEngine extends AbstractScriptEngine implements Compilable, Cont
                     .map(Path::toString)
                     .collect(joining(File.pathSeparator, "", File.pathSeparator));
         } catch (IOException e) {
-            if (log != null) {
-                log.debug(e);
+            if (getLog() != null) {
+                getLog().debug(e);
             }
             return "";
         }
@@ -320,6 +313,10 @@ public class JavaEngine extends AbstractScriptEngine implements Compilable, Cont
         return factory;
     }
 
+    private Log getLog() {
+        return (Log) this.getContext().getAttribute("log");
+    }
+
     private void doClose(final CompiledScript compile) {
         if (!AutoCloseable.class.isInstance(compile)) {
             return;
@@ -327,8 +324,8 @@ public class JavaEngine extends AbstractScriptEngine implements Compilable, Cont
         try {
             AutoCloseable.class.cast(compile).close();
         } catch (Exception e) {
-            if (log != null) {
-                log.debug(e);
+            if (getLog() != null) {
+                getLog().debug(e);
             }
         }
     }