KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > profiler > ProfilingCachingProcessingPipeline


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.profiler;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import org.apache.avalon.framework.component.ComponentException;
22 import org.apache.avalon.framework.component.ComponentManager;
23 import org.apache.avalon.framework.parameters.Parameters;
24 import org.apache.cocoon.ProcessingException;
25 import org.apache.cocoon.components.pipeline.impl.CachingProcessingPipeline;
26 import org.apache.cocoon.environment.Environment;
27 import org.apache.cocoon.sitemap.SitemapModelComponent;
28 import org.apache.cocoon.transformation.Transformer;
29 import org.apache.cocoon.xml.XMLConsumer;
30 import org.apache.cocoon.xml.XMLProducer;
31 import org.xml.sax.SAXException JavaDoc;
32
33 /**
34  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
35  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
36  * @author <a HREF="mailto:bruno@outerthought.org">Bruno Dumon</a>
37  * @version $Id: ProfilingCachingProcessingPipeline.java 157221 2005-03-12 03:20:53Z vgritsenko $
38  */

39 public class ProfilingCachingProcessingPipeline extends CachingProcessingPipeline {
40
41     private Profiler profiler;
42
43     private ProfilerData data;
44
45     private int index;
46
47     /**
48      * Composable
49      *
50      * @param manager
51      */

52     public void compose(ComponentManager manager) throws ComponentException {
53         super.compose(manager);
54         this.profiler = (Profiler) manager.lookup(Profiler.ROLE);
55     }
56
57     /**
58      * Disposable
59      */

60     public void dispose() {
61         if (this.profiler!=null) {
62             this.manager.release(this.profiler);
63             this.profiler = null;
64         }
65         super.dispose();
66     }
67
68     /**
69      * Recyclable
70      */

71     public void recycle() {
72         this.data = null;
73         this.index = 0;
74         super.recycle();
75     }
76
77     /**
78      * Set the generator that will be used as the initial step in the pipeline.
79      * The generator role is given : the actual <code>Generator</code> is fetched
80      * from the latest <code>ComponentManager</code> given by <code>compose()</code>
81      * or <code>recompose()</code>.
82      *
83      * @param role the generator role in the component manager.
84      * @param source the source where to produce XML from, or <code>null</code> if no
85      * source is given.
86      * @param param the parameters for the generator.
87      * @param hintParam
88      * @throws ProcessingException if the generator couldn't be obtained.
89      */

90     public void setGenerator(String JavaDoc role, String JavaDoc source, Parameters param,
91                              Parameters hintParam)
92     throws ProcessingException {
93
94         super.setGenerator(role, source, param, hintParam);
95
96         if (this.data==null) {
97             this.data = new ProfilerData();
98         }
99         this.data.addComponent(super.generator, role, source);
100     }
101
102     /**
103      * Add a transformer at the end of the pipeline.
104      * The transformer role is given : the actual <code>Transformer</code> is fetched
105      * from the latest <code>ComponentManager</code> given by <code>compose()</code>
106      * or <code>recompose()</code>.
107      *
108      * @param role the transformer role in the component manager.
109      * @param source the source used to setup the transformer (e.g. XSL file), or
110      * <code>null</code> if no source is given.
111      * @param param the parameters for the transfomer.
112      * @param hintParam
113      * @throws ProcessingException if the generator couldn't be obtained.
114      */

115     public void addTransformer(String JavaDoc role, String JavaDoc source, Parameters param,
116                                Parameters hintParam)
117     throws ProcessingException {
118
119         super.addTransformer(role, source, param, hintParam);
120
121         if (this.data==null) {
122             this.data = new ProfilerData();
123         }
124         this.data.addComponent(super.transformers.get(super.transformers.size()-
125             1), role, source);
126     }
127
128     /**
129      * Set the serializer for this pipeline
130      *
131      * @param role
132      * @param source
133      * @param param
134      * @param hintParam
135      * @param mimeType
136      */

137     public void setSerializer(String JavaDoc role, String JavaDoc source, Parameters param,
138                               Parameters hintParam,
139                               String JavaDoc mimeType)
140     throws ProcessingException {
141
142         super.setSerializer(role, source, param, hintParam, mimeType);
143
144         if (this.data==null) {
145             this.data = new ProfilerData();
146         }
147         this.data.addComponent(super.serializer, role, source);
148     }
149
150     /**
151      * Set the reader for this pipeline
152      *
153      * @param role
154      * @param source
155      * @param param
156      * @param mimeType
157      */

158     public void setReader(String JavaDoc role, String JavaDoc source, Parameters param,
159                           String JavaDoc mimeType)
160     throws ProcessingException {
161
162         super.setReader(role, source, param, mimeType);
163
164         if (this.data==null) {
165             this.data = new ProfilerData();
166         }
167         this.data.addComponent(super.reader, role, source);
168     }
169
170     /**
171      * Timed version of {@link org.apache.cocoon.components.pipeline.AbstractProcessingPipeline#setupPipeline}
172      * and {@link org.apache.cocoon.components.pipeline.impl.AbstractCachingProcessingPipeline#setupPipeline}.
173      */

174     protected void setupPipeline(Environment environment)
175     throws ProcessingException {
176         try {
177             // Setup the generator
178
long time = System.currentTimeMillis();
179             this.generator.setup(environment, environment.getObjectModel(),
180                                  generatorSource, generatorParam);
181             this.data.setSetupTime(0, System.currentTimeMillis() - time);
182
183             Iterator JavaDoc transformerItt = this.transformers.iterator();
184             Iterator JavaDoc transformerSourceItt = this.transformerSources.iterator();
185             Iterator JavaDoc transformerParamItt = this.transformerParams.iterator();
186
187             // Setup transformers
188
int index = 1;
189             while (transformerItt.hasNext()) {
190                 Transformer trans = (Transformer) transformerItt.next();
191
192                 time = System.currentTimeMillis();
193                 trans.setup(environment, environment.getObjectModel(),
194                             (String JavaDoc) transformerSourceItt.next(),
195                             (Parameters) transformerParamItt.next());
196                 this.data.setSetupTime(index++, System.currentTimeMillis() - time);
197             }
198
199             // Setup serializer
200
time = System.currentTimeMillis();
201             if (this.serializer instanceof SitemapModelComponent) {
202                 ((SitemapModelComponent)this.serializer).setup(
203                     environment,
204                     environment.getObjectModel(),
205                     serializerSource,
206                     serializerParam
207                 );
208             }
209             this.data.setSetupTime(index++, System.currentTimeMillis() - time);
210
211             setMimeTypeForSerializer(environment);
212         } catch (SAXException JavaDoc e) {
213             throw new ProcessingException("Could not setup pipeline.", e);
214         } catch (IOException JavaDoc e) {
215             throw new ProcessingException("Could not setup pipeline.", e);
216         }
217
218         // Generate the key to fill the cache
219
generateCachingKey(environment);
220
221         // Test the cache for a valid response
222
if (this.toCacheKey!=null) {
223             validatePipeline(environment);
224         }
225
226         setupValidities();
227     }
228
229     /**
230      * Process the given <code>Environment</code>, producing the output.
231      *
232      * @param environment
233      *
234      * @return true on success
235      */

236     public boolean process(Environment environment)
237     throws ProcessingException {
238         this.index = 0;
239         if (this.data != null) {
240             // Capture environment info
241
this.data.setEnvironmentInfo(new EnvironmentInfo(environment));
242
243             // Execute pipeline
244
long time = System.currentTimeMillis();
245             boolean result = super.process(environment);
246             this.data.setTotalTime(System.currentTimeMillis() - time);
247
248             // Report
249
profiler.addResult(environment.getURI(), this.data);
250             return result;
251         } else {
252             getLogger().warn("Profiler data has no components to measure");
253             return super.process(environment);
254         }
255     }
256
257     /**
258      * Process the SAX event pipeline
259      * FIXME: VG: Why override processXMLPipeline, not process(env, consumer)?
260      */

261     protected boolean processXMLPipeline(Environment environment)
262     throws ProcessingException {
263         this.index = 0;
264         if (this.data != null) {
265             // Capture environment info
266
this.data.setEnvironmentInfo(new EnvironmentInfo(environment));
267
268             // Execute pipeline
269
long time = System.currentTimeMillis();
270             boolean result = super.processXMLPipeline(environment);
271             this.data.setTotalTime(System.currentTimeMillis() - time);
272
273             // Report
274
profiler.addResult(environment.getURI(), this.data);
275             return result;
276         } else {
277             getLogger().warn("Profiler data has no components to measure");
278             return super.processXMLPipeline(environment);
279         }
280     }
281
282     /**
283      * Process the pipeline using a reader.
284      */

285     protected boolean processReader(Environment environment)
286     throws ProcessingException {
287         this.index = 0;
288         if (this.data != null) {
289             // Capture environment info
290
this.data.setEnvironmentInfo(new EnvironmentInfo(environment));
291
292             // Execute pipeline
293
long time = System.currentTimeMillis();
294             boolean result = super.processReader(environment);
295             this.data.setTotalTime(System.currentTimeMillis() - time);
296
297             // Report
298
profiler.addResult(environment.getURI(), this.data);
299             return result;
300         } else {
301             getLogger().warn("Profiler data has no components to measure");
302             return super.processReader(environment);
303         }
304     }
305
306     /**
307      * Connect the next component
308      *
309      * @param environment
310      * @param producer
311      * @param consumer
312      */

313     protected void connect(Environment environment, XMLProducer producer,
314                            XMLConsumer consumer)
315     throws ProcessingException {
316         ProfilingXMLPipe connector = new ProfilingXMLPipe();
317         connector.setup(this.index, this.data);
318         this.index++;
319         super.connect(environment, producer, connector);
320         super.connect(environment, connector, consumer);
321     }
322 }
323
Popular Tags