Author: cziegeler
Date: Wed Aug 20 18:00:21 2014
New Revision: 1619190

URL: http://svn.apache.org/r1619190
Log:
SLING-3848 : JcrNodeResource takes too long and initializes too much too soon. 
Apply patch from Julian Sedding

Modified:
    
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemAdapterFactory.java
    
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java
    
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java
    
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceIterator.java
    
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java

Modified: 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemAdapterFactory.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemAdapterFactory.java?rev=1619190&r1=1619189&r2=1619190&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemAdapterFactory.java
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemAdapterFactory.java
 Wed Aug 20 18:00:21 2014
@@ -76,7 +76,7 @@ public class JcrItemAdapterFactory imple
                 } else if (adaptable instanceof Property) {
                     Property property = (Property) adaptable;
                     return (AdapterType) new 
JcrPropertyResource(resourceResolverFactory.getResourceResolver(property
-                            .getSession()), property);
+                            .getSession()), property.getPath(), property);
                 }
             } catch (RepositoryException e) {
                 logger.error("Unable to adapt JCR Item to a Resource", e);

Modified: 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java?rev=1619190&r1=1619189&r2=1619190&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java
 Wed Aug 20 18:00:21 2014
@@ -21,6 +21,7 @@ package org.apache.sling.jcr.resource.in
 import java.io.UnsupportedEncodingException;
 import java.util.Iterator;
 
+import javax.jcr.Item;
 import javax.jcr.Node;
 import javax.jcr.Property;
 import javax.jcr.PropertyType;
@@ -35,7 +36,7 @@ import org.apache.sling.jcr.resource.Jcr
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-abstract class JcrItemResource // this should be package private, see 
SLING-1414
+abstract class JcrItemResource<T extends Item> // this should be package 
private, see SLING-1414
     extends AbstractResource
     implements Resource {
 
@@ -46,16 +47,20 @@ abstract class JcrItemResource // this s
 
     private final ResourceResolver resourceResolver;
 
-    private final String path;
+    private String path;
+
+    private final T item;
 
     private final ResourceMetadata metadata;
 
     protected JcrItemResource(final ResourceResolver resourceResolver,
                               final String path,
+                              final T item,
                               final ResourceMetadata metadata) {
 
         this.resourceResolver = resourceResolver;
         this.path = path;
+        this.item = item;
         this.metadata = metadata;
     }
 
@@ -70,6 +75,13 @@ abstract class JcrItemResource // this s
      * @see org.apache.sling.api.resource.Resource#getPath()
      */
     public String getPath() {
+        if (path == null) {
+            try {
+                path = getItem().getPath();
+            } catch (RepositoryException e) {
+                throw new IllegalStateException("Failed to retrieve path from 
Item:", e);
+            }
+        }
         return path;
     }
 
@@ -81,6 +93,17 @@ abstract class JcrItemResource // this s
     }
 
     /**
+     * Get the underlying item. Depending on the concrete implementation either
+     * a {@link javax.jcr.Node} or a {@link javax.jcr.Property}.
+     *
+     * @return a {@link javax.jcr.Node} or a {@link javax.jcr.Property}, 
depending
+     *         on the implementation
+     */
+    protected T getItem() {
+        return item;
+    }
+
+    /**
      * Compute the resource type of the given node, using either the
      * SLING_RESOURCE_TYPE_PROPERTY, or the node's primary node type, if the
      * property is not set

Modified: 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java?rev=1619190&r1=1619189&r2=1619190&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java
 Wed Aug 20 18:00:21 2014
@@ -53,7 +53,7 @@ import org.slf4j.LoggerFactory;
         @Adapter(value=PersistableValueMap.class, condition="If the resource 
is a JcrNodeResource and the user has set property privileges on the node."),
         @Adapter(value=InputStream.class, condition="If the resource is a 
JcrNodeResource and has a jcr:data property or is an nt:file node.")
 })
-class JcrNodeResource extends JcrItemResource { // this should be package 
private, see SLING-1414
+class JcrNodeResource extends JcrItemResource<Node> { // this should be 
package private, see SLING-1414
 
     /** marker value for the resourceSupertType before trying to evaluate */
     private static final String UNSET_RESOURCE_SUPER_TYPE = "<unset>";
@@ -61,8 +61,6 @@ class JcrNodeResource extends JcrItemRes
     /** default log */
     private static final Logger LOGGER = 
LoggerFactory.getLogger(JcrNodeResource.class);
 
-    private final Node node;
-
     private String resourceType;
 
     private String resourceSuperType;
@@ -72,8 +70,8 @@ class JcrNodeResource extends JcrItemRes
     /**
      * Constructor
      * @param resourceResolver
-     * @param The path of the resource
-     * @param node
+     * @param path The path of the resource (lazily initialized if null)
+     * @param node The Node underlying this resource
      * @param dynamicClassLoader Dynamic class loader for loading serialized 
objects.
      * @throws RepositoryException
      */
@@ -81,9 +79,8 @@ class JcrNodeResource extends JcrItemRes
                            final String path,
                            final Node node,
                            final ClassLoader dynamicClassLoader) {
-        super(resourceResolver, path, new JcrNodeResourceMetadata(node));
+        super(resourceResolver, path, node, new JcrNodeResourceMetadata(node));
         this.dynamicClassLoader = dynamicClassLoader;
-        this.node = node;
         this.resourceSuperType = UNSET_RESOURCE_SUPER_TYPE;
     }
 
@@ -93,9 +90,9 @@ class JcrNodeResource extends JcrItemRes
     public String getResourceType() {
         if ( this.resourceType == null ) {
             try {
-                this.resourceType = getResourceTypeForNode(this.node);
+                this.resourceType = getResourceTypeForNode(getNode());
             } catch (final RepositoryException e) {
-                LOGGER.error("Unable to get resource type for node " + node, 
e);
+                LOGGER.error("Unable to get resource type for node " + 
getNode(), e);
                 this.resourceType = "<unknown resource type>";
             }
         }
@@ -109,8 +106,8 @@ class JcrNodeResource extends JcrItemRes
         // Yes, this isn't how you're supposed to compare Strings, but this is 
intentional.
         if ( resourceSuperType == UNSET_RESOURCE_SUPER_TYPE ) {
             try {
-                if 
(node.hasProperty(JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY)) {
-                    resourceSuperType = 
node.getProperty(JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY).getValue().getString();
+                if 
(getNode().hasProperty(JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY))
 {
+                    resourceSuperType = 
getNode().getProperty(JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY).getValue().getString();
                 }
             } catch (RepositoryException re) {
                 // we ignore this
@@ -184,7 +181,7 @@ class JcrNodeResource extends JcrItemRes
     // ---------- internal 
-----------------------------------------------------
 
     private Node getNode() {
-        return node;
+        return getItem();
     }
 
     /**
@@ -194,6 +191,7 @@ class JcrNodeResource extends JcrItemRes
      */
     private InputStream getInputStream() {
         // implement this for nt:file only
+        final Node node = getNode();
         if (node != null) {
             try {
                 // find the content node: for nt:file it is jcr:content
@@ -239,7 +237,7 @@ class JcrNodeResource extends JcrItemRes
 
     private URL getURL() {
         try {
-            return URLFactory.createURL(node.getSession(), getPath());
+            return URLFactory.createURL(getNode().getSession(), getPath());
         } catch (final Exception ex) {
             LOGGER.error("getURL: Cannot create URL for " + this, ex);
         }

Modified: 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceIterator.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceIterator.java?rev=1619190&r1=1619189&r2=1619190&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceIterator.java
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceIterator.java
 Wed Aug 20 18:00:21 2014
@@ -92,14 +92,10 @@ public class JcrNodeResourceIterator imp
             try {
                 final Node n = nodes.nextNode();
                 Resource resource = new JcrNodeResource(resourceResolver,
-                    n.getPath(),
+                    null, // do not eagerly initialize path due to performance 
considerations
                     n, dynamicClassLoader);
                 LOGGER.debug("seek: Returning Resource {}", resource);
                 return resource;
-            } catch (final ItemNotFoundException infe) {
-                LOGGER.debug(
-                                "seek: Problem creating Resource for next 
node, skipping",
-                                infe);
             } catch (final Throwable t) {
                 LOGGER.error(
                     "seek: Problem creating Resource for next node, skipping",

Modified: 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java?rev=1619190&r1=1619189&r2=1619190&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java
 Wed Aug 20 18:00:21 2014
@@ -44,24 +44,21 @@ import org.slf4j.LoggerFactory;
                 Double.class, BigDecimal.class, Calendar.class, 
InputStream.class, Value[].class, String[].class,
                 Boolean[].class, Long[].class, Double[].class, 
BigDecimal[].class }),
         @Adapter(value = Node.class, condition = "If the resource is a 
JcrPropertyResource and the property is a reference or weak reference 
property.") })
-class JcrPropertyResource extends JcrItemResource { // this should be package 
private, see SLING-1414
+class JcrPropertyResource extends JcrItemResource<Property> { // this should 
be package private, see SLING-1414
 
     /** default log */
     private static final Logger LOGGER = 
LoggerFactory.getLogger(JcrPropertyResource.class);
 
-    private final Property property;
-
     private final String resourceType;
 
     public JcrPropertyResource(final ResourceResolver resourceResolver,
                                final String path,
                                final Property property)
     throws RepositoryException {
-        super(resourceResolver, path, new ResourceMetadata());
-        this.property = property;
+        super(resourceResolver, path, property, new ResourceMetadata());
         this.resourceType = getResourceTypeForNode(property.getParent())
                 + "/" + property.getName();
-        if (PropertyType.BINARY != this.property.getType()) {
+        if (PropertyType.BINARY != getProperty().getType()) {
             this.getResourceMetadata().setContentType("text/plain");
             this.getResourceMetadata().setCharacterEncoding("UTF-8");
         }
@@ -69,12 +66,6 @@ class JcrPropertyResource extends JcrIte
         
this.getResourceMetadata().setContentLength(getContentLength(property));
     }
 
-    public JcrPropertyResource(final ResourceResolver resourceResolver,
-                               final Property property)
-    throws RepositoryException {
-        this(resourceResolver, property.getPath(), property);
-    }
-
     public String getResourceType() {
         return resourceType;
     }
@@ -217,7 +208,7 @@ class JcrPropertyResource extends JcrIte
     }
 
     private Property getProperty() {
-        return property;
+        return getItem();
     }
 
     private InputStream getInputStream() {


Reply via email to