KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > pipeline > impl > CachingProcessingPipeline


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
32 import java.io.Serializable JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 /**
36  * The CachingProcessingPipeline
37  *
38  * @since 2.1
39  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
40  * @version $Id: CachingProcessingPipeline.java 157159 2005-03-11 21:13:35Z vgritsenko $
41  */

42 public class CachingProcessingPipeline extends AbstractCachingProcessingPipeline {
43
44     /**
45      * Cache longest cacheable key
46      */

47     protected void cacheResults(Environment environment, OutputStream os)
48     throws Exception JavaDoc {
49         if (this.toCacheKey != null) {
50             // See if there is an expires object for this resource.
51
Long JavaDoc expiresObj = (Long JavaDoc) 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     /**
70      * Create a new cache key
71      */

72     protected ComponentCacheKey newComponentCacheKey(int type, String JavaDoc role,Serializable JavaDoc key) {
73         return new ComponentCacheKey(type, role, key);
74     }
75
76
77     /**
78      * Connect the pipeline.
79      */

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 JavaDoc 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                 // connect the pipeline:
120
XMLProducer prev = xmlDeserializer;
121                 XMLConsumer next;
122
123                 int cacheableTransformerCount = 0;
124                 Iterator JavaDoc 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