svn commit: r1347606 - in /cocoon/cocoon3/trunk: cocoon-sax/src/main/java/org/apache/cocoon/sax/component/ cocoon-sax/src/main/java/org/apache/cocoon/sax/util/ cocoon-sitemap/src/main/java/org/apache/cocoon/sitemap/component/ cocoon-stringtemplate/src/...

[email protected]
Newsgroups gmane.text.xml.cocoon.cvs
Message-ID <[email protected]>
Author: ilgrosso
Date: Thu Jun  7 13:29:26 2012
New Revision: 1347606

URL: http://svn.apache.org/viewvc?rev=1347606&view=rev
Log:
Making local LRU cache use URLManipulationUtils.getLastModified()

Modified:
    cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/SchemaProcessorTransformer.java
    cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/XMLGenerator.java
    cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/XSLTTransformer.java
    cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/ValidityValue.java
    cocoon/cocoon3/trunk/cocoon-sitemap/src/main/java/org/apache/cocoon/sitemap/component/FileReaderComponent.java
    cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateGenerator.java

Modified: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/SchemaProcessorTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/SchemaProcessorTransformer.java?rev=1347606&r1=1347605&r2=1347606&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/SchemaProcessorTransformer.java (original)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/SchemaProcessorTransformer.java Thu Jun  7 13:29:26 2012
@@ -18,8 +18,6 @@
  */
 package org.apache.cocoon.sax.component;
 
-import java.io.File;
-import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.Map;
 import javax.xml.XMLConstants;
@@ -32,6 +30,7 @@ import org.apache.cocoon.sax.AbstractSAX
 import org.apache.cocoon.sax.SAXConsumer;
 import org.apache.cocoon.sax.util.InMemoryLRUResourceCache;
 import org.apache.cocoon.sax.util.SAXConsumerAdapter;
+import org.apache.cocoon.sax.util.URLManipulationUtils;
 import org.apache.cocoon.sax.util.ValidityValue;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -46,7 +45,7 @@ public final class SchemaProcessorTransf
 
     private static final String SOURCE = "source";
 
-    private static final InMemoryLRUResourceCache<Schema> SCHEMA_LRU_CACHE = new InMemoryLRUResourceCache<Schema>();
+    private static final InMemoryLRUResourceCache<Schema> SCHEMA_CACHE = new InMemoryLRUResourceCache<Schema>();
 
     private static final SchemaFactory SCHEMA_FACTORY = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 
@@ -91,35 +90,28 @@ public final class SchemaProcessorTransf
 
         this.source = source;
 
-        final File schemaFile;
-        try {
-            schemaFile = "file".equals(this.source.getProtocol())
-                    ? new File(this.source.toURI()) : null;
-        } catch (URISyntaxException e) {
-            throw new IllegalArgumentException(
-                    "Invalid source specified: " + source, e);
-        }
+        Long lastModified = URLManipulationUtils.getLastModified(this.source);
+
+        if (SCHEMA_CACHE.containsKey(source.toExternalForm())) {
+            ValidityValue<Schema> cacheEntry = SCHEMA_CACHE.get(source.toExternalForm());
+            if (lastModified == null || cacheEntry.getLastModified() >= lastModified) {
+                LOG.debug("{} local cache hit: {}", getClass().getSimpleName(), this.source.toExternalForm());
 
-        this.schema = null;
-        if (SCHEMA_LRU_CACHE.containsKey(source.toExternalForm())) {
-            ValidityValue<Schema> cacheEntry = SCHEMA_LRU_CACHE.get(source.toExternalForm());
-            if (schemaFile == null || cacheEntry.getLastModified() >= schemaFile.lastModified()) {
                 this.schema = cacheEntry.getValue();
             }
         }
         if (this.schema == null) {
+            LOG.debug("{} local cache miss: {}", getClass().getSimpleName(), this.source.toExternalForm());
+
             try {
-                this.schema = schemaFile != null
-                        ? SCHEMA_FACTORY.newSchema(schemaFile)
-                        : SCHEMA_FACTORY.newSchema(source);
-
-                ValidityValue<Schema> cacheEntry = (schemaFile != null)
-                        ? new ValidityValue<Schema>(this.schema, schemaFile.lastModified())
-                        : new ValidityValue<Schema>(this.schema);
-                SCHEMA_LRU_CACHE.put(source.toExternalForm(), cacheEntry);
+                this.schema = SCHEMA_FACTORY.newSchema(source);
+
+                LOG.debug("{} local cache put: {}", getClass().getSimpleName(), this.source.toExternalForm());
+
+                ValidityValue<Schema> cacheEntry = new ValidityValue<Schema>(this.schema, lastModified);
+                SCHEMA_CACHE.put(this.source.toExternalForm(), cacheEntry);
             } catch (SAXException e) {
-                throw new SetupException(
-                        "Could not initialize xschema source", e);
+                throw new SetupException("Could not initialize xschema source", e);
             }
         }
     }

Modified: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/XMLGenerator.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/XMLGenerator.java?rev=1347606&r1=1347605&r2=1347606&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/XMLGenerator.java (original)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/XMLGenerator.java Thu Jun  7 13:29:26 2012
@@ -397,13 +397,12 @@ public class XMLGenerator extends Abstra
                 throw new ProcessingException(this.getClass().getSimpleName() + " has no source.");
             }
 
-            LOG.debug("Using the URL {} to produce SAX events.", this.source);
+            LOG.debug("Using the URL {} to produce SAX events.", this.source.toExternalForm());
 
             try {
                 XMLUtils.toSax(this.source.openConnection(), XMLGenerator.this.getSAXConsumer());
             } catch (IOException e) {
-                throw new ProcessingException("Can't open connection to "
-                        + this.source, e);
+                throw new ProcessingException("Can't open connection to " + this.source.toExternalForm(), e);
             }
         }
 
@@ -413,8 +412,7 @@ public class XMLGenerator extends Abstra
 
         @Override
         public String toString() {
-            return StringRepresentation.buildString(this,
-                    "source=" + this.source);
+            return StringRepresentation.buildString(this, "source=" + this.source);
         }
     }
 }

Modified: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/XSLTTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/XSLTTransformer.java?rev=1347606&r1=1347605&r2=1347606&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/XSLTTransformer.java (original)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/XSLTTransformer.java Thu Jun  7 13:29:26 2012
@@ -16,14 +16,11 @@
  */
 package org.apache.cocoon.sax.component;
 
-import java.io.File;
-import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.regex.Pattern;
-import javax.xml.transform.Source;
 import javax.xml.transform.Templates;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerConfigurationException;
@@ -130,29 +127,23 @@ public class XSLTTransformer extends Abs
 
         this.source = source;
 
-        final File xsltFile;
-        try {
-            xsltFile = "file".equals(this.source.getProtocol())
-                    ? new File(this.source.toURI()) : null;
-        } catch (URISyntaxException e) {
-            throw new IllegalArgumentException("Invalid source specified: " + source, e);
-        }
+        Long lastModified = URLManipulationUtils.getLastModified(this.source);
 
         this.templates = null;
         // check the XSLT is in the cache first
         if (XSLT_CACHE.containsKey(this.source.toExternalForm())) {
             // get the XSLT directly from the cache
             ValidityValue<Templates> cacheEntry = XSLT_CACHE.get(this.source.toExternalForm());
-            if (xsltFile == null || cacheEntry.getLastModified() >= xsltFile.lastModified()) {
+            if (lastModified == null || cacheEntry.getLastModified() >= lastModified) {
+                LOG.debug("{} local cache hit: {}", getClass().getSimpleName(), this.source.toExternalForm());
+
                 this.templates = cacheEntry.getValue();
             }
         }
         if (this.templates == null) {
-            // XSLT has to be parsed
-            Source urlSource = xsltFile != null
-                    ? new StreamSource(xsltFile)
-                    : new StreamSource(this.source.toExternalForm());
+            LOG.debug("{} local cache miss: {}", getClass().getSimpleName(), this.source.toExternalForm());
 
+            // XSLT has to be parsed
             SAXTransformerFactory transformerFactory;
             if (attributes != null && !attributes.isEmpty()) {
                 transformerFactory = createNewSAXTransformerFactory();
@@ -164,11 +155,12 @@ public class XSLTTransformer extends Abs
             }
 
             try {
-                this.templates = transformerFactory.newTemplates(urlSource);
+                this.templates = transformerFactory.newTemplates(new StreamSource(this.source.toExternalForm()));
+
                 // store the XSLT into the cache for future reuse
-                ValidityValue<Templates> cacheEntry = (xsltFile != null)
-                        ? new ValidityValue<Templates>(this.templates, xsltFile.lastModified())
-                        : new ValidityValue<Templates>(this.templates);
+                LOG.debug("{} local cache put: {}", getClass().getSimpleName(), this.source.toExternalForm());
+
+                ValidityValue<Templates> cacheEntry = new ValidityValue<Templates>(this.templates, lastModified);
                 XSLT_CACHE.put(this.source.toExternalForm(), cacheEntry);
             } catch (TransformerConfigurationException e) {
                 throw new SetupException("Impossible to read XSLT from '" + this.source.toExternalForm()
@@ -211,11 +203,8 @@ public class XSLTTransformer extends Abs
                 this.loadXSLT(this.source, null);
             }
         } else {
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Impossible to load XSLT parameters from '"
-                        + this.source + "' source, make sure it is NOT null "
-                        + "and is a valid URL");
-            }
+            LOG.debug("Impossible to load XSLT parameters from '{}' source, "
+                    + "make sure it is NOT null and is a valid URL", this.source.toExternalForm());
         }
 
         this.setParameters(configuration);
@@ -229,9 +218,8 @@ public class XSLTTransformer extends Abs
         TransformerHandler transformerHandler;
         try {
             transformerHandler = TRAX_FACTORY.newTransformerHandler(this.templates);
-        } catch (Exception ex) {
-            throw new SetupException(
-                    "Could not initialize transformer handler.", ex);
+        } catch (Exception e) {
+            throw new SetupException("Could not initialize transformer handler.", e);
         }
 
         if (this.parameters != null) {
@@ -253,8 +241,7 @@ public class XSLTTransformer extends Abs
         result.setLexicalHandler(consumer);
         transformerHandler.setResult(result);
 
-        TraxErrorListener traxErrorListener = new TraxErrorListener(LOG,
-                this.source.toExternalForm());
+        TraxErrorListener traxErrorListener = new TraxErrorListener(LOG, this.source.toExternalForm());
         transformerHandler.getTransformer().setErrorListener(traxErrorListener);
 
         SAXConsumerAdapter saxConsumerAdapter = new SAXConsumerAdapter();

Modified: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/ValidityValue.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/ValidityValue.java?rev=1347606&r1=1347605&r2=1347606&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/ValidityValue.java (original)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/util/ValidityValue.java Thu Jun  7 13:29:26 2012
@@ -19,11 +19,6 @@ public class ValidityValue<V> {
 
     final private long lastModified;
 
-    public ValidityValue(final V value) {
-        this.value = value;
-        this.lastModified = 0;
-    }
-
     public ValidityValue(final V value, long lastModified) {
         this.value = value;
         this.lastModified = lastModified;

Modified: cocoon/cocoon3/trunk/cocoon-sitemap/src/main/java/org/apache/cocoon/sitemap/component/FileReaderComponent.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sitemap/src/main/java/org/apache/cocoon/sitemap/component/FileReaderComponent.java?rev=1347606&r1=1347605&r2=1347606&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sitemap/src/main/java/org/apache/cocoon/sitemap/component/FileReaderComponent.java (original)
+++ cocoon/cocoon3/trunk/cocoon-sitemap/src/main/java/org/apache/cocoon/sitemap/component/FileReaderComponent.java Thu Jun  7 13:29:26 2012
@@ -28,16 +28,9 @@ import org.apache.cocoon.pipeline.cachin
 import org.apache.cocoon.pipeline.component.CachingPipelineComponent;
 import org.apache.cocoon.pipeline.util.URLConnectionUtils;
 import org.apache.cocoon.sax.util.URLManipulationUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 public class FileReaderComponent extends AbstractReader implements CachingPipelineComponent {
 
-    /**
-     * Logger.
-     */
-    private static final Logger LOG = LoggerFactory.getLogger(FileReaderComponent.class);
-
     public FileReaderComponent() {
         super();
     }
@@ -83,9 +76,7 @@ public class FileReaderComponent extends
             }
 
         } catch (IOException e) {
-            String message = "FileReader cannot read from '" + this.source + "'";
-            LOG.error(message, e);
-            throw new ProcessingException(message, e);
+            throw new ProcessingException("FileReader cannot read from '" + this.source + "'", e);
         } finally {
             URLConnectionUtils.closeQuietly(connection);
         }

Modified: cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateGenerator.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateGenerator.java?rev=1347606&r1=1347605&r2=1347606&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateGenerator.java (original)
+++ cocoon/cocoon3/trunk/cocoon-stringtemplate/src/main/java/org/apache/cocoon/stringtemplate/StringTemplateGenerator.java Thu Jun  7 13:29:26 2012
@@ -25,7 +25,6 @@ import java.net.URL;
 import java.util.HashMap;
 import java.util.Map;
 import org.apache.cocoon.pipeline.ProcessingException;
-import org.apache.cocoon.pipeline.SetupException;
 import org.apache.cocoon.pipeline.caching.CacheKey;
 import org.apache.cocoon.pipeline.caching.CompoundCacheKey;
 import org.apache.cocoon.pipeline.caching.ParameterCacheKey;
@@ -178,7 +177,6 @@ public class StringTemplateGenerator ext
      */
     @Override
     public void setConfiguration(final Map<String, ? extends Object> parameters) {
-
         this.url = (URL) parameters.get("source");
         setup((Map<String, Object>) parameters);
     }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.