You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2018/09/24 16:05:24 UTC

[sling-whiteboard] branch master updated: updated the way bundle classloaders are passed down to script engines

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

radu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 5de520a  updated the way bundle classloaders are passed down to script engines
5de520a is described below

commit 5de520a65de7106042414f035bc58a12680b66b3
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Mon Sep 24 18:05:04 2018 +0200

    updated the way bundle classloaders are passed down to script engines
---
 .../scripting/resolver/internal/BundledScriptFinder.java  |  5 ++---
 .../scripting/resolver/internal/BundledScriptServlet.java | 14 ++++++++++++++
 .../scripting/resolver/internal/PrecompiledScript.java    | 12 +++++++++---
 .../apache/sling/scripting/resolver/internal/Script.java  | 12 +++++++++++-
 .../resolver/internal/ScriptContextProvider.java          | 15 +++------------
 .../resolver/internal/ScriptEngineExecutable.java         |  4 ++++
 .../sling-org-apache-sling-scripting-sightly              |  2 +-
 7 files changed, 44 insertions(+), 20 deletions(-)

diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptFinder.java b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptFinder.java
index 2787c45..746b356 100644
--- a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptFinder.java
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptFinder.java
@@ -45,7 +45,6 @@ public class BundledScriptFinder {
     private static final String SLASH = "/";
     private static final String DOT = ".";
     private static final Set<String> DEFAULT_METHODS = new HashSet<>(Arrays.asList("GET", "HEAD"));
-    private static final Pattern STARTS_WITH_NUMBER = Pattern.compile("^[0-9].*");
 
     @Reference
     private ScriptEngineManager scriptEngineManager;
@@ -68,7 +67,7 @@ public class BundledScriptFinder {
                     String className = fromScriptPathToClassName(match);
                     try {
                         Class clazz = bundle.loadClass(className);
-                        return new PrecompiledScript(scriptEngineManager.getEngineByExtension(extension),
+                        return new PrecompiledScript(bundle, scriptEngineManager.getEngineByExtension(extension),
                                 clazz.getDeclaredConstructor().newInstance());
                     } catch (ClassNotFoundException e) {
                         // do nothing here
@@ -78,7 +77,7 @@ public class BundledScriptFinder {
                 } else {
                     bundledScriptURL = bundle.getEntry(NS_JAVAX_SCRIPT_CAPABILITY + SLASH + match + DOT + extension);
                     if (bundledScriptURL != null) {
-                        return new Script(bundledScriptURL, scriptEngineManager.getEngineByExtension(extension));
+                        return new Script(bundle, bundledScriptURL, scriptEngineManager.getEngineByExtension(extension));
                     }
                 }
             }
diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptServlet.java b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptServlet.java
index 19036b8..8564120 100644
--- a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptServlet.java
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptServlet.java
@@ -25,6 +25,7 @@ import java.util.Set;
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
+import javax.script.Bindings;
 import javax.script.ScriptContext;
 import javax.script.ScriptException;
 import javax.servlet.GenericServlet;
@@ -38,6 +39,8 @@ import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
 import org.apache.sling.api.request.RequestPathInfo;
 import org.apache.sling.api.scripting.ScriptEvaluationException;
+import org.apache.sling.api.scripting.SlingBindings;
+import org.apache.sling.scripting.core.ScriptHelper;
 import org.osgi.framework.Bundle;
 
 class BundledScriptServlet extends GenericServlet {
@@ -122,6 +125,17 @@ class BundledScriptServlet extends GenericServlet {
                 } catch (ScriptException se) {
                     Throwable cause = (se.getCause() == null) ? se : se.getCause();
                     throw new ScriptEvaluationException(script.getName(), se.getMessage(), cause);
+                } finally {
+                    if (scriptContext != null) {
+                        Bindings engineBindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
+                        if (engineBindings != null && engineBindings.containsKey(SlingBindings.SLING)) {
+                            Object scriptHelper = engineBindings.get(SlingBindings.SLING);
+                            if (scriptHelper instanceof ScriptHelper) {
+                                ((ScriptHelper) scriptHelper).cleanup();
+                            }
+                        }
+
+                    }
                 }
             } else {
                 throw new ServletException("Unable to locate a " + (m_precompiledScripts ? "class" : "script") + " for rendering.");
diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/PrecompiledScript.java b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/PrecompiledScript.java
index f0fc1c2..99e2ba2 100644
--- a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/PrecompiledScript.java
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/PrecompiledScript.java
@@ -26,6 +26,7 @@ import javax.script.ScriptException;
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.api.scripting.SlingScriptConstants;
+import org.osgi.framework.Bundle;
 
 public class PrecompiledScript implements ScriptEngineExecutable {
 
@@ -33,10 +34,12 @@ public class PrecompiledScript implements ScriptEngineExecutable {
 
     private final ScriptEngine scriptEngine;
     private final Object precompiledScript;
+    private final Bundle bundle;
 
-    PrecompiledScript(ScriptEngine scriptEngine, Object precompiledScript) {
+    PrecompiledScript(Bundle bundle, ScriptEngine scriptEngine, Object precompiledScript) {
         this.scriptEngine = scriptEngine;
         this.precompiledScript = precompiledScript;
+        this.bundle = bundle;
     }
 
     @Override
@@ -52,8 +55,11 @@ public class PrecompiledScript implements ScriptEngineExecutable {
     @Override
     public void eval(ScriptContext context) throws ScriptException {
         context.setAttribute("precompiled.unit", precompiledScript, SlingScriptConstants.SLING_SCOPE);
-        context.setAttribute("precompiled.bundle.classloader", precompiledScript.getClass().getClassLoader(),
-                SlingScriptConstants.SLING_SCOPE);
         scriptEngine.eval(EMPTY_READER, context);
     }
+
+    @Override
+    public Bundle getBundle() {
+        return bundle;
+    }
 }
diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/Script.java b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/Script.java
index 9bb8b8c..0d863e4 100644
--- a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/Script.java
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/Script.java
@@ -33,9 +33,11 @@ import javax.script.ScriptException;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.sling.scripting.core.ScriptNameAwareReader;
+import org.osgi.framework.Bundle;
 
 class Script implements ScriptEngineExecutable {
 
+    private final Bundle bundle;
     private final URL url;
     private final ScriptEngine scriptEngine;
     private String sourceCode;
@@ -44,7 +46,8 @@ class Script implements ScriptEngineExecutable {
     private Lock readLock = new ReentrantLock();
 
 
-    Script(URL url, ScriptEngine scriptEngine) {
+    Script(Bundle bundle, URL url, ScriptEngine scriptEngine) {
+        this.bundle = bundle;
         this.url = url;
         this.scriptEngine = scriptEngine;
     }
@@ -63,14 +66,17 @@ class Script implements ScriptEngineExecutable {
         return sourceCode;
     }
 
+    @Override
     public String getName() {
         return url.getPath();
     }
 
+    @Override
     public ScriptEngine getScriptEngine() {
         return scriptEngine;
     }
 
+    @Override
     public void eval(ScriptContext context) throws ScriptException {
         try {
             if (scriptEngine instanceof Compilable && compiledScript == null) {
@@ -93,6 +99,10 @@ class Script implements ScriptEngineExecutable {
         } catch (IOException e) {
             throw new ScriptException(e);
         }
+    }
 
+    @Override
+    public Bundle getBundle() {
+        return bundle;
     }
 }
diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/ScriptContextProvider.java b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/ScriptContextProvider.java
index 0a183e2..efd2639 100644
--- a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/ScriptContextProvider.java
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/ScriptContextProvider.java
@@ -40,9 +40,6 @@ import org.apache.sling.scripting.api.BindingsValuesProvider;
 import org.apache.sling.scripting.api.BindingsValuesProvidersByContext;
 import org.apache.sling.scripting.api.resource.ScriptingResourceResolverProvider;
 import org.apache.sling.scripting.core.ScriptHelper;
-import org.osgi.framework.BundleContext;
-import org.osgi.service.component.ComponentContext;
-import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Reference;
 import org.slf4j.Logger;
@@ -52,9 +49,7 @@ import org.slf4j.LoggerFactory;
         service = ScriptContextProvider.class
 )
 public class ScriptContextProvider {
-
-    private BundleContext m_bundleContext;
-
+    
     private static final Set<String> PROTECTED_BINDINGS = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
             SlingBindings.REQUEST,
             SlingBindings.RESPONSE,
@@ -73,11 +68,6 @@ public class ScriptContextProvider {
     @Reference
     private ScriptingResourceResolverProvider scriptingResourceResolverProvider;
 
-    @Activate
-    private void activate(ComponentContext componentContext) {
-        m_bundleContext = componentContext.getBundleContext();
-    }
-
     ScriptContext prepareScriptContext(SlingHttpServletRequest request, SlingHttpServletResponse response, ScriptEngineExecutable executable)
             throws IOException {
         // prepare the SlingBindings
@@ -90,7 +80,7 @@ public class ScriptContextProvider {
         bindings.put(SlingBindings.OUT, response.getWriter());
         Logger scriptLogger = LoggerFactory.getLogger(executable.getName());
         bindings.put(SlingBindings.LOG, scriptLogger);
-        bindings.put(SlingBindings.SLING, new ScriptHelper(m_bundleContext, null, request, response));
+        bindings.put(SlingBindings.SLING, new ScriptHelper(executable.getBundle().getBundleContext(), null, request, response));
         bindings.put(ScriptEngine.FILENAME, executable.getName());
         bindings.put(ScriptEngine.FILENAME.replaceAll("\\.", "_"), executable.getName());
 
@@ -109,6 +99,7 @@ public class ScriptContextProvider {
         scriptContext.setWriter(response.getWriter());
         scriptContext.setErrorWriter(new LogWriter(scriptLogger));
         scriptContext.setReader(request.getReader());
+        scriptContext.setAttribute("org.apache.sling.scripting.resolver.provider.bundle", executable.getBundle(), SlingScriptConstants.SLING_SCOPE);
         return scriptContext;
     }
 
diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/ScriptEngineExecutable.java b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/ScriptEngineExecutable.java
index fd0f8f3..a62188d 100644
--- a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/ScriptEngineExecutable.java
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/ScriptEngineExecutable.java
@@ -22,11 +22,15 @@ import javax.script.ScriptContext;
 import javax.script.ScriptEngine;
 import javax.script.ScriptException;
 
+import org.osgi.framework.Bundle;
+
 interface ScriptEngineExecutable {
 
     String getName();
 
     ScriptEngine getScriptEngine();
 
+    Bundle getBundle();
+
     void eval(ScriptContext context) throws ScriptException;
 }
diff --git a/scripting-resolver/sling-org-apache-sling-scripting-sightly b/scripting-resolver/sling-org-apache-sling-scripting-sightly
index 4ca98cd..f491640 160000
--- a/scripting-resolver/sling-org-apache-sling-scripting-sightly
+++ b/scripting-resolver/sling-org-apache-sling-scripting-sightly
@@ -1 +1 @@
-Subproject commit 4ca98cd13cfe41aab16a3402988564b7ca8b1b4f
+Subproject commit f49164058cf2c81e5569a4849d60747c87c91f21