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 c36961e  SLING-9580 - Make the dependency resolution more 
resource-type centric
c36961e is described below

commit c36961edba3e616beed4dac583894bd6aa45a1f1
Author: Radu Cotescu <[email protected]>
AuthorDate: Fri Jul 10 13:23:20 2020 +0200

    SLING-9580 - Make the dependency resolution more resource-type centric
    
    * simplified the DependencyResolver implementation
    * removed unused methods from Utils
    * removed HTL engine dependency, since it's not required any more
---
 pom.xml                                            |  6 --
 .../sling/scripting/sightly/js/impl/Utils.java     | 55 --------------
 .../sightly/js/impl/use/DependencyResolver.java    | 83 ++++++++++++++--------
 3 files changed, 52 insertions(+), 92 deletions(-)

diff --git a/pom.xml b/pom.xml
index 6dc3832..1ac1e48 100644
--- a/pom.xml
+++ b/pom.xml
@@ -119,12 +119,6 @@
             <version>1.2.3-1.4.0-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
-        <dependency>
-            <groupId>org.apache.sling</groupId>
-            <artifactId>org.apache.sling.scripting.sightly</artifactId>
-            <version>1.4.1-1.4.0-SNAPSHOT</version>
-            <scope>provided</scope>
-        </dependency>
 
         <!-- Logging -->
         <dependency>
diff --git 
a/src/main/java/org/apache/sling/scripting/sightly/js/impl/Utils.java 
b/src/main/java/org/apache/sling/scripting/sightly/js/impl/Utils.java
index 01d5521..612713c 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/js/impl/Utils.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/js/impl/Utils.java
@@ -22,14 +22,8 @@ import javax.script.Bindings;
 import javax.script.SimpleBindings;
 
 import org.apache.commons.lang3.StringUtils;
-import org.apache.sling.api.SlingHttpServletRequest;
-import org.apache.sling.api.resource.Resource;
-import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.api.scripting.SlingBindings;
 import org.apache.sling.api.scripting.SlingScriptHelper;
-import org.apache.sling.scripting.sightly.SightlyException;
-import org.apache.sling.scripting.sightly.engine.ResourceResolution;
 
 /**
  * Utilities for script evaluation
@@ -47,53 +41,4 @@ public class Utils {
         String extension = StringUtils.substringAfterLast(identifier, ".");
         return JS_EXTENSION.equalsIgnoreCase(extension);
     }
-
-    public static Resource getScriptResource(Resource caller, String path, 
Bindings bindings) {
-        Resource scriptResource = caller.getChild(path);
-        if (scriptResource == null) {
-            Resource componentCaller = ResourceResolution
-                    .getResourceForRequest(caller.getResourceResolver(), 
(SlingHttpServletRequest) bindings.get(SlingBindings.REQUEST));
-            if (isResourceOverlay(caller, componentCaller)) {
-                scriptResource = 
ResourceResolution.getResourceFromSearchPath(componentCaller, path);
-            } else {
-                scriptResource = 
ResourceResolution.getResourceFromSearchPath(caller, path);
-            }
-        }
-        if (scriptResource == null) {
-            throw new SightlyException("Required script resource could not be 
located: " + path + ". The caller is " + caller.getPath());
-        }
-        return scriptResource;
-    }
-
-    /**
-     * Using the inheritance chain created with the help of {@code 
sling:resourceSuperType} this method checks if {@code resourceB} inherits
-     * from {@code resourceA}. In case {@code resourceA} is a {@code nt:file}, 
its parent will be used for the inheritance check.
-     *
-     * @param resourceA the base resource
-     * @param resourceB the potentially overlaid resource
-     * @return {@code true} if {@code resourceB} overlays {@code resourceB}, 
{@code false} otherwise
-     */
-    private static boolean isResourceOverlay(Resource resourceA, Resource 
resourceB) {
-        String resourceBSuperType = resourceB.getResourceSuperType();
-        if (StringUtils.isNotEmpty(resourceBSuperType)) {
-            ResourceResolver resolver = resourceA.getResourceResolver();
-            String parentResourceType = resourceA.getResourceType();
-            if ("nt:file".equals(parentResourceType)) {
-                parentResourceType = 
ResourceUtil.getParent(resourceA.getPath());
-                if (parentResourceType.equals(resourceB.getPath())) {
-                    return true;
-                }
-            }
-            Resource parentB = resolver.getResource(resourceBSuperType);
-            while (parentB != null && !"/".equals(parentB.getPath()) && 
StringUtils.isNotEmpty(resourceBSuperType)) {
-                if (parentB.getPath().equals(parentResourceType)) {
-                    return true;
-                }
-                resourceBSuperType = parentB.getResourceSuperType();
-                parentB = resolver.getResource(resourceBSuperType);
-            }
-        }
-        return false;
-    }
-
 }
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 d9422ec..f3f502a 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
@@ -29,10 +29,11 @@ import javax.script.ScriptEngine;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.sling.api.resource.NonExistingResource;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceUtil;
 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;
 import org.apache.sling.scripting.sightly.SightlyException;
@@ -58,49 +59,69 @@ public class DependencyResolver {
         ScriptNameAwareReader reader = null;
         IOException ioException = null;
         try {
-            if (reader == null) {
-                Resource scriptResource = null;
-                if (dependency.startsWith("/")) {
-                    scriptResource = 
scriptingResourceResolver.getResource(dependency);
+            Resource scriptResource = null;
+            if (dependency.startsWith("/")) {
+                scriptResource = 
scriptingResourceResolver.getResource(dependency);
+            }
+            if (scriptResource == null) {
+                String callerName = (String) 
bindings.get(ScriptEngine.FILENAME);
+                Resource caller = null;
+                if (StringUtils.isNotEmpty(callerName)) {
+                    caller = scriptingResourceResolver.getResource(callerName);
                 }
-                if (scriptResource == null) {
+                if (caller == null) {
                     SlingScriptHelper scriptHelper = Utils.getHelper(bindings);
                     if (scriptHelper != null) {
-                        String callerName = (String) 
bindings.get(ScriptEngine.FILENAME);
-                        Resource caller = null;
-                        if (StringUtils.isNotEmpty(callerName)) {
-                            caller = 
scriptingResourceResolver.getResource(callerName);
-                        }
-                        SlingScript slingScript = scriptHelper.getScript();
-                        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);
+                        caller = scriptHelper.getScript().getScriptResource();
+                    }
+                }
+                if (caller != null && Utils.isJsScript(caller.getName()) &&
+                        
("sling/bundle/resource".equals(caller.getResourceType()) || 
"nt:file".equals(caller.getResourceType()))) {
+                    caller = caller.getParent();
+                    if (caller != null) {
+                        scriptResource = caller.getChild(dependency);
+                    }
+                }
+
+            }
+            if (scriptResource == null) {
+                Resource requestResource = (Resource) 
bindings.get(SlingBindings.RESOURCE);
+                String type = requestResource.getResourceType();
+                while (scriptResource == null && type != null) {
+                    Resource servletResource = null;
+                    if (!type.startsWith("/")) {
+                        for (String searchPath : 
scriptingResourceResolver.getSearchPath()) {
+                            String normalizedPath = 
ResourceUtil.normalize(searchPath + "/" + type);
+                            if (normalizedPath != null) {
+                                servletResource =
+                                        
scriptingResourceResolver.resolve(normalizedPath);
+                                if (!(servletResource instanceof 
NonExistingResource)) {
+                                    break;
                                 }
                             }
                         }
-                        if (caller != null) {
-                            scriptResource = Utils.getScriptResource(caller, 
dependency, bindings);
-                        }
+                    } else {
+                        servletResource = 
scriptingResourceResolver.resolve(type);
+                    }
+                    if (servletResource != null) {
+                        scriptResource = servletResource.getChild(dependency);
+                        type = servletResource.getResourceSuperType();
+                    } else {
+                        type = null;
                     }
                 }
-                if (scriptResource != null) {
-                    reader = new ScriptNameAwareReader(new 
StringReader(IOUtils.toString(scriptResource.adaptTo(InputStream.class),
-                            StandardCharsets.UTF_8)), 
scriptResource.getPath());
-                }
             }
+
+            if (scriptResource == null) {
+                throw  new SightlyException(String.format("Unable to load 
script dependency %s.", dependency));
+            }
+            reader = new ScriptNameAwareReader(new 
StringReader(IOUtils.toString(scriptResource.adaptTo(InputStream.class),
+                    StandardCharsets.UTF_8)), scriptResource.getPath());
         } catch (IOException e) {
             ioException = e;
         }
         if (ioException != null) {
-            throw  new SightlyException(String.format("Unable to load script 
dependency %s.",
-                    dependency), ioException);
+            throw new SightlyException(String.format("Unable to load script 
dependency %s.", dependency), ioException);
         }
         return reader;
     }

Reply via email to