jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ang...@apache.org
Subject svn commit: r477172 - in /jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi: WorkspaceManager.java name/NamespaceRegistryImpl.java name/NamespaceStorage.java
Date Mon, 20 Nov 2006 13:42:56 GMT
Author: angela
Date: Mon Nov 20 05:42:55 2006
New Revision: 477172

URL: http://svn.apache.org/viewvc?view=rev&rev=477172
Log:
work in progress

- Completeness/Freshness of Namespace Registry

Modified:
    jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceManager.java
    jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/name/NamespaceRegistryImpl.java
    jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/name/NamespaceStorage.java

Modified: jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceManager.java?view=diff&rev=477172&r1=477171&r2=477172
==============================================================================
--- jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceManager.java
(original)
+++ jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/WorkspaceManager.java
Mon Nov 20 05:42:55 2006
@@ -330,7 +330,7 @@
      */
     private NamespaceRegistryImpl createNamespaceRegistry(Map descriptors) throws RepositoryException
{
         boolean level2 = Boolean.valueOf((String) descriptors.get(Repository.LEVEL_2_SUPPORTED)).booleanValue();
-        return new NamespaceRegistryImpl(this, service.getRegisteredNamespaces(sessionInfo),
level2);
+        return new NamespaceRegistryImpl(this, level2);
     }
 
     /**
@@ -591,6 +591,11 @@
     }
 
     //---------------------------------------------------< NamespaceStorage >---
+
+    public Map getRegisteredNamespaces() throws RepositoryException {
+        return service.getRegisteredNamespaces(sessionInfo);
+    }
+
     /**
      * @inheritDoc
      */

Modified: jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/name/NamespaceRegistryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/name/NamespaceRegistryImpl.java?view=diff&rev=477172&r1=477171&r2=477172
==============================================================================
--- jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/name/NamespaceRegistryImpl.java
(original)
+++ jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/name/NamespaceRegistryImpl.java
Mon Nov 20 05:42:55 2006
@@ -24,6 +24,8 @@
 import org.apache.jackrabbit.name.NameCache;
 import org.apache.jackrabbit.name.NameFormat;
 import org.apache.jackrabbit.util.XMLChar;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import javax.jcr.NamespaceRegistry;
 import javax.jcr.NamespaceException;
@@ -40,6 +42,8 @@
 public class NamespaceRegistryImpl extends AbstractNamespaceResolver
     implements NamespaceRegistry, NameCache {
 
+    private static Logger log = LoggerFactory.getLogger(NamespaceRegistryImpl.class);
+
     private static final HashSet reservedPrefixes = new HashSet();
     private static final HashSet reservedURIs = new HashSet();
 
@@ -72,27 +76,96 @@
 
     private final boolean level2Repository;
 
-    public NamespaceRegistryImpl(NamespaceStorage storage, Map nsValues, boolean level2Repository)
{
+    /**
+     * Create a new <code>NamespaceRegistryImpl</code>.
+     *
+     * @param storage
+     * @param level2Repository
+     * @throws RepositoryException
+     */
+    public NamespaceRegistryImpl(NamespaceStorage storage, boolean level2Repository)
+        throws RepositoryException {
         super(true); // enable listener support
+
         resolver = new CachingNamespaceResolver(this, 1000);
         this.storage = storage;
         this.level2Repository = level2Repository;
-        load(nsValues);
+
+        load();
     }
 
-    private void load(Map nsValues) {
+    /**
+     * Load all mappings from the <code>NamespaceStorage</code> and update this
+     * registry.
+     *
+     * @throws RepositoryException
+     */
+    private void load() throws RepositoryException {
+        Map nsValues = storage.getRegisteredNamespaces();
         Iterator prefixes = nsValues.keySet().iterator();
         while (prefixes.hasNext()) {
             String prefix = (String) prefixes.next();
-            if (!prefixToURI.containsKey(prefix)) {
-                String uri = (String) nsValues.get(prefix);
-                prefixToURI.put(prefix, uri);
-                uriToPrefix.put(uri, prefix);
-            }
+            String uri = (String) nsValues.get(prefix);
+            addMapping(prefix, uri);
+        }
+    }
+
+    /**
+     * Add a namespace with the given uri and prefix. If for the given
+     * <code>uri</code> is already registered with a different prefix, the
+     * existing mapping gets replaced.
+     *
+     * @param prefix
+     * @param uri
+     */
+    private void addMapping(String prefix, String uri) {
+        if (uriToPrefix.containsKey(uri)) {
+            String oldPrefix = (String) uriToPrefix.get(uri);
+            replaceMapping(oldPrefix, prefix, uri);
+        } else {
+            prefixToURI.put(prefix, uri);
+            uriToPrefix.put(uri, prefix);
+            notifyNamespaceAdded(prefix, uri);
         }
     }
 
     /**
+     * Remove the entries with the given prefix and uri from the registry
+     * and inform all listeners.
+     *
+     * @param prefix
+     * @param uri
+     */
+    private void removeMapping(String prefix, String uri) {
+        prefixToURI.remove(prefix).toString();
+        uriToPrefix.remove(uri);
+        // notify listeners
+        notifyNamespaceRemoved(uri);
+    }
+
+    /**
+     * Replace an existing registered namespace with the given <code>oldPrefix</code>
+     * by an entry with the new prefix. Subsequently all listeners are informed
+     * about the remapped namespace.
+     *
+     * @param oldPrefix
+     * @param prefix
+     * @param uri
+     */
+    private void replaceMapping(String oldPrefix, String prefix, String uri) {
+        if (oldPrefix.equals(prefix)) {
+            // mapping already existing -> nothing to do.
+            return;
+        }
+        prefixToURI.remove(oldPrefix);
+        prefixToURI.put(prefix, uri);
+        uriToPrefix.put(uri, prefix);
+        // notify: remapped existing namespace uri to new prefix
+        notifyNamespaceRemapped(oldPrefix, prefix, uri);
+    }
+
+    //--------------------------------------------------< NamespaceRegistry >---
+    /**
      * @see NamespaceRegistry#registerNamespace(String, String)
      */
     public void registerNamespace(String prefix, String uri) throws NamespaceException, UnsupportedRepositoryOperationException,
RepositoryException {
@@ -145,23 +218,10 @@
 
         // inform storage before mappings are added to maps and propagated to listeners
         storage.registerNamespace(prefix, uri);
-
-        // remove old prefix mapping
-        if (oldPrefix != null) {
-            prefixToURI.remove(oldPrefix);
-            uriToPrefix.remove(uri);
-        }
-        // add new prefix mapping
-        prefixToURI.put(prefix, uri);
-        uriToPrefix.put(uri, prefix);
-
-        // notify listeners
-        if (oldPrefix != null) {
-            // remapped existing namespace uri to new prefix
-            notifyNamespaceRemapped(oldPrefix, prefix, uri);
+        if (oldPrefix == null) {
+            addMapping(prefix, uri);
         } else {
-            // added new namespace uri mapped to prefix
-            notifyNamespaceAdded(prefix, uri);
+            replaceMapping(oldPrefix, prefix, uri);
         }
     }
 
@@ -181,14 +241,11 @@
         }
 
         // inform storage before mappings are added to maps and propagated to listeners
-        storage.unregisterNamespace(prefixToURI.get(prefix).toString());
+        String uri = prefixToURI.get(prefix).toString();
+        storage.unregisterNamespace(uri);
 
-        // update caches
-        String uri = prefixToURI.remove(prefix).toString();
-        uriToPrefix.remove(uri);
-
-        // notify listeners
-        notifyNamespaceRemoved(uri);
+        // update caches and notify listeners
+        removeMapping(prefix, uri);
     }
 
     /**
@@ -204,7 +261,6 @@
      */
     public String[] getURIs() throws RepositoryException {
         return (String[]) uriToPrefix.keySet().toArray(new String[uriToPrefix.keySet().size()]);
-
     }
 
     /**
@@ -212,10 +268,21 @@
      * @see org.apache.jackrabbit.name.NamespaceResolver#getURI(String)
      */
     public String getURI(String prefix) throws NamespaceException {
+        if (!prefixToURI.containsKey(prefix)) {
+            // reload mappings in order to make sure, the NamespaceRegistry is
+            // up to date, and try to retrieve the uri again.
+            try {
+                load();
+            } catch (RepositoryException ex) {
+                log.warn("Internal error while loading registered namespaces.");
+            }
+        }
+
         String uri = (String) prefixToURI.get(prefix);
         if (uri == null) {
             throw new NamespaceException(prefix + ": is not a registered namespace prefix.");
         }
+
         return uri;
     }
 
@@ -224,10 +291,20 @@
      * @see org.apache.jackrabbit.name.NamespaceResolver#getPrefix(String)
      */
     public String getPrefix(String uri) throws NamespaceException {
+        if (!uriToPrefix.containsKey(uri)) {
+            // reload mappings in order to make sure, the NamespaceRegistry is
+            // up to date, and try to retrieve the prefix again.
+            try {
+                load();
+            } catch (RepositoryException ex) {
+                log.warn("Internal error while loading registered namespaces.");
+            }
+        }
         String prefix = (String) uriToPrefix.get(uri);
         if (prefix == null) {
             throw new NamespaceException(uri + ": is not a registered namespace uri.");
         }
+
         return prefix;
     }
 
@@ -257,18 +334,27 @@
         return resolver.retrieveName(jcrName);
     }
 
+    /**
+     * {@inheritDoc}
+     */
     public String retrieveName(QName name) {
         // just delegate to internal cache
         return resolver.retrieveName(name);
     }
 
+    /**
+     * {@inheritDoc}
+     */
     public void cacheName(String jcrName, QName name) {
         // just delegate to internal cache
         resolver.cacheName(jcrName, name);
     }
 
+    /**
+     * {@inheritDoc}
+     */
     public void evictAllNames() {
         // just delegate to internal cache
         resolver.evictAllNames();
     }
-}
\ No newline at end of file
+}

Modified: jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/name/NamespaceStorage.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/name/NamespaceStorage.java?view=diff&rev=477172&r1=477171&r2=477172
==============================================================================
--- jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/name/NamespaceStorage.java
(original)
+++ jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/name/NamespaceStorage.java
Mon Nov 20 05:42:55 2006
@@ -16,6 +16,8 @@
  */
 package org.apache.jackrabbit.jcr2spi.name;
 
+import java.util.Map;
+
 import javax.jcr.NamespaceException;
 import javax.jcr.UnsupportedRepositoryOperationException;
 import javax.jcr.AccessDeniedException;
@@ -26,8 +28,10 @@
  */
 public interface NamespaceStorage {
 
+    public Map getRegisteredNamespaces() throws RepositoryException;
+  
     public void	registerNamespace(String prefix, String uri) throws NamespaceException, UnsupportedRepositoryOperationException,
AccessDeniedException, RepositoryException;
 
     public void unregisterNamespace(String uri) throws NamespaceException, UnsupportedRepositoryOperationException,
AccessDeniedException, RepositoryException;
 
-}
\ No newline at end of file
+}



Mime
View raw message