You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by ju...@apache.org on 2013/03/07 09:55:03 UTC

svn commit: r1453738 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit: Editor.java EditorHook.java EditorProvider.java

Author: jukka
Date: Thu Mar  7 08:55:02 2013
New Revision: 1453738

URL: http://svn.apache.org/r1453738
Log:
OAK-673: Unified hook processing

Add the proposed Editor mechanism

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/Editor.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/EditorHook.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/EditorProvider.java

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/Editor.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/Editor.java?rev=1453738&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/Editor.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/Editor.java Thu Mar  7 08:55:02 2013
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.spi.commit;
+
+import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+
+import javax.annotation.CheckForNull;
+
+/**
+ * Content change editor. An editor receives information about changes
+ * to the content tree and can reject the changes by throwing a
+ * {@link CommitFailedException} or adjust them using the {@link NodeBuilder}
+ * instance passed to the {@link EditorProvider} that returned this editor.
+ * Note that the given builder can contain updates from multiple different
+ * editors, so its state might not match exactly the state of the given
+ * after state.
+ */
+public interface Editor {
+
+    /**
+     * Processes an added property.
+     *
+     * @param after the added property
+     * @throws CommitFailedException if processing failed
+     */
+    void propertyAdded(PropertyState after) throws CommitFailedException;
+
+    /**
+     * Processes a changed property.
+     *
+     * @param before the original property
+     * @param after  the changed property
+     * @throws CommitFailedException if processing failed
+     */
+    void propertyChanged(PropertyState before, PropertyState after)
+            throws CommitFailedException;
+
+    /**
+     * Processes a removed property.
+     *
+     * @param before the removed property
+     * @throws CommitFailedException if processing failed
+     */
+    void propertyDeleted(PropertyState before) throws CommitFailedException;
+
+    /**
+     * Processes an added child node.
+     *
+     * @param name name of the added node
+     * @param after the added child node
+     * @return an editor for processing the subtree below the added node,
+     *         or {@code null} if the subtree does not need processing
+     * @throws CommitFailedException if processing failed
+     */
+    @CheckForNull
+    Editor childNodeAdded(String name, NodeState after)
+            throws CommitFailedException;
+
+    /**
+     * Processes a changed child node.
+     *
+     * @param name name of the changed node
+     * @param before child node before the change
+     * @param after child node after the change
+     * @return an editor for processing the subtree below the added node,
+     *         or {@code null} if the subtree does not need processing
+     * @throws CommitFailedException if processing failed
+     */
+    @CheckForNull
+    Editor childNodeChanged(
+            String name, NodeState before, NodeState after)
+            throws CommitFailedException;
+
+    /**
+     * Processes a deleted child node.
+     *
+     * @param name name of the deleted node
+     * @param before the deleted child node
+     * @return an editor for processing the subtree below the removed node,
+     *         or {@code null} if the subtree does not need processing
+     * @throws CommitFailedException if processing failed
+     */
+    @CheckForNull
+    Editor childNodeDeleted(String name, NodeState before)
+            throws CommitFailedException;
+
+}

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/EditorHook.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/EditorHook.java?rev=1453738&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/EditorHook.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/EditorHook.java Thu Mar  7 08:55:02 2013
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.spi.commit;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.apache.jackrabbit.oak.plugins.memory.MemoryNodeState.EMPTY_NODE;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
+
+import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.apache.jackrabbit.oak.spi.state.NodeStateDiff;
+
+/**
+ * This commit hook implementation processes changes to be committed
+ * using the {@link Editor} instance provided by the {@link EditorProvider}
+ * passed to the constructor.
+ */
+public class EditorHook implements CommitHook {
+
+    private final EditorProvider provider;
+
+    public EditorHook(@Nonnull EditorProvider provider) {
+        this.provider = checkNotNull(provider);
+    }
+
+    @Override @Nonnull
+    public NodeState processCommit(
+            @Nonnull NodeState before, @Nonnull NodeState after)
+            throws CommitFailedException {
+        checkNotNull(before);
+        checkNotNull(after);
+        NodeBuilder builder = after.builder();
+        Editor editor = provider.getRootEditor(before, after, builder);
+        CommitFailedException exception = process(editor, before, after);
+        if (exception == null) {
+            return builder.getNodeState();
+        } else {
+            throw exception;
+        }
+    }
+
+    //------------------------------------------------------------< private >---
+
+    /**
+     * Validates the given subtree by diffing and recursing through it.
+     *
+     * @param validator validator for the root of the subtree
+     * @param before state of the original subtree
+     * @param after state of the modified subtree
+     * @return exception if the processing failed, {@code null} otherwise
+     */
+    @CheckForNull
+    private static CommitFailedException process(
+            @CheckForNull Editor editor,
+            @Nonnull NodeState before, @Nonnull NodeState after) {
+        checkNotNull(before);
+        checkNotNull(after);
+        if (editor != null) {
+            EditorDiff diff = new EditorDiff(editor);
+            after.compareAgainstBaseState(before, diff);
+            return diff.exception;
+        } else {
+            return null;
+        }
+    }
+
+    private static class EditorDiff implements NodeStateDiff {
+
+        private final Editor editor;
+
+        /**
+         * Checked exceptions don't compose. So we need to hack around.
+         * See http://markmail.org/message/ak67n5k7mr3vqylm and
+         * http://markmail.org/message/bhocbruikljpuhu6
+         */
+        private CommitFailedException exception;
+
+        private EditorDiff(Editor editor) {
+            this.editor = editor;
+        }
+
+        //-------------------------------------------------< NodeStateDiff >--
+
+        @Override
+        public void propertyAdded(PropertyState after) {
+            if (exception == null) {
+                try {
+                    editor.propertyAdded(after);
+                } catch (CommitFailedException e) {
+                    exception = e;
+                }
+            }
+        }
+
+        @Override
+        public void propertyChanged(PropertyState before, PropertyState after) {
+            if (exception == null) {
+                try {
+                    editor.propertyChanged(before, after);
+                } catch (CommitFailedException e) {
+                    exception = e;
+                }
+            }
+        }
+
+        @Override
+        public void propertyDeleted(PropertyState before) {
+            if (exception == null) {
+                try {
+                    editor.propertyDeleted(before);
+                } catch (CommitFailedException e) {
+                    exception = e;
+                }
+            }
+        }
+
+        @Override
+        public void childNodeAdded(String name, NodeState after) {
+            if (exception == null) {
+                try {
+                    Editor e = editor.childNodeAdded(name, after);
+                    exception = process(e, EMPTY_NODE, after);
+                } catch (CommitFailedException e) {
+                    exception = e;
+                }
+            }
+        }
+
+        @Override
+        public void childNodeChanged(
+                String name, NodeState before, NodeState after) {
+            if (exception == null) {
+                try {
+                    Editor e = editor.childNodeChanged(name, before, after);
+                    exception = process(e, before, after);
+                } catch (CommitFailedException e) {
+                    exception = e;
+                }
+            }
+        }
+
+        @Override
+        public void childNodeDeleted(String name, NodeState before) {
+            if (exception == null) {
+                try {
+                    Editor e = editor.childNodeDeleted(name, before);
+                    exception = process(e, before, EMPTY_NODE);
+                } catch (CommitFailedException e) {
+                    exception = e;
+                }
+            }
+        }
+
+    }
+
+}

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/EditorProvider.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/EditorProvider.java?rev=1453738&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/EditorProvider.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/spi/commit/EditorProvider.java Thu Mar  7 08:55:02 2013
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.spi.commit;
+
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+
+import javax.annotation.CheckForNull;
+
+/**
+ * Extension point for content change editors. Used by the {@link EditorHook}
+ * class to allow multiple components to process content changes during just
+ * a single content diff.
+ */
+public interface EditorProvider {
+
+    /**
+     * Returns an editor for processing changes between the given two states.
+     * Returns {@code null} if the changes don't require processing.
+     * <p>
+     * An implementation of this method should generally not compare the
+     * given before and after states, as the caller is expected to compare
+     * the states and invoke the respective callback methods on the
+     * {@link Editor} instance returned by this method. Instead the
+     * implementation can use the opportunity for other preparatory work.
+     *
+     * @param before  original root state
+     * @param after   modified root state
+     * @param builder node builder based on the after state
+     * @return editor for processing the changes, or {@code null}
+     */
+    @CheckForNull
+    Editor getRootEditor(NodeState before, NodeState after, NodeBuilder builder);
+
+}