You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by an...@apache.org on 2008/03/04 02:10:49 UTC

svn commit: r633346 - /lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UuidToUrlTransformer.java

Author: andreas
Date: Mon Mar  3 17:10:46 2008
New Revision: 633346

URL: http://svn.apache.org/viewvc?rev=633346&view=rev
Log:
Cache source extensions in UuidToUrlTransformer because meta data acquisition is very expensive.

Modified:
    lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UuidToUrlTransformer.java

Modified: lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UuidToUrlTransformer.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UuidToUrlTransformer.java?rev=633346&r1=633345&r2=633346&view=diff
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UuidToUrlTransformer.java (original)
+++ lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UuidToUrlTransformer.java Mon Mar  3 17:10:46 2008
@@ -30,6 +30,7 @@
 import org.apache.cocoon.environment.ObjectModelHelper;
 import org.apache.cocoon.environment.Request;
 import org.apache.cocoon.environment.SourceResolver;
+import org.apache.commons.collections.map.LRUMap;
 import org.apache.excalibur.source.Source;
 import org.apache.excalibur.source.SourceValidity;
 import org.apache.lenya.ac.AccessControlException;
@@ -37,6 +38,8 @@
 import org.apache.lenya.cms.linking.LinkResolver;
 import org.apache.lenya.cms.linking.LinkRewriter;
 import org.apache.lenya.cms.linking.LinkTarget;
+import org.apache.lenya.cms.metadata.MetaData;
+import org.apache.lenya.cms.metadata.MetaDataException;
 import org.apache.lenya.cms.publication.Area;
 import org.apache.lenya.cms.publication.Document;
 import org.apache.lenya.cms.publication.DocumentFactory;
@@ -95,7 +98,13 @@
     private Document currentDoc;
     private String cacheKey;
     private SourceValidity validity;
-
+    
+    private static LRUMap extensionCache;
+    
+    static {
+        extensionCache = new LRUMap(10000);
+    }
+    
     /**
      * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver,
      *      java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
@@ -228,8 +237,7 @@
      * @return The required extension or, if it is null, the document's default extension.
      */
     protected String getExtension(Document targetDocument, String requiredExtension) {
-        String extension = requiredExtension != null ? requiredExtension : targetDocument
-                .getExtension();
+        String extension = requiredExtension != null ? requiredExtension : getExtension(targetDocument);
         if (extension.length() > 0) {
             extension = "." + extension;
         }
@@ -237,6 +245,34 @@
     }
 
     /**
+     * Get the extension of a document. The lookup of the extension requires loading the meta
+     * data and is therefore very expensive, so we cache the extension.
+     * @param targetDocument The target document.
+     * @return A string.
+     */
+    protected String getExtension(Document targetDocument) {
+        String key = targetDocument.getRepositoryNode().getSourceURI();
+        Extension ex = null;
+        try {
+            MetaData meta = targetDocument.getMetaData("http://apache.org/lenya/metadata/document/1.0");
+            if (extensionCache.containsKey(key)) {
+                ex = (Extension) extensionCache.get(key);
+                if (meta.getLastModified() > ex.lastModified) {
+                    ex = null;
+                }
+            }
+            if (ex == null) {
+                ex = new Extension(targetDocument.getExtension(), meta.getLastModified());
+                extensionCache.put(key, ex);
+            }
+        } catch (MetaDataException e) {
+            throw new RuntimeException(e);
+        }
+        
+        return ex.extension;
+    }
+
+    /**
      * Marks a <code>&lt;a/&gt;</code> element as broken and removes href attribute.
      * 
      * @param newAttrs The new attributes.
@@ -343,6 +379,15 @@
             throw new IllegalStateException("setup() was not called.");
         }
         return this.validity;
+    }
+    
+    protected static class Extension {
+        public Extension(String extension, long lastModified) {
+            this.extension = extension;
+            this.lastModified = lastModified;
+        }
+        protected String extension;
+        protected long lastModified;
     }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@lenya.apache.org
For additional commands, e-mail: commits-help@lenya.apache.org