You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by ju...@apache.org on 2008/10/19 00:02:08 UTC

svn commit: r705932 - in /jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi: NamespaceRegistryImpl.java NamespaceStorage.java WorkspaceManager.java

Author: jukka
Date: Sat Oct 18 15:02:07 2008
New Revision: 705932

URL: http://svn.apache.org/viewvc?rev=705932&view=rev
Log:
JCR-1612: Reintroduce NamespaceStorage and namespace-caching

Restore a simple repository-level namespace cache.

Personally I think this kind of caching should be up to a remoting layer to implement if needed. But as discussed in JCR-1612 I'm restoring caching functionality for now.

Modified:
    jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/NamespaceRegistryImpl.java
    jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/NamespaceStorage.java
    jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceManager.java

Modified: jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/NamespaceRegistryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/NamespaceRegistryImpl.java?rev=705932&r1=705931&r2=705932&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/NamespaceRegistryImpl.java (original)
+++ jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/NamespaceRegistryImpl.java Sat Oct 18 15:02:07 2008
@@ -16,14 +16,12 @@
  */
 package org.apache.jackrabbit.jcr2spi;
 
-import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.jcr.NamespaceRegistry;
 import javax.jcr.NamespaceException;
-import javax.jcr.UnsupportedRepositoryOperationException;
+import javax.jcr.NamespaceRegistry;
 import javax.jcr.RepositoryException;
 
 /**
@@ -32,10 +30,15 @@
  */
 public class NamespaceRegistryImpl implements NamespaceRegistry {
 
-    private static Logger log = LoggerFactory.getLogger(NamespaceRegistryImpl.class);
-
+    /**
+     * The namespace storage.
+     */
     private final NamespaceStorage storage;
 
+    private final Map prefixToUri = new HashMap();
+
+    private final Map uriToPrefix = new HashMap();
+
     /**
      * Create a new <code>NamespaceRegistryImpl</code>.
      *
@@ -50,58 +53,93 @@
     /**
      * @see NamespaceRegistry#registerNamespace(String, String)
      */
-    public void registerNamespace(String prefix, String uri) throws NamespaceException, UnsupportedRepositoryOperationException, RepositoryException {
+    public synchronized void registerNamespace(String prefix, String uri)
+            throws RepositoryException {
         storage.registerNamespace(prefix, uri);
+        reloadNamespaces();
     }
 
     /**
      * @see NamespaceRegistry#unregisterNamespace(String)
      */
-    public void unregisterNamespace(String prefix) throws NamespaceException, UnsupportedRepositoryOperationException, RepositoryException {
+    public synchronized void unregisterNamespace(String prefix)
+            throws RepositoryException {
         storage.unregisterNamespace(prefix);
+        reloadNamespaces();
     }
 
     /**
      * @see javax.jcr.NamespaceRegistry#getPrefixes()
      */
-    public String[] getPrefixes() throws RepositoryException {
-        Collection prefixes = storage.getRegisteredNamespaces().keySet();
-        return (String[]) prefixes.toArray(new String[prefixes.size()]);
+    public synchronized String[] getPrefixes() throws RepositoryException {
+        reloadNamespaces();
+        return (String[]) prefixToUri.keySet().toArray(new String[prefixToUri.size()]);
     }
 
     /**
      * @see javax.jcr.NamespaceRegistry#getURIs()
      */
-    public String[] getURIs() throws RepositoryException {
-        Collection uris = storage.getRegisteredNamespaces().values();
-        return (String[]) uris.toArray(new String[uris.size()]);
+    public synchronized String[] getURIs() throws RepositoryException {
+        reloadNamespaces();
+        return (String[]) uriToPrefix.keySet().toArray(new String[uriToPrefix.size()]);
     }
 
     /**
      * @see javax.jcr.NamespaceRegistry#getURI(String)
      * @see org.apache.jackrabbit.spi.commons.namespace.NamespaceResolver#getURI(String)
      */
-    public String getURI(String prefix) throws NamespaceException {
-        // try to load the uri
-        try {
-            return storage.getURI(prefix);
-        } catch (RepositoryException ex) {
-            log.debug("Internal error while loading registered namespaces.");
-            throw new NamespaceException(prefix + ": is not a registered namespace prefix.");
+    public synchronized String getURI(String prefix)
+            throws RepositoryException {
+        String uri = (String) prefixToUri.get(prefix);
+        if (uri == null) {
+            // Not found, try loading latest state from storage
+            reloadNamespaces();
+            uri = (String) prefixToUri.get(prefix);
         }
+        if (uri == null) {
+            // Still not found, it's not a known prefix
+            throw new NamespaceException("Namespace not found: " + prefix);
+        }
+        return uri;
     }
 
     /**
      * @see javax.jcr.NamespaceRegistry#getPrefix(String)
      * @see org.apache.jackrabbit.spi.commons.namespace.NamespaceResolver#getPrefix(String)
      */
-    public String getPrefix(String uri) throws NamespaceException {
-        // try to load the prefix
-        try {
-            return storage.getPrefix(uri);
-        } catch (RepositoryException ex) {
-            log.debug("Internal error while loading registered namespaces.");
-            throw new NamespaceException(uri + ": is not a registered namespace uri.");
+    public synchronized String getPrefix(String uri) throws RepositoryException {
+        String prefix = (String) uriToPrefix.get(uri);
+        if (prefix == null) {
+            // Not found, try loading latest state from storage
+            reloadNamespaces();
+            prefix = (String) uriToPrefix.get(uri);
+        }
+        if (prefix == null) {
+            // Still not found, it's not a known URI
+            throw new NamespaceException("Namespace not found: " + uri);
+        }
+        return prefix;
+    }
+
+    //-------------------------------------------------------------< private >
+
+    /**
+     * Clears the current namespace cache and loads new mappings from
+     * the underlying namespace storage.
+     *
+     * @throws RepositoryException if new mappings could not be loaded
+     */
+    private synchronized void reloadNamespaces() throws RepositoryException {
+        Map namespaces = storage.getRegisteredNamespaces();
+
+        prefixToUri.clear();
+        uriToPrefix.clear();
+
+        Iterator iterator = namespaces.entrySet().iterator();
+        while (iterator.hasNext()) {
+            Map.Entry entry = (Map.Entry) iterator.next();
+            prefixToUri.put(entry.getKey(), entry.getValue());
+            uriToPrefix.put(entry.getValue(), entry.getKey());
         }
     }
 

Modified: jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/NamespaceStorage.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/NamespaceStorage.java?rev=705932&r1=705931&r2=705932&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/NamespaceStorage.java (original)
+++ jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/NamespaceStorage.java Sat Oct 18 15:02:07 2008
@@ -18,9 +18,6 @@
 
 import java.util.Map;
 
-import javax.jcr.NamespaceException;
-import javax.jcr.UnsupportedRepositoryOperationException;
-import javax.jcr.AccessDeniedException;
 import javax.jcr.RepositoryException;
 
 /**
@@ -28,14 +25,10 @@
  */
 public interface NamespaceStorage {
 
-    public Map getRegisteredNamespaces() throws RepositoryException;
+    Map getRegisteredNamespaces() throws RepositoryException;
 
-    public String getPrefix(String uri) throws NamespaceException, RepositoryException;
+    void registerNamespace(String prefix, String uri) throws RepositoryException;
 
-    public String getURI(String prefix) throws NamespaceException, RepositoryException;
-
-    public void registerNamespace(String prefix, String uri) throws NamespaceException, UnsupportedRepositoryOperationException, AccessDeniedException, RepositoryException;
-
-    public void unregisterNamespace(String uri) throws NamespaceException, UnsupportedRepositoryOperationException, AccessDeniedException, RepositoryException;
+    void unregisterNamespace(String uri) throws RepositoryException;
 
 }

Modified: jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceManager.java?rev=705932&r1=705931&r2=705932&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceManager.java (original)
+++ jackrabbit/trunk/jackrabbit-jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceManager.java Sat Oct 18 15:02:07 2008
@@ -16,103 +16,102 @@
  */
 package org.apache.jackrabbit.jcr2spi;
 
-import org.apache.jackrabbit.jcr2spi.nodetype.NodeTypeRegistryImpl;
-import org.apache.jackrabbit.jcr2spi.nodetype.NodeTypeRegistry;
-import org.apache.jackrabbit.jcr2spi.nodetype.NodeTypeStorage;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.jcr.AccessDeniedException;
+import javax.jcr.InvalidItemStateException;
+import javax.jcr.ItemExistsException;
+import javax.jcr.ItemNotFoundException;
+import javax.jcr.MergeException;
+import javax.jcr.NamespaceRegistry;
+import javax.jcr.NoSuchWorkspaceException;
+import javax.jcr.PathNotFoundException;
+import javax.jcr.ReferentialIntegrityException;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.UnsupportedRepositoryOperationException;
+import javax.jcr.lock.LockException;
+import javax.jcr.nodetype.ConstraintViolationException;
+import javax.jcr.nodetype.NoSuchNodeTypeException;
+import javax.jcr.query.InvalidQueryException;
+import javax.jcr.version.VersionException;
+
+import org.apache.jackrabbit.jcr2spi.config.CacheBehaviour;
+import org.apache.jackrabbit.jcr2spi.hierarchy.HierarchyEventListener;
+import org.apache.jackrabbit.jcr2spi.hierarchy.HierarchyManager;
+import org.apache.jackrabbit.jcr2spi.hierarchy.HierarchyManagerImpl;
+import org.apache.jackrabbit.jcr2spi.nodetype.EffectiveNodeTypeProvider;
 import org.apache.jackrabbit.jcr2spi.nodetype.ItemDefinitionProvider;
 import org.apache.jackrabbit.jcr2spi.nodetype.ItemDefinitionProviderImpl;
-import org.apache.jackrabbit.jcr2spi.nodetype.EffectiveNodeTypeProvider;
 import org.apache.jackrabbit.jcr2spi.nodetype.NodeTypeCache;
-import org.apache.jackrabbit.jcr2spi.state.ItemState;
-import org.apache.jackrabbit.jcr2spi.state.ChangeLog;
-import org.apache.jackrabbit.jcr2spi.state.UpdatableItemStateManager;
-import org.apache.jackrabbit.jcr2spi.state.ItemStateFactory;
-import org.apache.jackrabbit.jcr2spi.state.WorkspaceItemStateFactory;
-import org.apache.jackrabbit.jcr2spi.state.NodeState;
-import org.apache.jackrabbit.jcr2spi.state.TransientItemStateFactory;
-import org.apache.jackrabbit.jcr2spi.state.TransientISFactory;
-import org.apache.jackrabbit.jcr2spi.state.Status;
-import org.apache.jackrabbit.jcr2spi.operation.OperationVisitor;
+import org.apache.jackrabbit.jcr2spi.nodetype.NodeTypeRegistry;
+import org.apache.jackrabbit.jcr2spi.nodetype.NodeTypeRegistryImpl;
+import org.apache.jackrabbit.jcr2spi.nodetype.NodeTypeStorage;
+import org.apache.jackrabbit.jcr2spi.observation.InternalEventListener;
+import org.apache.jackrabbit.jcr2spi.operation.AddLabel;
 import org.apache.jackrabbit.jcr2spi.operation.AddNode;
 import org.apache.jackrabbit.jcr2spi.operation.AddProperty;
+import org.apache.jackrabbit.jcr2spi.operation.Checkin;
+import org.apache.jackrabbit.jcr2spi.operation.Checkout;
 import org.apache.jackrabbit.jcr2spi.operation.Clone;
 import org.apache.jackrabbit.jcr2spi.operation.Copy;
-import org.apache.jackrabbit.jcr2spi.operation.Move;
-import org.apache.jackrabbit.jcr2spi.operation.Remove;
-import org.apache.jackrabbit.jcr2spi.operation.SetMixin;
-import org.apache.jackrabbit.jcr2spi.operation.SetPropertyValue;
-import org.apache.jackrabbit.jcr2spi.operation.ReorderNodes;
-import org.apache.jackrabbit.jcr2spi.operation.Operation;
-import org.apache.jackrabbit.jcr2spi.operation.Checkout;
-import org.apache.jackrabbit.jcr2spi.operation.Checkin;
-import org.apache.jackrabbit.jcr2spi.operation.Update;
-import org.apache.jackrabbit.jcr2spi.operation.Restore;
-import org.apache.jackrabbit.jcr2spi.operation.ResolveMergeConflict;
-import org.apache.jackrabbit.jcr2spi.operation.Merge;
 import org.apache.jackrabbit.jcr2spi.operation.LockOperation;
 import org.apache.jackrabbit.jcr2spi.operation.LockRefresh;
 import org.apache.jackrabbit.jcr2spi.operation.LockRelease;
-import org.apache.jackrabbit.jcr2spi.operation.AddLabel;
+import org.apache.jackrabbit.jcr2spi.operation.Merge;
+import org.apache.jackrabbit.jcr2spi.operation.Move;
+import org.apache.jackrabbit.jcr2spi.operation.Operation;
+import org.apache.jackrabbit.jcr2spi.operation.OperationVisitor;
+import org.apache.jackrabbit.jcr2spi.operation.Remove;
 import org.apache.jackrabbit.jcr2spi.operation.RemoveLabel;
 import org.apache.jackrabbit.jcr2spi.operation.RemoveVersion;
+import org.apache.jackrabbit.jcr2spi.operation.ReorderNodes;
+import org.apache.jackrabbit.jcr2spi.operation.ResolveMergeConflict;
+import org.apache.jackrabbit.jcr2spi.operation.Restore;
+import org.apache.jackrabbit.jcr2spi.operation.SetMixin;
+import org.apache.jackrabbit.jcr2spi.operation.SetPropertyValue;
+import org.apache.jackrabbit.jcr2spi.operation.Update;
 import org.apache.jackrabbit.jcr2spi.operation.WorkspaceImport;
 import org.apache.jackrabbit.jcr2spi.security.AccessManager;
-import org.apache.jackrabbit.jcr2spi.observation.InternalEventListener;
-import org.apache.jackrabbit.jcr2spi.config.CacheBehaviour;
-import org.apache.jackrabbit.jcr2spi.hierarchy.HierarchyEventListener;
-import org.apache.jackrabbit.jcr2spi.hierarchy.HierarchyManager;
-import org.apache.jackrabbit.jcr2spi.hierarchy.HierarchyManagerImpl;
-import org.apache.jackrabbit.spi.Path;
-import org.apache.jackrabbit.spi.Name;
-import org.apache.jackrabbit.spi.RepositoryService;
-import org.apache.jackrabbit.spi.SessionInfo;
-import org.apache.jackrabbit.spi.NodeId;
-import org.apache.jackrabbit.spi.IdFactory;
-import org.apache.jackrabbit.spi.LockInfo;
-import org.apache.jackrabbit.spi.QueryInfo;
-import org.apache.jackrabbit.spi.ItemId;
-import org.apache.jackrabbit.spi.PropertyId;
+import org.apache.jackrabbit.jcr2spi.state.ChangeLog;
+import org.apache.jackrabbit.jcr2spi.state.ItemState;
+import org.apache.jackrabbit.jcr2spi.state.ItemStateFactory;
+import org.apache.jackrabbit.jcr2spi.state.NodeState;
+import org.apache.jackrabbit.jcr2spi.state.Status;
+import org.apache.jackrabbit.jcr2spi.state.TransientISFactory;
+import org.apache.jackrabbit.jcr2spi.state.TransientItemStateFactory;
+import org.apache.jackrabbit.jcr2spi.state.UpdatableItemStateManager;
+import org.apache.jackrabbit.jcr2spi.state.WorkspaceItemStateFactory;
 import org.apache.jackrabbit.spi.Batch;
+import org.apache.jackrabbit.spi.Event;
 import org.apache.jackrabbit.spi.EventBundle;
 import org.apache.jackrabbit.spi.EventFilter;
-import org.apache.jackrabbit.spi.QNodeTypeDefinition;
-import org.apache.jackrabbit.spi.QValue;
-import org.apache.jackrabbit.spi.Event;
+import org.apache.jackrabbit.spi.IdFactory;
+import org.apache.jackrabbit.spi.ItemId;
+import org.apache.jackrabbit.spi.LockInfo;
+import org.apache.jackrabbit.spi.Name;
 import org.apache.jackrabbit.spi.NameFactory;
+import org.apache.jackrabbit.spi.NodeId;
+import org.apache.jackrabbit.spi.Path;
 import org.apache.jackrabbit.spi.PathFactory;
+import org.apache.jackrabbit.spi.PropertyId;
+import org.apache.jackrabbit.spi.QNodeTypeDefinition;
+import org.apache.jackrabbit.spi.QValue;
+import org.apache.jackrabbit.spi.QueryInfo;
+import org.apache.jackrabbit.spi.RepositoryService;
+import org.apache.jackrabbit.spi.SessionInfo;
 import org.apache.jackrabbit.spi.Subscription;
-import org.slf4j.LoggerFactory;
 import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-import javax.jcr.RepositoryException;
-import javax.jcr.NamespaceRegistry;
-import javax.jcr.UnsupportedRepositoryOperationException;
-import javax.jcr.AccessDeniedException;
-import javax.jcr.PathNotFoundException;
-import javax.jcr.ItemNotFoundException;
-import javax.jcr.NamespaceException;
-import javax.jcr.NoSuchWorkspaceException;
-import javax.jcr.ItemExistsException;
-import javax.jcr.InvalidItemStateException;
-import javax.jcr.MergeException;
-import javax.jcr.Session;
-import javax.jcr.ReferentialIntegrityException;
-import javax.jcr.query.InvalidQueryException;
-import javax.jcr.version.VersionException;
-import javax.jcr.lock.LockException;
-import javax.jcr.nodetype.NoSuchNodeTypeException;
-import javax.jcr.nodetype.ConstraintViolationException;
-
-import java.util.List;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.Set;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Collection;
-
-import EDU.oswego.cs.dl.util.concurrent.Sync;
 import EDU.oswego.cs.dl.util.concurrent.Mutex;
+import EDU.oswego.cs.dl.util.concurrent.Sync;
 
 /**
  * <code>WorkspaceManager</code>...
@@ -631,28 +630,16 @@
     /**
      * @inheritDoc
      */
-    public String getPrefix(String uri) throws NamespaceException, RepositoryException {
-        return service.getNamespacePrefix(sessionInfo, uri);
-    }
-
-    /**
-     * @inheritDoc
-     */
-    public String getURI(String prefix) throws NamespaceException, RepositoryException {
-        return service.getNamespaceURI(sessionInfo, prefix);
-    }
-
-    /**
-     * @inheritDoc
-     */
-    public void registerNamespace(String prefix, String uri) throws NamespaceException, UnsupportedRepositoryOperationException, AccessDeniedException, RepositoryException {
+    public void registerNamespace(String prefix, String uri)
+            throws RepositoryException {
         service.registerNamespace(sessionInfo, prefix, uri);
     }
 
     /**
      * @inheritDoc
      */
-    public void unregisterNamespace(String uri) throws NamespaceException, UnsupportedRepositoryOperationException, AccessDeniedException, RepositoryException {
+    public void unregisterNamespace(String uri)
+            throws RepositoryException {
         service.unregisterNamespace(sessionInfo, uri);
     }