KickJava   Java API By Example, From Geeks To Geeks.

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


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.activity.Disposable;
22 import org.apache.avalon.framework.component.ComponentException;
23 import org.apache.avalon.framework.component.ComponentManager;
24 import org.apache.avalon.framework.parameters.Parameters;
25 import org.apache.cocoon.ProcessingException;
26 import org.apache.cocoon.components.pipeline.impl.NonCachingProcessingPipeline;
27 import org.apache.cocoon.environment.Environment;
28 import org.apache.cocoon.sitemap.SitemapModelComponent;
29 import org.apache.cocoon.transformation.Transformer;
30 import org.apache.cocoon.xml.XMLConsumer;
31 import org.apache.cocoon.xml.XMLProducer;
32 import org.xml.sax.SAXException JavaDoc;
33
34 /**
35  * Special version of the NonCachingProcessingPipeline that supports capturing
36  * the SAX-events that go through it and stores the result in the
37  * ProfilerData.
38  *
39  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
40  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
41  * @author <a HREF="mailto:bruno@outerthought.org">Bruno Dumon</a>
42  * @version $Id: ProfilingNonCachingProcessingPipeline.java 157221 2005-03-12 03:20:53Z vgritsenko $
43  */

44 public class ProfilingNonCachingProcessingPipeline extends NonCachingProcessingPipeline
45                                                    implements Disposable {
46
47     private Profiler profiler;
48
49     private ProfilerData data;
50
51     private int index;
52
53     /**
54      * Composable
55      *
56      * @param manager
57      */

58     public void compose(ComponentManager manager) throws ComponentException {
59         super.compose(manager);
60         this.profiler = (Profiler) manager.lookup(Profiler.ROLE);
61     }
62
63     /**
64      * Disposable
65      */

66     public void dispose() {
67         if (this.profiler!=null) {
68             this.manager.release(this.profiler);
69             this.profiler = null;
70         }
71     }
72
73     /**
74      * Recyclable
75      */

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

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

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

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

163     public void setReader(String JavaDoc role, String JavaDoc source, Parameters param,
164                           String JavaDoc mimeType)
165     throws ProcessingException {
166
167         super.setReader(role, source, param, mimeType);
168
169         if (this.data==null) {
170             this.data = new ProfilerData();
171         }
172         this.data.addComponent(super.reader, role, source);
173     }
174
175     /**
176      * Setup pipeline components.
177      *
178      * @param environment
179      */

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

233     public boolean process(Environment environment)
234       throws ProcessingException {
235
236         this.index = 0;
237         if (this.data!=null) {
238             // Capture environment info
239
this.data.setEnvironmentInfo(new EnvironmentInfo(environment));
240
241             // Execute pipeline
242
long time = System.currentTimeMillis();
243             boolean result = super.process(environment);
244
245             this.data.setTotalTime(System.currentTimeMillis()-time);
246
247             // Report
248
profiler.addResult(environment.getURI(), this.data);
249             return result;
250         } else {
251             getLogger().warn("Profiler Data havn't any components to measure");
252             return super.process(environment);
253         }
254     }
255
256     /**
257      * Process the SAX event pipeline
258      */

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

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

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