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/04/30 10:51:09 UTC

[sling-whiteboard] branch master updated (ea88df9 -> 914df20)

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-whiteboard.git.


    from ea88df9  Move Feature Mode Example to sling-samples
     new 6ca19f6  Added HTTP method to script cache key
     new a616a42  Added capability for BundledScriptServlet description
     new 914df20  Added the scripting resolver as a lazy binding

The 3 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:
 .../resolver/internal/BundledScriptServlet.java    |   3 +-
 .../resolver/internal/BundledScriptTracker.java    |   2 +
 .../scripting/resolver/internal/LazyBindings.java  |  78 ++++++++++++++
 .../resolver/internal/ScriptContextProvider.java   |  13 ++-
 .../resolver/internal/LazyBindingsTest.java        | 112 +++++++++++++++++++++
 5 files changed, 204 insertions(+), 4 deletions(-)
 create mode 100644 scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/LazyBindings.java
 create mode 100644 scripting-resolver/org-apache-sling-scripting-resolver/src/test/java/org/apache/sling/scripting/resolver/internal/LazyBindingsTest.java

-- 
To stop receiving notification emails like this one, please contact
radu@apache.org.

[sling-whiteboard] 01/03: Added HTTP method to script cache 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-whiteboard.git

commit 6ca19f624718a5f8910f04a2dbc505f953f64557
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Mon Apr 30 12:49:20 2018 +0200

    Added HTTP method to script cache key
---
 .../apache/sling/scripting/resolver/internal/BundledScriptServlet.java | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

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 163464c..f331587 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
@@ -131,7 +131,8 @@ class BundledScriptServlet extends GenericServlet {
         RequestPathInfo requestPathInfo = request.getRequestPathInfo();
         String selectorString = requestPathInfo.getSelectorString();
         String requestExtension = requestPathInfo.getExtension();
-        return request.getResource().getResourceType() + (StringUtils.isNotEmpty(selectorString) ? ":" + selectorString : "") +
+        return request.getMethod() + ":" + request.getResource().getResourceType() +
+                (StringUtils.isNotEmpty(selectorString) ? ":" + selectorString : "") +
                 (StringUtils.isNotEmpty(requestExtension) ? ":" + requestExtension : "");
     }
 }

-- 
To stop receiving notification emails like this one, please contact
radu@apache.org.

[sling-whiteboard] 02/03: Added capability for BundledScriptServlet description

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-whiteboard.git

commit a616a42fc5a0129bc25e3f832bb0538b6a1f1b50
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Mon Apr 30 12:50:02 2018 +0200

    Added capability for BundledScriptServlet description
---
 .../apache/sling/scripting/resolver/internal/BundledScriptTracker.java  | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptTracker.java b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptTracker.java
index afb7f17..451b338 100644
--- a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptTracker.java
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/BundledScriptTracker.java
@@ -50,6 +50,7 @@ import org.apache.sling.commons.osgi.PropertiesUtil;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
+import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.framework.Version;
@@ -121,6 +122,7 @@ public class BundledScriptTracker implements BundleTrackerCustomizer<List<Servic
                 {
                     Hashtable<String, Object> properties = new Hashtable<>();
                     properties.put(ServletResolverConstants.SLING_SERVLET_NAME, BundledScriptServlet.class.getName());
+                    properties.put(Constants.SERVICE_DESCRIPTION, cap.toString());
 
                     Map<String, Object> attributes = cap.getAttributes();
                     String resourceType = (String) attributes.get(NS_SLING_RESOURCE_TYPE);

-- 
To stop receiving notification emails like this one, please contact
radu@apache.org.

[sling-whiteboard] 03/03: Added the scripting resolver as a lazy binding

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-whiteboard.git

commit 914df20fa98ae36c477e1a8608b3a1a284bdb990
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Mon Apr 30 12:50:35 2018 +0200

    Added the scripting resolver as a lazy binding
---
 .../scripting/resolver/internal/LazyBindings.java  |  78 ++++++++++++++
 .../resolver/internal/ScriptContextProvider.java   |  13 ++-
 .../resolver/internal/LazyBindingsTest.java        | 112 +++++++++++++++++++++
 3 files changed, 200 insertions(+), 3 deletions(-)

diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/LazyBindings.java b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/LazyBindings.java
new file mode 100644
index 0000000..7a45b01
--- /dev/null
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/src/main/java/org/apache/sling/scripting/resolver/internal/LazyBindings.java
@@ -0,0 +1,78 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~     http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+package org.apache.sling.scripting.resolver.internal;
+
+import java.util.AbstractMap;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import javax.script.SimpleBindings;
+
+class LazyBindings extends SimpleBindings {
+
+    private final Map<String, Supplier<Object>> suppliers;
+
+    LazyBindings(Map<String, Supplier<Object>> suppliers) {
+        this.suppliers = suppliers;
+    }
+
+    @Override
+    public Object get(Object key) {
+        if (!super.containsKey(key) && suppliers.containsKey(key)) {
+            Object value = suppliers.get(key).get();
+            put((String) key, value);
+        }
+        return super.get(key);
+    }
+
+    @Override
+    public boolean containsKey(Object key) {
+        return super.containsKey(key) || suppliers.containsKey(key);
+    }
+
+    @Override
+    public Set<Entry<String, Object>> entrySet() {
+        Set<Entry<String, Object>> entrySet = new HashSet<>(super.entrySet());
+        for (Map.Entry<String, Supplier<Object>> supplierEntry : suppliers.entrySet()) {
+            entrySet.add(new AbstractMap.SimpleEntry<>(supplierEntry.getKey(), supplierEntry.getValue().get()));
+        }
+        return Collections.unmodifiableSet(entrySet);
+    }
+
+    @Override
+    public Set<String> keySet() {
+        Set<String> keySet = new HashSet<>(super.keySet());
+        if (!suppliers.isEmpty()) {
+            keySet.addAll(suppliers.keySet());
+        }
+        return Collections.unmodifiableSet(keySet);
+    }
+
+    @Override
+    public Object getOrDefault(Object key, Object defaultValue) {
+        if (!super.containsKey(key) && suppliers.containsKey(key)) {
+            Object value = suppliers.get(key).get();
+            put((String) key, value);
+        }
+        return super.getOrDefault(key, defaultValue);
+    }
+}
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 4ebb36b..d1459ce 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
@@ -21,8 +21,11 @@ package org.apache.sling.scripting.resolver.internal;
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
+import java.util.function.Supplier;
 
 import javax.script.Bindings;
 import javax.script.ScriptContext;
@@ -50,6 +53,8 @@ import org.slf4j.LoggerFactory;
 )
 public class ScriptContextProvider {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(ScriptContextProvider.class);
+
     private BundleContext m_bundleContext;
 
     private static final Set<String> PROTECTED_BINDINGS = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
@@ -97,9 +102,11 @@ public class ScriptContextProvider {
             bindingsValuesProvider.addBindings(protectedBindings);
         }
         ScriptContext scriptContext = new BundledScriptContext();
-        scriptContext.setBindings(new SimpleBindings(), SlingScriptConstants.SLING_SCOPE);
-        scriptContext.setAttribute(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER, scriptingResourceResolverProvider
-                .getRequestScopedResourceResolver(), SlingScriptConstants.SLING_SCOPE);
+        Map<String, Supplier<Object>> slingBindingsSuppliers = new HashMap<>();
+        slingBindingsSuppliers.put(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER,
+                () -> scriptingResourceResolverProvider.getRequestScopedResourceResolver());
+        LazyBindings slingScopeBindings = new LazyBindings(Collections.unmodifiableMap(slingBindingsSuppliers));
+        scriptContext.setBindings(slingScopeBindings, SlingScriptConstants.SLING_SCOPE);
         scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
         scriptContext.setWriter(response.getWriter());
         scriptContext.setErrorWriter(new LogWriter(scriptLogger));
diff --git a/scripting-resolver/org-apache-sling-scripting-resolver/src/test/java/org/apache/sling/scripting/resolver/internal/LazyBindingsTest.java b/scripting-resolver/org-apache-sling-scripting-resolver/src/test/java/org/apache/sling/scripting/resolver/internal/LazyBindingsTest.java
new file mode 100644
index 0000000..b77c29b
--- /dev/null
+++ b/scripting-resolver/org-apache-sling-scripting-resolver/src/test/java/org/apache/sling/scripting/resolver/internal/LazyBindingsTest.java
@@ -0,0 +1,112 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~     http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+package org.apache.sling.scripting.resolver.internal;
+
+import java.util.AbstractMap;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+public class LazyBindingsTest {
+
+    private static final String THE_QUESTION = "the answer";
+    private static final int THE_ANSWER = 42;
+
+    private Set<String> usedSuppliers;
+    private LazyBindings lazyBindings;
+
+    @Before
+    public void setUp() {
+        usedSuppliers = new HashSet<>();
+        final Map<String, Supplier<Object>> supplierMap = new HashMap<>();
+        supplierMap.put(THE_QUESTION, () -> {
+            usedSuppliers.add(THE_QUESTION);
+            return THE_ANSWER;
+        });
+        lazyBindings = new LazyBindings(supplierMap);
+    }
+
+    @After
+    public void tearDown() {
+        usedSuppliers = null;
+        lazyBindings = null;
+    }
+
+    @Test
+    public void testLazyGet() {
+        assertFalse(usedSuppliers.contains(THE_QUESTION));
+        assertEquals(THE_ANSWER, lazyBindings.get(THE_QUESTION));
+        assertTrue(usedSuppliers.contains(THE_QUESTION));
+        assertNull(lazyBindings.get("none"));
+    }
+
+    @Test
+    public void testLazyContainsKey() {
+        lazyBindings.put("a", 0);
+        assertTrue(lazyBindings.containsKey(THE_QUESTION));
+        assertTrue(lazyBindings.containsKey("a"));
+        assertFalse(usedSuppliers.contains(THE_QUESTION));
+
+    }
+
+    @Test
+    public void testLazyEntrySet() {
+        lazyBindings.put("a", 0);
+        Set<Map.Entry<String, Object>> expectedEntrySet = new HashSet<>();
+        expectedEntrySet.add(new AbstractMap.SimpleEntry<>(THE_QUESTION, THE_ANSWER));
+        expectedEntrySet.add(new AbstractMap.SimpleEntry<>("a", 0));
+        assertFalse(usedSuppliers.contains(THE_QUESTION));
+        assertEquals(expectedEntrySet, lazyBindings.entrySet());
+        assertTrue(usedSuppliers.contains(THE_QUESTION));
+    }
+
+    @Test
+    public void testLazyKeySet() {
+        lazyBindings.put("a", 0);
+        assertEquals(new HashSet<>(Arrays.asList(THE_QUESTION, "a")), lazyBindings.keySet());
+        assertFalse(usedSuppliers.contains(THE_QUESTION));
+    }
+
+    @Test
+    public void testLazyGetOrDefault() {
+        lazyBindings.put("a", 0);
+        assertEquals(0, lazyBindings.getOrDefault("a", 1));
+        assertFalse(usedSuppliers.contains(THE_QUESTION));
+        assertEquals(THE_ANSWER, lazyBindings.getOrDefault(THE_QUESTION, THE_ANSWER + 1));
+        assertTrue(usedSuppliers.contains(THE_QUESTION));
+        assertEquals(1, lazyBindings.getOrDefault("b", 1));
+    }
+
+
+
+
+
+}

-- 
To stop receiving notification emails like this one, please contact
radu@apache.org.