Author: cziegeler
Date: Tue Aug 19 19:18:10 2014
New Revision: 1618944

URL: http://svn.apache.org/r1618944
Log:
SLING-3848 : JcrNodeResource takes too long and initializes too much too soon

Modified:
    
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/JcrNodeResourceMetadata.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/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=1618944&r1=1618943&r2=1618944&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
 Tue Aug 19 19:18:10 2014
@@ -25,6 +25,7 @@ import javax.jcr.Node;
 import javax.jcr.Property;
 import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
+import javax.jcr.ValueFormatException;
 
 import org.apache.sling.api.resource.AbstractResource;
 import org.apache.sling.api.resource.Resource;
@@ -100,25 +101,32 @@ abstract class JcrItemResource // this s
         return result;
     }
 
-    protected void setContentLength(final Property property) throws 
RepositoryException {
+    public static long getContentLength(final Property property) throws 
RepositoryException {
         if (property.isMultiple()) {
-            return;
+            return -1;
         }
 
         try {
-            final long length;
+            long length = -1;
             if (property.getType() == PropertyType.BINARY ) {
                 // we're interested in the number of bytes, not the
                 // number of characters
-                length = property.getLength();
+                try {
+                    length =  property.getLength();
+                } catch (final ValueFormatException vfe) {
+                    LOGGER.debug(
+                        "Length of Property {} cannot be retrieved, ignored 
({})",
+                        property.getPath(), vfe);
+                }
             } else {
                 length = property.getString().getBytes("UTF-8").length;
             }
-            getResourceMetadata().setContentLength(length);
+            return length;
         } catch (UnsupportedEncodingException uee) {
             LOGGER.warn("getPropertyContentLength: Cannot determine length of 
non-binary property {}: {}",
-                    toString(), uee);
+                    property, uee);
         }
+        return -1;
     }
 
     /**

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=1618944&r1=1618943&r2=1618944&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
 Tue Aug 19 19:18:10 2014
@@ -190,8 +190,7 @@ class JcrNodeResource extends JcrItemRes
     /**
      * Returns a stream to the <em>jcr:data</em> property if the
      * {@link #getNode() node} is an <em>nt:file</em> or <em>nt:resource</em>
-     * node. Otherwise returns <code>null</code>. If a valid stream can be
-     * returned, this method also sets the content length resource metadata.
+     * node. Otherwise returns <code>null</code>.
      */
     private InputStream getInputStream() {
         // implement this for nt:file only
@@ -215,13 +214,7 @@ class JcrNodeResource extends JcrItemRes
                         while (item.isNode()) {
                             item = ((Node) item).getPrimaryItem();
                         }
-                        data = ((Property) item);
-
-                        // set the content length property as a side effect
-                        // for resources which are not nt:file based and whose
-                        // data is not in jcr:content/jcr:data this will lazily
-                        // set the correct content length
-                        this.setContentLength(data);
+                        data = (Property) item;
 
                     } catch (ItemNotFoundException infe) {
                         // we don't actually care, but log for completeness

Modified: 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceMetadata.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceMetadata.java?rev=1618944&r1=1618943&r2=1618944&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceMetadata.java
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceMetadata.java
 Tue Aug 19 19:18:10 2014
@@ -30,6 +30,7 @@ import java.util.Collection;
 import java.util.Map;
 import java.util.Set;
 
+import javax.jcr.Item;
 import javax.jcr.Node;
 import javax.jcr.Property;
 import javax.jcr.RepositoryException;
@@ -60,11 +61,7 @@ class JcrNodeResourceMetadata extends Re
         this.node = inNode;
     }
 
-    private Node getTargetNode() {
-        return contentNode != null ? contentNode : node;
-    }
-
-    private void promoteNode() {
+    private Node promoteNode() {
         // check stuff for nt:file nodes
         try {
             if ( (!nodePromotionChecked) && node.isNodeType(NT_FILE)) {
@@ -81,13 +78,13 @@ class JcrNodeResourceMetadata extends Re
         } catch (final RepositoryException re) {
             report(re);
         }
-
+        return contentNode != null ? contentNode : node;
     }
 
     private void report(final RepositoryException re) {
         String nodePath = "<unknown node path>";
         try {
-            nodePath = getTargetNode().getPath();
+            nodePath = contentNode != null ? contentNode.getPath() : 
node.getPath();
         } catch (RepositoryException e) {
             // ignore
         }
@@ -109,12 +106,12 @@ class JcrNodeResourceMetadata extends Re
             return creationTime;
         } else if (CONTENT_TYPE.equals(key)) {
             if (contentType == null) {
-                promoteNode();
+                final Node targetNode = promoteNode();
                 try {
-                    if (getTargetNode().hasProperty(JCR_MIMETYPE)) {
-                        contentType = 
getTargetNode().getProperty(JCR_MIMETYPE).getString();
+                    if (targetNode.hasProperty(JCR_MIMETYPE)) {
+                        contentType = 
targetNode.getProperty(JCR_MIMETYPE).getString();
                     }
-                } catch (RepositoryException re) {
+                } catch (final RepositoryException re) {
                     report(re);
                 }
 
@@ -123,12 +120,12 @@ class JcrNodeResourceMetadata extends Re
             return contentType;
         } else if (CHARACTER_ENCODING.equals(key)) {
             if (characterEncoding == null) {
-                promoteNode();
+                final Node targetNode = promoteNode();
                 try {
-                    if (getTargetNode().hasProperty(JCR_ENCODING)) {
-                        characterEncoding = 
getTargetNode().getProperty(JCR_ENCODING).getString();
+                    if (targetNode.hasProperty(JCR_ENCODING)) {
+                        characterEncoding = 
targetNode.getProperty(JCR_ENCODING).getString();
                     }
-                } catch (RepositoryException re) {
+                } catch (final RepositoryException re) {
                     report(re);
                 }
                 internalPut(CHARACTER_ENCODING, characterEncoding);
@@ -136,14 +133,14 @@ class JcrNodeResourceMetadata extends Re
             return characterEncoding;
         } else if (MODIFICATION_TIME.equals(key)) {
             if (modificationTime == -1) {
-                promoteNode();
+                final Node targetNode = promoteNode();
                 try {
-                    if (getTargetNode().hasProperty(JCR_LASTMODIFIED)) {
+                    if (targetNode.hasProperty(JCR_LASTMODIFIED)) {
                         // We don't check node type, so JCR_LASTMODIFIED might 
not be a long
-                        final Property prop = 
getTargetNode().getProperty(JCR_LASTMODIFIED);
+                        final Property prop = 
targetNode.getProperty(JCR_LASTMODIFIED);
                         try {
                             modificationTime = prop.getLong();
-                        } catch(ValueFormatException vfe) {
+                        } catch (final ValueFormatException vfe) {
                             LOGGER.debug("Property {} cannot be converted to a 
long, ignored ({})",
                                 prop.getPath(), vfe);
                         }
@@ -156,17 +153,25 @@ class JcrNodeResourceMetadata extends Re
             return modificationTime;
         } else if (CONTENT_LENGTH.equals(key)) {
             if (contentLength == -1) {
-                promoteNode();
+                final Node targetNode = promoteNode();
                 try {
-                    if (getTargetNode().hasProperty(JCR_DATA)) {
-                        final Property prop = 
getTargetNode().getProperty(JCR_DATA);
-                        try {
-                            contentLength = prop.getLength();
-                        } catch (ValueFormatException vfe) {
-                            LOGGER.debug(
-                                "Length of Property {} cannot be retrieved, 
ignored ({})",
-                                prop.getPath(), vfe);
+                    // if the node has a jcr:data property, use that property
+                    if (targetNode.hasProperty(JCR_DATA)) {
+                        final Property prop = targetNode.getProperty(JCR_DATA);
+                        contentLength = JcrItemResource.getContentLength(prop);
+                    } else {
+                        // otherwise try to follow default item trail
+                        Item item = targetNode.getPrimaryItem();
+                        while (item.isNode()) {
+                            item = ((Node) item).getPrimaryItem();
                         }
+                        final Property data = (Property) item;
+
+                        // set the content length property as a side effect
+                        // for resources which are not nt:file based and whose
+                        // data is not in jcr:content/jcr:data this will lazily
+                        // set the correct content length
+                        contentLength = JcrItemResource.getContentLength(data);
                     }
                 } catch (final RepositoryException re) {
                     report(re);

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=1618944&r1=1618943&r2=1618944&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
 Tue Aug 19 19:18:10 2014
@@ -66,7 +66,7 @@ class JcrPropertyResource extends JcrIte
             this.getResourceMetadata().setCharacterEncoding("UTF-8");
         }
 
-        this.setContentLength(property);
+        
this.getResourceMetadata().setContentLength(getContentLength(property));
     }
 
     public JcrPropertyResource(final ResourceResolver resourceResolver,


Reply via email to