You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2004/09/27 09:02:47 UTC

svn commit: rev 47276 - in cocoon/trunk: . src/java/org/apache/cocoon/components/pipeline src/java/org/apache/cocoon/components/pipeline/impl

Author: cziegeler
Date: Mon Sep 27 00:02:46 2004
New Revision: 47276

Modified:
   cocoon/trunk/src/java/org/apache/cocoon/components/pipeline/AbstractProcessingPipeline.java
   cocoon/trunk/src/java/org/apache/cocoon/components/pipeline/impl/AbstractCachingProcessingPipeline.java
   cocoon/trunk/src/java/org/apache/cocoon/components/pipeline/impl/CachingPointProcessingPipeline.java
   cocoon/trunk/src/java/org/apache/cocoon/components/pipeline/impl/CachingProcessingPipeline.java
   cocoon/trunk/status.xml
Log:
Cache the mime-type of readers and serializers.

Modified: cocoon/trunk/src/java/org/apache/cocoon/components/pipeline/AbstractProcessingPipeline.java
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/components/pipeline/AbstractProcessingPipeline.java	(original)
+++ cocoon/trunk/src/java/org/apache/cocoon/components/pipeline/AbstractProcessingPipeline.java	Mon Sep 27 00:02:46 2004
@@ -179,7 +179,9 @@
      * Informs pipeline we have come across a branch point
      * Default Behaviour is do nothing
      */
-    public void informBranchPoint() {}
+    public void informBranchPoint() {
+        // this can be overwritten in subclasses
+    }
 
     /**
      * Set the generator that will be used as the initial step in the pipeline.
@@ -377,28 +379,6 @@
                 );
             }
 
-            if (this.lastConsumer == null) {
-                // internal processing: text/xml
-                environment.setContentType("text/xml");
-            } else {
-                // Set the mime-type
-                // the behaviour has changed from 2.1.x to 2.2 according to bug #10277
-                if (serializerMimeType != null) {
-                    // there was a serializer defined in the sitemap
-                    environment.setContentType(serializerMimeType);
-                } else {
-                    // ask to the component itself
-                    String mimeType = this.serializer.getMimeType();
-                    if (mimeType != null) {
-                        environment.setContentType (mimeType);
-                    } else {
-                        // No mimeType available
-                        String message = "Unable to determine MIME type for " +
-                            environment.getURIPrefix() + "/" + environment.getURI();
-                        throw new ProcessingException(message);
-                    }
-                }
-            }
         } catch (SAXException e) {
             throw new ProcessingException(
                 "Could not setup pipeline.",
@@ -515,6 +495,8 @@
     protected boolean processXMLPipeline(Environment environment)
     throws ProcessingException {
 
+        this.setMimeTypeForSerializer(environment);
+        
         try {
             if (this.serializer != this.lastConsumer) {
                 // internal processing
@@ -567,6 +549,10 @@
         }
     }
 
+    /**
+     * Set the mime-type for a reader
+     * @param environment The current environment
+     */
     protected void setMimeTypeForReader(Environment environment) {
         // Set the mime-type
         // the behaviour has changed from 2.1.x to 2.2 according to bug #10277:
@@ -586,6 +572,36 @@
         }        
     }
     
+    /**
+     * Set the mime-type for a serializer
+     * @param environment The current environment
+     */
+    protected void setMimeTypeForSerializer(Environment environment) 
+    throws ProcessingException {
+        if (this.lastConsumer == null) {
+            // internal processing: text/xml
+            environment.setContentType("text/xml");
+        } else {
+            // Set the mime-type
+            // the behaviour has changed from 2.1.x to 2.2 according to bug #10277
+            if (serializerMimeType != null) {
+                // there was a serializer defined in the sitemap
+                environment.setContentType(serializerMimeType);
+            } else {
+                // ask to the component itself
+                String mimeType = this.serializer.getMimeType();
+                if (mimeType != null) {
+                    environment.setContentType (mimeType);
+                } else {
+                    // No mimeType available
+                    String message = "Unable to determine MIME type for " +
+                        environment.getURIPrefix() + "/" + environment.getURI();
+                    throw new ProcessingException(message);
+                }
+            }
+        }
+    }
+
     protected boolean checkIfModified(Environment environment,
                                         long lastModified)
     throws ProcessingException {

Modified: cocoon/trunk/src/java/org/apache/cocoon/components/pipeline/impl/AbstractCachingProcessingPipeline.java
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/components/pipeline/impl/AbstractCachingProcessingPipeline.java	(original)
+++ cocoon/trunk/src/java/org/apache/cocoon/components/pipeline/impl/AbstractCachingProcessingPipeline.java	Mon Sep 27 00:02:46 2004
@@ -62,8 +62,8 @@
     /** The role name of the reader */
     protected String readerRole;
 
-    /** The cached byte stream */
-    protected byte[]           cachedResponse;
+    /** The cached response */
+    protected CachedResponse   cachedResponse;
     /** The timestamp of the cached byte stream */
     protected long             cachedLastModified;
 
@@ -173,17 +173,24 @@
                 return true;
             }
 
+            // set mime-type
+            if ( this.cachedResponse.getContentType() != null ) {
+                environment.setContentType(this.cachedResponse.getContentType());
+            } else {
+                this.setMimeTypeForSerializer(environment);
+            }
             try {
-                final OutputStream outputStream =
-                            environment.getOutputStream(0);
-                if (this.cachedResponse.length > 0) {
-                    environment.setContentLength(this.cachedResponse.length);
-                    outputStream.write(this.cachedResponse);
+                final OutputStream outputStream = environment.getOutputStream(0);
+                final byte[] content = this.cachedResponse.getResponse();
+                if (content.length > 0) {
+                    environment.setContentLength(content.length);
+                    outputStream.write(content);
                 }
             } catch (Exception e) {
                 handleException(e);
             }
         } else {
+            this.setMimeTypeForSerializer(environment);
             if (getLogger().isDebugEnabled() && this.toCacheKey != null) {
                 getLogger().debug("processXMLPipeline: caching content for further" +
                                   " requests of '" + environment.getURI() +
@@ -439,7 +446,7 @@
                                 ", ignoring all other cache settings. This entry expires on "+
                                 new Date(responseExpires.longValue()));
                         }
-                        this.cachedResponse = response.getResponse();
+                        this.cachedResponse = response;
                         this.cachedLastModified = response.getLastModified();
                         return;
                     } else {
@@ -518,7 +525,7 @@
                         this.getLogger().debug("validatePipeline: using valid cached content for '" + environment.getURI() + "'.");
                     }
                     // we are valid, ok that's it
-                    this.cachedResponse = response.getResponse();
+                    this.cachedResponse = response;
                     this.cachedLastModified = response.getLastModified();
                     this.toCacheSourceValidities = fromCacheValidityObjects;
                 } else {

Modified: cocoon/trunk/src/java/org/apache/cocoon/components/pipeline/impl/CachingPointProcessingPipeline.java
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/components/pipeline/impl/CachingPointProcessingPipeline.java	(original)
+++ cocoon/trunk/src/java/org/apache/cocoon/components/pipeline/impl/CachingPointProcessingPipeline.java	Mon Sep 27 00:02:46 2004
@@ -41,7 +41,7 @@
  *
  * @since 2.1
  * @author <a href="mailto:Michael.Melhem@managesoft.com">Michael Melhem</a>
- * @version CVS $Id: CachingPointProcessingPipeline.java,v 1.8 2004/07/17 10:51:15 joerg Exp $
+ * @version CVS $Id$
  */
 public class CachingPointProcessingPipeline
     extends AbstractCachingProcessingPipeline {
@@ -50,7 +50,7 @@
     protected ArrayList xmlSerializerArray = new ArrayList();
     protected boolean nextIsCachePoint = false;
     protected String autoCachingPointSwitch;
-    protected  boolean autoCachingPoint = true;
+    protected boolean autoCachingPoint = true;
 
 
    /**
@@ -79,9 +79,9 @@
         }
     }
 
-   /**
-    * Set the generator.
-    */
+    /**
+     * Set the generator.
+     */
     public void setGenerator (String role, String source, Parameters param, Parameters hintParam) 
     throws ProcessingException {
         super.setGenerator(role, source, param, hintParam);
@@ -94,9 +94,7 @@
             if (this.getLogger().isDebugEnabled()) {
                 getLogger().debug("generator caching-point pipeline-hint is set to: " + pipelinehint);
             }
-        }
-        catch (Exception ex)
-        {
+        } catch (Exception ex) {
             if (this.getLogger().isWarnEnabled()) {
                 getLogger().warn("caching-point hint Exception, pipeline-hint ignored: " + ex);
             }
@@ -125,9 +123,7 @@
             if (this.getLogger().isDebugEnabled()) {
                 getLogger().debug("transformer caching-point pipeline-hint is set to: " + pipelinehint);
             }
-        }
-        catch (Exception ex)
-        {
+        } catch (Exception ex) {
             if (this.getLogger().isWarnEnabled()) {
                 getLogger().warn("caching-point hint Exception, pipeline-hint ignored: " + ex);
             }
@@ -155,16 +151,17 @@
      */
     public void informBranchPoint() {
 
-        if (this.generator == null)
-        return;
-
-    if (!this.autoCachingPoint)
-        return;
+        if (this.generator == null) {
+            return;
+        }
+        if (!this.autoCachingPoint) {
+            return;
+        }   
 
         this.nextIsCachePoint = true;
-         if (this.getLogger().isDebugEnabled()) {
-           this.getLogger().debug("Informed Pipeline of branch point");
-         }
+        if (this.getLogger().isDebugEnabled()) {
+            this.getLogger().debug("Informed Pipeline of branch point");
+        }
     }
 
     /**
@@ -180,6 +177,7 @@
                 }
                 CachedResponse response = new CachedResponse(this.toCacheSourceValidities,
                                           ((CachingOutputStream)os).getContent());
+                response.setContentType(environment.getContentType());
                 this.cache.store(this.toCacheKey.copy(),
                                  response);
                 //
@@ -260,137 +258,134 @@
      */
     protected void connectCachingPipeline(Environment   environment)
     throws ProcessingException {
-            try {
-                XMLSerializer localXMLSerializer = null;
-                XMLSerializer cachePointXMLSerializer = null;
-                if (!this.cacheCompleteResponse) {
-                    this.xmlSerializer = (XMLSerializer)this.manager.lookup( XMLSerializer.ROLE );
-                    localXMLSerializer = this.xmlSerializer;
-                }
-                if ( this.cachedResponse == null ) {
-
-                    XMLProducer prev = super.generator;
-                    XMLConsumer next;
-
-                    int cacheableTransformerCount = this.firstNotCacheableTransformerIndex;
-                    int currentTransformerIndex = 0; //start with the first transformer
-
-                    Iterator itt = this.transformers.iterator();
-                    while ( itt.hasNext() ) {
-                        next = (XMLConsumer) itt.next();
-
-                        // if we have cacheable transformers,
-                        // check the tranformers for cachepoints
-                        if (cacheableTransformerCount > 0) {
-                            if ( (this.isCachePoint.get(currentTransformerIndex) != null)  &&
-                                    ((Boolean)this.isCachePoint.get(currentTransformerIndex)).booleanValue()) {
+        try {
+            XMLSerializer localXMLSerializer = null;
+            XMLSerializer cachePointXMLSerializer = null;
+            if (!this.cacheCompleteResponse) {
+                this.xmlSerializer = (XMLSerializer)this.manager.lookup( XMLSerializer.ROLE );
+                localXMLSerializer = this.xmlSerializer;
+            }
+            if ( this.cachedResponse == null ) {
 
-                                cachePointXMLSerializer = ((XMLSerializer)
-                                this.manager.lookup( XMLSerializer.ROLE ));
-                                next = new XMLTeePipe(next, cachePointXMLSerializer);
-                                this.xmlSerializerArray.add(cachePointXMLSerializer);
-                            }
-                        }
+                XMLProducer prev = super.generator;
+                XMLConsumer next;
 
+                int cacheableTransformerCount = this.firstNotCacheableTransformerIndex;
+                int currentTransformerIndex = 0; //start with the first transformer
 
-                        // Serializer is not cacheable,
-                        // but we  have the longest cacheable key. Do default longest key caching
-                        if (localXMLSerializer != null) {
-                            if (cacheableTransformerCount == 0) {
-                                next = new XMLTeePipe(next, localXMLSerializer);
-                                this.xmlSerializerArray.add(localXMLSerializer);
-                                localXMLSerializer = null;
-                            } else {
-                                cacheableTransformerCount--;
-                            }
+                Iterator itt = this.transformers.iterator();
+                while ( itt.hasNext() ) {
+                    next = (XMLConsumer) itt.next();
+
+                    // if we have cacheable transformers,
+                    // check the tranformers for cachepoints
+                    if (cacheableTransformerCount > 0) {
+                        if ( (this.isCachePoint.get(currentTransformerIndex) != null)  &&
+                                ((Boolean)this.isCachePoint.get(currentTransformerIndex)).booleanValue()) {
+
+                            cachePointXMLSerializer = ((XMLSerializer)
+                            this.manager.lookup( XMLSerializer.ROLE ));
+                            next = new XMLTeePipe(next, cachePointXMLSerializer);
+                            this.xmlSerializerArray.add(cachePointXMLSerializer);
                         }
-                        this.connect(environment, prev, next);
-                        prev = (XMLProducer) next;
-
-                        currentTransformerIndex++;
                     }
-                    next = super.lastConsumer;
 
 
-                    // if the serializer is not cacheable, but all the transformers are:
-                    // (this is default longest key caching)
+                    // Serializer is not cacheable,
+                    // but we  have the longest cacheable key. Do default longest key caching
                     if (localXMLSerializer != null) {
-                        next = new XMLTeePipe(next, localXMLSerializer);
-                        this.xmlSerializerArray.add(localXMLSerializer);
-                        localXMLSerializer = null;
-                    }
-
-                    // else if the serializer is cacheable and has cocoon views
-                    else if ((currentTransformerIndex == this.firstNotCacheableTransformerIndex) &&
-                            this.nextIsCachePoint) {
-                        cachePointXMLSerializer = ((XMLSerializer)this.manager.lookup( XMLSerializer.ROLE ));
-                        next = new XMLTeePipe(next, cachePointXMLSerializer);
-                        this.xmlSerializerArray.add(cachePointXMLSerializer);
+                        if (cacheableTransformerCount == 0) {
+                            next = new XMLTeePipe(next, localXMLSerializer);
+                            this.xmlSerializerArray.add(localXMLSerializer);
+                            localXMLSerializer = null;
+                        } else {
+                            cacheableTransformerCount--;
+                        }
                     }
                     this.connect(environment, prev, next);
+                    prev = (XMLProducer) next;
+
+                    currentTransformerIndex++;
+                }
+                next = super.lastConsumer;
 
 
-                } else {
-                    // Here the first part of the pipeline has been retrived from cache
-                    // we now check if any part of the rest of the pipeline can be cached
-                    this.xmlDeserializer = (XMLDeserializer)this.manager.lookup(XMLDeserializer.ROLE);
-                    // connect the pipeline:
-                    XMLProducer prev = xmlDeserializer;
-                    XMLConsumer next;
-                    int cacheableTransformerCount = 0;
-                    Iterator itt = this.transformers.iterator();
-                    while ( itt.hasNext() ) {
-                        next = (XMLConsumer) itt.next();
-
-                        if (cacheableTransformerCount >= this.firstProcessedTransformerIndex) {
-
-                            // if we have cacheable transformers left,
-                            // then check the tranformers for cachepoints
-                            if (cacheableTransformerCount < this.firstNotCacheableTransformerIndex) {
-                                if ( !(prev instanceof XMLDeserializer) &&
-                                        (this.isCachePoint.get(cacheableTransformerCount) != null)  &&
-                                        ((Boolean)this.isCachePoint.get(cacheableTransformerCount)).booleanValue()) {
-                                    cachePointXMLSerializer = ((XMLSerializer)this.manager.lookup( XMLSerializer.ROLE ));
-                                    next = new XMLTeePipe(next, cachePointXMLSerializer);
-                                    this.xmlSerializerArray.add(cachePointXMLSerializer);
-                                }
-                            }
+                // if the serializer is not cacheable, but all the transformers are:
+                // (this is default longest key caching)
+                if (localXMLSerializer != null) {
+                    next = new XMLTeePipe(next, localXMLSerializer);
+                    this.xmlSerializerArray.add(localXMLSerializer);
+                    localXMLSerializer = null;
+                }
 
-                            // Serializer is not cacheable,
-                            // but we  have the longest cacheable key. Do default longest key caching
-                            if (localXMLSerializer != null && !(prev instanceof XMLDeserializer)
-                                    && cacheableTransformerCount == this.firstNotCacheableTransformerIndex) {
-                                next = new XMLTeePipe(next, localXMLSerializer);
-                                this.xmlSerializerArray.add(localXMLSerializer);
-                                localXMLSerializer = null;
+                // else if the serializer is cacheable and has cocoon views
+                else if ((currentTransformerIndex == this.firstNotCacheableTransformerIndex) &&
+                        this.nextIsCachePoint) {
+                    cachePointXMLSerializer = ((XMLSerializer)this.manager.lookup( XMLSerializer.ROLE ));
+                    next = new XMLTeePipe(next, cachePointXMLSerializer);
+                    this.xmlSerializerArray.add(cachePointXMLSerializer);
+                }
+                this.connect(environment, prev, next);
+
+            } else {
+                // Here the first part of the pipeline has been retrived from cache
+                // we now check if any part of the rest of the pipeline can be cached
+                this.xmlDeserializer = (XMLDeserializer)this.manager.lookup(XMLDeserializer.ROLE);
+                // connect the pipeline:
+                XMLProducer prev = xmlDeserializer;
+                XMLConsumer next;
+                int cacheableTransformerCount = 0;
+                Iterator itt = this.transformers.iterator();
+                while ( itt.hasNext() ) {
+                    next = (XMLConsumer) itt.next();
+
+                    if (cacheableTransformerCount >= this.firstProcessedTransformerIndex) {
+
+                        // if we have cacheable transformers left,
+                        // then check the tranformers for cachepoints
+                        if (cacheableTransformerCount < this.firstNotCacheableTransformerIndex) {
+                            if ( !(prev instanceof XMLDeserializer) &&
+                                    (this.isCachePoint.get(cacheableTransformerCount) != null)  &&
+                                    ((Boolean)this.isCachePoint.get(cacheableTransformerCount)).booleanValue()) {
+                                cachePointXMLSerializer = ((XMLSerializer)this.manager.lookup( XMLSerializer.ROLE ));
+                                next = new XMLTeePipe(next, cachePointXMLSerializer);
+                                this.xmlSerializerArray.add(cachePointXMLSerializer);
                             }
-                            this.connect(environment, prev, next);
-                            prev = (XMLProducer)next;
                         }
-                        cacheableTransformerCount++;
-                    }
-                    next = super.lastConsumer;
-
-                    //*all* the transformers are cacheable, but the serializer is not!! this is longest key
-                    if (localXMLSerializer != null && !(prev instanceof XMLDeserializer)) {
-                        next = new XMLTeePipe(next, localXMLSerializer);
-                        this.xmlSerializerArray.add(localXMLSerializer);
-                        localXMLSerializer = null;
 
-            }
-            //	else the serializer is cacheable but has views
-            else if (this.nextIsCachePoint && !(prev instanceof XMLDeserializer) &&
-                            cacheableTransformerCount == this.firstNotCacheableTransformerIndex) {
-                        cachePointXMLSerializer = ((XMLSerializer)this.manager.lookup( XMLSerializer.ROLE ));
-                        next = new XMLTeePipe(next,  cachePointXMLSerializer);
-                        this.xmlSerializerArray.add(cachePointXMLSerializer);
+                        // Serializer is not cacheable,
+                        // but we  have the longest cacheable key. Do default longest key caching
+                        if (localXMLSerializer != null && !(prev instanceof XMLDeserializer)
+                                && cacheableTransformerCount == this.firstNotCacheableTransformerIndex) {
+                            next = new XMLTeePipe(next, localXMLSerializer);
+                            this.xmlSerializerArray.add(localXMLSerializer);
+                            localXMLSerializer = null;
+                        }
+                        this.connect(environment, prev, next);
+                        prev = (XMLProducer)next;
                     }
-                    this.connect(environment, prev, next);
+                    cacheableTransformerCount++;
                 }
+                next = super.lastConsumer;
 
-            } catch ( ServiceException e ) {
-                throw new ProcessingException("Could not connect pipeline.", e);
+                //*all* the transformers are cacheable, but the serializer is not!! this is longest key
+                if (localXMLSerializer != null && !(prev instanceof XMLDeserializer)) {
+                    next = new XMLTeePipe(next, localXMLSerializer);
+                    this.xmlSerializerArray.add(localXMLSerializer);
+                    localXMLSerializer = null;
+                } else if (this.nextIsCachePoint && !(prev instanceof XMLDeserializer) &&
+                        cacheableTransformerCount == this.firstNotCacheableTransformerIndex) {
+                    // else the serializer is cacheable but has views
+                    cachePointXMLSerializer = ((XMLSerializer)this.manager.lookup( XMLSerializer.ROLE ));
+                    next = new XMLTeePipe(next,  cachePointXMLSerializer);
+                    this.xmlSerializerArray.add(cachePointXMLSerializer);
+                }
+                this.connect(environment, prev, next);
             }
+
+        } catch ( ServiceException e ) {
+            throw new ProcessingException("Could not connect pipeline.", e);
+        }
     }
 
 

Modified: cocoon/trunk/src/java/org/apache/cocoon/components/pipeline/impl/CachingProcessingPipeline.java
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/components/pipeline/impl/CachingProcessingPipeline.java	(original)
+++ cocoon/trunk/src/java/org/apache/cocoon/components/pipeline/impl/CachingProcessingPipeline.java	Mon Sep 27 00:02:46 2004
@@ -37,7 +37,7 @@
  *
  * @since 2.1
  * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
- * @version CVS $Id: CachingProcessingPipeline.java,v 1.7 2004/07/15 12:49:50 sylvain Exp $
+ * @version CVS $Id$
  */
 public class CachingProcessingPipeline
     extends AbstractCachingProcessingPipeline {
@@ -53,6 +53,7 @@
                 CachedResponse response = new CachedResponse(this.toCacheSourceValidities,
                                           ((CachingOutputStream)os).getContent(),
                                           expiresObj);
+                response.setContentType(environment.getContentType());
                 this.cache.store(this.toCacheKey,
                                  response);
             } else {

Modified: cocoon/trunk/status.xml
==============================================================================
--- cocoon/trunk/status.xml	(original)
+++ cocoon/trunk/status.xml	Mon Sep 27 00:02:46 2004
@@ -329,6 +329,9 @@
    </action>
  </release>
  <release version="2.1.6" date="TBD">
+   <action dev="CZ" type="update">
+     Cache the mime-type of readers and serializers.
+   </action>
    <action dev="VG" type="update">
      Add support for translating attribute values which contain i18n expressions
      rather than complete i18n key.