1 16 package org.apache.cocoon.components.profiler; 17 18 import java.io.IOException ; 19 import java.util.Iterator ; 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 ; 33 34 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 58 public void compose(ComponentManager manager) throws ComponentException { 59 super.compose(manager); 60 this.profiler = (Profiler) manager.lookup(Profiler.ROLE); 61 } 62 63 66 public void dispose() { 67 if (this.profiler!=null) { 68 this.manager.release(this.profiler); 69 this.profiler = null; 70 } 71 } 72 73 76 public void recycle() { 77 this.data = null; 78 this.index = 0; 79 super.recycle(); 80 } 81 82 95 public void setGenerator(String role, String 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 120 public void addTransformer(String role, String 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 142 public void setSerializer(String role, String source, Parameters param, 143 Parameters hintParam, 144 String 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 163 public void setReader(String role, String source, Parameters param, 164 String 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 180 protected void setupPipeline(Environment environment) 181 throws ProcessingException { 182 try { 183 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 transformerItt = this.transformers.iterator(); 190 Iterator transformerSourceItt = this.transformerSources.iterator(); 191 Iterator transformerParamItt = this.transformerParams.iterator(); 192 193 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 ) transformerSourceItt.next(), 201 (Parameters) transformerParamItt.next()); 202 this.data.setSetupTime(index++, 203 System.currentTimeMillis()-time); 204 } 205 206 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 e) { 220 throw new ProcessingException("Could not setup pipeline.", e); 221 } catch (IOException e) { 222 throw new ProcessingException("Could not setup pipeline.", e); 223 } 224 } 225 226 233 public boolean process(Environment environment) 234 throws ProcessingException { 235 236 this.index = 0; 237 if (this.data!=null) { 238 this.data.setEnvironmentInfo(new EnvironmentInfo(environment)); 240 241 long time = System.currentTimeMillis(); 243 boolean result = super.process(environment); 244 245 this.data.setTotalTime(System.currentTimeMillis()-time); 246 247 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 259 protected boolean processXMLPipeline(Environment environment) throws ProcessingException { 260 this.index = 0; 261 if (this.data!=null) { 262 this.data.setEnvironmentInfo(new EnvironmentInfo(environment)); 264 265 long time = System.currentTimeMillis(); 267 boolean result = super.processXMLPipeline(environment); 268 269 this.data.setTotalTime(System.currentTimeMillis()-time); 270 271 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 283 protected boolean processReader(Environment environment) throws ProcessingException { 284 this.index = 0; 285 if (this.data!=null) { 286 this.data.setEnvironmentInfo(new EnvironmentInfo(environment)); 288 289 long time = System.currentTimeMillis(); 291 boolean result = super.processReader(environment); 292 293 this.data.setTotalTime(System.currentTimeMillis()-time); 294 295 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 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 |