jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r498558 - in /jackrabbit/trunk/contrib/spi: jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/ jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/name/ spi/src/main/java/org/apache/jackrabbit/spi/ spi2dav/src/main/java/org/apache/jackrab...
Date Mon, 22 Jan 2007 08:41:18 GMT
Author: mreutegg
Date: Mon Jan 22 00:41:17 2007
New Revision: 498558

URL: http://svn.apache.org/viewvc?view=rev&rev=498558
Log:
SPI
- NamespaceRegistryImpl now only relies on RepositoryService.getRegisteredNamespaces() for
methods: getPrefixes() and getURIs().
- Added methods getPrefix() and getURI() to NamespaceStorage.
- Separated nsResolver in RepositoryServiceImpl into namespace cache and session info bound
namespace resolver
- Added methods getNamespacePrefix() and getNamespaceURI() to RepositoryService.

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
    jackrabbit/trunk/contrib/spi/spi/src/main/java/org/apache/jackrabbit/spi/RepositoryService.java
    jackrabbit/trunk/contrib/spi/spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/RepositoryServiceImpl.java
    jackrabbit/trunk/contrib/spi/spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/URIResolverImpl.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=498558&r1=498557&r2=498558
==============================================================================
--- 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 Jan 22 00:41:17 2007
@@ -612,6 +612,20 @@
     /**
      * @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 {
         service.registerNamespace(sessionInfo, prefix, uri);
     }

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=498558&r1=498557&r2=498558
==============================================================================
--- 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 Jan 22 00:41:17 2007
@@ -39,6 +39,8 @@
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Iterator;
+import java.util.Set;
+import java.util.Collection;
 
 /**
  * <code>NamespaceRegistryImpl</code>...
@@ -48,28 +50,37 @@
 
     private static Logger log = LoggerFactory.getLogger(NamespaceRegistryImpl.class);
 
-    private static final HashSet reservedPrefixes = new HashSet();
-    private static final HashSet reservedURIs = new HashSet();
+    private static final Set RESERVED_PREFIXES = new HashSet();
+    private static final Set RESERVED_URIS = new HashSet();
+    private static final Map RESERVED_NAMESPACES = new HashMap();
 
     static {
         // reserved prefixes
-        reservedPrefixes.add(QName.NS_XML_PREFIX);
-        reservedPrefixes.add(QName.NS_XMLNS_PREFIX);
+        RESERVED_PREFIXES.add(QName.NS_XML_PREFIX);
+        RESERVED_PREFIXES.add(QName.NS_XMLNS_PREFIX);
         // predefined (e.g. built-in) prefixes
-        reservedPrefixes.add(QName.NS_REP_PREFIX);
-        reservedPrefixes.add(QName.NS_JCR_PREFIX);
-        reservedPrefixes.add(QName.NS_NT_PREFIX);
-        reservedPrefixes.add(QName.NS_MIX_PREFIX);
-        reservedPrefixes.add(QName.NS_SV_PREFIX);
+        RESERVED_PREFIXES.add(QName.NS_REP_PREFIX);
+        RESERVED_PREFIXES.add(QName.NS_JCR_PREFIX);
+        RESERVED_PREFIXES.add(QName.NS_NT_PREFIX);
+        RESERVED_PREFIXES.add(QName.NS_MIX_PREFIX);
+        RESERVED_PREFIXES.add(QName.NS_SV_PREFIX);
         // reserved namespace URI's
-        reservedURIs.add(QName.NS_XML_URI);
-        reservedURIs.add(QName.NS_XMLNS_URI);
+        RESERVED_URIS.add(QName.NS_XML_URI);
+        RESERVED_URIS.add(QName.NS_XMLNS_URI);
         // predefined (e.g. built-in) namespace URI's
-        reservedURIs.add(QName.NS_REP_URI);
-        reservedURIs.add(QName.NS_JCR_URI);
-        reservedURIs.add(QName.NS_NT_URI);
-        reservedURIs.add(QName.NS_MIX_URI);
-        reservedURIs.add(QName.NS_SV_URI);
+        RESERVED_URIS.add(QName.NS_REP_URI);
+        RESERVED_URIS.add(QName.NS_JCR_URI);
+        RESERVED_URIS.add(QName.NS_NT_URI);
+        RESERVED_URIS.add(QName.NS_MIX_URI);
+        RESERVED_URIS.add(QName.NS_SV_URI);
+        // reserved and predefined namespaces
+        RESERVED_NAMESPACES.put(QName.NS_XML_PREFIX, QName.NS_XML_URI);
+        RESERVED_NAMESPACES.put(QName.NS_XMLNS_PREFIX, QName.NS_XMLNS_URI);
+        RESERVED_NAMESPACES.put(QName.NS_REP_PREFIX, QName.NS_REP_URI);
+        RESERVED_NAMESPACES.put(QName.NS_JCR_PREFIX, QName.NS_JCR_URI);
+        RESERVED_NAMESPACES.put(QName.NS_NT_PREFIX, QName.NS_NT_URI);
+        RESERVED_NAMESPACES.put(QName.NS_MIX_PREFIX, QName.NS_MIX_URI);
+        RESERVED_NAMESPACES.put(QName.NS_SV_PREFIX, QName.NS_SV_URI);
     }
 
     private final HashMap prefixToURI = new HashMap();
@@ -85,32 +96,20 @@
      *
      * @param storage
      * @param level2Repository
-     * @throws RepositoryException
      */
-    public NamespaceRegistryImpl(NamespaceStorage storage, boolean level2Repository)
-        throws RepositoryException {
+    public NamespaceRegistryImpl(NamespaceStorage storage,
+                                 boolean level2Repository) {
         super(true); // enable listener support
 
         resolver = new CachingNameResolver(new ParsingNameResolver(this));
         this.storage = storage;
         this.level2Repository = level2Repository;
 
-        load();
-    }
-
-    /**
-     * 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();
-            String uri = (String) nsValues.get(prefix);
-            addMapping(prefix, uri);
+        // prefill with reserved namespaces
+        prefixToURI.putAll(RESERVED_NAMESPACES);
+        for (Iterator it = RESERVED_NAMESPACES.keySet().iterator(); it.hasNext(); ) {
+            String prefix = (String) it.next();
+            uriToPrefix.put(prefixToURI.get(prefix), prefix);
         }
     }
 
@@ -141,7 +140,7 @@
      * @param uri
      */
     private void removeMapping(String prefix, String uri) {
-        prefixToURI.remove(prefix).toString();
+        prefixToURI.remove(prefix);
         uriToPrefix.remove(uri);
         // notify listeners
         notifyNamespaceRemoved(uri);
@@ -168,6 +167,22 @@
         notifyNamespaceRemapped(oldPrefix, prefix, uri);
     }
 
+    /**
+     * Syncs the cached namespace mappings with the given namespaces map.
+     *
+     * @param namespaces the up-to-date namespace mapping.
+     */
+    private void syncNamespaces(Map namespaces) {
+        prefixToURI.clear();
+        prefixToURI.putAll(namespaces);
+        uriToPrefix.clear();
+        for (Iterator it = namespaces.keySet().iterator(); it.hasNext(); ) {
+            String prefix = (String) it.next();
+            String uri = (String) namespaces.get(prefix);
+            uriToPrefix.put(uri, prefix);
+        }
+    }
+
     //--------------------------------------------------< NamespaceRegistry >---
     /**
      * @see NamespaceRegistry#registerNamespace(String, String)
@@ -183,11 +198,11 @@
         if (QName.NS_EMPTY_PREFIX.equals(prefix) || QName.NS_DEFAULT_URI.equals(uri)) {
             throw new NamespaceException("default namespace is reserved and can not be changed");
         }
-        if (reservedURIs.contains(uri)) {
+        if (RESERVED_URIS.contains(uri)) {
             throw new NamespaceException("failed to register namespace "
                 + prefix + " -> " + uri + ": reserved URI");
         }
-        if (reservedPrefixes.contains(prefix)) {
+        if (RESERVED_PREFIXES.contains(prefix)) {
             throw new NamespaceException("failed to register namespace "
                 + prefix + " -> " + uri + ": reserved prefix");
         }
@@ -203,12 +218,18 @@
         }
 
         // check existing mappings
-        String oldPrefix = (String) uriToPrefix.get(uri);
+        String oldPrefix = null;
+        try {
+            oldPrefix = getPrefix(uri);
+        } catch (NamespaceException e) {
+            // does not exist
+        }
         if (prefix.equals(oldPrefix)) {
             throw new NamespaceException("failed to register namespace "
                 + prefix + " -> " + uri + ": mapping already exists");
         }
-        if (prefixToURI.containsKey(prefix)) {
+        try {
+            getURI(prefix);
             /**
              * prevent remapping of existing prefixes because this would in effect
              * remove the previously assigned namespace;
@@ -218,6 +239,8 @@
             throw new NamespaceException("failed to register namespace "
                 + prefix + " -> " + uri
                 + ": remapping existing prefixes is not supported.");
+        } catch (NamespaceException e) {
+            // ok
         }
 
         // inform storage before mappings are added to maps and propagated to listeners
@@ -237,15 +260,12 @@
             throw new UnsupportedRepositoryOperationException("Repository is Level1 only.");
         }
 
-        if (reservedPrefixes.contains(prefix)) {
+        if (RESERVED_PREFIXES.contains(prefix)) {
             throw new NamespaceException("reserved prefix: " + prefix);
         }
-        if (!prefixToURI.containsKey(prefix)) {
-            throw new NamespaceException("unknown prefix: " + prefix);
-        }
 
+        String uri = getURI(prefix);
         // inform storage before mappings are added to maps and propagated to listeners
-        String uri = prefixToURI.get(prefix).toString();
         storage.unregisterNamespace(uri);
 
         // update caches and notify listeners
@@ -256,15 +276,20 @@
      * @see javax.jcr.NamespaceRegistry#getPrefixes()
      */
     public String[] getPrefixes() throws RepositoryException {
-        return (String[]) prefixToURI.keySet().toArray(new String[prefixToURI.keySet().size()]);
-
+        Map namespaces = storage.getRegisteredNamespaces();
+        syncNamespaces(namespaces);
+        Set prefixes = namespaces.keySet();
+        return (String[]) prefixes.toArray(new String[prefixes.size()]);
     }
 
     /**
      * @see javax.jcr.NamespaceRegistry#getURIs()
      */
     public String[] getURIs() throws RepositoryException {
-        return (String[]) uriToPrefix.keySet().toArray(new String[uriToPrefix.keySet().size()]);
+        Map namespaces = storage.getRegisteredNamespaces();
+        syncNamespaces(namespaces);
+        Collection uris = namespaces.values();
+        return (String[]) uris.toArray(new String[uris.size()]);
     }
 
     /**
@@ -272,17 +297,17 @@
      * @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.
+        String uri = (String) prefixToURI.get(prefix);
+        if (uri == null) {
+            // try to load the uri
             try {
-                load();
+                uri = storage.getURI(prefix);
+                prefixToURI.put(prefix, uri);
             } 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.");
         }
@@ -295,16 +320,17 @@
      * @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.
+        String prefix = (String) uriToPrefix.get(uri);
+        if (prefix == null) {
+            // try to load the prefix
             try {
-                load();
+                prefix = storage.getPrefix(uri);
+                uriToPrefix.put(uri, prefix);
             } 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.");
         }

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=498558&r1=498557&r2=498558
==============================================================================
--- 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 Jan 22 00:41:17 2007
@@ -29,8 +29,12 @@
 public interface NamespaceStorage {
 
     public Map getRegisteredNamespaces() throws RepositoryException;
-  
-    public void	registerNamespace(String prefix, String uri) throws NamespaceException, UnsupportedRepositoryOperationException,
AccessDeniedException, RepositoryException;
+
+    public String getPrefix(String uri) throws NamespaceException, 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;
 

Modified: jackrabbit/trunk/contrib/spi/spi/src/main/java/org/apache/jackrabbit/spi/RepositoryService.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/spi/src/main/java/org/apache/jackrabbit/spi/RepositoryService.java?view=diff&rev=498558&r1=498557&r2=498558
==============================================================================
--- jackrabbit/trunk/contrib/spi/spi/src/main/java/org/apache/jackrabbit/spi/RepositoryService.java
(original)
+++ jackrabbit/trunk/contrib/spi/spi/src/main/java/org/apache/jackrabbit/spi/RepositoryService.java
Mon Jan 22 00:41:17 2007
@@ -649,10 +649,34 @@
      * @see javax.jcr.Workspace#getNamespaceRegistry()
      * @see javax.jcr.NamespaceRegistry#getPrefixes()
      * @see javax.jcr.NamespaceRegistry#getURIs()
-     * @see javax.jcr.NamespaceRegistry#getPrefix(String)
-     * @see javax.jcr.NamespaceRegistry#getURI(String)
      */
     public Map getRegisteredNamespaces(SessionInfo sessionInfo) throws RepositoryException;
+
+    /**
+     * Returns the namespace URI for the given namespace <code>prefix</code>.
+     *
+     * @param sessionInfo the session info.
+     * @param prefix a namespace prefix to resolve.
+     * @return the namespace URI for the given namespace <code>prefix</code>.
+     * @throws NamespaceException if prefix is not mapped to a namespace URI.
+     * @throws RepositoryException if another error occurs.
+     * @see javax.jcr.NamespaceRegistry#getURI(String)
+     */
+    public String getNamespaceURI(SessionInfo sessionInfo, String prefix)
+            throws NamespaceException, RepositoryException;
+
+    /**
+     * Returns the namespace prefix for the given namespace <code>uri</code>.
+     *
+     * @param sessionInfo the session info.
+     * @param uri the namespace URI.
+     * @return the namespace prefix.
+     * @throws NamespaceException if the URI unknown.
+     * @throws RepositoryException if another error occurs.
+     * @see javax.jcr.NamespaceRegistry#getPrefix(String)
+     */
+    public String getNamespacePrefix(SessionInfo sessionInfo, String uri)
+            throws NamespaceException, RepositoryException;
 
     /**
      * Register a new namespace with the given prefix and uri

Modified: jackrabbit/trunk/contrib/spi/spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/RepositoryServiceImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/RepositoryServiceImpl.java?view=diff&rev=498558&r1=498557&r2=498558
==============================================================================
--- jackrabbit/trunk/contrib/spi/spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/RepositoryServiceImpl.java
(original)
+++ jackrabbit/trunk/contrib/spi/spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/RepositoryServiceImpl.java
Mon Jan 22 00:41:17 2007
@@ -102,6 +102,10 @@
 import org.apache.jackrabbit.name.NameFormat;
 import org.apache.jackrabbit.name.Path;
 import org.apache.jackrabbit.name.NameException;
+import org.apache.jackrabbit.name.NamespaceResolver;
+import org.apache.jackrabbit.name.IllegalNameException;
+import org.apache.jackrabbit.name.UnknownPrefixException;
+import org.apache.jackrabbit.name.AbstractNamespaceResolver;
 import org.apache.jackrabbit.spi.Batch;
 import org.apache.jackrabbit.spi.RepositoryService;
 import org.apache.jackrabbit.spi.SessionInfo;
@@ -193,7 +197,7 @@
     private final ValueFactory valueFactory;
 
     private final Document domFactory;
-    private final NamespaceResolverImpl nsResolver;
+    private final NamespaceCache nsCache;
     private final URIResolverImpl uriResolver;
 
     private final HostConfiguration hostConfig;
@@ -224,8 +228,8 @@
             hostConfig = new HostConfiguration();
             hostConfig.setHost(repositoryUri);
 
-            nsResolver = new NamespaceResolverImpl();
-            uriResolver = new URIResolverImpl(repositoryUri, this, nsResolver, domFactory);
+            nsCache = new NamespaceCache();
+            uriResolver = new URIResolverImpl(repositoryUri, this, nsCache, domFactory);
 
         } catch (URIException e) {
             throw new RepositoryException(e);
@@ -307,7 +311,8 @@
     private String getItemUri(NodeId parentId, QName childName, SessionInfo sessionInfo)
throws RepositoryException {
         String parentUri = uriResolver.getItemUri(parentId, sessionInfo.getWorkspaceName(),
sessionInfo);
         try {
-            return parentUri + "/" + Text.escape(NameFormat.format(childName, nsResolver));
+            NamespaceResolver resolver = new NamespaceResolverImpl(sessionInfo);
+            return parentUri + "/" + Text.escape(NameFormat.format(childName, resolver));
         } catch (NoPrefixDeclaredException e) {
             throw new RepositoryException(e);
         }
@@ -334,7 +339,7 @@
         }
     }
 
-    QName getQName(DavPropertySet propSet) throws RepositoryException {
+    QName getQName(DavPropertySet propSet, NamespaceResolver nsResolver) throws RepositoryException
{
         DavProperty nameProp = propSet.get(ItemResourceConstants.JCR_NAME);
         if (nameProp != null && nameProp.getValue() != null) {
             // not root node. Note that 'unespacing' is not required since
@@ -385,7 +390,6 @@
         }
     }
 
-
     //--------------------------------------------------< RepositoryService >---
     /**
      * @see RepositoryService#getIdFactory()
@@ -645,6 +649,8 @@
                 throw new RepositoryException("Internal error: requested node definition
and got property definition.");
             }
 
+            NamespaceResolver resolver = new NamespaceResolverImpl(sessionInfo);
+
             // build the definition
             QItemDefinition definition = null;
             if (propertySet.contains(ItemResourceConstants.JCR_DEFINITION)) {
@@ -653,9 +659,9 @@
                 if (value != null && value instanceof Element) {
                     Element idfElem = (Element) value;
                     if (itemId.denotesNode()) {
-                        definition = new QNodeDefinitionImpl(null, idfElem, nsResolver);
+                        definition = new QNodeDefinitionImpl(null, idfElem, resolver);
                     } else {
-                        definition = new QPropertyDefinitionImpl(null, idfElem, nsResolver,
getQValueFactory());
+                        definition = new QPropertyDefinitionImpl(null, idfElem, resolver,
getQValueFactory());
                     }
                 }
             }
@@ -743,7 +749,8 @@
             NodeId parentId = getParentId(propSet, sessionInfo);
             NodeId id = uriResolver.buildNodeId(parentId, nodeResponse, sessionInfo.getWorkspaceName());
 
-            NodeInfoImpl nInfo = new NodeInfoImpl(id, parentId, propSet, nsResolver);
+            NamespaceResolver resolver = new NamespaceResolverImpl(sessionInfo);
+            NodeInfoImpl nInfo = new NodeInfoImpl(id, parentId, propSet, resolver);
             if (propSet.contains(ItemResourceConstants.JCR_REFERENCES)) {
                 HrefProperty refProp = new HrefProperty(propSet.get(ItemResourceConstants.JCR_REFERENCES));
                 Iterator hrefIter = refProp.getHrefs().iterator();
@@ -807,6 +814,8 @@
                 return Collections.EMPTY_LIST.iterator();
             }
 
+            NamespaceResolver resolver = new NamespaceResolverImpl(sessionInfo);
+
             List childEntries = new ArrayList();
             for (int i = 0; i < responses.length; i++) {
                 if (!isSameResource(uri, responses[i])) {
@@ -815,7 +824,7 @@
                     if (childProps.contains(DavPropertyName.RESOURCETYPE) &&
                         childProps.get(DavPropertyName.RESOURCETYPE).getValue() != null)
{
 
-                        QName qName = getQName(childProps);
+                        QName qName = getQName(childProps, resolver);
                         int index = getIndex(childProps);
                         String uuid = getUniqueID(childProps);
 
@@ -866,7 +875,9 @@
             NodeId parentId = getParentId(propSet, sessionInfo);
             PropertyId id = uriResolver.buildPropertyId(parentId, responses[0], sessionInfo.getWorkspaceName());
 
-            PropertyInfo pInfo = new PropertyInfoImpl(id, parentId, propSet, nsResolver,
valueFactory, getQValueFactory());
+            NamespaceResolver resolver = new NamespaceResolverImpl(sessionInfo);
+            PropertyInfo pInfo = new PropertyInfoImpl(id, parentId, propSet,
+                    resolver, valueFactory, getQValueFactory());
             return pInfo;
         } catch (IOException e) {
             throw new RepositoryException(e);
@@ -1194,9 +1205,10 @@
      * @see RepositoryService#addVersionLabel(SessionInfo,NodeId,NodeId,QName,boolean)
      */
     public void addVersionLabel(SessionInfo sessionInfo, NodeId versionHistoryId, NodeId
versionId, QName label, boolean moveLabel) throws VersionException, RepositoryException {
-         try {
+        try {
             String uri = getItemUri(versionId, sessionInfo);
-            LabelMethod method = new LabelMethod(uri, NameFormat.format(label, nsResolver),
(moveLabel) ? LabelInfo.TYPE_SET : LabelInfo.TYPE_ADD);
+            String strLabel = NameFormat.format(label, new NamespaceResolverImpl(sessionInfo));
+            LabelMethod method = new LabelMethod(uri, strLabel, (moveLabel) ? LabelInfo.TYPE_SET
: LabelInfo.TYPE_ADD);
             execute(method, sessionInfo);
         } catch (IOException e) {
             throw new RepositoryException(e);
@@ -1211,7 +1223,8 @@
     public void removeVersionLabel(SessionInfo sessionInfo, NodeId versionHistoryId, NodeId
versionId, QName label) throws VersionException, RepositoryException {
         try {
             String uri = getItemUri(versionId, sessionInfo);
-            LabelMethod method = new LabelMethod(uri, NameFormat.format(label, nsResolver),
LabelInfo.TYPE_REMOVE);
+            String strLabel = NameFormat.format(label, new NamespaceResolverImpl(sessionInfo));
+            LabelMethod method = new LabelMethod(uri, strLabel, LabelInfo.TYPE_REMOVE);
             execute(method, sessionInfo);
         } catch (IOException e) {
             throw new RepositoryException(e);
@@ -1263,8 +1276,9 @@
             method.checkSuccess();
 
             MultiStatus ms = method.getResponseBodyAsMultiStatus();
+            NamespaceResolver resolver = new NamespaceResolverImpl(sessionInfo);
             return new QueryInfoImpl(ms, sessionInfo, uriResolver,
-                nsResolver, valueFactory, getQValueFactory());
+                resolver, valueFactory, getQValueFactory());
         } catch (IOException e) {
             throw new RepositoryException(e);
         } catch (DavException e) {
@@ -1506,7 +1520,7 @@
                     if (uri != null) {
                         namespaces.put(prefix, uri);
                         // TODO: not correct since nsRegistry is retrieved from each session
-                        nsResolver.add(prefix, uri);
+                        nsCache.add(prefix, uri);
                     } else {
                         log.error("Invalid prefix / uri pair: " + prefix + " -> " + uri);
                     }
@@ -1525,32 +1539,66 @@
     }
 
     /**
+     * @see RepositoryService#getNamespaceURI(SessionInfo, String)
+     */
+    public String getNamespaceURI(SessionInfo sessionInfo, String prefix)
+            throws NamespaceException, RepositoryException {
+        try {
+            return nsCache.getURI(prefix);
+        } catch (NamespaceException e) {
+            // refresh namespaces and try again
+            getRegisteredNamespaces(sessionInfo);
+            return nsCache.getURI(prefix);
+        }
+    }
+
+    /**
+     * @see RepositoryService#getNamespacePrefix(SessionInfo, String)
+     */
+    public String getNamespacePrefix(SessionInfo sessionInfo, String uri)
+            throws NamespaceException, RepositoryException {
+        try {
+            return nsCache.getPrefix(uri);
+        } catch (NamespaceException e) {
+            // refresh namespaces and try again
+            getRegisteredNamespaces(sessionInfo);
+            return nsCache.getPrefix(uri);
+        }
+    }
+
+    /**
      * @see RepositoryService#registerNamespace(SessionInfo, String, String)
      */
     public void registerNamespace(SessionInfo sessionInfo, String prefix, String uri) throws
NamespaceException, UnsupportedRepositoryOperationException, AccessDeniedException, RepositoryException
{
-        Map namespaces = new HashMap(nsResolver.getNamespaces());
+        // make sure we have them all
+        getRegisteredNamespaces(sessionInfo);
+
+        Map namespaces = new HashMap(nsCache.getNamespaces());
         // add new pair that needs to be registered.
         namespaces.put(prefix, uri);
 
         internalSetNamespaces(sessionInfo, namespaces);
         // adjust internal mappings:
         // TODO: not correct since nsRegistry is retrieved from each session
-        nsResolver.add(prefix, uri);
+        nsCache.add(prefix, uri);
     }
 
     /**
      * @see RepositoryService#unregisterNamespace(SessionInfo, String)
      */
     public void unregisterNamespace(SessionInfo sessionInfo, String uri) throws NamespaceException,
UnsupportedRepositoryOperationException, AccessDeniedException, RepositoryException {
-        String prefix = nsResolver.getPrefix(uri);
-        Map namespaces = new HashMap(nsResolver.getNamespaces());
+        // make sure we have them all
+        getRegisteredNamespaces(sessionInfo);
+
+        String prefix = nsCache.getPrefix(uri);
+        Map namespaces = new HashMap(nsCache.getNamespaces());
         // remove pair that needs to be unregistered
         namespaces.remove(prefix);
 
         internalSetNamespaces(sessionInfo, namespaces);
         // adjust internal mappings:
         // TODO: not correct since nsRegistry is retrieved from each session
-        nsResolver.remove(prefix, uri);
+        nsCache.remove(prefix, uri);
     }
 
     /**
@@ -1603,8 +1651,9 @@
             Document reportDoc = method.getResponseBodyAsDocument();
             ElementIterator it = DomUtil.getChildren(reportDoc.getDocumentElement(), NodeTypeConstants.NODETYPE_ELEMENT,
null);
             List ntDefs = new ArrayList();
+            NamespaceResolver resolver = new NamespaceResolverImpl(sessionInfo);
             while (it.hasNext()) {
-                ntDefs.add(new QNodeTypeDefinitionImpl(it.nextElement(), nsResolver, getQValueFactory()));
+                ntDefs.add(new QNodeTypeDefinitionImpl(it.nextElement(), resolver, getQValueFactory()));
             }
             // refresh node type definitions map
             synchronized (nodeTypeDefinitions) {
@@ -1642,6 +1691,7 @@
         private final SessionInfo sessionInfo;
         private final ItemId targetId;
         private final List methods = new ArrayList();
+        private final NamespaceResolver nsResolver;
 
         private String batchId;
 
@@ -1650,6 +1700,7 @@
         private BatchImpl(ItemId targetId, SessionInfo sessionInfo) {
             this.targetId = targetId;
             this.sessionInfo = sessionInfo;
+            this.nsResolver = new NamespaceResolverImpl(sessionInfo);
         }
 
         private HttpClient start() throws RepositoryException {
@@ -1946,6 +1997,103 @@
             MoveMethod method = new MoveMethod(uri, destUri, true);
 
             methods.add(method);
+        }
+    }
+
+    //----------------------------------------------< NamespaceResolverImpl >---
+
+    /**
+     * Implements a namespace resolver based on a session info.
+     */
+    private class NamespaceResolverImpl implements NamespaceResolver {
+
+        private final SessionInfo sessionInfo;
+
+        /**
+         * Creates a new namespace resolver using the given session info.
+         *
+         * @param sessionInfo the session info to contact the repository.
+         */
+        NamespaceResolverImpl(SessionInfo sessionInfo) {
+            this.sessionInfo = sessionInfo;
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public String getURI(String prefix) throws NamespaceException {
+            try {
+                return getNamespaceURI(sessionInfo, prefix);
+            } catch (RepositoryException e) {
+                String msg = "Error retrieving namespace uri";
+                throw new NamespaceException(msg, e);
+            }
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public String getPrefix(String uri) throws NamespaceException {
+            try {
+                return getNamespacePrefix(sessionInfo, uri);
+            } catch (RepositoryException e) {
+                String msg = "Error retrieving namespace prefix";
+                throw new NamespaceException(msg, e);
+            }
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public QName getQName(String jcrName) throws IllegalNameException, UnknownPrefixException
{
+            return NameFormat.parse(jcrName, this);
+        }
+
+        /**
+         * @inheritDoc
+         */
+        public String getJCRName(QName qName) throws NoPrefixDeclaredException {
+            return NameFormat.format(qName, this);
+        }
+    }
+
+    private static class NamespaceCache extends AbstractNamespaceResolver {
+
+        private final HashMap prefixToURI = new HashMap();
+        private final HashMap uriToPrefix = new HashMap();
+
+        public Map getNamespaces() {
+            return new HashMap(prefixToURI);
+        }
+
+        public void add(String prefix, String uri) {
+            prefixToURI.put(prefix, uri);
+            uriToPrefix.put(uri, prefix);
+        }
+
+        public void remove(String prefix, String uri) {
+            prefixToURI.remove(prefix);
+            uriToPrefix.remove(uri);
+        }
+
+        //----------------------------------------------< NamespaceResolver >---
+
+        public String getURI(String prefix) throws NamespaceException {
+            String uri = (String) prefixToURI.get(prefix);
+            if (uri != null) {
+                return uri;
+            } else {
+                throw new NamespaceException(prefix + ": is not a registered namespace prefix.");
+            }
+        }
+
+        public String getPrefix(String uri) throws NamespaceException {
+            String prefix = (String) uriToPrefix.get(uri);
+            if (prefix != null) {
+                return prefix;
+            } else {
+                throw new NamespaceException(uri + ": is not a registered namespace uri.");
+            }
         }
     }
 }

Modified: jackrabbit/trunk/contrib/spi/spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/URIResolverImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/URIResolverImpl.java?view=diff&rev=498558&r1=498557&r2=498558
==============================================================================
--- jackrabbit/trunk/contrib/spi/spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/URIResolverImpl.java
(original)
+++ jackrabbit/trunk/contrib/spi/spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/URIResolverImpl.java
Mon Jan 22 00:41:17 2007
@@ -187,7 +187,7 @@
         if (uniqueID != null) {
             nodeId = service.getIdFactory().createNodeId(uniqueID);
         } else {
-            QName qName = service.getQName(propSet);
+            QName qName = service.getQName(propSet, nsResolver);
             if (qName == QName.ROOT) {
                 nodeId = service.getIdFactory().createNodeId((String) null, Path.ROOT);
             } else {



Mime
View raw message