You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by jo...@apache.org on 2022/07/18 07:29:31 UTC

[sling-org-apache-sling-jcr-resource] branch improvements/SLING-11449 created (now 10c7742)

This is an automated email from the ASF dual-hosted git repository.

joerghoh pushed a change to branch improvements/SLING-11449
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-resource.git


      at 10c7742  update to Oak 1.44.0

This branch includes the following new commits:

     new d7cdaf8  initial commit -- update for POM required
     new 10c7742  update to Oak 1.44.0

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[sling-org-apache-sling-jcr-resource] 02/02: update to Oak 1.44.0

Posted by jo...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

joerghoh pushed a commit to branch improvements/SLING-11449
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-resource.git

commit 10c7742091cdfe75c244999e86d3070c61229c83
Author: Joerg Hoh <jo...@apache.org>
AuthorDate: Mon Jul 18 09:28:27 2022 +0200

    update to Oak 1.44.0
---
 pom.xml | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/pom.xml b/pom.xml
index dc4f077..f717752 100644
--- a/pom.xml
+++ b/pom.xml
@@ -44,9 +44,8 @@
     <properties>
         <site.jira.version.id>12314286</site.jira.version.id>
         <site.javadoc.exclude>**.internal.**</site.javadoc.exclude>
-        <!-- aok 1.10.0 was first version compatible with Jackrabbit API 2.18 (https://issues.apache.org/jira/browse/OAK-7943) -->
-        <!-- aok 1.42.0 first version to include JackrabbitSession.getParentOrNull in oak-jackrabbit-api (https://issues.apache.org/jira/browse/SLING-10011) -->
-        <oak.version>1.43-SNAPSHOT</oak.version>
+        <!-- oak 1.44.0 first version to include JackrabbitNode.getPropertyOrNull in oak-jackrabbit-api (https://issues.apache.org/jira/browse/SLING-11449) -->
+        <oak.version>1.44.0</oak.version>
         <jackrabbit.version>2.18.0</jackrabbit.version><!-- required for direct binary access, https://issues.apache.org/jira/browse/JCR-4335 -->
         <project.build.outputTimestamp>1</project.build.outputTimestamp>
     </properties>


[sling-org-apache-sling-jcr-resource] 01/02: initial commit -- update for POM required

Posted by jo...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

joerghoh pushed a commit to branch improvements/SLING-11449
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-resource.git

commit d7cdaf8951ccb8ead8eeedfb2d0f152f6d6834a7
Author: Joerg Hoh <jo...@apache.org>
AuthorDate: Tue Jul 12 18:31:05 2022 +0200

    initial commit -- update for POM required
---
 pom.xml                                            |  2 +-
 .../resource/internal/JcrModifiableValueMap.java   |  6 ++-
 .../sling/jcr/resource/internal/JcrValueMap.java   | 25 +++++++-----
 .../sling/jcr/resource/internal/NodeUtil.java      | 46 +++++++++++++++++++++-
 .../internal/helper/jcr/JcrItemResource.java       | 11 ++++--
 .../helper/jcr/JcrItemResourceFactory.java         | 24 ++++++-----
 .../internal/helper/jcr/JcrNodeResource.java       |  5 ++-
 .../helper/jcr/JcrNodeResourceMetadata.java        | 33 +++++++++-------
 8 files changed, 107 insertions(+), 45 deletions(-)

diff --git a/pom.xml b/pom.xml
index fd803b0..dc4f077 100644
--- a/pom.xml
+++ b/pom.xml
@@ -46,7 +46,7 @@
         <site.javadoc.exclude>**.internal.**</site.javadoc.exclude>
         <!-- aok 1.10.0 was first version compatible with Jackrabbit API 2.18 (https://issues.apache.org/jira/browse/OAK-7943) -->
         <!-- aok 1.42.0 first version to include JackrabbitSession.getParentOrNull in oak-jackrabbit-api (https://issues.apache.org/jira/browse/SLING-10011) -->
-        <oak.version>1.42.0</oak.version>
+        <oak.version>1.43-SNAPSHOT</oak.version>
         <jackrabbit.version>2.18.0</jackrabbit.version><!-- required for direct binary access, https://issues.apache.org/jira/browse/JCR-4335 -->
         <project.build.outputTimestamp>1</project.build.outputTimestamp>
     </properties>
diff --git a/src/main/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMap.java b/src/main/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMap.java
index 7d1048f..3c2408e 100644
--- a/src/main/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMap.java
+++ b/src/main/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMap.java
@@ -22,6 +22,7 @@ import java.util.Iterator;
 import java.util.Map;
 
 import javax.jcr.Node;
+import javax.jcr.Property;
 import javax.jcr.RepositoryException;
 import javax.jcr.Value;
 
@@ -104,8 +105,9 @@ public class JcrModifiableValueMap extends JcrValueMap implements ModifiableValu
         final Object oldValue = this.valueCache.remove(key);
         try {
             final String name = escapeKeyName(key);
-            if (node.hasProperty(name)) {
-                node.getProperty(name).remove();
+            Property property = NodeUtil.getPropertyOrNull(node, name);
+            if (property != null) {
+                property.remove();
             }
         } catch (final RepositoryException re) {
             throw new IllegalArgumentException("Property '" + key + "' can't be removed from node '" + getPath() + "'.", re);
diff --git a/src/main/java/org/apache/sling/jcr/resource/internal/JcrValueMap.java b/src/main/java/org/apache/sling/jcr/resource/internal/JcrValueMap.java
index 81e0313..8dfb2c1 100644
--- a/src/main/java/org/apache/sling/jcr/resource/internal/JcrValueMap.java
+++ b/src/main/java/org/apache/sling/jcr/resource/internal/JcrValueMap.java
@@ -26,6 +26,8 @@ import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Set;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
 
 import javax.jcr.Node;
 import javax.jcr.Property;
@@ -33,6 +35,7 @@ import javax.jcr.PropertyIterator;
 import javax.jcr.RepositoryException;
 import javax.jcr.Value;
 
+import org.apache.jackrabbit.api.JackrabbitNode;
 import org.apache.jackrabbit.util.ISO9075;
 import org.apache.jackrabbit.util.Text;
 import org.apache.sling.api.resource.ModifiableValueMap;
@@ -278,8 +281,9 @@ public class JcrValueMap implements ValueMap {
             // encoding
             final String path = ISO9075.encodePath(name);
             try {
-                if (node.hasProperty(path)) {
-                    return new JcrPropertyMapCacheEntry(node.getProperty(path));
+                Property property = NodeUtil.getPropertyOrNull(node, path);
+                if (property != null) {
+                    return new JcrPropertyMapCacheEntry(property);
                 }
             } catch (final RepositoryException re) {
                 throw new IllegalArgumentException(re);
@@ -303,8 +307,9 @@ public class JcrValueMap implements ValueMap {
             }
             final String newPath = sb.toString();
             try {
-                if (node.hasProperty(newPath)) {
-                    return new JcrPropertyMapCacheEntry(node.getProperty(newPath));
+                Property property = NodeUtil.getPropertyOrNull(node,newPath);
+                if (property != null) {
+                    return new JcrPropertyMapCacheEntry(property);
                 }
             } catch (final RepositoryException re) {
                 throw new IllegalArgumentException(re);
@@ -321,9 +326,9 @@ public class JcrValueMap implements ValueMap {
 
         try {
             final String key = escapeKeyName(name);
-            if (node.hasProperty(key)) {
-                final Property prop = node.getProperty(key);
-                return cacheProperty(prop);
+            Property property = NodeUtil.getPropertyOrNull(node,key);
+            if (property != null) {
+                return cacheProperty(property);
             }
         } catch (final RepositoryException re) {
             throw new IllegalArgumentException(re);
@@ -333,9 +338,9 @@ public class JcrValueMap implements ValueMap {
             // for compatibility with older versions we use the (wrong) ISO9075 path
             // encoding
             final String oldKey = ISO9075.encodePath(name);
-            if (node.hasProperty(oldKey)) {
-                final Property prop = node.getProperty(oldKey);
-                return cacheProperty(prop);
+            Property property = NodeUtil.getPropertyOrNull(node,oldKey);
+            if (property != null) {
+                return cacheProperty(property);
             }
         } catch (final RepositoryException re) {
             // we ignore this
diff --git a/src/main/java/org/apache/sling/jcr/resource/internal/NodeUtil.java b/src/main/java/org/apache/sling/jcr/resource/internal/NodeUtil.java
index a108b58..9861c66 100644
--- a/src/main/java/org/apache/sling/jcr/resource/internal/NodeUtil.java
+++ b/src/main/java/org/apache/sling/jcr/resource/internal/NodeUtil.java
@@ -36,7 +36,9 @@ import javax.jcr.Property;
 import javax.jcr.RepositoryException;
 import javax.jcr.nodetype.NodeType;
 
+import org.apache.jackrabbit.api.JackrabbitNode;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 
 public abstract class NodeUtil {
     
@@ -89,8 +91,9 @@ public abstract class NodeUtil {
                 : node.isNodeType(NT_LINKED_FILE) ? node.getProperty(JCR_CONTENT).getNode() : node;
         Property data;
         // if the node has a jcr:data property, use that property
-        if (content.hasProperty(JCR_DATA)) {
-            data = content.getProperty(JCR_DATA);
+        final Property property = getPropertyOrNull(content,JCR_DATA);
+        if (property != null) {
+            data = property;
         } else {
             // otherwise try to follow default item trail
             Item item = content.getPrimaryItem();
@@ -101,4 +104,43 @@ public abstract class NodeUtil {
         }
         return data;
     }
+
+    /**
+     * Return a named property from a node.
+     *
+     * In case a recent Oak JCR version is present, it switches to an optimized
+     * version which just does one lookup of the property. This is not possible
+     * with plain JCR.
+     *
+     * @param node the node
+     * @param propertyName the property to check for
+     * @return the property if it exists, null otherwise
+     * @throws RepositoryException in case of a problem
+     */
+    public static @Nullable Property getPropertyOrNull (@NotNull Node node, @NotNull String propertyName) throws RepositoryException {
+        if (node instanceof JackrabbitNode) {
+            JackrabbitNode jnode = (JackrabbitNode) node;
+            Property prop = jnode.getPropertyOrNull(propertyName);
+            return prop;
+        } else {
+            if (node.hasProperty(propertyName)) {
+                return node.getProperty(propertyName);
+            } else {
+                return null;
+            }
+        }
+    }
+
+    public static @Nullable Node getNodeOrNull (@NotNull Node node, @NotNull String childNode) throws RepositoryException {
+        if (node instanceof JackrabbitNode) {
+            JackrabbitNode jnode = (JackrabbitNode) node;
+            return jnode.getNodeOrNull(childNode);
+        } else {
+            if (node.hasNode(childNode)) {
+                return node.getNode(childNode);
+            } else {
+                return null;
+            }
+        }
+    }
 }
diff --git a/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java b/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java
index 87ddd6f..ace704a 100644
--- a/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java
+++ b/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java
@@ -33,6 +33,7 @@ import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.jcr.resource.api.JcrResourceConstants;
+import org.apache.sling.jcr.resource.internal.NodeUtil;
 import org.jetbrains.annotations.NotNull;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -118,15 +119,17 @@ abstract class JcrItemResource<T extends Item> // this should be package private
             throws RepositoryException {
         String result = null;
 
-        if (node.hasProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY)) {
-            result = node.getProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY).getString();
+        Property property = NodeUtil.getPropertyOrNull(node, JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY);
+        if (property != null) {
+            result = property.getString();
         }
 
         if (result == null || result.length() == 0) {
             // Do not load the relatively expensive NodeType object unless necessary. See OAK-2441 for the reason why it
             // cannot only use getProperty here.
-            if (node.hasProperty(Property.JCR_PRIMARY_TYPE)) {
-                result = node.getProperty(Property.JCR_PRIMARY_TYPE).getString();
+            property = NodeUtil.getPropertyOrNull(node, Property.JCR_PRIMARY_TYPE);
+            if (property != null) {
+                result = property.getString();
             } else {
                 result = node.getPrimaryNodeType().getName();
             }
diff --git a/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceFactory.java b/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceFactory.java
index 23c03cd..961b84b 100644
--- a/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceFactory.java
+++ b/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceFactory.java
@@ -35,6 +35,7 @@ import org.apache.jackrabbit.api.JackrabbitSession;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.jcr.resource.internal.HelperData;
+import org.apache.sling.jcr.resource.internal.NodeUtil;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.slf4j.Logger;
@@ -142,15 +143,18 @@ public class JcrItemResourceFactory {
 
     private static Item getSubitem(Node node, String relPath) {
         try {
-            if (relPath.length() == 0) { // not using isEmpty() due to 1.5 compatibility
+            if (relPath.isEmpty()) {
                 return node;
-            } else if (node.hasNode(relPath)) {
-                return node.getNode(relPath);
-            } else if (node.hasProperty(relPath)) {
-                return node.getProperty(relPath);
-            } else {
-                return null;
             }
+            Node childNode = NodeUtil.getNodeOrNull(node, relPath);
+            if (childNode != null) {
+                return childNode;
+            }
+            Property property = NodeUtil.getPropertyOrNull(node, relPath);
+            if (property != null) {
+                return property;
+            }
+            return null;
         } catch (RepositoryException e) {
             log.debug("getSubitem: Can't get subitem {} of {}: {}", relPath, node, e.toString());
             return null;
@@ -162,11 +166,11 @@ public class JcrItemResourceFactory {
         final VersionHistory history = versionManager.getVersionHistory(node.getPath());
         if (history.hasVersionLabel(versionSpecifier)) {
             return history.getVersionByLabel(versionSpecifier).getFrozenNode();
-        } else if (history.hasNode(versionSpecifier)) {
+        }
+        if (history.hasNode(versionSpecifier)) {
             return history.getVersion(versionSpecifier).getFrozenNode();
-        } else {
-            return null;
         }
+        return null;
     }
 
     private static boolean isVersionable(Item item) throws RepositoryException {
diff --git a/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java b/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java
index ce94337..f4f9fd5 100644
--- a/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java
+++ b/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java
@@ -106,8 +106,9 @@ class JcrNodeResource extends JcrItemResource<Node> { // this should be package
         // Yes, this isn't how you're supposed to compare Strings, but this is intentional.
         if (resourceSuperType == UNSET_RESOURCE_SUPER_TYPE) {
             try {
-                if (getNode().hasProperty(JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY)) {
-                    resourceSuperType = getNode().getProperty(JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY).getValue().getString();
+                Property property = NodeUtil.getPropertyOrNull(getNode(), JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY);
+                if (property != null) {
+                    resourceSuperType = property.getValue().getString();
                 }
             } catch (RepositoryException re) {
                 // we ignore this
diff --git a/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceMetadata.java b/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceMetadata.java
index 3707603..b6dd6ca 100644
--- a/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceMetadata.java
+++ b/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceMetadata.java
@@ -39,6 +39,7 @@ import javax.jcr.RepositoryException;
 import javax.jcr.ValueFormatException;
 
 import org.apache.sling.api.resource.ResourceMetadata;
+import org.apache.sling.jcr.resource.internal.NodeUtil;
 import org.jetbrains.annotations.NotNull;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -108,8 +109,9 @@ class JcrNodeResourceMetadata extends ResourceMetadata {
             String contentType = null;
             final Node targetNode = promoteNode();
             try {
-                if (targetNode.hasProperty(JCR_MIMETYPE)) {
-                    contentType = targetNode.getProperty(JCR_MIMETYPE).getString();
+                Property property = NodeUtil.getPropertyOrNull(targetNode, JCR_MIMETYPE);
+                if (property != null) {
+                    contentType = property.getString();
                 }
             } catch (final RepositoryException re) {
                 report(re);
@@ -121,8 +123,9 @@ class JcrNodeResourceMetadata extends ResourceMetadata {
             String characterEncoding = null;
             final Node targetNode = promoteNode();
             try {
-                if (targetNode.hasProperty(JCR_ENCODING)) {
-                    characterEncoding = targetNode.getProperty(JCR_ENCODING).getString();
+                Property property = NodeUtil.getPropertyOrNull(targetNode, JCR_ENCODING);
+                if (property != null) {
+                    characterEncoding = property.getString();
                 }
             } catch (final RepositoryException re) {
                 report(re);
@@ -133,9 +136,9 @@ class JcrNodeResourceMetadata extends ResourceMetadata {
             long modificationTime = -1;
             final Node targetNode = promoteNode();
             try {
-                if (targetNode.hasProperty(JCR_LAST_MODIFIED)) {
+                Property prop = NodeUtil.getPropertyOrNull(targetNode, JCR_LAST_MODIFIED);
+                if (prop != null) {
                     // We don't check node type, so JCR_LASTMODIFIED might not be a long
-                    final Property prop = targetNode.getProperty(JCR_LAST_MODIFIED);
                     try {
                         modificationTime = prop.getLong();
                     } catch (final ValueFormatException vfe) {
@@ -153,8 +156,8 @@ class JcrNodeResourceMetadata extends ResourceMetadata {
             final Node targetNode = promoteNode();
             try {
                 // if the node has a jcr:data property, use that property
-                if (targetNode.hasProperty(JCR_DATA)) {
-                    final Property prop = targetNode.getProperty(JCR_DATA);
+                Property prop = NodeUtil.getPropertyOrNull(targetNode, JCR_DATA);
+                if (prop != null) {
                     contentLength = JcrItemResource.getContentLength(prop);
                 } else {
                     // otherwise try to follow default item trail
@@ -186,13 +189,15 @@ class JcrNodeResourceMetadata extends ResourceMetadata {
         if (name == null) {
             return null;
         }
-        if (node.hasProperty(name)) {
-            return node.getProperty(name);
-        } else if (node.hasNode(name)) {
-            return node.getNode(name);
-        } else {
-            return null;
+        Property property = NodeUtil.getPropertyOrNull(node, name);
+        if (property != null) {
+            return property;
+        }
+        Node childNode = NodeUtil.getNodeOrNull(node, name);
+        if (childNode != null) {
+            return childNode;
         }
+        return null;
     }
 
     private void populate() {