jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r1463454 - in /jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr: SessionImpl.java SessionNamespaces.java
Date Tue, 02 Apr 2013 09:15:57 GMT
Author: mreutegg
Date: Tue Apr  2 09:15:57 2013
New Revision: 1463454

URL: http://svn.apache.org/r1463454
Log:
OAK-727: Use snapshot of namespaces in SessionImpl

Added:
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionNamespaces.java
  (with props)
Modified:
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java?rev=1463454&r1=1463453&r2=1463454&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java
Tue Apr  2 09:15:57 2013
@@ -19,11 +19,7 @@ package org.apache.jackrabbit.oak.jcr;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.Locale;
 import java.util.Map;
-import java.util.Set;
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
@@ -32,7 +28,6 @@ import javax.jcr.Credentials;
 import javax.jcr.InvalidSerializedDataException;
 import javax.jcr.Item;
 import javax.jcr.ItemNotFoundException;
-import javax.jcr.NamespaceException;
 import javax.jcr.Node;
 import javax.jcr.PathNotFoundException;
 import javax.jcr.Property;
@@ -64,7 +59,6 @@ import org.apache.jackrabbit.oak.jcr.xml
 import org.apache.jackrabbit.oak.spi.security.authentication.ImpersonationCredentials;
 import org.apache.jackrabbit.oak.util.TODO;
 import org.apache.jackrabbit.util.Text;
-import org.apache.jackrabbit.util.XMLChar;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.xml.sax.ContentHandler;
@@ -79,18 +73,12 @@ public class SessionImpl implements Jack
     private final SessionContext sessionContext;
     private final SessionDelegate sd;
 
-    /**
-     * Local namespace remappings. Prefixes as keys and namespace URIs as values.
-     * <p/>
-     * This map is only accessed from synchronized methods (see
-     * <a href="https://issues.apache.org/jira/browse/JCR-1793">JCR-1793</a>).
-     */
-    private final Map<String, String> namespaces;
+    private final SessionNamespaces namespaces;
 
     SessionImpl(SessionContext sessionContext, Map<String, String> namespaces) {
         this.sessionContext = sessionContext;
         this.sd = sessionContext.getSessionDelegate();
-        this.namespaces = namespaces;
+        this.namespaces = new SessionNamespaces(namespaces, sessionContext);
     }
 
     static void checkProtectedNodes(Session session, String... absJcrPaths) throws RepositoryException
{
@@ -410,9 +398,7 @@ public class SessionImpl implements Jack
         if (sd.isAlive()) {
             sessionContext.dispose();
             sd.logout();
-            synchronized (namespaces) {
-                namespaces.clear();
-            }
+            namespaces.clear();
         }
     }
 
@@ -597,109 +583,25 @@ public class SessionImpl implements Jack
     }
 
     //---------------------------------------------------------< Namespaces >---
-    // The code below was initially copied from JCR Commons AbstractSession, but
-    // provides information the "hasRemappings" information
 
     @Override
     public void setNamespacePrefix(String prefix, String uri) throws RepositoryException
{
-        if (prefix == null) {
-            throw new IllegalArgumentException("Prefix must not be null");
-        } else if (uri == null) {
-            throw new IllegalArgumentException("Namespace must not be null");
-        } else if (prefix.isEmpty()) {
-            throw new NamespaceException(
-                    "Empty prefix is reserved and can not be remapped");
-        } else if (uri.isEmpty()) {
-            throw new NamespaceException(
-                    "Default namespace is reserved and can not be remapped");
-        } else if (prefix.toLowerCase(Locale.ENGLISH).startsWith("xml")) {
-            throw new NamespaceException(
-                    "XML prefixes are reserved: " + prefix);
-        } else if (!XMLChar.isValidNCName(prefix)) {
-            throw new NamespaceException(
-                    "Prefix is not a valid XML NCName: " + prefix);
-        }
-
-        synchronized (namespaces) {
-            // Remove existing mapping for the given prefix
-            namespaces.remove(prefix);
-
-            // Remove existing mapping(s) for the given URI
-            Set<String> prefixes = new HashSet<String>();
-            for (Map.Entry<String, String> entry : namespaces.entrySet()) {
-                if (entry.getValue().equals(uri)) {
-                    prefixes.add(entry.getKey());
-                }
-            }
-            namespaces.keySet().removeAll(prefixes);
-
-            // Add the new mapping
-            namespaces.put(prefix, uri);
-        }
+        namespaces.setNamespacePrefix(prefix, uri);
     }
 
     @Override
     public String[] getNamespacePrefixes() throws RepositoryException {
-        synchronized (namespaces) {
-            if (namespaces.isEmpty()) {
-                return getWorkspace().getNamespaceRegistry().getPrefixes();
-            }
-        }
-        Set<String> uris = new HashSet<String>();
-        uris.addAll(Arrays.asList(getWorkspace().getNamespaceRegistry().getURIs()));
-        synchronized (namespaces) {
-            // Add namespace uris only visible to session
-            uris.addAll(namespaces.values());
-        }
-        Set<String> prefixes = new HashSet<String>();
-        for (String uri : uris) {
-            prefixes.add(getNamespacePrefix(uri));
-        }
-        return prefixes.toArray(new String[prefixes.size()]);
+        return namespaces.getNamespacePrefixes();
     }
 
     @Override
     public String getNamespaceURI(String prefix) throws RepositoryException {
-        synchronized (namespaces) {
-            String uri = namespaces.get(prefix);
-
-            if (uri == null) {
-                // Not in local mappings, try the global ones
-                uri = getWorkspace().getNamespaceRegistry().getURI(prefix);
-                if (namespaces.containsValue(uri)) {
-                    // The global URI is locally mapped to some other prefix,
-                    // so there are no mappings for this prefix
-                    throw new NamespaceException("Namespace not found: " + prefix);
-                }
-            }
-
-            return uri;
-        }
+        return namespaces.getNamespaceURI(prefix);
     }
 
     @Override
     public String getNamespacePrefix(String uri) throws RepositoryException {
-        synchronized (namespaces) {
-            for (Map.Entry<String, String> entry : namespaces.entrySet()) {
-                if (entry.getValue().equals(uri)) {
-                    return entry.getKey();
-                }
-            }
-
-            // The following throws an exception if the URI is not found, that's OK
-            String prefix = getWorkspace().getNamespaceRegistry().getPrefix(uri);
-
-            // Generate a new prefix if the global mapping is already taken
-            String base = prefix;
-            for (int i = 2; namespaces.containsKey(prefix); i++) {
-                prefix = base + i;
-            }
-
-            if (!base.equals(prefix)) {
-                namespaces.put(prefix, uri);
-            }
-            return prefix;
-        }
+        return namespaces.getNamespacePrefix(uri);
     }
 
     //--------------------------------------------------< JackrabbitSession >---

Added: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionNamespaces.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionNamespaces.java?rev=1463454&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionNamespaces.java
(added)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionNamespaces.java
Tue Apr  2 09:15:57 2013
@@ -0,0 +1,216 @@
+/*
+ * 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.jcr;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+
+import javax.annotation.Nonnull;
+import javax.jcr.NamespaceException;
+import javax.jcr.NamespaceRegistry;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.jackrabbit.util.XMLChar;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * <code>SessionNamespaces</code> implements namespace handling on the JCR
+ * Session level. That is, it maintains a map of session local namespace
+ * re-mappings and takes a snapshot of the namespace registry when initialized
+ * (see JCR 2.0 specification, section 3.5.1).
+ */
+class SessionNamespaces {
+
+    /**
+     * Local namespace remappings. Prefixes as keys and namespace URIs as values.
+     * <p/>
+     * This map is only accessed from synchronized methods (see
+     * <a href="https://issues.apache.org/jira/browse/JCR-1793">JCR-1793</a>).
+     */
+    private final Map<String, String> namespaces;
+
+    /**
+     * A snapshot of the namespace registry when these SessionNamespaces
+     * are first accessed.
+     */
+    private Map<String, String> snapshot;
+
+    private final SessionContext sessionContext;
+
+    SessionNamespaces(@Nonnull Map<String, String> namespaces,
+                      @Nonnull SessionContext sessionContext) {
+        this.namespaces = checkNotNull(namespaces);
+        this.sessionContext = checkNotNull(sessionContext);
+    }
+
+    // The code below was initially copied from JCR Commons AbstractSession, but
+    // provides information the "hasRemappings" information
+
+    /**
+     * @see Session#setNamespacePrefix(String, String)
+     */
+    void setNamespacePrefix(String prefix, String uri) throws RepositoryException {
+        init();
+        if (prefix == null) {
+            throw new IllegalArgumentException("Prefix must not be null");
+        } else if (uri == null) {
+            throw new IllegalArgumentException("Namespace must not be null");
+        } else if (prefix.isEmpty()) {
+            throw new NamespaceException(
+                    "Empty prefix is reserved and can not be remapped");
+        } else if (uri.isEmpty()) {
+            throw new NamespaceException(
+                    "Default namespace is reserved and can not be remapped");
+        } else if (prefix.toLowerCase(Locale.ENGLISH).startsWith("xml")) {
+            throw new NamespaceException(
+                    "XML prefixes are reserved: " + prefix);
+        } else if (!XMLChar.isValidNCName(prefix)) {
+            throw new NamespaceException(
+                    "Prefix is not a valid XML NCName: " + prefix);
+        }
+
+        synchronized (namespaces) {
+            // Remove existing mapping for the given prefix
+            namespaces.remove(prefix);
+
+            // Remove existing mapping(s) for the given URI
+            Set<String> prefixes = new HashSet<String>();
+            for (Map.Entry<String, String> entry : namespaces.entrySet()) {
+                if (entry.getValue().equals(uri)) {
+                    prefixes.add(entry.getKey());
+                }
+            }
+            namespaces.keySet().removeAll(prefixes);
+
+            // Add the new mapping
+            namespaces.put(prefix, uri);
+        }
+    }
+
+    /**
+     * @see Session#getNamespacePrefixes()
+     */
+    String[] getNamespacePrefixes() throws RepositoryException {
+        init();
+        synchronized (namespaces) {
+            if (namespaces.isEmpty()) {
+                Set<String> prefixes = snapshot.keySet();
+                return prefixes.toArray(new String[prefixes.size()]);
+            }
+        }
+        Set<String> uris = new HashSet<String>();
+        uris.addAll(snapshot.values());
+        synchronized (namespaces) {
+            // Add namespace uris only visible to session
+            uris.addAll(namespaces.values());
+        }
+        Set<String> prefixes = new HashSet<String>();
+        for (String uri : uris) {
+            prefixes.add(getNamespacePrefix(uri));
+        }
+        return prefixes.toArray(new String[prefixes.size()]);
+    }
+
+    /**
+     * @see Session#getNamespaceURI(String)
+     */
+    String getNamespaceURI(String prefix) throws RepositoryException {
+        init();
+        synchronized (namespaces) {
+            String uri = namespaces.get(prefix);
+
+            if (uri == null) {
+                // Not in local mappings, try snapshot ones
+                uri = snapshot.get(prefix);
+                if (uri == null) {
+                    // Not in snapshot mappings, try the global ones
+                    uri = getNamespaceRegistry().getURI(prefix);
+                    if (namespaces.containsValue(uri)) {
+                        // The global URI is locally mapped to some other prefix,
+                        // so there are no mappings for this prefix
+                        throw new NamespaceException("Namespace not found: " + prefix);
+                    }
+                }
+            }
+
+            return uri;
+        }
+    }
+
+    /**
+     * @see Session#getNamespacePrefix(String)
+     */
+    String getNamespacePrefix(String uri) throws RepositoryException {
+        init();
+        synchronized (namespaces) {
+            for (Map.Entry<String, String> entry : namespaces.entrySet()) {
+                if (entry.getValue().equals(uri)) {
+                    return entry.getKey();
+                }
+            }
+
+            // try snapshot
+            for (Map.Entry<String, String> entry : snapshot.entrySet()) {
+                if (entry.getValue().equals(uri)) {
+                    return entry.getKey();
+                }
+            }
+
+            // The following throws an exception if the URI is not found, that's OK
+            String prefix = getNamespaceRegistry().getPrefix(uri);
+
+            // Generate a new prefix if the global mapping is already taken
+            String base = prefix;
+            for (int i = 2; namespaces.containsKey(prefix); i++) {
+                prefix = base + i;
+            }
+
+            if (!base.equals(prefix)) {
+                namespaces.put(prefix, uri);
+            }
+            return prefix;
+        }
+    }
+
+    /**
+     * Clears the re-mapped namespaces map.
+     */
+    void clear() {
+        namespaces.clear();
+    }
+
+    private NamespaceRegistry getNamespaceRegistry()
+            throws RepositoryException {
+        return sessionContext.getWorkspace().getNamespaceRegistry();
+    }
+
+    private void init() throws RepositoryException {
+        if (snapshot == null) {
+            NamespaceRegistry registry = getNamespaceRegistry();
+            Map<String, String> map = new HashMap<String, String>();
+            for (String prefix : registry.getPrefixes()) {
+                map.put(prefix, registry.getURI(prefix));
+            }
+            snapshot = map;
+        }
+    }
+}

Propchange: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionNamespaces.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionNamespaces.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL



Mime
View raw message