Author: mduerig
Date: Tue Sep 10 15:45:23 2013
New Revision: 1521529

URL: http://svn.apache.org/r1521529
Log:
OAK-993  Improve backward compatibility for Item.save and Item.refresh
Reverse default behaviour such that teh default is falling back to 
Session.save() in Item.save()

Modified:
    jackrabbit/oak/trunk/oak-doc/src/site/markdown/differences.md
    
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/ItemImpl.java
    
jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/ItemSaveTest.java

Modified: jackrabbit/oak/trunk/oak-doc/src/site/markdown/differences.md
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-doc/src/site/markdown/differences.md?rev=1521529&r1=1521528&r2=1521529&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-doc/src/site/markdown/differences.md (original)
+++ jackrabbit/oak/trunk/oak-doc/src/site/markdown/differences.md Tue Sep 10 
15:45:23 2013
@@ -64,8 +64,10 @@ relying on one session seeing the other 
 On Oak `Item.refresh()` is deprecated and will always cause an 
`Session.refresh()`. The former call
 will result in a warning written to the log in order to facilitate locating 
trouble spots.
 
-On Oak `Item.save()` is deprecated and will only work it the subtree rooted at 
the respective item
-covers all transient changes. Otherwise it will throw an 
`UnsupportedRepositoryException`. See
+On Oak `Item.save()` is deprecated and will per default log a warning and fall 
back to
+`Session.save()`. This behaviour can be tweaked with 
`-Ditem-save-does-session-save=false` in which
+case no fall back to `Session#save()` will happen but an 
`UnsupportedRepositoryException` is thrown
+if the sub-tree rooted at the respective item does not contain all transient 
changes. See
 [OAK-993](https://issues.apache.org/jira/browse/OAK-993) for details.
 
 Query

Modified: 
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/ItemImpl.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/ItemImpl.java?rev=1521529&r1=1521528&r2=1521529&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/ItemImpl.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/ItemImpl.java
 Tue Sep 10 15:45:23 2013
@@ -37,6 +37,7 @@ import javax.jcr.Node;
 import javax.jcr.PathNotFoundException;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
+import javax.jcr.UnsupportedRepositoryOperationException;
 import javax.jcr.Value;
 import javax.jcr.ValueFactory;
 import javax.jcr.nodetype.ConstraintViolationException;
@@ -62,13 +63,19 @@ import org.slf4j.LoggerFactory;
 abstract class ItemImpl<T extends ItemDelegate> implements Item {
     private static final Logger log = LoggerFactory.getLogger(ItemImpl.class);
 
+    public static final String ITEM_SAVE_DOES_SESSION_SAVE = 
"item-save-does-session-save";
+
     /**
      * The value of this flag determines the behaviour of {@link #save()}. If 
{@code false},
      * save will throw a {@link 
javax.jcr.UnsupportedRepositoryOperationException} if the
      * sub tree rooted at this item does not contain <em>all</em> transient 
changes. If
      * {@code true}, save will delegate to {@link Session#save()}.
      */
-    public static final boolean SAVE_SESSION = 
Boolean.getBoolean("item-safe-does-session-safe");
+    public static final boolean SAVE_SESSION;
+    static {
+        String property = System.getProperty(ITEM_SAVE_DOES_SESSION_SAVE);
+        SAVE_SESSION = property == null || Boolean.parseBoolean(property);
+    }
 
     protected final SessionContext sessionContext;
     protected final T dlg;
@@ -242,12 +249,12 @@ abstract class ItemImpl<T extends ItemDe
      * {@link javax.jcr.UnsupportedRepositoryOperationException}
      *
      * @see javax.jcr.Item#save()
+     *
+     *
      */
     @Override
     public void save() throws RepositoryException {
-        if (SAVE_SESSION) {
-            getSession().save();
-        } else {
+        try {
             perform(new ItemWriteOperation<Void>() {
                 @Override
                 public Void perform() throws RepositoryException {
@@ -260,6 +267,19 @@ abstract class ItemImpl<T extends ItemDe
                     return true;
                 }
             });
+        } catch (UnsupportedRepositoryOperationException e) {
+            if (SAVE_SESSION) {
+                if (isNew()) {
+                    throw new RepositoryException("Item.save() not allowed on 
new item");
+                }
+
+                log.warn("Item#save is only supported when the subtree rooted 
at that item " +
+                        "contains all transient changes. Falling back to 
Session#save since " +
+                        "system property " + ITEM_SAVE_DOES_SESSION_SAVE + " 
is true.");
+                getSession().save();
+            } else {
+                throw e;
+            }
         }
     }
 

Modified: 
jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/ItemSaveTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/ItemSaveTest.java?rev=1521529&r1=1521528&r2=1521529&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/ItemSaveTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/ItemSaveTest.java
 Tue Sep 10 15:45:23 2013
@@ -21,6 +21,7 @@ package org.apache.jackrabbit.oak.jcr;
 
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
 
 import javax.jcr.Node;
 import javax.jcr.Property;
@@ -29,6 +30,7 @@ import javax.jcr.Session;
 import javax.jcr.UnsupportedRepositoryOperationException;
 
 import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.jcr.session.NodeImpl;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -78,6 +80,7 @@ public class ItemSaveTest extends Abstra
 
     @Test
     public void saveMissesNode() throws RepositoryException {
+        assumeTrue(!NodeImpl.SAVE_SESSION);
         try {
             root.addNode("child1");
             foo.addNode("child2");
@@ -90,6 +93,7 @@ public class ItemSaveTest extends Abstra
 
     @Test
     public void saveOnNewNode() throws RepositoryException {
+        assumeTrue(!NodeImpl.SAVE_SESSION);
         try {
             foo.addNode("child").save();
             fail("Expected UnsupportedRepositoryOperationException");
@@ -111,6 +115,7 @@ public class ItemSaveTest extends Abstra
 
     @Test
     public void saveMissesProperty() throws RepositoryException {
+        assumeTrue(!NodeImpl.SAVE_SESSION);
         try {
             prop1.setValue("changed");
             prop2.setValue("changed");
@@ -125,6 +130,7 @@ public class ItemSaveTest extends Abstra
 
     @Test
     public void saveOnNewProperty() throws RepositoryException {
+        assumeTrue(!NodeImpl.SAVE_SESSION);
         try {
             foo.setProperty("p3", "v3").save();
             fail("Expected UnsupportedRepositoryOperationException");
@@ -133,7 +139,6 @@ public class ItemSaveTest extends Abstra
         } finally {
             session.refresh(false);
         }
-
     }
 
 }


Reply via email to