[java-support] branch master updated: IDP-893 Add InputStream as a potential Script source

Rod Widdowson rdw at steadingsoftware.com
Thu Dec 31 12:05:56 EST 2015


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

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

The following commit(s) were added to refs/heads/master by this push:
       new  f4067e2   IDP-893 Add InputStream as a potential Script source
f4067e2 is described below

commit f4067e25bfc5fc2d8cbf77e978e1e0eba57759f4
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Thu Dec 31 17:03:09 2015 +0000

    IDP-893 Add InputStream as a potential Script source
    
    https://issues.shibboleth.net/jira/browse/IDP-893
    
    EvaluableScript has a constructor which takes a File.  This encourages
    people to use this to create scripts from Resources.  We need one which
    takes an InputStream to allow easy use of all Resource types.
---
 .../java/support/scripting/EvaluableScript.java    | 52 +++++++++++++++++-----
 .../support/scripting/EvaluableScriptTest.java     | 18 +++++---
 2 files changed, 53 insertions(+), 17 deletions(-)

diff --git a/src/main/java/net/shibboleth/utilities/java/support/scripting/EvaluableScript.java b/src/main/java/net/shibboleth/utilities/java/support/scripting/EvaluableScript.java
index f43671b..7c23e06 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/scripting/EvaluableScript.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/scripting/EvaluableScript.java
@@ -19,6 +19,7 @@ package net.shibboleth.utilities.java.support.scripting;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.nio.charset.Charset;
 
 import javax.annotation.Nonnull;
@@ -60,10 +61,11 @@ public class EvaluableScript {
      * 
      * @throws ScriptException thrown if the scripting engine supports compilation and the script does not compile
      */
-    public EvaluableScript(@Nonnull @NotEmpty String engineName, @Nonnull @NotEmpty String scriptSource)
+    public EvaluableScript(@Nonnull @NotEmpty final String engineName, @Nonnull @NotEmpty final String scriptSource)
             throws ScriptException {
         scriptLanguage =
-            Constraint.isNotNull(StringSupport.trimOrNull(engineName), "Scripting language can not be null or empty");
+                Constraint.isNotNull(StringSupport.trimOrNull(engineName),
+                        "Scripting language can not be null or empty");
         script = Constraint.isNotNull(StringSupport.trimOrNull(scriptSource), "Script source can not be null or empty");
 
         initialize();
@@ -76,12 +78,37 @@ public class EvaluableScript {
      * 
      * @throws ScriptException thrown if the scripting engine supports compilation and the script does not compile
      */
-    public EvaluableScript(@Nonnull @NotEmpty String scriptSource)
-            throws ScriptException {
+    public EvaluableScript(@Nonnull @NotEmpty final String scriptSource) throws ScriptException {
         this("javascript", scriptSource);
     }
 
     /**
+     * Constructor. The provided stream is <strong>not</strong> closed.
+     * 
+     * @param engineName the JSR-223 scripting engine name
+     * @param scriptSource the script source
+     * 
+     * @throws ScriptException thrown if the script source file can not be read or the scripting engine supports
+     *             compilation and the script does not compile
+     * 
+     * 
+     */
+    public EvaluableScript(@Nonnull @NotEmpty final String engineName, @Nonnull final InputStream scriptSource)
+            throws ScriptException {
+        scriptLanguage =
+                Constraint.isNotNull(StringSupport.trimOrNull(engineName),
+                        "Scripting language can not be null or empty");
+        try {
+            script = StringSupport.inputStreamToString(
+                            Constraint.isNotNull(scriptSource, "Script source can not be null or empty"), null);
+        } catch (final IOException e) {
+            throw new ScriptException(e);
+        }
+
+        initialize();
+    }
+
+    /**
      * Constructor.
      * 
      * @param engineName the JSR-223 scripting engine name
@@ -90,9 +117,11 @@ public class EvaluableScript {
      * @throws ScriptException thrown if the script source file can not be read or the scripting engine supports
      *             compilation and the script does not compile
      */
-    public EvaluableScript(@Nonnull @NotEmpty String engineName, @Nonnull File scriptSource) throws ScriptException {
+    public EvaluableScript(@Nonnull @NotEmpty final String engineName, @Nonnull final File scriptSource)
+            throws ScriptException {
         scriptLanguage =
-            Constraint.isNotNull(StringSupport.trimOrNull(engineName), "Scripting language can not be null or empty");
+                Constraint.isNotNull(StringSupport.trimOrNull(engineName),
+                        "Scripting language can not be null or empty");
 
         Constraint.isNotNull(scriptSource, "Script source file can not be null");
 
@@ -107,9 +136,10 @@ public class EvaluableScript {
 
         try {
             script =
-                Constraint.isNotNull(StringSupport.trimOrNull(Files.toString(scriptSource, Charset.defaultCharset())),
+                    Constraint.isNotNull(
+                            StringSupport.trimOrNull(Files.toString(scriptSource, Charset.defaultCharset())),
                             "Script source can not be empty");
-        } catch (IOException e) {
+        } catch (final IOException e) {
             throw new ScriptException("Unable to read data from source file " + scriptSource.getAbsolutePath());
         }
 
@@ -143,7 +173,7 @@ public class EvaluableScript {
      * 
      * @throws ScriptException thrown if there was a problem evaluating the script
      */
-    @Nullable public Object eval(Bindings scriptBindings) throws ScriptException {
+    @Nullable public Object eval(final Bindings scriptBindings) throws ScriptException {
         if (compiledScript != null) {
             return compiledScript.eval(scriptBindings);
         } else {
@@ -160,7 +190,7 @@ public class EvaluableScript {
      * 
      * @throws ScriptException thrown if there was a problem evaluating the script
      */
-    @Nullable public Object eval(ScriptContext scriptContext) throws ScriptException {
+    @Nullable public Object eval(final ScriptContext scriptContext) throws ScriptException {
         if (compiledScript != null) {
             return compiledScript.eval(scriptContext);
         } else {
@@ -174,7 +204,7 @@ public class EvaluableScript {
      * @throws ScriptException thrown if the scripting engine supports compilation and the script does not compile
      */
     private void initialize() throws ScriptException {
-        ScriptEngineManager engineManager = new ScriptEngineManager();
+        final ScriptEngineManager engineManager = new ScriptEngineManager();
         scriptEngine = engineManager.getEngineByName(scriptLanguage);
         Constraint.isNotNull(scriptEngine, "No scripting engine associated with scripting language " + scriptLanguage);
 
diff --git a/src/test/java/net/shibboleth/utilities/java/support/scripting/EvaluableScriptTest.java b/src/test/java/net/shibboleth/utilities/java/support/scripting/EvaluableScriptTest.java
index 027c690..b32e32c 100644
--- a/src/test/java/net/shibboleth/utilities/java/support/scripting/EvaluableScriptTest.java
+++ b/src/test/java/net/shibboleth/utilities/java/support/scripting/EvaluableScriptTest.java
@@ -18,8 +18,10 @@
 package net.shibboleth.utilities.java.support.scripting;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.InputStream;
 
 import javax.script.ScriptException;
 
@@ -54,28 +56,28 @@ public class EvaluableScriptTest {
         try {
             new EvaluableScript(" ", TEST_SIMPLE_SCRIPT);
             Assert.fail();
-        } catch (ConstraintViolationException e) {
+        } catch (final ConstraintViolationException e) {
             // OK
         }
         
         try {
             new EvaluableScript(SCRIPT_LANGUAGE, " ");
             Assert.fail();
-        } catch (ConstraintViolationException e) {
+        } catch (final ConstraintViolationException e) {
             // OK
         }
 
         try {
             new EvaluableScript(null, TEST_SIMPLE_SCRIPT);
             Assert.fail();
-        } catch (ConstraintViolationException e) {
+        } catch (final ConstraintViolationException e) {
             // OK
         }
         
         try {
             new EvaluableScript(SCRIPT_LANGUAGE, (String) null);
             Assert.fail();
-        } catch (ConstraintViolationException e) {
+        } catch (final ConstraintViolationException e) {
             // OK
         }
 
@@ -86,18 +88,22 @@ public class EvaluableScriptTest {
         s.close();
 
         Assert.assertEquals((new EvaluableScript(SCRIPT_LANGUAGE, theFile)).getScriptLanguage(), SCRIPT_LANGUAGE);
+        
+        try (InputStream is = new FileInputStream(theFile)) {
+            Assert.assertEquals((new EvaluableScript(SCRIPT_LANGUAGE, is)).getScriptLanguage(), SCRIPT_LANGUAGE);
+        }
 
         try {
             new EvaluableScript(null, theFile);
             Assert.fail();
-        } catch (ConstraintViolationException e) {
+        } catch (final ConstraintViolationException e) {
             // OK
         }
         
         try {
             new EvaluableScript(SCRIPT_LANGUAGE, (File) null);
             Assert.fail();
-        } catch (ConstraintViolationException e) {
+        } catch (final ConstraintViolationException e) {
             // OK
         }
 

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


More information about the commits mailing list