jackrabbit-oak-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Dürig <mdue...@apache.org>
Subject The revenge of the checked exception
Date Wed, 30 May 2012 16:12:24 GMT

Author: mduerig
Date: Wed May 30 16:09:19 2012
New Revision: 1344328

URL: http://svn.apache.org/viewvc?rev=1344328&view=rev
Log:
OAK-68 - Extension point for commit validation
commit hook implementation which validates a commit against a set of 
validators

+    /**
+     * Checked exceptions don't compose. So we need to hack around. See
+     * <ul>
+     * <li>http://markmail.org/message/ak67n5k7mr3vqylm</li>
+     * <li>http://markmail.org/message/7l26cofhyr3sk5pr</li>
+     * <li>http://markmail.org/message/nw7mg4cmgpeqq4i5</li>
+     * <li>http://markmail.org/message/bhocbruikljpuhu6</li>
+     * </ul>
+     */
+    private static class BreakOutException extends RuntimeException {
+        public BreakOutException(CommitFailedException cause) {
+            super(cause);
+        }
+    }
+
+    private static void validate(final NodeStore store, NodeState 
before, NodeState after,
+            final Validator validator) throws CommitFailedException {
+
+        try {
+            store.compare(before, after, new NodeStateDiff() {
+                @Override
+                public void propertyAdded(PropertyState after) {
+                    try {
+                        validator.propertyAdded(after);
+                    }
+                    catch (CommitFailedException e) {
+                        throw new BreakOutException(e);
+                    }
+                }
+
+                @Override
+                public void propertyChanged(PropertyState before, 
PropertyState after) {
+                    try {
+                        validator.propertyChanged(before, after);
+                    }
+                    catch (CommitFailedException e) {
+                        throw new BreakOutException(e);
+                    }
+                }
+
+                @Override
+                public void propertyDeleted(PropertyState before) {
+                    try {
+                        validator.propertyDeleted(before);
+                    }
+                    catch (CommitFailedException e) {
+                        throw new BreakOutException(e);
+                    }
+                }
+
+                @Override
+                public void childNodeAdded(String name, NodeState after) {
+                    try {
+                        Validator childValidator = 
validator.childNodeAdded(name, after);
+                        if (childValidator != null) {
+                            validate(after, validator);
+                        }
+                    }
+                    catch (CommitFailedException e) {
+                        throw new BreakOutException(e);
+                    }
+                }
+
+                @Override
+                public void childNodeChanged(String name, NodeState 
before, NodeState after) {
+                    try {
+                        Validator childValidator = 
validator.childNodeChanged(name, before, after);
+                        if (childValidator != null) {
+                            validate(store, before, after, childValidator);
+                        }
+                    }
+                    catch (CommitFailedException e) {
+                        throw new BreakOutException(e);
+                    }
+                }
+
+                @Override
+                public void childNodeDeleted(String name, NodeState 
before) {
+                    try {
+                        validator.childNodeDeleted(name, before);
+                    }
+                    catch (CommitFailedException e) {
+                        throw new BreakOutException(e);
+                    }
+                }
+            });
+        }
+        catch (BreakOutException e) {
+            throw new CommitFailedException(e);
+        }
+    }
+
+    private static void validate(NodeState nodeState, Validator validator)
+            throws CommitFailedException {
+
+        for (PropertyState property : nodeState.getProperties()) {
+            validator.propertyAdded(property);
+        }
+
+        for (ChildNodeEntry child : nodeState.getChildNodeEntries()) {
+            Validator childValidator = validator.childNodeAdded(
+                    child.getName(), child.getNodeState());
+            validate(child.getNodeState(), childValidator);
+        }
+    }
+}



Mime
View raw message