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 + * + */ + 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); + } + } +}