1 16 package org.apache.cocoon.components.pipeline.impl; 17 18 import org.apache.avalon.framework.component.ComponentException; 19 import org.apache.cocoon.ProcessingException; 20 import org.apache.cocoon.caching.CachedResponse; 21 import org.apache.cocoon.caching.CachingOutputStream; 22 import org.apache.cocoon.caching.ComponentCacheKey; 23 import org.apache.cocoon.components.sax.XMLDeserializer; 24 import org.apache.cocoon.components.sax.XMLSerializer; 25 import org.apache.cocoon.components.sax.XMLTeePipe; 26 import org.apache.cocoon.environment.Environment; 27 import org.apache.cocoon.environment.ObjectModelHelper; 28 import org.apache.cocoon.xml.XMLConsumer; 29 import org.apache.cocoon.xml.XMLProducer; 30 31 import java.io.OutputStream ; 32 import java.io.Serializable ; 33 import java.util.Iterator ; 34 35 42 public class CachingProcessingPipeline extends AbstractCachingProcessingPipeline { 43 44 47 protected void cacheResults(Environment environment, OutputStream os) 48 throws Exception { 49 if (this.toCacheKey != null) { 50 Long expiresObj = (Long ) environment.getObjectModel().get(ObjectModelHelper.EXPIRES_OBJECT); 52 53 CachedResponse response; 54 if (this.cacheCompleteResponse) { 55 response = new CachedResponse(this.toCacheSourceValidities, 56 ((CachingOutputStream) os).getContent(), 57 expiresObj); 58 response.setContentType(environment.getContentType()); 59 } else { 60 response = new CachedResponse(this.toCacheSourceValidities, 61 (byte[]) this.xmlSerializer.getSAXFragment(), 62 expiresObj); 63 } 64 65 this.cache.store(this.toCacheKey, response); 66 } 67 } 68 69 72 protected ComponentCacheKey newComponentCacheKey(int type, String role,Serializable key) { 73 return new ComponentCacheKey(type, role, key); 74 } 75 76 77 80 protected void connectCachingPipeline(Environment environment) 81 throws ProcessingException { 82 try { 83 XMLSerializer localXMLSerializer = null; 84 if (!this.cacheCompleteResponse) { 85 this.xmlSerializer = (XMLSerializer) this.manager.lookup(XMLSerializer.ROLE); 86 localXMLSerializer = this.xmlSerializer; 87 } 88 89 if (this.cachedResponse == null) { 90 XMLProducer prev = super.generator; 91 XMLConsumer next; 92 93 int cacheableTransformerCount = this.firstNotCacheableTransformerIndex; 94 Iterator itt = this.transformers.iterator(); 95 while (itt.hasNext()) { 96 next = (XMLConsumer) itt.next(); 97 if (localXMLSerializer != null) { 98 if (cacheableTransformerCount == 0) { 99 next = new XMLTeePipe(next, localXMLSerializer); 100 localXMLSerializer = null; 101 } else { 102 cacheableTransformerCount--; 103 } 104 } 105 connect(environment, prev, next); 106 prev = (XMLProducer) next; 107 } 108 109 next = super.lastConsumer; 110 if (localXMLSerializer != null) { 111 next = new XMLTeePipe(next, localXMLSerializer); 112 localXMLSerializer = null; 113 } 114 connect(environment, prev, next); 115 116 } else { 117 this.xmlDeserializer = (XMLDeserializer) this.manager.lookup(XMLDeserializer.ROLE); 118 119 XMLProducer prev = xmlDeserializer; 121 XMLConsumer next; 122 123 int cacheableTransformerCount = 0; 124 Iterator itt = this.transformers.iterator(); 125 while (itt.hasNext()) { 126 next = (XMLConsumer) itt.next(); 127 if (cacheableTransformerCount >= this.firstProcessedTransformerIndex) { 128 if (localXMLSerializer != null 129 && cacheableTransformerCount == this.firstNotCacheableTransformerIndex) { 130 next = new XMLTeePipe(next, localXMLSerializer); 131 localXMLSerializer = null; 132 } 133 connect(environment, prev, next); 134 prev = (XMLProducer) next; 135 } 136 cacheableTransformerCount++; 137 } 138 139 next = super.lastConsumer; 140 if (localXMLSerializer != null) { 141 next = new XMLTeePipe(next, localXMLSerializer); 142 localXMLSerializer = null; 143 } 144 connect(environment, prev, next); 145 } 146 147 } catch (ComponentException e) { 148 throw new ProcessingException("Could not connect pipeline.", e); 149 } 150 } 151 } 152 | Popular Tags |