[ 
https://issues.apache.org/jira/browse/SLING-7241?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16266522#comment-16266522
 ] 

ASF GitHub Bot commented on SLING-7241:
---------------------------------------

npeltier closed pull request #2: SLING-7241 ScriptEngine Initialization & 
Cleanup
URL: https://github.com/apache/sling-org-apache-sling-pipes/pull/2
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/main/java/org/apache/sling/pipes/PipeBindings.java 
b/src/main/java/org/apache/sling/pipes/PipeBindings.java
index 923302d..5cbb46d 100644
--- a/src/main/java/org/apache/sling/pipes/PipeBindings.java
+++ b/src/main/java/org/apache/sling/pipes/PipeBindings.java
@@ -44,46 +44,52 @@
  * Execution bindings of a pipe, and all expression related
  */
 public class PipeBindings {
+    /**
+     * interface mapping a javascript date
+     */
+    public interface JsDate {
+        long getTime();
+        int getTimezoneOffset();
+    }
+
     private static final Logger log = 
LoggerFactory.getLogger(PipeBindings.class);
 
+    public static final String NASHORNSCRIPTENGINE = "nashorn";
+    
     public static final String NN_ADDITIONALBINDINGS = "additionalBindings";
 
     public static final String PN_ADDITIONALSCRIPTS = "additionalScripts";
 
-    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
-
-    ScriptContext scriptContext = new SimpleScriptContext();
-
     /**
      * add ${path.pipeName} binding allowing to retrieve pipeName's current 
resource path
      */
     public static final String PATH_BINDING = "path";
-    Map<String, String> pathBindings = new HashMap<>();
 
     /**
      * add ${name.pipeName} binding allowing to retrieve pipeName's current 
resource name
      */
     public static final String NAME_BINDING = "name";
+    
+    private static final Pattern INJECTED_SCRIPT = 
Pattern.compile("\\$\\{(([^\\{^\\}]*(\\{[0-9,]+\\})?)*)\\}");
+
+    ScriptEngine engine;
+    
+    ScriptContext scriptContext = new SimpleScriptContext();
+
+    Map<String, String> pathBindings = new HashMap<>();
+
     Map<String, String> nameBindings = new HashMap<>();
 
     Map<String, Resource> outputResources = new HashMap<>();
 
-    private static final Pattern INJECTED_SCRIPT = 
Pattern.compile("\\$\\{(([^\\{^\\}]*(\\{[0-9,]+\\})?)*)\\}");
-
     /**
      * public constructor, built from pipe's resource
      * @param resource pipe's configuration resource
      */
-    public PipeBindings(Resource resource) throws Exception {
-       //In some contexts the nashorn engine cannot be obtained. Do fallback 
to system classloader.
-       if(engine == null){
-               engine = new 
ScriptEngineManager(null).getEngineByName("nashorn");
-               //Check if nashorn can still not be instantiated
-               if(engine == null){
-                       throw new Exception("Can not instantiate nashorn 
scriptengine. Check JVM version & capabilities.");
-               }
-       }
-       engine.setContext(scriptContext);
+    public PipeBindings(Resource resource) throws ScriptException {
+       //Setup script engines
+       initializeScriptEngine();
+       
         //add path bindings where path.MyPipe will give MyPipe current 
resource path
         getBindings().put(PATH_BINDING, pathBindings);
 
@@ -111,6 +117,25 @@ public PipeBindings(Resource resource) throws Exception {
         }
     }
 
+    /**
+     * add a binding
+     * @param name binding's name
+     * @param value binding's value
+     */
+    public void addBinding(String name, Object value){
+        log.debug("Adding binding {}={}", name, value);
+        getBindings().put(name, value);
+    }
+
+    /**
+     * adds additional bindings (global variables to use in child pipes 
expressions)
+     * @param bindings key/values bindings to add to the existing bindings
+     */
+    public void addBindings(Map bindings) {
+        log.info("Adding bindings {}", bindings);
+        getBindings().putAll(bindings);
+    }
+
     /**
      * add a script file to the engine
      * @param resolver resolver with which the file should be read
@@ -144,73 +169,6 @@ public void addScript(ResourceResolver resolver, String 
path) {
         }
     }
 
-    /**
-     * adds additional bindings (global variables to use in child pipes 
expressions)
-     * @param bindings key/values bindings to add to the existing bindings
-     */
-    public void addBindings(Map bindings) {
-        log.info("Adding bindings {}", bindings);
-        getBindings().putAll(bindings);
-    }
-
-    /**
-     * copy bindings
-     * @param original original bindings to copy
-     */
-    public void copyBindings(PipeBindings original){
-        getBindings().putAll(original.getBindings());
-    }
-
-    /**
-     * Update current resource of a given pipe, and appropriate binding
-     * @param pipe pipe we'll extract the output binding from
-     * @param resource current resource in the pipe execution
-     */
-    public void updateBindings(Pipe pipe, Resource resource) {
-        outputResources.put(pipe.getName(), resource);
-        updateStaticBindings(pipe.getName(), resource);
-        addBinding(pipe.getName(), pipe.getOutputBinding());
-    }
-
-    /**
-     * Update all the static bindings related to a given resource
-     * @param name name under which static bindings should be recorded
-     * @param resource resource from which static bindings will be built
-     */
-    public void updateStaticBindings(String name, Resource resource){
-        if (resource != null) {
-            pathBindings.put(name, resource.getPath());
-            nameBindings.put(name, resource.getName());
-        }
-    }
-
-    /**
-     * add a binding
-     * @param name binding's name
-     * @param value binding's value
-     */
-    public void addBinding(String name, Object value){
-        log.debug("Adding binding {}={}", name, value);
-        getBindings().put(name, value);
-    }
-
-    /**
-     * check if a given bindings is defined or not
-     * @param name name of the binding
-     * @return true if <code>name</code> is registered
-     */
-    public boolean isBindingDefined(String name){
-        return getBindings().containsKey(name);
-    }
-
-    /**
-     * return registered bindings
-     * @return bindings
-     */
-    public Bindings getBindings() {
-        return scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
-    }
-
     /**
      * Doesn't look like nashorn likes template strings :-(
      * @param expr ECMA like expression <code>blah${'some' + 'ecma' + 
'expression'}</code>
@@ -245,6 +203,14 @@ protected String computeECMA5Expression(String expr){
         return null;
     }
 
+    /**
+     * copy bindings
+     * @param original original bindings to copy
+     */
+    public void copyBindings(PipeBindings original){
+        getBindings().putAll(original.getBindings());
+    }
+
     /**
      * evaluate a given expression
      * @param expr ecma like expression
@@ -260,6 +226,41 @@ protected Object evaluate(String expr) throws 
ScriptException {
         return expr;
     }
 
+    /**
+     * return registered bindings
+     * @return bindings
+     */
+    public Bindings getBindings() {
+        return scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
+    }
+    
+    /**
+     * return Pipe <code>name</code>'s output binding
+     * @param name name of the pipe
+     * @return resource corresponding to that pipe output
+     */
+    public Resource getExecutedResource(String name) {
+        return outputResources.get(name);
+    }
+
+    /**
+     * Initialize the ScriptEngine.
+     * In some contexts the nashorn engine cannot be obtained from thread's 
class loader. Do fallback to system classloader.
+     * @throws ScriptException
+     */
+    private void initializeScriptEngine() throws ScriptException{
+       engine = new 
ScriptEngineManager().getEngineByName(PipeBindings.NASHORNSCRIPTENGINE);
+       if(engine == null){
+               //Fallback to system classloader
+               engine = new 
ScriptEngineManager(null).getEngineByName(PipeBindings.NASHORNSCRIPTENGINE);
+               //Check if nashorn can still not be instantiated
+               if(engine == null){
+                       throw new ScriptException("Can not instantiate nashorn 
scriptengine. Check JVM version & capabilities.");
+               }
+       }
+       engine.setContext(scriptContext);
+    }
+
     /**
      * Expression is a function of variables from execution context, that
      * we implement here as a String
@@ -302,19 +303,34 @@ public Object instantiateObject(String expr){
     }
 
     /**
-     * return Pipe <code>name</code>'s output binding
-     * @param name name of the pipe
-     * @return resource corresponding to that pipe output
+     * check if a given bindings is defined or not
+     * @param name name of the binding
+     * @return true if <code>name</code> is registered
      */
-    public Resource getExecutedResource(String name) {
-        return outputResources.get(name);
+    public boolean isBindingDefined(String name){
+        return getBindings().containsKey(name);
     }
 
     /**
-     * interface mapping a javascript date
+     * Update current resource of a given pipe, and appropriate binding
+     * @param pipe pipe we'll extract the output binding from
+     * @param resource current resource in the pipe execution
      */
-    public interface JsDate {
-        long getTime();
-        int getTimezoneOffset();
+    public void updateBindings(Pipe pipe, Resource resource) {
+        outputResources.put(pipe.getName(), resource);
+        updateStaticBindings(pipe.getName(), resource);
+        addBinding(pipe.getName(), pipe.getOutputBinding());
+    }
+
+    /**
+     * Update all the static bindings related to a given resource
+     * @param name name under which static bindings should be recorded
+     * @param resource resource from which static bindings will be built
+     */
+    public void updateStaticBindings(String name, Resource resource){
+        if (resource != null) {
+            pathBindings.put(name, resource.getPath());
+            nameBindings.put(name, resource.getName());
+        }
     }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> [pipes] Nashorn ScriptEngine in PipeBindings is null
> ----------------------------------------------------
>
>                 Key: SLING-7241
>                 URL: https://issues.apache.org/jira/browse/SLING-7241
>             Project: Sling
>          Issue Type: Bug
>          Components: Extensions
>    Affects Versions: Pipes 1.0.4
>         Environment: java.runtime.name = Java(TM) SE Runtime Environment
> java.runtime.version = 1.8.0_144-b01
>            Reporter: Björn Csott
>            Assignee: Nicolas Peltier
>            Priority: Minor
>             Fix For: pipes 2.0.0
>
>         Attachments: error.log
>
>
> Under some unclear circumstances the ScriptEngine in PipeBindings does not 
> get initialized.
> There is a solution out there to attach Nashorn to the system bundle. Appart 
> from that I was able to fix it by using a different constructor: 
> ScriptEngineManager(null). 
> The issue can be reproduced by deploying 
> https://github.com/bcsott/migration-tool
> When it fails the following is written to stderror.log:
> ScriptEngineManager providers.next(): javax.script.ScriptEngineFactory:
> Provider jdk.nashorn.api.scripting.NashornScriptEngineFactory not found



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to