Author: fmeschbe
Date: Tue May 20 04:42:12 2008
New Revision: 658209

URL: http://svn.apache.org/viewvc?rev=658209&view=rev
Log:
SLING-457 Move actual code to resolve the resource super type of a resource
to the JcrResourceUtil and simplify (aka streamline) resolution.

Modified:
    
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/impl/request/SlingRequestDispatcher.java
    
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
    
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java

Modified: 
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/impl/request/SlingRequestDispatcher.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/impl/request/SlingRequestDispatcher.java?rev=658209&r1=658208&r2=658209&view=diff
==============================================================================
--- 
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/impl/request/SlingRequestDispatcher.java
 (original)
+++ 
incubator/sling/trunk/engine/src/main/java/org/apache/sling/engine/impl/request/SlingRequestDispatcher.java
 Tue May 20 04:42:12 2008
@@ -176,29 +176,7 @@
         @Override
         public String getResourceSuperType() {
             if (resourceSuperType == UNSET_RESOURCE_SUPER_TYPE) {
-
-                String rst = null;
-
-                // try local resourceSuperType "property"
-                Resource typeResource = getResourceResolver().getResource(this,
-                    SLING_RESOURCE_SUPER_TYPE_PROPERTY);
-                if (typeResource != null) {
-                    rst = typeResource.adaptTo(String.class);
-                }
-
-                // try explicit resourceSuperType resource
-                if (rst == null) {
-                    String typePath = 
JcrResourceUtil.resourceTypeToPath(getResourceType());
-                    typePath += "/" + SLING_RESOURCE_SUPER_TYPE_PROPERTY;
-                    typeResource = getResourceResolver().getResource(typePath);
-                    if (typeResource != null) {
-                        rst = typeResource.adaptTo(String.class);
-                    }
-                }
-
-                // now set the field to whatever we have, even null
-                resourceSuperType = rst;
-
+                resourceSuperType = JcrResourceUtil.getResourceSuperType(this);
             }
             return resourceSuperType;
         }

Modified: 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java?rev=658209&r1=658208&r2=658209&view=diff
==============================================================================
--- 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
 (original)
+++ 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
 Tue May 20 04:42:12 2008
@@ -96,7 +96,7 @@
      * Helper method, which returns the given resource type as returned from 
the
      * [EMAIL PROTECTED] 
org.apache.sling.api.resource.Resource#getResourceType()} as a
      * relative path.
-     *
+     * 
      * @param type The resource type to be converted into a path
      * @return The resource type as a path.
      */
@@ -106,12 +106,12 @@
 
     /**
      * Returns the super type of the given resource type. This is the result of
-     * calling the <code>getResourceSuperType()</code> method on the
-     * <code>Resource</code> addressed by the <code>resourceType</code>. If
-     * the resource type does not address a resource or if the addressed
-     * resource has no resource super type, this method returns
-     * <code>null</code>.
-     *
+     * adapting the child resource
+     * [EMAIL PROTECTED] 
JcrResourceConstants#SLING_RESOURCE_SUPER_TYPE_PROPERTY} of the
+     * <code>Resource</code> addressed by the <code>resourceType</code> to a
+     * string. If no such child resource exists or if the resource does not
+     * adapt to a string, this method returns <code>null</code>.
+     * 
      * @param resourceResolver The <code>ResourceResolver</code> used to
      *            access the resource whose path (relative or absolute) is 
given
      *            by the <code>resourceType</code> parameter.
@@ -120,18 +120,60 @@
      *            [EMAIL PROTECTED] #resourceTypeToPath(String)} method before 
trying to
      *            get the resource through the <code>resourceResolver</code>.
      * @return the super type of the <code>resourceType</code> or
-     *         <code>null</code> if the resource type does not address a
-     *         resource or if the addressed resource has no resource super 
type.
+     *         <code>null</code> if the resource type does not have a child
+     *         resource
+     *         [EMAIL PROTECTED] 
JcrResourceConstants#SLING_RESOURCE_SUPER_TYPE_PROPERTY}
+     *         adapting to a string.
      */
     public static String getResourceSuperType(
             ResourceResolver resourceResolver, String resourceType) {
         // normalize resource type to a path string
         String rtPath = resourceTypeToPath(resourceType);
 
-        // get a resource for the resource type
+        // create the path to the resource containing the super type
+        rtPath += "/" + 
JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY;
+
+        // get a resource for the resource supert type
         Resource rtResource = resourceResolver.getResource(rtPath);
 
         // get the resource super type from the resource
-        return (rtResource != null) ? rtResource.getResourceSuperType() : null;
+        return (rtResource != null) ? rtResource.adaptTo(String.class) : null;
+    }
+
+    /**
+     * Returns the resource super type of the given resource. This is either 
the
+     * child resource
+     * [EMAIL PROTECTED] 
JcrResourceConstants#SLING_RESOURCE_SUPER_TYPE_PROPERTY} if the
+     * given <code>resource</code> adapted to a string or the result of
+     * calling the [EMAIL PROTECTED] #getResourceSuperType(ResourceResolver, 
String)}
+     * method on the resource type of the <code>resource</code>.
+     * <p>
+     * This mechanism allows to specifically set the resource super type on a
+     * per-resource level overwriting any resource super type hierarchy
+     * pre-defined by the actual resource type of the resource.
+     * 
+     * @param resource The <code>Resource</code> whose resource super type is
+     *            requested.
+     * @return The resource super type or <code>null</code> if the algorithm
+     *         described above does not yield a resource super type.
+     */
+    public static String getResourceSuperType(Resource resource) {
+        ResourceResolver resolver = resource.getResourceResolver();
+
+        // try local resourceSuperType "property"
+        String resourceSuperType = null;
+        Resource typeResource = resolver.getResource(resource,
+            JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY);
+        if (typeResource != null) {
+            resourceSuperType = typeResource.adaptTo(String.class);
+        }
+
+        // try explicit resourceSuperType resource
+        if (resourceSuperType == null) {
+            String resourceType = resource.getResourceType();
+            resourceSuperType = getResourceSuperType(resolver, resourceType);
+        }
+
+        return resourceSuperType;
     }
 }

Modified: 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java?rev=658209&r1=658208&r2=658209&view=diff
==============================================================================
--- 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java
 (original)
+++ 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java
 Tue May 20 04:42:12 2008
@@ -72,29 +72,7 @@
 
     public String getResourceSuperType() {
         if (resourceSuperType == UNSET_RESOURCE_SUPER_TYPE) {
-
-            String rst = null;
-
-            // try local resourceSuperType "property"
-            Resource typeResource = getResourceResolver().getResource(this,
-                SLING_RESOURCE_SUPER_TYPE_PROPERTY);
-            if (typeResource != null) {
-                rst = typeResource.adaptTo(String.class);
-            }
-
-            // try explicit resourceSuperType resource
-            if (rst == null) {
-                String typePath = 
JcrResourceUtil.resourceTypeToPath(getResourceType());
-                typePath += "/" + SLING_RESOURCE_SUPER_TYPE_PROPERTY;
-                typeResource = getResourceResolver().getResource(typePath);
-                if (typeResource != null) {
-                    rst = typeResource.adaptTo(String.class);
-                }
-            }
-
-            // now set the field to whatever we have, even null
-            resourceSuperType = rst;
-
+            resourceSuperType = JcrResourceUtil.getResourceSuperType(this);
         }
         return resourceSuperType;
     }


Reply via email to