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-org-apache-sling-scripting-core.git

commit 3cd8077581d42a547f22d22c36b872c69d4805c0
Author: Radu Cotescu <[email protected]>
AuthorDate: Mon Aug 2 18:13:26 2021 +0200

    SLING-10697 - Make the ScriptContextProvider use the ServiceCache
---
 .../core/impl/bundled/ScriptContextProvider.java   | 42 ++++++++++++++++++++--
 1 file changed, 39 insertions(+), 3 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/scripting/core/impl/bundled/ScriptContextProvider.java
 
b/src/main/java/org/apache/sling/scripting/core/impl/bundled/ScriptContextProvider.java
index 3ce7ef1..f11586f 100644
--- 
a/src/main/java/org/apache/sling/scripting/core/impl/bundled/ScriptContextProvider.java
+++ 
b/src/main/java/org/apache/sling/scripting/core/impl/bundled/ScriptContextProvider.java
@@ -23,8 +23,10 @@ import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 
 import javax.script.Bindings;
 import javax.script.ScriptContext;
@@ -40,10 +42,17 @@ import org.apache.sling.api.scripting.SlingScriptConstants;
 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.impl.InternalScriptHelper;
+import org.apache.sling.scripting.core.impl.ServiceCache;
 import org.apache.sling.scripting.core.impl.helper.ProtectedBindings;
 import org.apache.sling.scripting.core.ScriptHelper;
 import org.apache.sling.scripting.spi.bundle.BundledRenderUnit;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
 import org.osgi.service.component.annotations.Reference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -51,7 +60,7 @@ import org.slf4j.LoggerFactory;
 @Component(
         service = ScriptContextProvider.class
 )
-public class ScriptContextProvider {
+public class ScriptContextProvider implements BundleListener {
 
     private static final Set<String> PROTECTED_BINDINGS = 
Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
             SlingBindings.REQUEST,
@@ -75,6 +84,8 @@ public class ScriptContextProvider {
     @Reference
     private ScriptingResourceResolverProvider 
scriptingResourceResolverProvider;
 
+    private final ConcurrentHashMap<BundleContext, ServiceCache> 
perContextServiceCache = new ConcurrentHashMap<>();
+
     public ExecutableContext prepareScriptContext(SlingHttpServletRequest 
request, SlingHttpServletResponse response, ExecutableUnit executable)
             throws IOException {
         ScriptEngine scriptEngine = 
scriptEngineManager.getEngineByName(executable.getScriptEngineName());
@@ -96,8 +107,9 @@ 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(executable.getBundleContext(), new 
SlingScriptAdapter(request.getResourceResolver(),
-                executable.getPath(), "sling/bundle/resource"), request, 
response));
+        bindings.put(SlingBindings.SLING, new 
InternalScriptHelper(executable.getBundleContext(), new 
SlingScriptAdapter(request.getResourceResolver(),
+                executable.getPath(), "sling/bundle/resource"), request, 
response,
+                
perContextServiceCache.computeIfAbsent(executable.getBundleContext(), 
ServiceCache::new)));
         bindings.put(BundledRenderUnit.VARIABLE, executable);
         bindings.put(ScriptEngine.FILENAME, executable.getPath());
         bindings.put(ScriptEngine.FILENAME.replaceAll("\\.", "_"), 
executable.getPath());
@@ -147,5 +159,29 @@ public class ScriptContextProvider {
         }
     }
 
+    @Activate
+    private void activate(BundleContext bundleContext) {
+        bundleContext.addBundleListener(this);
+    }
+
+    @Deactivate
+    private void deactivate(BundleContext bundleContext) {
+        bundleContext.removeBundleListener(this);
+    }
+
+    @Override
+    public void bundleChanged(BundleEvent event) {
+        if (event.getType() == BundleEvent.STOPPED) {
+            for (Iterator<BundleContext> iterator = 
perContextServiceCache.keySet().iterator(); iterator.hasNext();) {
+                try {
+                    iterator.next().getBundle();
+                } catch (IllegalStateException e) {
+                    iterator.remove();
+                }
+            }
+
+        }
+    }
+
 
 }

Reply via email to