You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2015/01/16 15:04:55 UTC

svn commit: r1652411 - in /sling/trunk/contrib/scripting/sightly: engine/src/main/java/org/apache/sling/scripting/sightly/ engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/ engine/src/main/java/org/apache/sling/scripting/sightly/im...

Author: fmeschbe
Date: Fri Jan 16 14:04:55 2015
New Revision: 1652411

URL: http://svn.apache.org/r1652411
Log:
SLING-4304 Refactor the ResourceResolution class and improve its JavaDoc

Apply patch by Radu Cotescu (thanks a lot)

Modified:
    sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/ResourceResolution.java
    sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyJavaCompilerService.java
    sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/PojoUseProvider.java
    sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/RenderUnitProvider.java
    sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/UseProviderUtils.java
    sling/trunk/contrib/scripting/sightly/js-use-provider/src/main/java/org/apache/sling/scripting/sightly/js/impl/JsEnvironment.java

Modified: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/ResourceResolution.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/ResourceResolution.java?rev=1652411&r1=1652410&r2=1652411&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/ResourceResolution.java (original)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/ResourceResolution.java Fri Jan 16 14:04:55 2015
@@ -38,30 +38,54 @@ public final class ResourceResolution {
     private static final int RECURSION_LIMIT = 100;
 
     /**
-     * Resolve the resource with the given path relative to the base resource, using the
-     * resource super type logic
-     * @param resourceResolver The resource resolver to be used
-     * @param base the base resource. It can be null if path is absolute
-     * @param path the path to the resource
-     * @return the retrieved resource or null if no resource was found
+     * <p>
+     * Resolves a resource from the search path relative to the {@code base} resource by traversing the {@code sling:resourceSuperType}
+     * chain.
+     * </p>
+     * <p>
+     * Since this method will traverse the {@code sling:resourceSuperType} chain, the {@code ResourceResolver} used for resolving the
+     * {@code base} resource should be able to read the super type resources.
+     * </p>
+     *
+     * @param base the base resource from which to start the lookup
+     * @param path the relative path to the resource; if the path is absolute the {@code Resource} identified by this path will be
+     *             returned
+     * @return the resource identified by the relative {@code path} or {@code null} if no resource was found
      * @throws java.lang.UnsupportedOperationException if the resource is not in the resource resolver's search path
-     * @throws java.lang.IllegalStateException if the number of steps necessary to search for the resource on the resource
-     * superType chain has reached the maximum limit
-     * @throws java.lang.IllegalArgumentException if a null componentResource is provided but the path is not absolute
+     * @throws java.lang.IllegalStateException         if the number of steps necessary to search for the resource on the resource
+     *                                                 superType chain has reached the maximum limit
+     * @see ResourceResolver#getSearchPath()
      */
-    public static Resource resolveComponentRelative(ResourceResolver resourceResolver, Resource base, String path) {
-        Resource componentResource = null;
+    public static Resource getResourceFromSearchPath(Resource base, String path) {
+        if (path.startsWith("/")) {
+            Resource resource = base.getResourceResolver().getResource(path);
+            if (resource != null) {
+                return searchPathChecked(resource);
+            }
+            return null;
+        }
+        Resource internalBase = null;
         if (base != null) {
             if ("nt:file".equals(base.getResourceType())) {
-                componentResource = retrieveParent(resourceResolver, base);
+                internalBase = retrieveParent(base);
             } else {
-                componentResource = base;
+                internalBase = base;
             }
         }
-        return resolveComponent(resourceResolver, componentResource, path);
+        return resolveComponentInternal(internalBase, path);
     }
 
-    public static Resource resolveComponentForRequest(ResourceResolver resolver, SlingHttpServletRequest request) {
+    /**
+     * <p>
+     * Resolves the resource accessed by a {@code request}. Since the {@code request} can use an anonymous {@code ResourceResolver}, the
+     * passed {@code resolver} parameter should have read access rights to resources from the search path.
+     * </p>
+     *
+     * @param resolver a {@link ResourceResolver} that has read access rights to resources from the search path
+     * @param request  the request
+     * @return the resource identified by the {@code request} or {@code null} if no resource was found
+     */
+    public static Resource getResourceForRequest(ResourceResolver resolver, SlingHttpServletRequest request) {
         String resourceType = request.getResource().getResourceType();
         if (StringUtils.isNotEmpty(resourceType)) {
             if (resourceType.startsWith("/")) {
@@ -80,43 +104,34 @@ public final class ResourceResolution {
 
     /**
      * Resolve the resource in the specified component
-     * @param resourceResolver The resource resolver to be used
-     * @param componentResource The resource for the component. It can be null if path is absolute
+     *
+     * @param base The resource for the component. It can be null if path is absolute
      * @param path the path to the resource
      * @return the retrieved resource or null if no resource was found
      * @throws java.lang.UnsupportedOperationException if the resource is not in the resource resolver's search path
-     * @throws java.lang.IllegalStateException if more than {@link ResourceResolution#RECURSION_LIMIT} steps were
-     * necessary to search for the resource on the resource superType chain
-     * @throws java.lang.IllegalArgumentException if a null componentResource is provided but the path is not absolute
+     * @throws java.lang.IllegalStateException         if more than {@link ResourceResolution#RECURSION_LIMIT} steps were
+     *                                                 necessary to search for the resource on the resource superType chain
      */
-    private static Resource resolveComponent(ResourceResolver resourceResolver, Resource componentResource, String path) {
-        if (resourceResolver == null || path == null) {
+    private static Resource resolveComponentInternal(Resource base, String path) {
+        if (base == null || path == null) {
             throw new NullPointerException("Arguments cannot be null");
         }
-        Resource resource = null;
-        if (isAbsolutePath(path)) {
-            resource = resourceResolver.getResource(path);
-        }
-        if (resource == null) {
-            if (componentResource == null) {
-                throw new IllegalArgumentException("Argument cannot be null if path is not absolute: " + path);
-            }
-            resource = recursiveResolution(resourceResolver, componentResource, path);
-        }
+        Resource resource = recursiveResolution(base, path);
         if (resource == null) {
-            resource = locateInSearchPath(resourceResolver, path);
+            resource = locateInSearchPath(base.getResourceResolver(), path);
         }
-        return resource != null ? searchPathChecked(resourceResolver, resource) : null;
+        return resource != null ? searchPathChecked(resource) : null;
     }
 
-    private static Resource recursiveResolution(ResourceResolver resourceResolver, Resource componentResource, String path) {
+    private static Resource recursiveResolution(Resource base, String path) {
+        ResourceResolver resolver = base.getResourceResolver();
         for (int iteration = 0; iteration < RECURSION_LIMIT; iteration++) {
-            Resource resource = resourceResolver.getResource(componentResource, path);
+            Resource resource = resolver.getResource(base, path);
             if (resource != null) {
                 return resource;
             }
-            componentResource = findSuperComponent(resourceResolver, componentResource);
-            if (componentResource == null) {
+            base = findSuperComponent(base);
+            if (base == null) {
                 return null;
             }
         }
@@ -136,9 +151,10 @@ public final class ResourceResolution {
         return null;
     }
 
-    private static boolean isInSearchPath(ResourceResolver resourceResolver, Resource resource) {
+    private static boolean isInSearchPath(Resource resource) {
         String resourcePath = resource.getPath();
-        for (String path : resourceResolver.getSearchPath()) {
+        ResourceResolver resolver = resource.getResourceResolver();
+        for (String path : resolver.getSearchPath()) {
             if (resourcePath.startsWith(path)) {
                 return true;
             }
@@ -146,31 +162,28 @@ public final class ResourceResolution {
         return false;
     }
 
-    private static Resource findSuperComponent(ResourceResolver resourceResolver, Resource base) {
-        String superType = resourceResolver.getParentResourceType(base);
+    private static Resource findSuperComponent(Resource base) {
+        ResourceResolver resolver = base.getResourceResolver();
+        String superType = resolver.getParentResourceType(base);
         if (superType == null) {
             return null;
         }
-        return resourceResolver.getResource(superType);
+        return resolver.getResource(superType);
     }
 
-    private static Resource searchPathChecked(ResourceResolver resourceResolver, Resource resource) {
-        if (!isInSearchPath(resourceResolver, resource)) {
-            throw new UnsupportedOperationException("Access to resource " + resource.getPath() + " is denied, since the resource does not reside on the search path");
+    private static Resource searchPathChecked(Resource resource) {
+        if (!isInSearchPath(resource)) {
+            throw new UnsupportedOperationException("Access to resource " + resource.getPath() + " is denied, since the resource does not" +
+                    " reside on the search path");
         }
         return resource;
     }
 
-    private static Resource retrieveParent(ResourceResolver resourceResolver, Resource resource) {
+    private static Resource retrieveParent(Resource resource) {
         String parentPath = ResourceUtil.getParent(resource.getPath());
         if (parentPath == null) {
             return null;
         }
-        return resourceResolver.getResource(parentPath);
+        return resource.getResourceResolver().getResource(parentPath);
     }
-
-    private static boolean isAbsolutePath(String path) {
-        return path.startsWith("/");
-    }
-
 }

Modified: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyJavaCompilerService.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyJavaCompilerService.java?rev=1652411&r1=1652410&r2=1652411&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyJavaCompilerService.java (original)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiler/SightlyJavaCompilerService.java Fri Jan 16 14:04:55 2015
@@ -105,7 +105,7 @@ public class SightlyJavaCompilerService
             LOG.debug("trying to find Java source based on resource: {}", resource.getPath());
             // try to find Java source in JCR from a non-fully qualified class name
             Resource scriptResource = ResourceResolution
-                    .resolveComponentRelative(resource.getResourceResolver(), resource, className + ".java");
+                    .getResourceFromSearchPath(resource, className + ".java");
             if (scriptResource != null) {
                 String pojoPath = scriptResource.getPath();
                 LOG.debug("found Java bean script resource: " + scriptResource.getPath());

Modified: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/PojoUseProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/PojoUseProvider.java?rev=1652411&r1=1652410&r2=1652411&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/PojoUseProvider.java (original)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/PojoUseProvider.java Fri Jan 16 14:04:55 2015
@@ -33,7 +33,6 @@ import org.apache.sling.api.resource.Res
 import org.apache.sling.api.scripting.SlingScriptHelper;
 import org.apache.sling.scripting.sightly.ResourceResolution;
 import org.apache.sling.scripting.sightly.impl.compiler.SightlyJavaCompilerService;
-import org.apache.sling.scripting.sightly.impl.engine.SightlyScriptEngineFactory;
 import org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl;
 import org.apache.sling.scripting.sightly.pojo.Use;
 import org.apache.sling.scripting.sightly.render.RenderContext;
@@ -84,7 +83,7 @@ public class PojoUseProvider implements
         SlingScriptHelper sling = UseProviderUtils.getHelper(bindings);
         try {
             ResourceResolver adminResolver = RenderContextImpl.getScriptResourceResolver(renderContext);
-            Resource resource = ResourceResolution.resolveComponentForRequest(adminResolver, sling.getRequest());
+            Resource resource = ResourceResolution.getResourceForRequest(adminResolver, sling.getRequest());
             Object result = sightlyJavaCompilerService.getInstance(resource, identifier);
             if (result instanceof Use) {
                 ((Use) result).init(bindings);

Modified: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/RenderUnitProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/RenderUnitProvider.java?rev=1652411&r1=1652410&r2=1652411&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/RenderUnitProvider.java (original)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/RenderUnitProvider.java Fri Jan 16 14:04:55 2015
@@ -30,7 +30,6 @@ import org.apache.sling.api.SlingHttpSer
 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.SlingScriptHelper;
 import org.apache.sling.scripting.sightly.ResourceResolution;
 import org.apache.sling.scripting.sightly.impl.engine.SightlyScriptEngineFactory;
 import org.apache.sling.scripting.sightly.impl.engine.UnitLoader;
@@ -79,12 +78,7 @@ public class RenderUnitProvider implemen
     private Resource locateResource(Bindings bindings, String script, RenderContext renderContext) {
         ResourceResolver adminResolver = RenderContextImpl.getScriptResourceResolver(renderContext);
         SlingHttpServletRequest request = (SlingHttpServletRequest) bindings.get(SlingBindings.REQUEST);
-        SlingScriptHelper ssh = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
-        Resource resource = ResourceResolution.resolveComponentForRequest(adminResolver, request);
-        if (resource != null) {
-            return ResourceResolution.resolveComponentRelative(adminResolver, resource, script);
-        } else {
-            return ResourceResolution.resolveComponentRelative(adminResolver, ssh.getScript().getScriptResource(), script);
-        }
+        Resource resource = ResourceResolution.getResourceForRequest(adminResolver, request);
+        return ResourceResolution.getResourceFromSearchPath(resource, script);
     }
 }

Modified: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/UseProviderUtils.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/UseProviderUtils.java?rev=1652411&r1=1652410&r2=1652411&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/UseProviderUtils.java (original)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/use/UseProviderUtils.java Fri Jan 16 14:04:55 2015
@@ -73,8 +73,8 @@ public class UseProviderUtils {
             result = resourceResolver.getResource(script);
         }
         if (result == null) {
-            Resource componentResource = ResourceResolution.resolveComponentForRequest(resourceResolver, sling.getRequest());
-            result = ResourceResolution.resolveComponentRelative(resourceResolver, componentResource, script);
+            Resource componentResource = ResourceResolution.getResourceForRequest(resourceResolver, sling.getRequest());
+            result = ResourceResolution.getResourceFromSearchPath(componentResource, script);
         }
         if (result != null) {
             checkSearchPath(result, resourceResolver);

Modified: sling/trunk/contrib/scripting/sightly/js-use-provider/src/main/java/org/apache/sling/scripting/sightly/js/impl/JsEnvironment.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/js-use-provider/src/main/java/org/apache/sling/scripting/sightly/js/impl/JsEnvironment.java?rev=1652411&r1=1652410&r2=1652411&view=diff
==============================================================================
--- sling/trunk/contrib/scripting/sightly/js-use-provider/src/main/java/org/apache/sling/scripting/sightly/js/impl/JsEnvironment.java (original)
+++ sling/trunk/contrib/scripting/sightly/js-use-provider/src/main/java/org/apache/sling/scripting/sightly/js/impl/JsEnvironment.java Fri Jan 16 14:04:55 2015
@@ -87,12 +87,12 @@ public class JsEnvironment {
     public void run(Resource caller, String path, Bindings globalBindings, Bindings arguments, UnaryCallback callback) {
         Resource scriptResource = caller.getChild(path);
         SlingScriptHelper scriptHelper = (SlingScriptHelper) globalBindings.get(SlingBindings.SLING);
-        Resource componentCaller = ResourceResolution.resolveComponentForRequest(caller.getResourceResolver(), scriptHelper.getRequest());
+        Resource componentCaller = ResourceResolution.getResourceForRequest(caller.getResourceResolver(), scriptHelper.getRequest());
         if (scriptResource == null) {
-            scriptResource = ResourceResolution.resolveComponentRelative(caller.getResourceResolver(), componentCaller, path);
+            scriptResource = ResourceResolution.getResourceFromSearchPath(componentCaller, path);
         }
         if (scriptResource == null) {
-            scriptResource = ResourceResolution.resolveComponentRelative(caller.getResourceResolver(), caller, path);
+            scriptResource = ResourceResolution.getResourceFromSearchPath(caller, path);
         }
         if (scriptResource == null) {
             throw new SightlyException("Required script resource could not be located: " + path);