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 2020/04/20 17:07:16 UTC

[sling-org-apache-sling-scripting-sightly-js-provider] branch master updated: SLING-9320 - Allow precompiled units to access objects from the same bundle through the Use API

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-sightly-js-provider.git


The following commit(s) were added to refs/heads/master by this push:
     new f63bda9  SLING-9320 - Allow precompiled units to access objects from the same bundle through the Use API
f63bda9 is described below

commit f63bda93d7ddf795f5a4881f703dc65ed186a0d2
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Mon Apr 20 19:07:02 2020 +0200

    SLING-9320 - Allow precompiled units to access objects from the same bundle through the Use API
    
    * extended the JsUseProvider to allow finding dependencies belonging to the same
    resource type even if the dependencies are served by different providers
---
 .../scripting/sightly/js/impl/JsUseProvider.java   | 13 +++++++++----
 .../sightly/js/impl/use/DependencyResolver.java    | 22 +++++++++++++++-------
 .../scripting/sightly/js/impl/use/UseFunction.java | 17 +++++++++++++++--
 3 files changed, 39 insertions(+), 13 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 d5c6b58..7cc1902 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
@@ -99,10 +99,15 @@ public class JsUseProvider implements UseProvider {
             environment = new JsEnvironment(jsEngine, dependencyResolver);
             environment.initialize();
             ScriptNameAwareReader reader = dependencyResolver.resolve(globalBindings, identifier);
-            globalBindings.put(ScriptEngine.FILENAME, reader.getScriptName());
-            proxyAsyncScriptableFactory.registerProxies(slingScriptingResolver, environment, globalBindings);
-            AsyncContainer asyncContainer = environment.runScript(reader, globalBindings, arguments);
-            return ProviderOutcome.success(jsValueAdapter.adapt(asyncContainer));
+            if (reader != null) {
+                globalBindings.put(ScriptEngine.FILENAME, reader.getScriptName());
+                proxyAsyncScriptableFactory.registerProxies(slingScriptingResolver, environment, globalBindings);
+                AsyncContainer asyncContainer = environment.runScript(reader, globalBindings, arguments);
+                return ProviderOutcome.success(jsValueAdapter.adapt(asyncContainer));
+            }
+            return ProviderOutcome.failure();
+        } catch (Exception e) {
+            return ProviderOutcome.failure(e);
         } finally {
             if (environment != null) {
                 environment.cleanup();
diff --git a/src/main/java/org/apache/sling/scripting/sightly/js/impl/use/DependencyResolver.java b/src/main/java/org/apache/sling/scripting/sightly/js/impl/use/DependencyResolver.java
index 073b84a..44fc612 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/js/impl/use/DependencyResolver.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/js/impl/use/DependencyResolver.java
@@ -32,6 +32,7 @@ import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.scripting.SlingBindings;
 import org.apache.sling.api.scripting.SlingScript;
 import org.apache.sling.api.scripting.SlingScriptHelper;
 import org.apache.sling.scripting.core.ScriptNameAwareReader;
@@ -54,7 +55,7 @@ public class DependencyResolver {
         this.scriptingResourceResolver = scriptingResourceResolver;
     }
 
-    public @NotNull ScriptNameAwareReader resolve(Bindings bindings, String dependency) {
+    public @Nullable ScriptNameAwareReader resolve(Bindings bindings, String dependency) {
         if (!Utils.isJsScript(dependency)) {
             throw new SightlyException("Only JS scripts are allowed as dependencies. Invalid dependency: " + dependency);
         }
@@ -85,6 +86,16 @@ public class DependencyResolver {
                         if (caller == null && slingScript != null) {
                             caller = scriptingResourceResolver.getResource(slingScript.getScriptResource().getPath());
                         }
+                        if (caller == null) {
+                            Resource resource = (Resource) bindings.get(SlingBindings.RESOURCE);
+                            if (resource != null) {
+                                String type = resource.getResourceType();
+                                caller = scriptingResourceResolver.getResource(type);
+                                if (caller != null) {
+                                    scriptResource = Utils.getScriptResource(caller, dependency, bindings);
+                                }
+                            }
+                        }
                         if (caller != null) {
                             scriptResource = Utils.getScriptResource(caller, dependency, bindings);
                         }
@@ -98,12 +109,9 @@ public class DependencyResolver {
         } catch (IOException e) {
             ioException = e;
         }
-        if (reader == null) {
-            SightlyException sightlyException = new SightlyException(String.format("Unable to load script dependency %s.", dependency));
-            if (ioException != null) {
-                sightlyException.initCause(ioException);
-            }
-            throw sightlyException;
+        if (ioException != null) {
+            throw  new SightlyException(String.format("Unable to load script dependency %s.",
+                    dependency), ioException);
         }
         return reader;
     }
diff --git a/src/main/java/org/apache/sling/scripting/sightly/js/impl/use/UseFunction.java b/src/main/java/org/apache/sling/scripting/sightly/js/impl/use/UseFunction.java
index 2cb098b..094e9fb 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/js/impl/use/UseFunction.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/js/impl/use/UseFunction.java
@@ -25,8 +25,11 @@ import java.util.List;
 import java.util.Map;
 
 import javax.script.Bindings;
+import javax.script.ScriptEngine;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.scripting.core.ScriptNameAwareReader;
+import org.apache.sling.scripting.sightly.SightlyException;
 import org.apache.sling.scripting.sightly.js.impl.JsEnvironment;
 import org.apache.sling.scripting.sightly.js.impl.Utils;
 import org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer;
@@ -82,8 +85,18 @@ public class UseFunction extends BaseFunction {
             final Object[] dependencies = new Object[depNames.size()];
             for (int i = 0; i < depNames.size(); i++) {
                 final int dependencyPos = i;
-                ScriptNameAwareReader dependency = dependencyResolver.resolve(globalBindings, depNames.get(i));
-                jsEnvironment.runScript(dependency, globalBindings, Utils.EMPTY_BINDINGS, arg -> {
+                String dependency = depNames.get(i);
+                ScriptNameAwareReader dependencyReader = dependencyResolver.resolve(globalBindings, dependency);
+                if (dependencyReader == null) {
+                    String caller = (String) globalBindings.get(ScriptEngine.FILENAME);
+                    if (StringUtils.isNotEmpty(caller)) {
+                        throw new SightlyException(String.format("Cannot locate use-function dependency %s from caller %s.", dependency,
+                                caller));
+                    } else {
+                        throw new SightlyException(String.format("Cannot locate use-function dependency %s.", dependencies));
+                    }
+                }
+                jsEnvironment.runScript(dependencyReader, globalBindings, Utils.EMPTY_BINDINGS, arg -> {
                     counter[0]--;
                     dependencies[dependencyPos] = arg;
                     if (counter[0] == 0) {