jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1392344 - /jackrabbit/oak/trunk/doc/nodestate.md
Date Mon, 01 Oct 2012 14:10:01 GMT
Author: jukka
Date: Mon Oct  1 14:10:01 2012
New Revision: 1392344

URL: http://svn.apache.org/viewvc?rev=1392344&view=rev
Log:
OAK-301: Document Oak internals

Simple commit hook example

Modified:
    jackrabbit/oak/trunk/doc/nodestate.md

Modified: jackrabbit/oak/trunk/doc/nodestate.md
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/doc/nodestate.md?rev=1392344&r1=1392343&r2=1392344&view=diff
==============================================================================
--- jackrabbit/oak/trunk/doc/nodestate.md (original)
+++ jackrabbit/oak/trunk/doc/nodestate.md Mon Oct  1 14:10:01 2012
@@ -212,9 +212,9 @@ TODO: Example of how the validator works
     Repository repository = new RepositoryImpl(contentRepository);
     Session session = repository.login();
     Node root = session.getRootNode();
-    root.setProperty("foo", "test");
+    root.setProperty("foo", "abc");
     session.save();
-    root.setProperty("bar", "test");
+    root.setProperty("bar", "def");
     session.save(); // will throw an exception
 
 TODO: Extended example that also works below root and covers also node names
@@ -260,3 +260,51 @@ TODO: Extended example that also works b
 
 TODO
 
+TODO: Basic commit hook example
+
+    class RenameContentHook implements CommitHook {
+
+        private final String name;
+
+        private final String rename;
+
+        public RenameContentHook(String name, String rename) {
+            this.name = name;
+            this.rename = rename;
+        }
+
+        @Override @Nonnull
+        public NodeState processCommit(NodeState before, NodeState after)
+                throws CommitFailedException {
+            PropertyState property = after.getProperty(name);
+            if (property != null) {
+                NodeBuilder builder = after.getBuilder();
+                builder.removeProperty(name);
+                if (property.isArray()) {
+                    builder.setProperty(rename, property.getValues());
+                } else {
+                    builder.setProperty(rename, property.getValue());
+                }
+                return builder.getNodeState();
+            }
+            return after;
+        }
+
+    }
+
+TODO: Using the commit hook to avoid the exception from a validator
+
+    ContentRepository contentRepository = new Oak()
+        .with(new RenameContentHook("bar", "foo"))
+        .with(new DenyContentWithName("bar"))
+        .createContentRepository();
+
+    Repository repository = new RepositoryImpl(contentRepository);
+    Session session = repository.login();
+    Node root = session.getRootNode();
+    root.setProperty("foo", "abc");
+    session.save();
+    root.setProperty("bar", "def");
+    session.save(); // will not throw an exception!
+    System.out.println(root.getProperty("foo").getString()); // Prints "def"!
+



Mime
View raw message