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

rombert pushed a commit to annotated tag 
org.apache.sling.scripting.sightly.js.provider-1.0.14
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly-js-provider.git

commit b24bc8bcc384b96755d6882dac49b6e1b4c092a9
Author: Radu Cotescu <[email protected]>
AuthorDate: Tue Oct 4 15:06:58 2016 +0000

    SLING-5254 - Remove getAdministrativeResourceResolver() from the Sightly JS 
Provider
    
    git-svn-id: 
https://svn.apache.org/repos/asf/sling/trunk/bundles/scripting/sightly/js-use-provider@1763288
 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  2 +-
 .../js/impl/jsapi/SlyBindingsValuesProvider.java   | 78 +++++++++-------------
 2 files changed, 34 insertions(+), 46 deletions(-)

diff --git a/pom.xml b/pom.xml
index 85f0714..d980884 100644
--- a/pom.xml
+++ b/pom.xml
@@ -101,7 +101,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.api</artifactId>
-            <version>2.4.0</version>
+            <version>2.5.0</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
diff --git 
a/src/main/java/org/apache/sling/scripting/sightly/js/impl/jsapi/SlyBindingsValuesProvider.java
 
b/src/main/java/org/apache/sling/scripting/sightly/js/impl/jsapi/SlyBindingsValuesProvider.java
index 648f00c..9ecb4e2 100644
--- 
a/src/main/java/org/apache/sling/scripting/sightly/js/impl/jsapi/SlyBindingsValuesProvider.java
+++ 
b/src/main/java/org/apache/sling/scripting/sightly/js/impl/jsapi/SlyBindingsValuesProvider.java
@@ -31,6 +31,7 @@ import javax.script.ScriptEngineManager;
 import javax.script.SimpleBindings;
 import javax.servlet.http.HttpServletRequest;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
@@ -94,6 +95,7 @@ public class SlyBindingsValuesProvider {
     private static final String REQ_NS = 
SlyBindingsValuesProvider.class.getCanonicalName();
 
     private static final Logger LOGGER = 
LoggerFactory.getLogger(SlyBindingsValuesProvider.class);
+    private static final String SLING_SCRIPTING_USER = "sling-scripting";
 
     @Reference
     private ScriptEngineManager scriptEngineManager = null;
@@ -104,8 +106,8 @@ public class SlyBindingsValuesProvider {
     private final AsyncExtractor asyncExtractor = new AsyncExtractor();
     private final JsValueAdapter jsValueAdapter = new 
JsValueAdapter(asyncExtractor);
 
-    private Map<String, String> scriptPaths = new HashMap<String, String>();
-    private Map<String, Function> factories = new HashMap<String, Function>();
+    private Map<String, String> scriptPaths = new HashMap<>();
+    private Map<String, Function> factories = new HashMap<>();
 
     private Script qScript;
     private final ScriptableObject qScope = createQScope();
@@ -145,7 +147,7 @@ public class SlyBindingsValuesProvider {
     protected void activate(ComponentContext componentContext) {
         Dictionary properties = componentContext.getProperties();
         String[] factories = 
PropertiesUtil.toStringArray(properties.get(SCR_PROP_JS_BINDING_IMPLEMENTATIONS),
 new String[]{SLING_NS_PATH});
-        scriptPaths = new HashMap<String, String>(factories.length);
+        scriptPaths = new HashMap<>(factories.length);
         for (String f : factories) {
             String[] parts = f.split(":");
             if (parts.length == 2) {
@@ -186,6 +188,7 @@ public class SlyBindingsValuesProvider {
 
     private void ensureFactoriesLoaded(Bindings bindings) {
         JsEnvironment jsEnvironment = null;
+        ResourceResolver resolver = null;
         try {
             ScriptEngine scriptEngine = obtainEngine();
             if (scriptEngine == null) {
@@ -193,43 +196,39 @@ public class SlyBindingsValuesProvider {
             }
             jsEnvironment = new JsEnvironment(scriptEngine);
             jsEnvironment.initialize();
-            factories = new HashMap<String, Function>(scriptPaths.size());
+            final Map<String, Object> authenticationInfo = new HashMap<>(1);
+            authenticationInfo.put(ResourceResolverFactory.SUBSERVICE, 
SLING_SCRIPTING_USER);
+            resolver = rrf.getServiceResourceResolver(authenticationInfo);
+            factories = new HashMap<>(scriptPaths.size());
             for (Map.Entry<String, String> entry : scriptPaths.entrySet()) {
-                factories.put(entry.getKey(), loadFactory(jsEnvironment, 
entry.getValue(), bindings));
+                factories.put(entry.getKey(), loadFactory(resolver, 
jsEnvironment, entry.getValue(), bindings));
             }
-            qScript = loadQScript();
+            qScript = loadQScript(resolver);
+        } catch (LoginException e) {
+            LOGGER.error("Cannot load HTL Use-API factories.", e);
         } finally {
             if (jsEnvironment != null) {
                 jsEnvironment.cleanup();
             }
-        }
-    }
-
-    private Function loadFactory(JsEnvironment jsEnvironment, String path, 
Bindings bindings) {
-        ResourceResolver resolver = null;
-        try {
-            resolver = rrf.getAdministrativeResourceResolver(null);
-            Resource resource = resolver.getResource(path);
-            if (resource == null) {
-                throw new SightlyException("Sly namespace loader could not 
find the following script: " + path);
-
-            }
-            AsyncContainer container = jsEnvironment.runResource(resource, 
createBindings(bindings), new SimpleBindings());
-            Object obj = container.getResult();
-            if (!(obj instanceof Function)) {
-                throw new SightlyException("Script " + path + " was expected 
to return a function.");
-            }
-            return (Function) obj;
-        } catch (LoginException e) {
-            LOGGER.error("Cannot evaluate script " + path, e);
-            return null;
-        } finally {
             if (resolver != null) {
                 resolver.close();
             }
         }
     }
 
+    private Function loadFactory(ResourceResolver resolver, JsEnvironment 
jsEnvironment, String path, Bindings bindings) {
+        Resource resource = resolver.getResource(path);
+        if (resource == null) {
+            throw new SightlyException("Sly namespace loader could not find 
the following script: " + path);
+        }
+        AsyncContainer container = jsEnvironment.runResource(resource, 
createBindings(bindings), new SimpleBindings());
+        Object obj = container.getResult();
+        if (!(obj instanceof Function)) {
+            throw new SightlyException("Script " + path + " was expected to 
return a function.");
+        }
+        return (Function) obj;
+    }
+
     private Bindings createBindings(Bindings global) {
         Bindings bindings = new SimpleBindings();
         bindings.putAll(global);
@@ -280,15 +279,13 @@ public class SlyBindingsValuesProvider {
         return module.getExports();
     }
 
-    private Script loadQScript() {
-        ResourceResolver resourceResolver = null;
+    private Script loadQScript(ResourceResolver resolver) {
         Context context = Context.enter();
         context.initStandardObjects();
         context.setOptimizationLevel(9);
         InputStream reader = null;
         try {
-            resourceResolver = rrf.getAdministrativeResourceResolver(null);
-            Resource resource = resourceResolver.getResource(Q_PATH);
+            Resource resource = resolver.getResource(Q_PATH);
             if (resource == null) {
                 LOGGER.warn("Could not load Q library at path: " + Q_PATH);
                 return null;
@@ -299,22 +296,13 @@ public class SlyBindingsValuesProvider {
                 return null;
             }
             return context.compileReader(new InputStreamReader(reader), 
Q_PATH, 0, null);
-        } catch (Exception e) {
-            e.printStackTrace();
-            return null;
+        } catch (IOException e) {
+            LOGGER.error("Unable to compile the Q library at path " + Q_PATH + 
".", e);
         } finally {
             Context.exit();
-            if (reader != null) {
-                try {
-                    reader.close();
-                } catch (IOException e) {
-                    LOGGER.error("Error while closing reader", e);
-                }
-            }
-            if (resourceResolver != null) {
-                resourceResolver.close();
-            }
+            IOUtils.closeQuietly(reader);
         }
+        return null;
     }
 
 }

-- 
To stop receiving notification emails like this one, please contact
"[email protected]" <[email protected]>.

Reply via email to