jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r314910 - in /incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit: core/CachingNamespaceResolver.java core/LocalNamespaceMappings.java core/NamespaceRegistryImpl.java name/CachingNamespaceResolver.java
Date Wed, 12 Oct 2005 14:07:19 GMT
Author: mreutegg
Date: Wed Oct 12 07:07:05 2005
New Revision: 314910

URL: http://svn.apache.org/viewcvs?rev=314910&view=rev
Log:
JCR-252: new CachingNamespaceResolver introduces dependency from commons-jackrabbit to commons-collections
- moved CachingNamespaceResolver to o.a.j.core package
- removed caching support from LocalNamespaceMappings

Added:
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/CachingNamespaceResolver.java
  (with props)
Removed:
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/name/CachingNamespaceResolver.java
Modified:
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/LocalNamespaceMappings.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/NamespaceRegistryImpl.java

Added: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/CachingNamespaceResolver.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/CachingNamespaceResolver.java?rev=314910&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/CachingNamespaceResolver.java
(added)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/CachingNamespaceResolver.java
Wed Oct 12 07:07:05 2005
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed 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.core;
+
+import org.apache.jackrabbit.name.QName;
+import org.apache.jackrabbit.name.IllegalNameException;
+import org.apache.jackrabbit.name.UnknownPrefixException;
+import org.apache.jackrabbit.name.NoPrefixDeclaredException;
+import org.apache.jackrabbit.name.NamespaceResolver;
+import org.apache.jackrabbit.name.NamespaceListener;
+import org.apache.jackrabbit.name.AbstractNamespaceResolver;
+import org.apache.commons.collections.map.LRUMap;
+
+import javax.jcr.NamespaceException;
+import java.util.Map;
+
+/**
+ * Implements a {@link NamespaceResolver} that caches QName to resolved jcr names
+ * and vice versa. The cache is invalidated when a namespace uri to prefix
+ * mapping is changed.
+ */
+class CachingNamespaceResolver
+        implements NamespaceResolver, NamespaceListener {
+
+    /**
+     * The base namespace resolver.
+     */
+    private final AbstractNamespaceResolver base;
+
+    /**
+     * Maps QName instances to resolved jcr name Strings.
+     */
+    private final Map qnameToJCRName;
+
+    /**
+     * Maps resolved jcr name Strings to QName instances.
+     */
+    private final Map jcrNameToQName;
+
+    /**
+     * Creates a new <code>CachingNamespaceResolver</code>.
+     *
+     * @param base      a base namespace resolver with support for listener
+     *                  registration.
+     * @param cacheSize number of mappings this resolver may cache.
+     */
+    public CachingNamespaceResolver(AbstractNamespaceResolver base, int cacheSize) {
+        this.base = base;
+        qnameToJCRName = new LRUMap(cacheSize);
+        jcrNameToQName = new LRUMap(cacheSize);
+        this.base.addListener(this);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public String getURI(String prefix) throws NamespaceException {
+        return base.getURI(prefix);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public String getPrefix(String uri) throws NamespaceException {
+        return base.getPrefix(uri);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public synchronized QName getQName(String name)
+            throws IllegalNameException, UnknownPrefixException {
+        QName qName = (QName) jcrNameToQName.get(name);
+        if (qName == null) {
+            qName = QName.fromJCRName(name, this);
+            jcrNameToQName.put(name, qName);
+        }
+        return qName;
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public synchronized String getJCRName(QName name)
+            throws NoPrefixDeclaredException {
+        String jcrName = (String) qnameToJCRName.get(name);
+        if (jcrName == null) {
+            jcrName = name.toJCRName(this);
+            qnameToJCRName.put(name, jcrName);
+        }
+        return jcrName;
+    }
+
+    /**
+     * Disposes this <code>CachingNamespaceResolver</code>.
+     */
+    public void dispose() {
+        base.removeListener(this);
+    }
+
+    //----------------------------------------------------< NamespaceListener >
+
+    /**
+     * @inheritDoc
+     * Invalidates all cached mappings.
+     */
+    public synchronized void prefixRemapped(String prefix, String uri) {
+        qnameToJCRName.clear();
+        jcrNameToQName.clear();
+    }
+}

Propchange: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/CachingNamespaceResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/LocalNamespaceMappings.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/LocalNamespaceMappings.java?rev=314910&r1=314909&r2=314910&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/LocalNamespaceMappings.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/LocalNamespaceMappings.java
Wed Oct 12 07:07:05 2005
@@ -21,7 +21,8 @@
 import org.apache.jackrabbit.name.IllegalNameException;
 import org.apache.jackrabbit.name.UnknownPrefixException;
 import org.apache.jackrabbit.name.NoPrefixDeclaredException;
-import org.apache.jackrabbit.name.CachingNamespaceResolver;
+import org.apache.jackrabbit.name.AbstractNamespaceResolver;
+import org.apache.jackrabbit.name.NamespaceListener;
 import org.apache.xerces.util.XMLChar;
 
 import javax.jcr.NamespaceException;
@@ -44,7 +45,8 @@
  * looking up the local namespace mapping and then backing to the
  * underlying namespace registry.
  */
-class LocalNamespaceMappings extends CachingNamespaceResolver {
+class LocalNamespaceMappings extends AbstractNamespaceResolver
+        implements NamespaceListener{
 
     /** The underlying global and persistent namespace registry. */
     private final NamespaceRegistryImpl nsReg;
@@ -65,8 +67,8 @@
      * @param nsReg namespace registry
      */
     LocalNamespaceMappings(NamespaceRegistryImpl nsReg) {
-        super(nsReg, 100);
         this.nsReg = nsReg;
+        this.nsReg.addListener(this);
     }
 
     /**
@@ -156,9 +158,6 @@
             uriToPrefix.put(uri, prefix);
             hiddenPrefixes.add(globalPrefix);
         }
-
-        // invalidate cache
-        super.prefixRemapped(prefix, uri);
     }
 
     /**
@@ -187,6 +186,13 @@
         return (String[]) prefixes.toArray(new String[prefixes.size()]);
     }
 
+    /**
+     * Disposes this <code>LocalNamespaceMappings</code>.
+     */
+    void dispose() {
+        nsReg.removeListener(this);
+    }
+
     //----------------------------------------------------< NamespaceResolver >
     /**
      * {@inheritDoc}
@@ -282,6 +288,5 @@
      */
     public void prefixRemapped(String prefix, String uri) {
         // todo check overlayed mappings and adjust prefixes if necessary
-        super.prefixRemapped(prefix, uri);
     }
 }

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/NamespaceRegistryImpl.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/NamespaceRegistryImpl.java?rev=314910&r1=314909&r2=314910&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/NamespaceRegistryImpl.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/NamespaceRegistryImpl.java
Wed Oct 12 07:07:05 2005
@@ -23,7 +23,6 @@
 import org.apache.jackrabbit.name.IllegalNameException;
 import org.apache.jackrabbit.name.UnknownPrefixException;
 import org.apache.jackrabbit.name.NoPrefixDeclaredException;
-import org.apache.jackrabbit.name.CachingNamespaceResolver;
 import org.apache.log4j.Logger;
 import org.apache.xerces.util.XMLChar;
 



Mime
View raw message