[java-support] branch dev/JSPT-111 updated: Mechanism to extend script contexts dynamically.

Scott Cantor cantor.2 at osu.edu
Tue Jun 21 17:44:46 UTC 2022


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

scantor pushed a commit to branch dev/JSPT-111
in repository java-support.

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

The following commit(s) were added to refs/heads/dev/JSPT-111 by this push:
     new 1c5ddb8  Mechanism to extend script contexts dynamically.
1c5ddb8 is described below

commit 1c5ddb84031eeadaecc12e15422b6f31d1a7e5f6
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Tue Jun 21 13:44:43 2022 -0400

    Mechanism to extend script contexts dynamically.
---
 .../support/scripting/AbstractScriptEvaluator.java | 44 ++++++++++++++++++++-
 .../support/scripting/ScriptContextExtender.java   | 46 ++++++++++++++++++++++
 2 files changed, 89 insertions(+), 1 deletion(-)

diff --git a/src/main/java/net/shibboleth/utilities/java/support/scripting/AbstractScriptEvaluator.java b/src/main/java/net/shibboleth/utilities/java/support/scripting/AbstractScriptEvaluator.java
index b2964e7..078c184 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/scripting/AbstractScriptEvaluator.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/scripting/AbstractScriptEvaluator.java
@@ -17,6 +17,10 @@
 
 package net.shibboleth.utilities.java.support.scripting;
 
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import javax.script.ScriptContext;
@@ -24,7 +28,10 @@ import javax.script.ScriptException;
 import javax.script.SimpleScriptContext;
 
 import net.shibboleth.utilities.java.support.annotation.ParameterName;
+import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
 import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.component.AbstractInitializableComponent;
+import net.shibboleth.utilities.java.support.component.ComponentSupport;
 import net.shibboleth.utilities.java.support.logic.Constraint;
 
 import org.slf4j.Logger;
@@ -36,7 +43,7 @@ import org.slf4j.LoggerFactory;
  * 
  * @since 7.4.0
  */
-public abstract class AbstractScriptEvaluator {
+public abstract class AbstractScriptEvaluator extends AbstractInitializableComponent {
 
     /** The default language is Javascript. */
     @Nonnull @NotEmpty public static final String DEFAULT_ENGINE = "JavaScript";
@@ -46,6 +53,9 @@ public abstract class AbstractScriptEvaluator {
 
     /** The script we care about. */
     @Nonnull private final EvaluableScript script;
+    
+    /** Extension objects to apply. */
+    @Nonnull private Collection<ScriptContextExtender> contextExtenders;
 
     /** Debugging info. */
     @Nullable private String logPrefix;
@@ -69,6 +79,7 @@ public abstract class AbstractScriptEvaluator {
      */
     public AbstractScriptEvaluator(@Nonnull @ParameterName(name="theScript") final EvaluableScript theScript) {
         script = Constraint.isNotNull(theScript, "Supplied script cannot be null");
+        contextExtenders = Collections.emptyList();
     }
 
     /**
@@ -86,6 +97,8 @@ public abstract class AbstractScriptEvaluator {
      * @param prefix log prefix
      */
     public void setLogPrefix(@Nullable final String prefix) {
+        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+        
         logPrefix = prefix;
     }
     
@@ -104,6 +117,8 @@ public abstract class AbstractScriptEvaluator {
      * @param type output type
      */
     protected void setOutputType(@Nullable final Class<?> type) {
+        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+        
         outputType = type;
     }
     
@@ -122,6 +137,8 @@ public abstract class AbstractScriptEvaluator {
      * @param object the custom object
      */
     public void setCustomObject(@Nullable final Object object) {
+        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+        
         customObject = object;
     }
 
@@ -140,6 +157,8 @@ public abstract class AbstractScriptEvaluator {
      * @param flag flag to set
      */
     public void setHideExceptions(final boolean flag) {
+        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+        
         hideExceptions = flag;
     }
 
@@ -158,8 +177,27 @@ public abstract class AbstractScriptEvaluator {
      * @param value value to return
      */
     protected void setReturnOnError(@Nullable final Object value) {
+        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+        
         returnOnError = value;
     }
+    
+    /**
+     * Set {@link ScriptContextExtender} instances to apply when populating script context.
+     * 
+     * @param extenders extenders to apply
+     * 
+     * @since 9.0.0
+     */
+    public void setContextExtenders(@Nullable @NonnullElements final Collection<ScriptContextExtender> extenders) {
+        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+        
+        if (extenders != null) {
+            contextExtenders = List.copyOf(extenders);
+        } else {
+            contextExtenders = Collections.emptyList();
+        }
+    }
 
     /**
      * Evaluate the script.
@@ -169,10 +207,14 @@ public abstract class AbstractScriptEvaluator {
      * @return script result
      */
     @Nullable protected Object evaluate(@Nullable final Object... input) {
+        ComponentSupport.ifNotInitializedThrowUninitializedComponentException(this);
+        
         final SimpleScriptContext scriptContext = new SimpleScriptContext();
         scriptContext.setAttribute("custom", getCustomObject(), ScriptContext.ENGINE_SCOPE);
         
         prepareContext(scriptContext, input);
+        
+        contextExtenders.forEach(x -> x.extendContext(getClass(), scriptContext));
 
         try {
             final Object result = script.eval(scriptContext);
diff --git a/src/main/java/net/shibboleth/utilities/java/support/scripting/ScriptContextExtender.java b/src/main/java/net/shibboleth/utilities/java/support/scripting/ScriptContextExtender.java
new file mode 100644
index 0000000..ca6b19f
--- /dev/null
+++ b/src/main/java/net/shibboleth/utilities/java/support/scripting/ScriptContextExtender.java
@@ -0,0 +1,46 @@
+/*
+ * 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.script.ScriptContext;
+
+/**
+ * Interface implemented by components that extend the {@link ScriptContext} of various
+ * components that rely on JSR-223 scripting.
+ * 
+ * @since 9.0.0
+ */
+public interface ScriptContextExtender {
+
+    /**
+     * Instructs the implementing component to decorate the input context with any additional
+     * content.
+     * 
+     * <p>The context will already contain any default objects and typically the implementation
+     * of this interface will access those objects for context while adding additional content.</p>
+     * 
+     * <p>Extenders should generally ensure that they do not overwrite existing context variables with
+     * the same name, but may do so if the semantics are understood.</p>
+     * 
+     * @param scriptedClass the class of the calling component
+     * @param scriptContext the context, as decorated by the calling component
+     */
+    void extendContext(@Nonnull final Class<?> scriptedClass, @Nonnull final ScriptContext scriptContext);
+
+}
\ No newline at end of file

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


More information about the commits mailing list