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 2021/08/02 16:14:44 UTC
[sling-org-apache-sling-scripting-core] branch master updated
(5aaf306 -> 6341c17)
This is an automated email from the ASF dual-hosted git repository.
radu pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-core.git.
from 5aaf306 SLING-10676 - remove SECURITY.md which is not needed
new 3cd8077 SLING-10697 - Make the ScriptContextProvider use the ServiceCache
new 6341c17 trivial: use the LazyBindings.Supplier for the properties key
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../core/impl/bundled/ScriptContextProvider.java | 44 ++++++++++++++++++++--
1 file changed, 40 insertions(+), 4 deletions(-)
[sling-org-apache-sling-scripting-core] 01/02: SLING-10697 - Make
the ScriptContextProvider use the ServiceCache
Posted by ra...@apache.org.
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 <ra...@apache.org>
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();
+ }
+ }
+
+ }
+ }
+
}
[sling-org-apache-sling-scripting-core] 02/02: trivial: use the
LazyBindings.Supplier for the properties key
Posted by ra...@apache.org.
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 6341c17afa74ca9eef7389fe2530d9335fc41766
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Mon Aug 2 18:13:50 2021 +0200
trivial: use the LazyBindings.Supplier for the properties key
---
.../apache/sling/scripting/core/impl/bundled/ScriptContextProvider.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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 f11586f..c4612fd 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
@@ -98,7 +98,7 @@ public class ScriptContextProvider implements BundleListener {
}
// prepare the SlingBindings
Bindings bindings = new LazyBindings();
- bindings.put("properties", request.getResource().getValueMap());
+ bindings.put("properties", (LazyBindings.Supplier) () -> request.getResource().getValueMap());
bindings.put(SlingBindings.REQUEST, request);
bindings.put(SlingBindings.RESPONSE, response);
bindings.put(SlingBindings.READER, request.getReader());