[java-support] branch master updated: JPAR-136 Remove old scripting engine and replace with the plguin derived ones

Rod Widdowson rdw at steadingsoftware.com
Sat Jul 25 13:34:37 UTC 2020


This is an automated email from the git hooks/post-receive script.

rdw pushed a commit to branch master
in repository java-support.

View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=49dfbaf75133d29be6df8187a0c44275cfc6410f

The following commit(s) were added to refs/heads/master by this push:
       new  49dfbaf   JPAR-136 Remove old scripting engine and replace with the plguin derived ones
49dfbaf is described below

commit 49dfbaf75133d29be6df8187a0c44275cfc6410f
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Thu Jul 23 15:54:18 2020 +0100

    JPAR-136 Remove old scripting engine and replace with the plguin derived ones
    
    https://issues.shibboleth.net/jira/browse/JPAR-136
---
 pom.xml                                            |   7 ++
 .../java/support/scripting/EngineTests.java        | 111 -------------------
 .../java/support/scripting/GraalEngine.java        | 123 ---------------------
 .../java/support/scripting/GraalFactory.java       |  96 ----------------
 .../java/support/scripting/RhinoEngine.java        | 110 ------------------
 .../java/support/scripting/RhinoFactory.java       |  96 ----------------
 .../services/javax.script.ScriptEngineFactory      |   2 -
 7 files changed, 7 insertions(+), 538 deletions(-)

diff --git a/pom.xml b/pom.xml
index 40a79bb..18b81c8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -142,6 +142,13 @@
             <scope>test</scope>
         </dependency>
 
+        <dependency>
+            <groupId>net.shibboleth.idp.plugin.scripting</groupId>
+            <artifactId>idp-plugin-nashorn-impl</artifactId>
+            <version>${nashorn.engine.version}</version>
+            <scope>test</scope>
+        </dependency>
+
         <dependency>
             <groupId>org.graalvm.sdk</groupId>
             <artifactId>graal-sdk</artifactId>
diff --git a/src/test/java/net/shibboleth/utilities/java/support/scripting/EngineTests.java b/src/test/java/net/shibboleth/utilities/java/support/scripting/EngineTests.java
deleted file mode 100644
index 7089f33..0000000
--- a/src/test/java/net/shibboleth/utilities/java/support/scripting/EngineTests.java
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
- * Licensed to the University Corporation for Advanced Internet Development,
- * Inc. (UCAID) under one or more contributor license agreements.  See the
- * NOTICE file distributed with this work for additional information regarding
- * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.scripting;
-
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertTrue;
-
-import java.util.Map;
-
-import javax.script.Compilable;
-import javax.script.CompiledScript;
-import javax.script.ScriptContext;
-import javax.script.ScriptEngine;
-import javax.script.ScriptEngineManager;
-import javax.script.ScriptException;
-import javax.script.SimpleScriptContext;
-
-import org.testng.annotations.Test;
-
-/** Minimal tests for our two JSR-223 engine plug ins. */
- at SuppressWarnings({"javadoc", "rawtypes", })
-public class EngineTests {
-    
-    @Test public void testRhino() throws ScriptException {
-        final ScriptEngineManager engineManager = new ScriptEngineManager();
-        final ScriptEngine scriptEngine = engineManager.getEngineByName("shibboleth-rhino");
-        final ScriptContext ctx = new SimpleScriptContext();
-        final String script = "var s = new java.util.HashMap(2); s.put('a',b); s";
-
-        ctx.setAttribute("b", this, ScriptContext.ENGINE_SCOPE);
-        
-        final Object o = scriptEngine.eval(script, ctx);
-        
-        assertTrue(o instanceof Map);
-        final Map map = (Map) o;
-        assertEquals(map.size(), 1);
-        assertEquals(map.get("a"), this);
-    }
-
-    @Test public void testRhinoCompiled() throws ScriptException {
-        final ScriptEngineManager engineManager = new ScriptEngineManager();
-        final ScriptEngine scriptEngine = engineManager.getEngineByName("shibboleth-rhino");
-        final Compilable compiler = (Compilable) scriptEngine;
-
-        
-        final ScriptContext ctx = new SimpleScriptContext();
-        final String script = "var s = new java.util.HashMap(2); s.put('a',b); s";
-
-        ctx.setAttribute("b", this, ScriptContext.ENGINE_SCOPE);
-        
-        final CompiledScript compiled = compiler.compile(script);
-        final Object o = compiled.eval(ctx);
-        
-        assertTrue(o instanceof Map);
-        final Map map = (Map) o;
-        assertEquals(map.size(), 1);
-        assertEquals(map.get("a"), this);
-    }
-
-    @Test public void testGraal() throws ScriptException {
-        final ScriptEngineManager engineManager = new ScriptEngineManager();
-        final ScriptEngine scriptEngine = engineManager.getEngineByName("shibboleth-nashorn");
-        final ScriptContext ctx = new SimpleScriptContext();
-        final String script = "var map = Java.type('java.util.HashMap');"
-                + "var s = new map(2); s.put('a',b); s";
-
-        ctx.setAttribute("b", this, ScriptContext.ENGINE_SCOPE);
-        
-        final Object o = scriptEngine.eval(script, ctx);
-
-        assertTrue(o instanceof Map);
-        final Map map = (Map) o;
-        assertEquals(map.size(), 1);
-        assertEquals(map.get("a"), this);
-    }
-
-    @Test public void testGraalCompiled() throws ScriptException {
-        final ScriptEngineManager engineManager = new ScriptEngineManager();
-        final ScriptEngine scriptEngine = engineManager.getEngineByName("shibboleth-nashorn");
-        final Compilable compiler = (Compilable) scriptEngine;
-
-        final ScriptContext ctx = new SimpleScriptContext();
-        ctx.setAttribute("b", this, ScriptContext.ENGINE_SCOPE);
-
-        final String script = "var map = Java.type('java.util.HashMap');"
-                + "var s = new map(2); s.put('a',b); s";
-
-        final CompiledScript compiled = compiler.compile(script);
-        final Object o = compiled.eval(ctx);
-
-        assertTrue(o instanceof Map);
-        final Map map = (Map) o;
-        assertEquals(map.size(), 1);
-        assertEquals(map.get("a"), this);
-    }
-}
diff --git a/src/test/java/net/shibboleth/utilities/java/support/scripting/GraalEngine.java b/src/test/java/net/shibboleth/utilities/java/support/scripting/GraalEngine.java
deleted file mode 100644
index bb90943..0000000
--- a/src/test/java/net/shibboleth/utilities/java/support/scripting/GraalEngine.java
+++ /dev/null
@@ -1,123 +0,0 @@
-/*
- * Licensed to the University Corporation for Advanced Internet Development,
- * Inc. (UCAID) under one or more contributor license agreements.  See the
- * NOTICE file distributed with this work for additional information regarding
- * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.scripting;
-
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
-import javax.script.Bindings;
-import javax.script.Compilable;
-import javax.script.CompiledScript;
-import javax.script.ScriptContext;
-import javax.script.ScriptEngine;
-import javax.script.ScriptException;
-
-import org.graalvm.polyglot.Context;
-import org.graalvm.polyglot.Source;
-import org.graalvm.polyglot.Value;
-
-/**
- * A subset of {@link ScriptEngine} implemented with GraaVm with enough function that 
- * {@link EvaluableScript} can work with it. 
- * 
- * NOTE that this does not (currently) implement {@link javax.script.Compilable}.
- */
-public class GraalEngine extends AbstractScriptEngine implements ScriptEngine, Compilable {
-
-    /** {@inheritDoc} */
-    public Object eval(final String script, final Bindings scriptBindings) throws ScriptException {
-        try (final Context context = Context.newBuilder().allowExperimentalOptions(true).
-                option("js.nashorn-compat", "true").
-                allowAllAccess(true).
-                build()) {
-            
-            final Value bindings  = context.getBindings("js");
-            for (final String name: scriptBindings.keySet()) {
-                bindings.putMember(name, scriptBindings.get(name));
-            }
-            final Source s = Source.newBuilder("js", script, "embedded").buildLiteral();
-            final Value v = context.eval(s);
-            return processValue(v);
-        }  catch (final RuntimeException e) {
-            throw new ScriptException(e);
-        }
-    }
-
-    /** {@inheritDoc} */
-    public CompiledScript compile(final String script) throws ScriptException {
-
-        return new GraalVMCompiledScript(script);
-    }
-
-    /** Convert the output from {@link Context#eval(String, CharSequence)} or
-     * {@link Context#eval(String, CharSequence)} into a java {@link Object}.
-     * @param value what to consider
-     * @return The output
-     */
-    @Nullable protected static Object processValue(@Nonnull final Value value) {
-        if (value.isNull()) {
-            return null;
-        } else if (value.isHostObject()) {
-            return value.asHostObject();
-        } else if (value.isProxyObject()) {
-            return value.asProxyObject();
-        } else {
-            return  value.as(Object.class);
-        }
-    }
-
-    /** GraalVM {@link CompiledScript} implementation. */
-    private class GraalVMCompiledScript extends CompiledScript {
-        
-        /** The compiled source. */
-        private final Source source;
-
-        /**
-         * Constructor.
-         *
-         * @param script the script (as text)
-         */
-        protected GraalVMCompiledScript(final String script) {
-            source = Source.newBuilder("js", script, "embedded").buildLiteral();
-        }
-
-        /** {@inheritDoc} */
-        public ScriptEngine getEngine() {
-            return GraalEngine.this;
-        }
-
-        /** {@inheritDoc} */
-        public Object eval(final ScriptContext scriptContext) throws ScriptException {
-
-            try (final Context context = Context.newBuilder().allowExperimentalOptions(true).
-                    option("js.nashorn-compat", "true").
-                    allowAllAccess(true).
-                    build()) {
-
-                final Bindings scriptBindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
-                final Value bindings  = context.getBindings("js");
-                for (final String name: scriptBindings.keySet()) {
-                    bindings.putMember(name, scriptBindings.get(name));
-                }
-                final Value v = context.eval(source);
-                return processValue(v);
-            }  catch (final RuntimeException e) {
-                throw new ScriptException(e);
-            }
-        }
-    }
-}
diff --git a/src/test/java/net/shibboleth/utilities/java/support/scripting/GraalFactory.java b/src/test/java/net/shibboleth/utilities/java/support/scripting/GraalFactory.java
deleted file mode 100644
index 59e6069..0000000
--- a/src/test/java/net/shibboleth/utilities/java/support/scripting/GraalFactory.java
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- * Licensed to the University Corporation for Advanced Internet Development,
- * Inc. (UCAID) under one or more contributor license agreements.  See the
- * NOTICE file distributed with this work for additional information regarding
- * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.scripting;
-
-import java.util.Collections;
-import java.util.List;
-
-import javax.script.ScriptEngine;
-import javax.script.ScriptEngineFactory;
-
-/**
- * A JSR-223 factory for {@link GraalEngine}.
- */
-public class GraalFactory implements ScriptEngineFactory {
-    
-    /** {@inheritDoc} */
-    public String getEngineName() {
-        
-        return this.getClass().getCanonicalName();
-    }
-
-    /** {@inheritDoc} */
-    public String getEngineVersion() {
-        return "1";
-    }
-
-    /** {@inheritDoc} */
-    public List<String> getExtensions() {
-        
-        return List.of("js","nashorn-js");
-    }
-
-    /** {@inheritDoc} */
-    public List<String> getMimeTypes() {
-        return Collections.emptyList();
-    }
-
-    /** {@inheritDoc} */
-    public List<String> getNames() {
-        return List.of("shibboleth-nashorn", "shibboleth-Nashorn", 
-                "shibboleth-js", "shibboleth-JS", "shibboleth-JavaScript", "shibboleth-javascript", 
-                "shibboleth-ECMAScript", "shibboleth-ecmascript");
-    }
-
-    /** {@inheritDoc} */
-    public String getLanguageName() {
-        return "ECMAScript";
-    }
-
-    /** {@inheritDoc} */
-    public String getLanguageVersion() {
-        throw new RuntimeScriptingException("Unsupported method getMethodCallSyntax");
-    }
-
-    /** {@inheritDoc} */
-    public Object getParameter(final String key) {
-        throw new RuntimeScriptingException("Unsupported method getMethodCallSyntax");
-    }
-
-    /** {@inheritDoc} */
-    public String getMethodCallSyntax(final String obj, final String m, final String... args) {
-        throw new RuntimeScriptingException("Unsupported method getMethodCallSyntax");
-    }
-
-    /** {@inheritDoc} */
-    public String getOutputStatement(final String toDisplay) {
-        throw new RuntimeScriptingException("Unsupported method getMethodCallSyntax");
-    }
-
-    /** {@inheritDoc} */
-    public String getProgram(final String... statements) {
-        throw new RuntimeScriptingException("Unsupported method getMethodCallSyntax");
-    }
-
-    /** {@inheritDoc} */
-    public ScriptEngine getScriptEngine() {
-       
-        return new GraalEngine();
-    }
-
-}
diff --git a/src/test/java/net/shibboleth/utilities/java/support/scripting/RhinoEngine.java b/src/test/java/net/shibboleth/utilities/java/support/scripting/RhinoEngine.java
deleted file mode 100644
index 33b4cc7..0000000
--- a/src/test/java/net/shibboleth/utilities/java/support/scripting/RhinoEngine.java
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
- * Licensed to the University Corporation for Advanced Internet Development,
- * Inc. (UCAID) under one or more contributor license agreements.  See the
- * NOTICE file distributed with this work for additional information regarding
- * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.scripting;
-
-import javax.script.Bindings;
-import javax.script.Compilable;
-import javax.script.CompiledScript;
-import javax.script.ScriptContext;
-import javax.script.ScriptEngine;
-import javax.script.ScriptException;
-
-import org.mozilla.javascript.Context;
-import org.mozilla.javascript.ImporterTopLevel;
-import org.mozilla.javascript.Script;
-import org.mozilla.javascript.Scriptable;
-import org.mozilla.javascript.ScriptableObject;
-
-/**
- * A subset of {@link ScriptEngine} implemented using Mozilla rhino with enough function 
- * that {@link EvaluableScript} can work with it. 
- * 
- * NOTE that this does not (currently) implement {@link javax.script.Compilable}.
- */
-public class RhinoEngine extends AbstractScriptEngine implements ScriptEngine, Compilable {
-
-    /** {@inheritDoc} */
-    public Object eval(final String script, final Bindings bindings) throws ScriptException {
-        final Context ctx = Context.enter();
-        try {
-            final Scriptable scope = new ImporterTopLevel(ctx);
-            
-            for (final String name: bindings.keySet()) {
-                final Object jsObj = Context.javaToJS(bindings.get(name), scope);
-                ScriptableObject.putProperty(scope, name, jsObj);
-            }
-            final Object o = ctx.evaluateString(scope, script, "rhino source", 1, null);
-            return Context.jsToJava(o, Object.class);
-            
-        } catch (final RuntimeException e) {
-            throw new ScriptException(e);
-        } finally {
-            Context.exit();
-        }
-    }
-
-    /** {@inheritDoc} */
-    public CompiledScript compile(final String script) throws ScriptException {
-        return new CompiledScriptImpl(script);
-    }
-    
-    /** Rhino {@link CompiledScript}. */
-    private class CompiledScriptImpl extends CompiledScript {
-
-        /** The compiled script. */
-        private final Script script;
-        
-        /** Constructor.
-         *
-         * @param source what to compile up.
-         */
-        public CompiledScriptImpl(final String source) {
-            final Context ctx = Context.enter();
-            try {
-                script = ctx.compileString(source, "Script", 1, null);  
-            } finally {
-                Context.exit();
-            }
-        }
-
-        /** {@inheritDoc} */
-        public Object eval(final ScriptContext context) throws ScriptException {
-            final Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
-            final Context ctx = Context.enter();
-            try {
-                final Scriptable scope = new ImporterTopLevel(ctx);
-                
-                for (final String name: bindings.keySet()) {
-                    final Object jsObj = Context.javaToJS(bindings.get(name), scope);
-                    ScriptableObject.putProperty(scope, name, jsObj);
-                }
-                final Object o = script.exec(ctx, scope);
-                return Context.jsToJava(o, Object.class);
-            } 
-            finally {
-                Context.exit();
-            }
-
-        }
-
-        /** {@inheritDoc} */
-        public ScriptEngine getEngine() {
-            return RhinoEngine.this;
-        }
-    }
-}
diff --git a/src/test/java/net/shibboleth/utilities/java/support/scripting/RhinoFactory.java b/src/test/java/net/shibboleth/utilities/java/support/scripting/RhinoFactory.java
deleted file mode 100644
index a772ebe..0000000
--- a/src/test/java/net/shibboleth/utilities/java/support/scripting/RhinoFactory.java
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- * Licensed to the University Corporation for Advanced Internet Development,
- * Inc. (UCAID) under one or more contributor license agreements.  See the
- * NOTICE file distributed with this work for additional information regarding
- * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.scripting;
-
-import java.util.Collections;
-import java.util.List;
-
-import javax.script.ScriptEngine;
-import javax.script.ScriptEngineFactory;
-
-/**
- * A JSR-223 factory for {@link RhinoEngine}.
- */
-public class RhinoFactory implements ScriptEngineFactory {
-    
-    /** {@inheritDoc} */
-    public String getEngineName() {
-        
-        return this.getClass().getCanonicalName();
-    }
-
-    /** {@inheritDoc} */
-    public String getEngineVersion() {
-        return "1";
-    }
-
-    /** {@inheritDoc} */
-    public List<String> getExtensions() {
-        
-        return List.of("js", "rhino-js");
-    }
-
-    /** {@inheritDoc} */
-    public List<String> getMimeTypes() {
-        return Collections.emptyList();
-    }
-
-    /** {@inheritDoc} */
-    public List<String> getNames() {
-        return List.of("shibboleth-rhino", "shibboleth-Rhino", 
-                "shibboleth-js", "shibboleth-JS", "shibboleth-JavaScript", "shibboleth-javascript", 
-                "shibboleth-ECMAScript", "shibboleth-ecmascript");
-    }
-
-    /** {@inheritDoc} */
-    public String getLanguageName() {
-        return "ECMAScript";
-    }
-
-    /** {@inheritDoc} */
-    public String getLanguageVersion() {
-        throw new RuntimeScriptingException("Unsupported method getMethodCallSyntax");
-    }
-
-    /** {@inheritDoc} */
-    public Object getParameter(String key) {
-        throw new RuntimeScriptingException("Unsupported method getMethodCallSyntax");
-    }
-
-    /** {@inheritDoc} */
-    public String getMethodCallSyntax(String obj, String m, String... args) {
-        throw new RuntimeScriptingException("Unsupported method getMethodCallSyntax");
-    }
-
-    /** {@inheritDoc} */
-    public String getOutputStatement(String toDisplay) {
-        throw new RuntimeScriptingException("Unsupported method getMethodCallSyntax");
-    }
-
-    /** {@inheritDoc} */
-    public String getProgram(String... statements) {
-        throw new RuntimeScriptingException("Unsupported method getMethodCallSyntax");
-    }
-
-    /** {@inheritDoc} */
-    public ScriptEngine getScriptEngine() {
-       
-        return new RhinoEngine();
-    }
-
-}
diff --git a/src/test/resources/META-INF/services/javax.script.ScriptEngineFactory b/src/test/resources/META-INF/services/javax.script.ScriptEngineFactory
deleted file mode 100644
index 2a1bdcd..0000000
--- a/src/test/resources/META-INF/services/javax.script.ScriptEngineFactory
+++ /dev/null
@@ -1,2 +0,0 @@
-net.shibboleth.utilities.java.support.scripting.GraalFactory
-net.shibboleth.utilities.java.support.scripting.RhinoFactory

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list