jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1387552 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/ oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/
Date Wed, 19 Sep 2012 12:33:16 GMT
Author: jukka
Date: Wed Sep 19 12:33:15 2012
New Revision: 1387552

URL: http://svn.apache.org/viewvc?rev=1387552&view=rev
Log:
OAK-306: Limit session refresh on namespace registry use

As discussed in OAK-312 divide the namespace registry to read-only and read-write parts.

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ReadOnlyNamespaceRegistry.java
      - copied, changed from r1387547, jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistry.java
  (contents, props changed)
      - copied, changed from r1387547, jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImpl.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistryTest.java
  (contents, props changed)
      - copied, changed from r1387547, jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImplTest.java
Removed:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImpl.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImplTest.java
Modified:
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/WorkspaceImpl.java

Copied: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ReadOnlyNamespaceRegistry.java
(from r1387547, jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImpl.java)
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ReadOnlyNamespaceRegistry.java?p2=jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ReadOnlyNamespaceRegistry.java&p1=jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImpl.java&r1=1387547&r2=1387552&rev=1387552&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ReadOnlyNamespaceRegistry.java
Wed Sep 19 12:33:15 2012
@@ -23,19 +23,18 @@ import javax.annotation.Nonnull;
 import javax.jcr.NamespaceException;
 import javax.jcr.NamespaceRegistry;
 import javax.jcr.RepositoryException;
+import javax.jcr.UnsupportedRepositoryOperationException;
 
-import org.apache.jackrabbit.JcrConstants;
-import org.apache.jackrabbit.oak.api.CommitFailedException;
-import org.apache.jackrabbit.oak.api.PropertyState;
-import org.apache.jackrabbit.oak.plugins.memory.StringValue;
-import org.apache.jackrabbit.oak.api.Root;
 import org.apache.jackrabbit.oak.api.Tree;
-import org.apache.jackrabbit.oak.core.DefaultConflictHandler;
 
 /**
- * Implementation of {@link NamespaceRegistry}.
+ * Read-only namespace registry. Used mostly internally when access to the
+ * in-content registered namespaces is needed. See the
+ * {@link ReadWriteNamespaceRegistry} subclass for a more complete registry
+ * implementation that supports also namespace modifications and that's thus
+ * better suited for use in in implementing the full JCR API.
  */
-public abstract class NamespaceRegistryImpl
+public abstract class ReadOnlyNamespaceRegistry
         implements NamespaceRegistry, NamespaceConstants {
 
     /**
@@ -45,95 +44,19 @@ public abstract class NamespaceRegistryI
      *
      * @return root {@link Tree} for reading the namespace mappings
      */
-    abstract protected Tree getReadTree();
-
-    /**
-     * Called by the {@link #registerNamespace(String, String)} and
-     * {@link #unregisterNamespace(String)} methods to acquire a fresh
-     * {@link Root} instance that can be used to persist the requested
-     * namespace changes (and nothing else).
-     * <p>
-     * The default implementation of this method throws an
-     * {@link UnsupportedOperationException}.
-     *
-     * @return fresh {@link Root} instance
-     */
-    protected Root getWriteRoot() {
-        throw new UnsupportedOperationException();
-    }
-
-    /**
-     * Called by the {@link NamespaceRegistry} implementation methods to
-     * refresh the state of the session associated with this instance.
-     * That way the session is kept in sync with the latest global state
-     * seen by the namespace registry.
-     *
-     * @throws RepositoryException if the session could not be refreshed
-     */
-    protected void refresh() throws RepositoryException {
-    }
+    protected abstract Tree getReadTree();
 
     //--------------------------------------------------< NamespaceRegistry >---
 
     @Override
     public void registerNamespace(String prefix, String uri)
             throws RepositoryException {
-        try {
-            Root root = getWriteRoot();
-            Tree namespaces =
-                    getOrCreate(root, JcrConstants.JCR_SYSTEM, REP_NAMESPACES);
-            // remove existing mapping to given uri
-            for (PropertyState p : namespaces.getProperties()) {
-                if (!p.isArray() && p.getValue().getString().equals(uri)) {
-                    namespaces.removeProperty(p.getName());
-                }
-            }
-            namespaces.setProperty(prefix, new StringValue(uri));
-            root.commit(DefaultConflictHandler.OURS);
-            refresh();
-        } catch (NamespaceValidatorException e) {
-            throw e.getNamespaceException();
-        } catch (CommitFailedException e) {
-            throw new RepositoryException(
-                    "Failed to register namespace mapping from "
-                    + prefix + " to " + uri, e);
-        }
+        throw new UnsupportedRepositoryOperationException();
     }
 
     @Override
     public void unregisterNamespace(String prefix) throws RepositoryException {
-        Root root = getWriteRoot();
-        Tree namespaces = root.getTree(NAMESPACES_PATH);
-        if (namespaces == null || !namespaces.hasProperty(prefix)) {
-            throw new NamespaceException(
-                    "Namespace mapping from " + prefix + " to "
-                    + getURI(prefix) + " can not be unregistered");
-        }
-
-        try {
-            namespaces.removeProperty(prefix);
-            root.commit(DefaultConflictHandler.OURS);
-            refresh();
-        } catch (NamespaceValidatorException e) {
-            throw e.getNamespaceException();
-        } catch (CommitFailedException e) {
-            throw new RepositoryException(
-                    "Failed to unregister namespace mapping for prefix "
-                    + prefix, e);
-        }
-    }
-
-    private static Tree getOrCreate(Root root, String... path) {
-        Tree tree = root.getTree("/");
-        assert tree != null;
-        for (String name : path) {
-            Tree child = tree.getChild(name);
-            if (child == null) {
-                child = tree.addChild(name);
-            }
-            tree = child;
-        }
-        return tree;
+        throw new UnsupportedRepositoryOperationException();
     }
 
     @Override

Copied: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistry.java
(from r1387547, jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImpl.java)
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistry.java?p2=jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistry.java&p1=jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImpl.java&r1=1387547&r2=1387552&rev=1387552&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistry.java
Wed Sep 19 12:33:15 2012
@@ -16,61 +16,55 @@
  */
 package org.apache.jackrabbit.oak.plugins.name;
 
-import java.util.Arrays;
-import java.util.Map;
-
-import javax.annotation.Nonnull;
 import javax.jcr.NamespaceException;
-import javax.jcr.NamespaceRegistry;
 import javax.jcr.RepositoryException;
 
 import org.apache.jackrabbit.JcrConstants;
 import org.apache.jackrabbit.oak.api.CommitFailedException;
 import org.apache.jackrabbit.oak.api.PropertyState;
-import org.apache.jackrabbit.oak.plugins.memory.StringValue;
 import org.apache.jackrabbit.oak.api.Root;
 import org.apache.jackrabbit.oak.api.Tree;
 import org.apache.jackrabbit.oak.core.DefaultConflictHandler;
+import org.apache.jackrabbit.oak.plugins.memory.StringValue;
 
 /**
- * Implementation of {@link NamespaceRegistry}.
+ * Writable namespace registry. Mainly for use to implement the full JCR API.
  */
-public abstract class NamespaceRegistryImpl
-        implements NamespaceRegistry, NamespaceConstants {
-
-    /**
-     * Called by the {@link NamespaceRegistry} implementation methods
-     * to acquire a root {@link Tree} instance from which to read the
-     * namespace mappings (under <code>jcr:system/rep:namespaces</code>).
-     *
-     * @return root {@link Tree} for reading the namespace mappings
-     */
-    abstract protected Tree getReadTree();
+public abstract class ReadWriteNamespaceRegistry
+        extends ReadOnlyNamespaceRegistry {
 
     /**
-     * Called by the {@link #registerNamespace(String, String)} and
-     * {@link #unregisterNamespace(String)} methods to acquire a fresh
-     * {@link Root} instance that can be used to persist the requested
-     * namespace changes (and nothing else).
-     * <p>
-     * The default implementation of this method throws an
-     * {@link UnsupportedOperationException}.
+     * Called by the write methods to acquire a fresh {@link Root} instance
+     * that can be used to persist the requested namespace changes (and
+     * nothing else).
      *
      * @return fresh {@link Root} instance
      */
-    protected Root getWriteRoot() {
-        throw new UnsupportedOperationException();
-    }
+    protected abstract Root getWriteRoot();
 
     /**
-     * Called by the {@link NamespaceRegistry} implementation methods to
-     * refresh the state of the session associated with this instance.
-     * That way the session is kept in sync with the latest global state
-     * seen by the namespace registry.
+     * Called by the write methods to refresh the state of the possible
+     * session associated with this instance. The default implementation
+     * of this method does nothing, but a subclass can use this callback
+     * to keep a session in sync with the persisted namespace changes.
      *
      * @throws RepositoryException if the session could not be refreshed
      */
     protected void refresh() throws RepositoryException {
+        // do nothing
+    }
+
+    private static Tree getOrCreate(Root root, String... path) {
+        Tree tree = root.getTree("/");
+        assert tree != null;
+        for (String name : path) {
+            Tree child = tree.getChild(name);
+            if (child == null) {
+                child = tree.addChild(name);
+            }
+            tree = child;
+        }
+        return tree;
     }
 
     //--------------------------------------------------< NamespaceRegistry >---
@@ -123,86 +117,4 @@ public abstract class NamespaceRegistryI
         }
     }
 
-    private static Tree getOrCreate(Root root, String... path) {
-        Tree tree = root.getTree("/");
-        assert tree != null;
-        for (String name : path) {
-            Tree child = tree.getChild(name);
-            if (child == null) {
-                child = tree.addChild(name);
-            }
-            tree = child;
-        }
-        return tree;
-    }
-
-    @Override
-    @Nonnull
-    public String[] getPrefixes() throws RepositoryException {
-        try {
-            Tree root = getReadTree();
-            Map<String, String> map = Namespaces.getNamespaceMap(root);
-            String[] prefixes = map.keySet().toArray(new String[map.size()]);
-            Arrays.sort(prefixes);
-            return prefixes;
-        } catch (RuntimeException e) {
-            throw new RepositoryException(
-                    "Failed to retrieve registered namespace prefixes", e);
-        }
-    }
-
-    @Override
-    @Nonnull
-    public String[] getURIs() throws RepositoryException {
-        try {
-            Tree root = getReadTree();
-            Map<String, String> map = Namespaces.getNamespaceMap(root);
-            String[] uris = map.values().toArray(new String[map.size()]);
-            Arrays.sort(uris);
-            return uris;
-        } catch (RuntimeException e) {
-            throw new RepositoryException(
-                    "Failed to retrieve registered namespace URIs", e);
-        }
-    }
-
-    @Override
-    @Nonnull
-    public String getURI(String prefix) throws RepositoryException {
-        try {
-            Tree root = getReadTree();
-            Map<String, String> map = Namespaces.getNamespaceMap(root);
-            String uri = map.get(prefix);
-            if (uri == null) {
-                throw new NamespaceException(
-                        "No namespace registered for prefix " + prefix);
-            }
-            return uri;
-        } catch (RuntimeException e) {
-            throw new RepositoryException(
-                    "Failed to retrieve the namespace URI for prefix "
-                    + prefix, e);
-        }
-    }
-
-    @Override
-    @Nonnull
-    public String getPrefix(String uri) throws RepositoryException {
-        try {
-            Tree root = getReadTree();
-            Map<String, String> map = Namespaces.getNamespaceMap(root);
-            for (Map.Entry<String, String> entry : map.entrySet()) {
-                if (entry.getValue().equals(uri)) {
-                    return entry.getKey();
-                }
-            }
-            throw new NamespaceException(
-                        "No namespace prefix registered for URI " + uri);
-        } catch (RuntimeException e) {
-            throw new RepositoryException(
-                    "Failed to retrieve the namespace prefix for URI "
-                    + uri, e);
-        }
-    }
-
 }

Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistry.java
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistryTest.java
(from r1387547, jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImplTest.java)
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistryTest.java?p2=jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistryTest.java&p1=jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImplTest.java&r1=1387547&r2=1387552&rev=1387552&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/NamespaceRegistryImplTest.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistryTest.java
Wed Sep 19 12:33:15 2012
@@ -27,7 +27,7 @@ import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
 
-public class NamespaceRegistryImplTest extends AbstractOakTest {
+public class ReadWriteNamespaceRegistryTest extends AbstractOakTest {
 
     @Override
     protected ContentRepository createRepository() {
@@ -37,7 +37,7 @@ public class NamespaceRegistryImplTest e
     @Test
     public void testMappings() throws Exception {
         final ContentSession session = createAdminSession();
-        NamespaceRegistry r = new NamespaceRegistryImpl() {
+        NamespaceRegistry r = new ReadWriteNamespaceRegistry() {
             @Override
             protected Tree getReadTree() {
                 return session.getLatestRoot().getTree("/");

Propchange: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/ReadWriteNamespaceRegistryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/WorkspaceImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/WorkspaceImpl.java?rev=1387552&r1=1387551&r2=1387552&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/WorkspaceImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/WorkspaceImpl.java
Wed Sep 19 12:33:15 2012
@@ -43,7 +43,7 @@ import org.apache.jackrabbit.oak.jcr.que
 import org.apache.jackrabbit.oak.jcr.security.privilege.PrivilegeManagerImpl;
 import org.apache.jackrabbit.oak.jcr.version.VersionManagerImpl;
 import org.apache.jackrabbit.oak.namepath.NameMapper;
-import org.apache.jackrabbit.oak.plugins.name.NamespaceRegistryImpl;
+import org.apache.jackrabbit.oak.plugins.name.ReadWriteNamespaceRegistry;
 import org.apache.jackrabbit.oak.plugins.type.ReadWriteNodeTypeManager;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -153,11 +153,7 @@ public class WorkspaceImpl implements Ja
 
     @Override
     public NamespaceRegistry getNamespaceRegistry() {
-        return new NamespaceRegistryImpl() {
-            @Override
-            protected void refresh() throws RepositoryException {
-                getSession().refresh(true);
-            }
+        return new ReadWriteNamespaceRegistry() {
             @Override
             protected Tree getReadTree() {
                 return sessionDelegate.getRoot().getTree("/");
@@ -166,6 +162,10 @@ public class WorkspaceImpl implements Ja
             protected Root getWriteRoot() {
                 return sessionDelegate.getContentSession().getLatestRoot();
             }
+            @Override
+            protected void refresh() throws RepositoryException {
+                getSession().refresh(true);
+            }
         };
     }
 



Mime
View raw message