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 2012/04/24 17:54:04 UTC

svn commit: r1329810 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/ oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/ oak-core/src/test/java/org/apache/jackrabbit/oak/namepath/ oak-core/src/test/java...

Author: jukka
Date: Tue Apr 24 15:54:03 2012
New Revision: 1329810

URL: http://svn.apache.org/viewvc?rev=1329810&view=rev
Log:
OAK-61: Implement JCR path handling

Namespace mappings based on /jcr:system/jcr:namespaces

Added:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceMappings.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/NamespaceMappingsTest.java
Removed:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/NamespaceMappings.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/namepath/NamespaceMappingsTest.java
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NameValidatorProvider.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NamespaceRegistryImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/WorkspaceImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NameValidatorProvider.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NameValidatorProvider.java?rev=1329810&r1=1329809&r2=1329810&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NameValidatorProvider.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NameValidatorProvider.java Tue Apr 24 15:54:03 2012
@@ -34,6 +34,7 @@ public class NameValidatorProvider imple
         prefixes.add("jcr");
         prefixes.add("nt");
         prefixes.add("mix");
+        prefixes.add("sv");
 
         // Jackrabbit 2.x prefixes are always available
         prefixes.add("rep");

Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceMappings.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceMappings.java?rev=1329810&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceMappings.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/name/NamespaceMappings.java Tue Apr 24 15:54:03 2012
@@ -0,0 +1,204 @@
+/*
+* 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.plugins.name;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import javax.jcr.RepositoryException;
+
+import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.api.ContentSession;
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.api.Root;
+import org.apache.jackrabbit.oak.api.Tree;
+
+/**
+ * Prove of concept implementation for OAK-61.
+ *
+ * For each registered mapping from a jcr prefix to a namespace a
+ * a mk prefix is generated. The mk prefixes are in one to one relation
+ * ship with the registered namespaces and should be used as shorthands
+ * in place of the actual namespaces in all further name and path handling.
+ *
+ * TODO: expose the relevant methods through the Oak API.
+ */
+public class NamespaceMappings {
+
+    private static final Map<String, String> defaults =
+            new HashMap<String, String>();
+
+    static {
+        // Standard namespace specified by JCR (default one not included)
+        defaults.put("", "");
+        defaults.put("jcr", "http://www.jcp.org/jcr/1.0");
+        defaults.put("nt",  "http://www.jcp.org/jcr/nt/1.0");
+        defaults.put("mix", "http://www.jcp.org/jcr/mix/1.0");
+        defaults.put("xml", "http://www.w3.org/XML/1998/namespace");
+
+        // Namespace included in Jackrabbit 2.x
+        defaults.put("sv", "http://www.jcp.org/jcr/sv/1.0");
+        defaults.put("rep", "internal");
+    }
+
+    private final ContentSession session;
+
+    public NamespaceMappings(ContentSession session) {
+        this.session = session;
+    }
+
+    /**
+     * Returns all registered namespace prefixes.
+     *
+     * @return newly allocated and sorted array of namespace prefixes
+     */
+    public String[] getPrefixes() {
+        Set<String> prefixes = new HashSet<String>();
+        prefixes.addAll(defaults.keySet());
+
+        Tree namespaces = getNamespaces(session.getCurrentRoot(), false);
+        if (namespaces != null) {
+            for (PropertyState property : namespaces.getProperties()) {
+                prefixes.add(property.getName());
+            }
+        }
+
+        String[] array = prefixes.toArray(new String[prefixes.size()]);
+        Arrays.sort(array);
+        return array;
+    }
+
+    /**
+     * Returns all registered namespace URIs.
+     *
+     * @return newly allocated and sorted array of namespace URIs
+     */
+    public String[] getURIs() {
+        Set<String> uris = new HashSet<String>();
+        uris.addAll(defaults.values());
+
+        Tree namespaces = getNamespaces(session.getCurrentRoot(), false);
+        if (namespaces != null) {
+            for (PropertyState property : namespaces.getProperties()) {
+                uris.add(property.getValue().getString());
+            }
+        }
+
+        String[] array = uris.toArray(new String[uris.size()]);
+        Arrays.sort(array);
+        return array;
+    }
+
+    /**
+     * Returns the namespace URI associated with the given prefix,
+     * or {@code null} if such a mapping does not exist.
+     *
+     * @param uri namespace URI
+     * @return matching namespace prefix, or {@code null}
+     */
+    public String getURI(String prefix) {
+        String uri = defaults.get(prefix);
+        if (uri != null) {
+            return uri;
+        }
+
+        Tree namespaces = getNamespaces(session.getCurrentRoot(), false);
+        if (namespaces != null) {
+            PropertyState property = namespaces.getProperty(prefix);
+            if (property != null) {
+                return property.getValue().getString();
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * Returns the namespace prefix associated with the given URI,
+     * or {@code null} if such a mapping does not exist.
+     *
+     * @param prefix namespace prefix
+     * @return matching namespace URI, or {@code null}
+     */
+    public String getPrefix(String uri) throws RepositoryException {
+        for (Map.Entry<String, String> entry : defaults.entrySet()) {
+            if (uri.equals(entry.getValue())) {
+                return entry.getKey();
+            }
+        }
+
+        Tree namespaces = getNamespaces(session.getCurrentRoot(), false);
+        for (PropertyState property : namespaces.getProperties()) {
+            if (uri.equals(property.getValue().getString())) {
+                return property.getName();
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * Adds the specified namespace mapping.
+     *
+     * @param prefix namespace prefix
+     * @param uri namespace URI
+     * @throws CommitFailedException if the registration failed
+     */
+    public void registerNamespace(String prefix, String uri)
+            throws CommitFailedException {
+        Root root = session.getCurrentRoot();
+        Tree namespaces = getNamespaces(root, true);
+        namespaces.setProperty(
+                prefix, session.getCoreValueFactory().createValue(uri));
+        root.commit();
+    }
+
+    /**
+     * Removes the namespace mapping for the given prefix.
+     *
+     * @param prefix namespace prefix
+     * @throws CommitFailedException if the unregistration failed
+     */
+    public void unregisterNamespace(String prefix)
+            throws CommitFailedException {
+        Root root = session.getCurrentRoot();
+        Tree namespaces = getNamespaces(root, true);
+        namespaces.removeProperty(prefix);
+        root.commit();
+    }
+
+    private Tree getNamespaces(Root root, boolean create) {
+        Tree tree = root.getTree("/");
+        Tree system = tree.getChild("jcr:system");
+        if (system == null) {
+            if (create) {
+                system = tree.addChild("jcr:system");
+            } else {
+                return null;
+            }
+        }
+        Tree namespaces = system.getChild("jcr:namespaces");
+        if (namespaces == null && create) {
+            namespaces = system.addChild("jcr:namespaces");
+        }
+        return namespaces;
+    }
+
+}

Added: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/NamespaceMappingsTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/NamespaceMappingsTest.java?rev=1329810&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/NamespaceMappingsTest.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/name/NamespaceMappingsTest.java Tue Apr 24 15:54:03 2012
@@ -0,0 +1,61 @@
+/*
+* 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.plugins.name;
+
+import org.apache.jackrabbit.mk.core.MicroKernelImpl;
+import org.apache.jackrabbit.oak.api.ContentRepository;
+import org.apache.jackrabbit.oak.api.ContentSession;
+import org.apache.jackrabbit.oak.core.KernelContentRepository;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class NamespaceMappingsTest {
+
+    @Test
+    public void testMappings() throws Exception {
+        ContentRepository repository =
+                new KernelContentRepository(new MicroKernelImpl());
+        ContentSession session = repository.login(null, "default");
+        NamespaceMappings r = new NamespaceMappings(session);
+
+        r.registerNamespace("p", "n");
+        assertEquals(r.getURI("p"), "n");
+        assertEquals(r.getPrefix("n"), "p");
+
+        r.registerNamespace("p", "n2");
+        assertEquals(r.getURI("p"), "n2");
+        assertEquals(r.getPrefix("n2"), "p");
+
+        r.registerNamespace("p2", "n");
+        assertEquals(r.getURI("p2"), "n");
+        assertEquals(r.getPrefix("n"), "p2");
+        assertEquals(r.getURI("p"), "n2");
+        assertEquals(r.getPrefix("n2"), "p");
+
+        r.registerNamespace("p2", "n2");
+        assertEquals(r.getURI("p2"), "n2");
+        assertEquals(r.getPrefix("n2"), "p2");
+
+        r.registerNamespace("p", "n");
+        assertEquals(r.getURI("p2"), "n2");
+        assertEquals(r.getPrefix("n2"), "p2");
+        assertEquals(r.getURI("p"), "n");
+        assertEquals(r.getPrefix("n"), "p");
+    }
+
+}

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NamespaceRegistryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NamespaceRegistryImpl.java?rev=1329810&r1=1329809&r2=1329810&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NamespaceRegistryImpl.java (original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NamespaceRegistryImpl.java Tue Apr 24 15:54:03 2012
@@ -16,64 +16,99 @@
  */
 package org.apache.jackrabbit.oak.jcr;
 
-import org.apache.jackrabbit.oak.namepath.NamespaceMappings;
-
 import javax.jcr.NamespaceException;
 import javax.jcr.NamespaceRegistry;
 import javax.jcr.RepositoryException;
 
+import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.api.ContentSession;
+import org.apache.jackrabbit.oak.plugins.name.NamespaceMappings;
+
 /**
  * A naive implementation of {@link NamespaceRegistry}, hard-wiring the
  * predefined namespaces for now.
  * TODO use API only
  */
 public class NamespaceRegistryImpl implements NamespaceRegistry {
-    NamespaceMappings nsMappings = new NamespaceMappings();
-    
-    public NamespaceRegistryImpl() {
-        nsMappings.registerNamespace(PREFIX_EMPTY, NAMESPACE_EMPTY);
-        nsMappings.registerNamespace(PREFIX_JCR, NAMESPACE_JCR);
-        nsMappings.registerNamespace(PREFIX_MIX, NAMESPACE_MIX);
-        nsMappings.registerNamespace(PREFIX_NT, NAMESPACE_NT);
-        nsMappings.registerNamespace(PREFIX_XML, NAMESPACE_XML);
-        nsMappings.registerNamespace("sv", "http://www.jcp.org/jcr/sv/1.0");
+
+    private final NamespaceMappings nsMappings;
+
+    public NamespaceRegistryImpl(ContentSession session) {
+        this.nsMappings = new NamespaceMappings(session);
     }
 
     @Override
-    public void registerNamespace(String prefix, String uri) throws RepositoryException {
-        nsMappings.registerNamespace(prefix, uri);
+    public void registerNamespace(String prefix, String uri)
+            throws RepositoryException {
+        try {
+            nsMappings.registerNamespace(prefix, uri);
+        } catch (CommitFailedException e) {
+            throw new RepositoryException(
+                    "Failed to register namespace mapping from "
+                    + prefix + " to " + uri, e);
+        }
     }
 
     @Override
     public void unregisterNamespace(String prefix) throws RepositoryException {
-        nsMappings.unregisterJcrPrefix(prefix);
+        try {
+            nsMappings.unregisterNamespace(prefix);
+        } catch (CommitFailedException e) {
+            throw new RepositoryException(
+                    "Failed to unregister a namespace mapping with prefix "
+                    + prefix, e);
+        }
     }
 
     @Override
     public String[] getPrefixes() throws RepositoryException {
-        return nsMappings.getJcrPrefixes();
+        try {
+            return nsMappings.getPrefixes();
+        } catch (RuntimeException e) {
+            throw new RepositoryException(
+                    "Failed to retrieve registered namespace prefixes", e);
+        }
     }
 
     @Override
     public String[] getURIs() throws RepositoryException {
-        return nsMappings.getNamespaces();
+        try {
+            return nsMappings.getURIs();
+        } catch (RuntimeException e) {
+            throw new RepositoryException(
+                    "Failed to retrieve registered namespace URIs", e);
+        }
     }
 
     @Override
     public String getURI(String prefix) throws RepositoryException {
-        String result = nsMappings.getNamespace(prefix);
-        if (result == null) {
-            throw new NamespaceException();
+        try {
+            String uri = nsMappings.getURI(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);
         }
-        return result;
     }
 
     @Override
     public String getPrefix(String uri) throws RepositoryException {
-        String result = nsMappings.getJcrPrefix(uri);
-        if (result == null) {
-            throw new NamespaceException();
+        try {
+            String prefix = nsMappings.getPrefix(uri);
+            if (prefix == null) {
+                throw new NamespaceException(
+                        "No namespace registered for prefix " + prefix);
+            }
+            return prefix;
+        } catch (RuntimeException e) {
+            throw new RepositoryException(
+                    "Failed to retrieve the namespace prefix for URI "
+                    + uri, e);
         }
-        return result;
     }
 }

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=1329810&r1=1329809&r2=1329810&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 Tue Apr 24 15:54:03 2012
@@ -160,7 +160,8 @@ public class WorkspaceImpl implements Ja
     public NamespaceRegistry getNamespaceRegistry() throws RepositoryException {
         ensureIsAlive();
         if (nsRegistry == null) {
-            nsRegistry = new NamespaceRegistryImpl();
+            nsRegistry = new NamespaceRegistryImpl(
+                    sessionContext.getContentSession());
         }
         return nsRegistry;
     }

Modified: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java?rev=1329810&r1=1329809&r2=1329810&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java (original)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/RepositoryTest.java Tue Apr 24 15:54:03 2012
@@ -1325,8 +1325,9 @@ public class RepositoryTest extends Abst
     }
 
     @Test
-    public void nameSpaceRegistry() throws RepositoryException {
-        NamespaceRegistry nsReg = getSession().getWorkspace().getNamespaceRegistry();
+    public void testNamespaceRegistry() throws RepositoryException {
+        NamespaceRegistry nsReg =
+                getSession().getWorkspace().getNamespaceRegistry();
         assertFalse(contains(nsReg.getPrefixes(), "foo"));
         assertFalse(contains(nsReg.getURIs(), "file:///foo"));