001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.metrics;
024:
025: import java.io.File;
026: import java.io.IOException;
027: import java.util.Date;
028: import java.util.Iterator;
029: import java.util.Map;
030: import java.util.TreeMap;
031:
032: import javax.xml.parsers.DocumentBuilderFactory;
033: import javax.xml.parsers.FactoryConfigurationError;
034: import javax.xml.parsers.ParserConfigurationException;
035: import javax.xml.transform.TransformerException;
036:
037: import org.w3c.dom.DOMException;
038: import org.w3c.dom.Document;
039: import org.w3c.dom.Element;
040:
041: import biz.hammurapi.RuntimeException;
042: import biz.hammurapi.config.Component;
043: import biz.hammurapi.config.ConfigurationException;
044: import biz.hammurapi.render.RenderRequest;
045: import biz.hammurapi.render.RenderingException;
046: import biz.hammurapi.xml.dom.DOMUtils;
047: import biz.hammurapi.xml.dom.DomSerializable;
048:
049: /**
050: * @author Pavel Vlasov
051: * @revision $Revision$
052: */
053: public class XmlMeasurementCategoryFactory extends
054: MeasurementCategoryFactory implements Component,
055: DomSerializable {
056: private Map categories = new TreeMap();
057:
058: public MeasurementConsumer getMeasurementConsumer(
059: String categoryName) {
060: synchronized (categories) {
061: MeasurementConsumer ret = (MeasurementConsumer) categories
062: .get(categoryName);
063: if (ret == null) {
064: ret = new SimpleMeasurementConsumer();
065: categories.put(categoryName, ret);
066: }
067: return ret;
068: }
069: }
070:
071: protected File out;
072: private long from;
073:
074: /**
075: *
076: * @param out Output file.
077: */
078: public XmlMeasurementCategoryFactory(File out) {
079: this .out = out;
080: }
081:
082: /**
083: *
084: */
085: public void start() throws ConfigurationException {
086: from = System.currentTimeMillis();
087: }
088:
089: /**
090: * Saves collected metrics to XML.
091: */
092: public void stop() throws ConfigurationException {
093: try {
094: Document doc = DocumentBuilderFactory.newInstance()
095: .newDocumentBuilder().newDocument();
096: Element root = doc.createElement("metrics");
097: doc.appendChild(root);
098: toDom(root);
099: DOMUtils.serialize(doc, out);
100: } catch (IOException e) {
101: throw new ConfigurationException(e);
102: } catch (TransformerException e) {
103: throw new ConfigurationException(e);
104: } catch (ParserConfigurationException e) {
105: throw new ConfigurationException(e);
106: } catch (FactoryConfigurationError e) {
107: throw new ConfigurationException(e);
108: } catch (DOMException e) {
109: throw new ConfigurationException(e);
110: }
111: }
112:
113: public void toDom(Element holder) {
114: holder.setAttribute("from", new Date(from).toString());
115: holder.setAttribute("to", new Date().toString());
116: Iterator it = categories.entrySet().iterator();
117: while (it.hasNext()) {
118: Map.Entry entry = (Map.Entry) it.next();
119: MetricSourceRenderer mcr = new MetricSourceRenderer(
120: new RenderRequest(entry.getValue()));
121: try {
122: Element element = mcr.render(holder.getOwnerDocument());
123: element.setAttribute("category", (String) entry
124: .getKey());
125: holder.appendChild(element);
126: } catch (RenderingException e) {
127: throw new RuntimeException(e);
128: }
129: }
130: }
131:
132: public void setOwner(Object owner) {
133: // Ignore
134: }
135: }
|