You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:11:08 UTC

[sling-org-apache-sling-scripting-sightly-js-provider] 11/18: SLING-4400 - The Use-API should return a falsy value if no Use-provider was able to solve the requested object

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.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly-js-provider.git

commit c60ac619b163fcb2a4e9ea7e0fa7e107bcf2ab76
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Thu Feb 12 13:29:57 2015 +0000

    SLING-4400 - The Use-API should return a falsy value if no Use-provider was able to solve the requested object
    
    * return null when no UseProvider was able to provide an object (an error is logged in this case)
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/scripting/sightly/js-use-provider@1659248 13f79535-47bb-0310-9956-ffa450edef68
---
 .../scripting/sightly/js/impl/JsUseProvider.java   | 53 +++++-----------------
 1 file changed, 12 insertions(+), 41 deletions(-)

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 b779c81..665ff4e 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
@@ -27,11 +27,14 @@ import org.apache.felix.scr.annotations.Properties;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.SlingException;
 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.impl.engine.runtime.RenderContextImpl;
 import org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer;
 import org.apache.sling.scripting.sightly.js.impl.async.AsyncExtractor;
 import org.apache.sling.scripting.sightly.js.impl.jsapi.SlyBindingsValuesProvider;
@@ -73,9 +76,6 @@ public class JsUseProvider implements UseProvider {
     private ScriptEngineManager scriptEngineManager = null;
 
     @Reference
-    private ResourceResolverFactory rrf = null;
-
-    @Reference
     private SlyBindingsValuesProvider slyBindingsValuesProvider = null;
 
     @Override
@@ -85,46 +85,17 @@ public class JsUseProvider implements UseProvider {
         if (!Utils.isJsScript(identifier)) {
             return ProviderOutcome.failure();
         }
-        ScriptEngine jsEngine = obtainEngine();
+        ScriptEngine jsEngine = scriptEngineManager.getEngineByName(JS_ENGINE_NAME);
         if (jsEngine == null) {
-            log.warn("No JavaScript engine defined");
-            return ProviderOutcome.failure();
+            return ProviderOutcome.failure(new SightlyException("No JavaScript engine was defined."));
         }
         SlingScriptHelper scriptHelper = Utils.getHelper(globalBindings);
-        JsEnvironment environment = null;
-        ResourceResolver adminResolver = null;
-        try {
-            environment = new JsEnvironment(jsEngine);
-            environment.initialize();
-            String callerPath = scriptHelper.getScript().getScriptResource().getPath();
-            boolean allowedExecutablePath = false;
-            adminResolver = rrf.getAdministrativeResourceResolver(null);
-            for (String path : adminResolver.getSearchPath()) {
-                if (callerPath.startsWith(path)) {
-                    allowedExecutablePath = true;
-                    break;
-                }
-            }
-            if (allowedExecutablePath) {
-                Resource caller = adminResolver.getResource(callerPath);
-                AsyncContainer asyncContainer = environment.run(caller, identifier, globalBindings, arguments);
-                return ProviderOutcome.success(jsValueAdapter.adapt(asyncContainer));
-            }
-            return ProviderOutcome.failure();
-        } catch (LoginException e) {
-            log.error("Unable to load JS script " + identifier, e);
-        } finally {
-            if (environment != null) {
-                environment.cleanup();
-            }
-            if (adminResolver != null) {
-                adminResolver.close();
-            }
-        }
-        return ProviderOutcome.failure();
-    }
-
-    private ScriptEngine obtainEngine() {
-        return scriptEngineManager.getEngineByName(JS_ENGINE_NAME);
+        JsEnvironment environment = new JsEnvironment(jsEngine);
+        environment.initialize();
+        String callerPath = scriptHelper.getScript().getScriptResource().getPath();
+        ResourceResolver adminResolver = renderContext.getScriptResourceResolver();
+        Resource caller = adminResolver.getResource(callerPath);
+        AsyncContainer asyncContainer = environment.run(caller, identifier, globalBindings, arguments);
+        return ProviderOutcome.success(jsValueAdapter.adapt(asyncContainer));
     }
 }

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