sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From romb...@apache.org
Subject [sling-org-apache-sling-scripting-sightly-js-provider] 02/04: SLING-6156 - The JsUseProvider should use the sling-scripting service user for solving scripting dependencies
Date Tue, 07 Nov 2017 10:12:02 GMT
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.16
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly-js-provider.git

commit d0b8c1502c3b0386b0dfe767209977aa0df6e6a3
Author: Radu Cotescu <radu@apache.org>
AuthorDate: Mon Oct 17 11:26:47 2016 +0000

    SLING-6156 - The JsUseProvider should use the sling-scripting service user for solving
scripting dependencies
    
    * switched to using a service user for solving dependencies
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/scripting/sightly/js-use-provider@1765242
13f79535-47bb-0310-9956-ffa450edef68
---
 .../scripting/sightly/js/impl/JsUseProvider.java   | 26 +++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/sling/scripting/sightly/js/impl/JsUseProvider.java b/src/main/java/org/apache/sling/scripting/sightly/js/impl/JsUseProvider.java
index 0e17a7b..f80471c 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/js/impl/JsUseProvider.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/js/impl/JsUseProvider.java
@@ -18,11 +18,16 @@
  ******************************************************************************/
 package org.apache.sling.scripting.sightly.js.impl;
 
+import java.util.HashMap;
+import java.util.Map;
 import javax.script.Bindings;
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngineManager;
 
+import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.apache.sling.api.scripting.SlingScriptHelper;
 import org.apache.sling.scripting.sightly.SightlyException;
 import org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer;
@@ -38,6 +43,8 @@ import org.osgi.service.component.annotations.Reference;
 import org.osgi.service.metatype.annotations.AttributeDefinition;
 import org.osgi.service.metatype.annotations.Designate;
 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Use provider for JavaScript Use-API objects.
@@ -69,8 +76,10 @@ public class JsUseProvider implements UseProvider {
 
     }
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(JsUseProvider.class);
     private static final String JS_ENGINE_NAME = "javascript";
     private static final JsValueAdapter jsValueAdapter = new JsValueAdapter(new AsyncExtractor());
+    private static final String SLING_SCRIPTING_USER = "sling-scripting";
 
     @Reference
     private ScriptEngineManager scriptEngineManager = null;
@@ -78,6 +87,9 @@ public class JsUseProvider implements UseProvider {
     @Reference
     private ProxyAsyncScriptableFactory proxyAsyncScriptableFactory = null;
 
+    @Reference
+    private ResourceResolverFactory rrf = null;
+
     @Override
     public ProviderOutcome provide(String identifier, RenderContext renderContext, Bindings
arguments) {
         Bindings globalBindings = renderContext.getBindings();
@@ -90,18 +102,30 @@ public class JsUseProvider implements UseProvider {
         }
         SlingScriptHelper scriptHelper = Utils.getHelper(globalBindings);
         JsEnvironment environment = null;
+        ResourceResolver slingScriptingResolver = null;
         try {
             environment = new JsEnvironment(jsEngine);
             environment.initialize();
-            Resource scriptResource = Utils.getScriptResource(scriptHelper.getScript().getScriptResource(),
identifier, globalBindings);
+            Map<String, Object> authenticationInfo = new HashMap<String, Object>()
{{
+                put(ResourceResolverFactory.SUBSERVICE, SLING_SCRIPTING_USER);
+            }};
+            slingScriptingResolver = rrf.getServiceResourceResolver(authenticationInfo);
+            Resource callerScript = slingScriptingResolver.getResource(scriptHelper.getScript().getScriptResource().getPath());
+            Resource scriptResource = Utils.getScriptResource(callerScript, identifier, globalBindings);
             globalBindings.put(ScriptEngine.FILENAME, scriptResource.getPath());
             proxyAsyncScriptableFactory.registerProxies(globalBindings);
             AsyncContainer asyncContainer = environment.runResource(scriptResource, globalBindings,
arguments);
             return ProviderOutcome.success(jsValueAdapter.adapt(asyncContainer));
+        } catch (LoginException e) {
+            LOGGER.error("Cannot obtain a resource resolver backed by the " + SLING_SCRIPTING_USER
+ " service user.", e);
+            return ProviderOutcome.failure(e);
         } finally {
             if (environment != null) {
                 environment.cleanup();
             }
+            if (slingScriptingResolver != null) {
+                slingScriptingResolver.close();
+            }
         }
     }
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <commits@sling.apache.org>.

Mime
View raw message