cziegeler 02/05/03 08:31:28 Modified: src/java/org/apache/cocoon/components/pipeline/impl CachingProcessingPipeline.java Added: src/java/org/apache/cocoon/caching CachedResponse.java Log: Caching of reader is nearly finished. Revision Changes Path 1.1 xml-cocoon2/src/java/org/apache/cocoon/caching/CachedResponse.java Index: CachedResponse.java =================================================================== /* ============================================================================ The Apache Software License, Version 1.1 ============================================================================ Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved. Redistribution and use in source and binary forms, with or without modifica- tion, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software developed by the Apache Software Foundation (http://www.apache.org/)." Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. 4. The names "Apache Cocoon" and "Apache Software Foundation" must not be used to endorse or promote products derived from this software without prior written permission. For written permission, please contact [EMAIL PROTECTED] 5. Products derived from this software may not be called "Apache", nor may "Apache" appear in their name, without prior written permission of the Apache Software Foundation. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This software consists of voluntary contributions made by many individuals on behalf of the Apache Software Foundation and was originally created by Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache Software Foundation, please see <http://www.apache.org/>. */ package org.apache.cocoon.caching; import org.apache.excalibur.source.SourceValidity; /** * This is a cached response. This can either contain a byte array with * the complete character response or a byte array with compiled SAX events. * * This class replaces the <code>CachedEventObject</code> and the * <code>CachedStreamObject</code>. * * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> * @version CVS $Id: CachedResponse.java,v 1.1 2002/05/03 15:31:28 cziegeler Exp $ */ public final class CachedResponse implements java.io.Serializable { private SourceValidity[] validityObjects; private byte[] response; /** * Create a new entry for the cache. * * @param validityObjects The SourceValidity objects in the order * they occured in the pipeline * @param saxFragment The cached sax stream or character stream */ public CachedResponse(SourceValidity[] validityObjects, byte[] response) { this.validityObjects = validityObjects; this.response = response; } /** * Get the validity objects */ public SourceValidity[] getValidityObjects() { return this.validityObjects; } /** * Get the cached response. * * @return The sax stream or character stream */ public byte[] getResponse() { return this.response; } } 1.3 +51 -43 xml-cocoon2/src/java/org/apache/cocoon/components/pipeline/impl/CachingProcessingPipeline.java Index: CachingProcessingPipeline.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/pipeline/impl/CachingProcessingPipeline.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- CachingProcessingPipeline.java 3 May 2002 14:47:33 -0000 1.2 +++ CachingProcessingPipeline.java 3 May 2002 15:31:28 -0000 1.3 @@ -57,7 +57,10 @@ import org.apache.avalon.framework.parameters.Parameters; import org.apache.cocoon.ConnectionResetException; import org.apache.cocoon.ProcessingException; -import org.apache.cocoon.caching.*; +import org.apache.cocoon.caching.Cacheable; +import org.apache.cocoon.caching.CacheableProcessingComponent; +import org.apache.cocoon.caching.CachedResponse; +import org.apache.cocoon.caching.CachingOutputStream; import org.apache.cocoon.components.pipeline.AbstractProcessingPipeline; import org.apache.cocoon.components.sax.XMLDeserializer; import org.apache.cocoon.components.sax.XMLSerializer; @@ -68,6 +71,7 @@ import org.apache.cocoon.transformation.Transformer; import org.apache.cocoon.xml.XMLConsumer; import org.apache.cocoon.xml.XMLProducer; +import org.apache.excalibur.source.SourceValidity; import org.xml.sax.SAXException; import java.io.IOException; @@ -83,7 +87,7 @@ * * * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> - * @version CVS $Id: CachingProcessingPipeline.java,v 1.2 2002/05/03 14:47:33 cziegeler Exp $ + * @version CVS $Id: CachingProcessingPipeline.java,v 1.3 2002/05/03 15:31:28 cziegeler Exp $ */ public class CachingProcessingPipeline extends AbstractProcessingPipeline @@ -274,10 +278,6 @@ if (key != 0) { // CacheValidity validity = ((Cacheable)this.generator).generateValidity(); - ComponentCacheKey cck = new ComponentCacheKey( - ComponentCacheKey.ComponentType_Generator, - this.generatorRole, - key); this.pipelineCacheKey = new StringBuffer(); this.pipelineCacheKey.append("PCK:CCK:1-") .append(this.generatorRole).append('-') @@ -412,85 +412,93 @@ try { boolean usedCache = false; OutputStream outputStream = environment.getOutputStream();; - Map validityObjects = new HashMap(); + SourceValidity readerValidity = null; + StringBuffer pcKey = null; // test if reader is cacheable long readerKey = 0; - boolean isCacheable = false; boolean isCacheableProcessingComponent = false; if (this.reader instanceof CacheableProcessingComponent) { readerKey = ((CacheableProcessingComponent)this.reader).generateKey(); + isCacheableProcessingComponent = true; } else if (this.reader instanceof Cacheable) { readerKey = ((Cacheable)this.reader).generateKey(); } if ( readerKey != 0) { // response is cacheable, build the key - ComponentCacheKey ccKey; - StringBuffer pcKey = new StringBuffer(); + pcKey = new StringBuffer(); pcKey.append("PCK:CCK:4-").append(this.readerRole) .append('-') .append(readerKey); // now we have the key to get the cached object - CachedStreamObject cachedObject = (CachedStreamObject)this.cache.get(pcKey); + CachedResponse cachedObject = (CachedResponse)this.cache.get(pcKey); if (cachedObject != null) { if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("Found cached response for '" + environment.getURI() + "'."); } - } - } - /* Iterator validityIterator = validityObjects.keySet().iterator(); - ComponentCacheKey validityKey; - boolean valid = true; - while (validityIterator.hasNext() && valid) { - validityKey = (ComponentCacheKey)validityIterator.next(); - valid = cachedObject.isValid(validityKey, (CacheValidity)validityObjects.get(validityKey)); - if (getLogger().isDebugEnabled()) { - CacheValidity cachedValidity = cachedObject.getCacheValidity(validityKey); - getLogger().debug("Compared cached validity '" + cachedValidity + - "' with new validity '" + validityObjects.get(validityKey) + - "' : " + (valid ? "valid" : "changed")); + SourceValidity[] validities = cachedObject.getValidityObjects(); + if (validities == null || validities.length != 1) { + throw new ProcessingException("Cached response is not correct."); + } + SourceValidity cachedValidity = validities[0]; + boolean valid = cachedValidity.isValid(); + if ( !valid ) { + // get reader validity and compare + if (isCacheableProcessingComponent) { + readerValidity = ((CacheableProcessingComponent)this.reader).generateValidity(); + } else { + // FIXME: wrap it } + valid = cachedValidity.isValid(readerValidity); } - if (valid) { - getLogger().debug("Using valid cached content for '" + environment.getURI() + "'."); + if (valid) { + if (this.getLogger().isDebugEnabled()) { + this.getLogger().debug("Using valid cached content for '" + environment.getURI() + "'."); + } byte[] response = cachedObject.getResponse(); - if(response.length > 0) { + if (response.length > 0) { usedCache = true; environment.setContentLength(response.length); outputStream.write(response); } + } else { + if (this.getLogger().isDebugEnabled()) { + this.getLogger().debug("Cached content is invalid for '" + environment.getURI() + "'."); + } + // remove invalid cached object + this.cache.remove(pcKey); } + } + } - if(!usedCache) { - getLogger().debug("Cached content is invalid for '" + environment.getURI() + "'."); + if (!usedCache) { - // remove invalid cached object - this.streamCache.remove(pcKey); - cachedObject = null; + if ( pcKey != null ) { + if (this.getLogger().isDebugEnabled()) { + this.getLogger().debug("Caching content for further requests of '" + environment.getURI() + "'."); + } + if (readerValidity == null) { + if (isCacheableProcessingComponent) { + readerValidity = ((CacheableProcessingComponent)this.reader).generateValidity(); + } else { + // FIXME: wrap it + } } - } - if (cachedObject == null) { - getLogger().debug("Caching content for further requests of '" + environment.getURI() + "'."); outputStream = new CachingOutputStream(outputStream); } - validityObjects.put(ccKey, readerValidity); - - } -*/ - if (!usedCache) { super.processReader( environment, outputStream ); // store the response - /* if (pcKey != null) { + if (pcKey != null) { this.cache.store(pcKey, - new CachedStreamObject(validityObjects, + new CachedResponse( new SourceValidity[] {readerValidity}, ((CachingOutputStream)outputStream).getContent())); - }*/ + } } } catch ( SocketException se ) { if (se.getMessage().indexOf("reset") > 0
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]