XmlMeasurementCategoryFactory.java

biz/hammurapi/metrics/XmlMeasurementCategoryFactory.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 089 Type is not documented 2 54:1
Java Inspector 089 Undocumented method 2 57:9
Java Inspector 089 Undocumented field 2 68:9
Java Inspector 089 Constructor is not properly documented 2 75:9
Java Inspector 089 Method is not properly documented 2 82:9
Java Inspector 089 Undocumented exception ConfigurationException 2 82:9
Java Inspector 089 Undocumented exception ConfigurationException 2 89:9
Java Inspector 089 Undocumented method 2 109:9
Java Inspector 089 Undocumented method 2 126:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 92:56
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 110:37
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 111:37
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 118:54
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 75:9

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.metrics;
24
25import java.io.File;
26import java.io.IOException;
27import java.util.Date;
28import java.util.Iterator;
29import java.util.Map;
30import java.util.TreeMap;
31
32import javax.xml.parsers.DocumentBuilderFactory;
33import javax.xml.parsers.FactoryConfigurationError;
34import javax.xml.parsers.ParserConfigurationException;
35import javax.xml.transform.TransformerException;
36
37import org.w3c.dom.DOMException;
38import org.w3c.dom.Document;
39import org.w3c.dom.Element;
40
41import biz.hammurapi.RuntimeException;
42import biz.hammurapi.config.Component;
43import biz.hammurapi.config.ConfigurationException;
44import biz.hammurapi.render.RenderRequest;
45import biz.hammurapi.render.RenderingException;
46import biz.hammurapi.xml.dom.DOMUtils;
47import biz.hammurapi.xml.dom.DomSerializable;
48
49
50/**
51 * @author Pavel Vlasov
52 * @revision $Revision$
53 */
54public class XmlMeasurementCategoryFactory extends MeasurementCategoryFactory implements Component, DomSerializable {
55 private Map categories=new TreeMap();
56
57 public MeasurementConsumer getMeasurementConsumer(String categoryName) {
58 synchronized (categories) {
59 MeasurementConsumer ret=(MeasurementConsumer) categories.get(categoryName);
60 if (ret==null) {
61 ret=new SimpleMeasurementConsumer();
62 categories.put(categoryName, ret);
63 }
64 return ret;
65 }
66 }
67
68 protected File out;
69 private long from;
70
71 /**
72 *
73 * @param out Output file.
74 */
75 public XmlMeasurementCategoryFactory(File out) {
76 this.out=out;
77 }
78
79 /**
80 *
81 */
82 public void start() throws ConfigurationException {
83 from=System.currentTimeMillis();
84 }
85
86 /**
87 * Saves collected metrics to XML.
88 */
89 public void stop() throws ConfigurationException {
90 try {
91 Document doc=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
92 Element root=doc.createElement("metrics");
93 doc.appendChild(root);
94 toDom(root);
95 DOMUtils.serialize(doc, out);
96 } catch (IOException e) {
97 throw new ConfigurationException(e);
98 } catch (TransformerException e) {
99 throw new ConfigurationException(e);
100 } catch (ParserConfigurationException e) {
101 throw new ConfigurationException(e);
102 } catch (FactoryConfigurationError e) {
103 throw new ConfigurationException(e);
104 } catch (DOMException e) {
105 throw new ConfigurationException(e);
106 }
107 }
108
109 public void toDom(Element holder) {
110 holder.setAttribute("from", new Date(from).toString());
111 holder.setAttribute("to", new Date().toString());
112 Iterator it=categories.entrySet().iterator();
113 while (it.hasNext()) {
114 Map.Entry entry=(Map.Entry) it.next();
115 MetricSourceRenderer mcr = new MetricSourceRenderer(new RenderRequest(entry.getValue()));
116 try {
117 Element element = mcr.render(holder.getOwnerDocument());
118 element.setAttribute("category", (String) entry.getKey());
119 holder.appendChild(element);
120 } catch (RenderingException e) {
121 throw new RuntimeException(e);
122 }
123 }
124 }
125
126 public void setOwner(Object owner) {
127 // Ignore
128 }
129}
130