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.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 ; 32 33 39 public class ProfilingCachingProcessingPipeline extends CachingProcessingPipeline { 40 41 private Profiler profiler; 42 43 private ProfilerData data; 44 45 private int index; 46 47 52 public void compose(ComponentManager manager) throws ComponentException { 53 super.compose(manager); 54 this.profiler = (Profiler) manager.lookup(Profiler.ROLE); 55 } 56 57 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 71 public void recycle() { 72 this.data = null; 73 this.index = 0; 74 super.recycle(); 75 } 76 77 90 public void setGenerator(String role, String 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 115 public void addTransformer(String role, String 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 137 public void setSerializer(String role, String source, Parameters param, 138 Parameters hintParam, 139 String 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 158 public void setReader(String role, String source, Parameters param, 159 String 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 174 protected void setupPipeline(Environment environment) 175 throws ProcessingException { 176 try { 177 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 transformerItt = this.transformers.iterator(); 184 Iterator transformerSourceItt = this.transformerSources.iterator(); 185 Iterator transformerParamItt = this.transformerParams.iterator(); 186 187 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 ) transformerSourceItt.next(), 195 (Parameters) transformerParamItt.next()); 196 this.data.setSetupTime(index++, System.currentTimeMillis() - time); 197 } 198 199 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 e) { 213 throw new ProcessingException("Could not setup pipeline.", e); 214 } catch (IOException e) { 215 throw new ProcessingException("Could not setup pipeline.", e); 216 } 217 218 generateCachingKey(environment); 220 221 if (this.toCacheKey!=null) { 223 validatePipeline(environment); 224 } 225 226 setupValidities(); 227 } 228 229 236 public boolean process(Environment environment) 237 throws ProcessingException { 238 this.index = 0; 239 if (this.data != null) { 240 this.data.setEnvironmentInfo(new EnvironmentInfo(environment)); 242 243 long time = System.currentTimeMillis(); 245 boolean result = super.process(environment); 246 this.data.setTotalTime(System.currentTimeMillis() - time); 247 248 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 261 protected boolean processXMLPipeline(Environment environment) 262 throws ProcessingException { 263 this.index = 0; 264 if (this.data != null) { 265 this.data.setEnvironmentInfo(new EnvironmentInfo(environment)); 267 268 long time = System.currentTimeMillis(); 270 boolean result = super.processXMLPipeline(environment); 271 this.data.setTotalTime(System.currentTimeMillis() - time); 272 273 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 285 protected boolean processReader(Environment environment) 286 throws ProcessingException { 287 this.index = 0; 288 if (this.data != null) { 289 this.data.setEnvironmentInfo(new EnvironmentInfo(environment)); 291 292 long time = System.currentTimeMillis(); 294 boolean result = super.processReader(environment); 295 this.data.setTotalTime(System.currentTimeMillis() - time); 296 297 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 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 |