001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.profiling.generation;
018:
019: import java.io.IOException;
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.Comparator;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.avalon.framework.parameters.Parameters;
028: import org.apache.avalon.framework.service.ServiceException;
029: import org.apache.avalon.framework.service.ServiceManager;
030: import org.apache.cocoon.ProcessingException;
031: import org.apache.cocoon.environment.ObjectModelHelper;
032: import org.apache.cocoon.environment.Request;
033: import org.apache.cocoon.environment.SourceResolver;
034: import org.apache.cocoon.generation.ServiceableGenerator;
035: import org.apache.cocoon.profiling.statistics.Collector;
036: import org.apache.cocoon.profiling.statistics.PageReport;
037: import org.apache.cocoon.profiling.statistics.Report;
038: import org.apache.cocoon.profiling.statistics.Statistics;
039: import org.apache.cocoon.xml.AttributesImpl;
040: import org.apache.cocoon.xml.XMLUtils;
041: import org.xml.sax.SAXException;
042:
043: /**
044: * Statistic-Generator.
045: *
046: * @version $Id: StatisticsGenerator.java 485636 2006-12-11 12:20:15Z cziegeler $
047: * @since 2.1.10
048: */
049: public class StatisticsGenerator extends ServiceableGenerator {
050:
051: protected Collector collector;
052:
053: /**
054: * @see org.apache.cocoon.generation.AbstractGenerator#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
055: */
056: public void setup(SourceResolver resolver, Map objectModel,
057: String src, Parameters par) throws ProcessingException,
058: SAXException, IOException {
059: super .setup(resolver, objectModel, src, par);
060: final Request req = ObjectModelHelper.getRequest(objectModel);
061: if (req.getParameter("clear") != null) {
062: this .collector.clear();
063: }
064: if (req.getParameter("enable") != null) {
065: this .collector.setCollectingStatistics(true);
066: }
067: if (req.getParameter("disable") != null) {
068: this .collector.setCollectingStatistics(false);
069: }
070: }
071:
072: /**
073: * @see org.apache.cocoon.generation.Generator#generate()
074: */
075: public void generate() throws IOException, SAXException,
076: ProcessingException {
077: this .xmlConsumer.startDocument();
078: XMLUtils.startElement(this .xmlConsumer, "statistics");
079:
080: if (this .collector.isCollectingStatistics()) {
081: XMLUtils.startElement(this .xmlConsumer, "reports");
082: boolean found = false;
083: List entries = new ArrayList(this .collector.getStatistics());
084: Collections.sort(entries, new ReportComparator());
085: final Iterator i = entries.iterator();
086: while (i.hasNext()) {
087: found = true;
088: final Report report = (Report) i.next();
089: final AttributesImpl attrs = new AttributesImpl();
090: attrs.addCDATAAttribute("name", report.getCategory());
091: XMLUtils
092: .startElement(this .xmlConsumer, "report", attrs);
093: XMLUtils.createElement(this .xmlConsumer, "count",
094: String.valueOf(report.getCount()));
095: XMLUtils.createElement(this .xmlConsumer, "min", this
096: .getTime(report.getMin()));
097: XMLUtils.createElement(this .xmlConsumer, "max", this
098: .getTime(report.getMax()));
099: XMLUtils.createElement(this .xmlConsumer, "last", this
100: .getTime(report.getLast()));
101: XMLUtils.createElement(this .xmlConsumer, "all", report
102: .getAll());
103: XMLUtils.createElement(this .xmlConsumer, "average",
104: this .getTime(report.getAverage()));
105: XMLUtils.endElement(this .xmlConsumer, "report");
106: }
107: if (!found) {
108: XMLUtils.data(this .xmlConsumer, "No reports");
109: }
110: XMLUtils.endElement(this .xmlConsumer, "reports");
111: if (found) {
112: XMLUtils.startElement(this .xmlConsumer, "pages");
113: entries = new ArrayList(this .collector.getPageReports());
114: Collections.sort(entries, new PageReportComparator());
115: final Iterator pi = entries.iterator();
116: while (pi.hasNext()) {
117: final PageReport report = (PageReport) pi.next();
118: final AttributesImpl attrs = new AttributesImpl();
119: attrs.addCDATAAttribute("id", report.getId());
120: attrs.addCDATAAttribute("date", report.getDate()
121: .toString());
122: XMLUtils.startElement(this .xmlConsumer, "report",
123: attrs);
124: final Iterator si = report.getStatistics()
125: .iterator();
126: while (si.hasNext()) {
127: final Statistics stats = (Statistics) si.next();
128: attrs.clear();
129: attrs.addCDATAAttribute("name", stats
130: .getCategory());
131: attrs.addCDATAAttribute("duraration", String
132: .valueOf(stats.getDuration()));
133: XMLUtils.createElement(this .xmlConsumer,
134: "component", attrs);
135: }
136: XMLUtils.endElement(this .xmlConsumer, "report");
137: }
138: XMLUtils.endElement(this .xmlConsumer, "pages");
139: }
140: } else {
141: XMLUtils.data(this .xmlConsumer, "Turned off");
142: }
143: XMLUtils.endElement(this .xmlConsumer, "statistics");
144: this .xmlConsumer.endDocument();
145: }
146:
147: protected String getTime(long msecs) {
148: long secs = msecs / 1000;
149: StringBuffer buffer = new StringBuffer();
150: buffer.append(secs);
151: buffer.append('.');
152: long rest = (msecs - secs * 1000);
153: if (rest < 100) {
154: buffer.append('0');
155: }
156: if (rest < 10) {
157: buffer.append('0');
158: }
159: buffer.append(rest);
160: buffer.append('s');
161: return buffer.toString();
162: }
163:
164: /**
165: * @see org.apache.cocoon.generation.ServiceableGenerator#dispose()
166: */
167: public void dispose() {
168: if (this .manager != null) {
169: this .manager.release(this .collector);
170: this .collector = null;
171: }
172: super .dispose();
173: }
174:
175: /**
176: * @see org.apache.cocoon.generation.ServiceableGenerator#service(org.apache.avalon.framework.service.ServiceManager)
177: */
178: public void service(ServiceManager aManager)
179: throws ServiceException {
180: super .service(aManager);
181: this .collector = (Collector) this .manager
182: .lookup(Collector.class.getName());
183: }
184:
185: public static final class ReportComparator implements Comparator {
186:
187: public int compare(Object o1, Object o2) {
188: Report r1 = (Report) o1;
189: Report r2 = (Report) o2;
190: return r1.getCategory().compareTo(r2.getCategory());
191: }
192: }
193:
194: public static final class PageReportComparator implements
195: Comparator {
196:
197: public int compare(Object o1, Object o2) {
198: PageReport r1 = (PageReport) o1;
199: PageReport r2 = (PageReport) o2;
200: return r1.getId().compareTo(r2.getId());
201: }
202: }
203: }
|