sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1636284 - in /sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource: ResourceUtil.java package-info.java
Date Mon, 03 Nov 2014 07:33:35 GMT
Author: cziegeler
Date: Mon Nov  3 07:33:34 2014
New Revision: 1636284

URL: http://svn.apache.org/r1636284
Log:
SLING-4125 : ResourceUtil.getOrCreateResource might create SNS nodes

Modified:
    sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
    sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/package-info.java

Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java?rev=1636284&r1=1636283&r2=1636284&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
(original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
Mon Nov  3 07:33:34 2014
@@ -527,6 +527,9 @@ public class ResourceUtil {
 
     /**
      * Creates or gets the resource at the given path.
+     * If an exception occurs, it retries the operation up to five times if autoCommit is
enabled.
+     * In this case, {@link ResourceResolver#revert()} is called on the resolver before the
+     * creation is retried.
      *
      * @param resolver The resource resolver to use for creation
      * @param path     The full path to be created
@@ -542,6 +545,44 @@ public class ResourceUtil {
             final String intermediateResourceType,
             final boolean autoCommit)
     throws PersistenceException {
+        PersistenceException mostRecentPE = null;
+        for(int i=0;i<5;i++) {
+            try {
+                return ResourceUtil.getOrCreateResourceInternal(resolver,
+                        path,
+                        resourceProperties,
+                        intermediateResourceType,
+                        autoCommit);
+            } catch ( final PersistenceException pe ) {
+                if ( autoCommit ) {
+                    // in case of exception, revert to last clean state and retry
+                    resolver.revert();
+                    resolver.refresh();
+                    mostRecentPE = pe;
+                } else {
+                    throw pe;
+                }
+            }
+        }
+        throw mostRecentPE;
+    }
+
+    /**
+     * Creates or gets the resource at the given path.
+     *
+     * @param resolver The resource resolver to use for creation
+     * @param path     The full path to be created
+     * @param resourceProperties The optional resource properties of the final resource to
create
+     * @param intermediateResourceType THe optional resource type of all intermediate resources
+     * @param autoCommit If set to true, a commit is performed after each resource creation.
+     */
+    private static Resource getOrCreateResourceInternal(
+            final ResourceResolver resolver,
+            final String path,
+            final Map<String, Object> resourceProperties,
+            final String intermediateResourceType,
+            final boolean autoCommit)
+    throws PersistenceException {
         Resource rsrc = resolver.getResource(path);
         if ( rsrc == null ) {
             final int lastPos = path.lastIndexOf('/');
@@ -562,7 +603,20 @@ public class ResourceUtil {
                 resolver.refresh();
             }
             try {
-                rsrc = resolver.create(parentResource, name, resourceProperties);
+                int retry = 5;
+                while ( retry > 0 && rsrc == null ) {
+                    rsrc = resolver.create(parentResource, name, resourceProperties);
+                    // check for SNS
+                    if ( !name.equals(rsrc.getName()) ) {
+                        resolver.refresh();
+                        resolver.delete(rsrc);
+                        rsrc = resolver.getResource(parentResource, name);
+                    }
+                    retry--;
+                }
+                if ( rsrc == null ) {
+                    throw new PersistenceException("Unable to create resource.");
+                }
             } catch ( final PersistenceException pe ) {
                 // this could be thrown because someone else tried to create this
                 // node concurrently

Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/package-info.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/package-info.java?rev=1636284&r1=1636283&r2=1636284&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/package-info.java
(original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/package-info.java
Mon Nov  3 07:33:34 2014
@@ -17,7 +17,7 @@
  * under the License.
  */
 
-@Version("2.7")
+@Version("2.7.2")
 package org.apache.sling.api.resource;
 
 import aQute.bnd.annotation.Version;



Mime
View raw message