001 /* 002 @license.text@ 003 */ 004 package biz.hammurapi.metrics; 005 006 import java.io.File; 007 import java.io.IOException; 008 import java.util.Date; 009 import java.util.Iterator; 010 import java.util.Map; 011 import java.util.TreeMap; 012 013 import javax.xml.parsers.DocumentBuilderFactory; 014 import javax.xml.parsers.FactoryConfigurationError; 015 import javax.xml.parsers.ParserConfigurationException; 016 import javax.xml.transform.TransformerException; 017 018 import org.w3c.dom.DOMException; 019 import org.w3c.dom.Document; 020 import org.w3c.dom.Element; 021 022 import biz.hammurapi.RuntimeException; 023 import biz.hammurapi.config.Component; 024 import biz.hammurapi.config.ConfigurationException; 025 import biz.hammurapi.render.RenderRequest; 026 import biz.hammurapi.render.RenderingException; 027 import biz.hammurapi.xml.dom.DOMUtils; 028 import biz.hammurapi.xml.dom.DomSerializable; 029 030 031 /** 032 * @author Pavel Vlasov 033 * @revision $Revision$ 034 */ 035 public class XmlMeasurementCategoryFactory extends MeasurementCategoryFactory implements Component, DomSerializable { 036 private Map categories=new TreeMap(); 037 038 public MeasurementConsumer getMeasurementConsumer(String categoryName) { 039 synchronized (categories) { 040 MeasurementConsumer ret=(MeasurementConsumer) categories.get(categoryName); 041 if (ret==null) { 042 ret=new SimpleMeasurementConsumer(); 043 categories.put(categoryName, ret); 044 } 045 return ret; 046 } 047 } 048 049 protected File out; 050 private long from; 051 052 /** 053 * 054 * @param out Output file. 055 */ 056 public XmlMeasurementCategoryFactory(File out) { 057 this.out=out; 058 } 059 060 /** 061 * 062 */ 063 public void start() throws ConfigurationException { 064 from=System.currentTimeMillis(); 065 } 066 067 /** 068 * Saves collected metrics to XML. 069 */ 070 public void stop() throws ConfigurationException { 071 try { 072 Document doc=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 073 Element root=doc.createElement("metrics"); 074 doc.appendChild(root); 075 toDom(root); 076 DOMUtils.serialize(doc, out); 077 } catch (IOException e) { 078 throw new ConfigurationException(e); 079 } catch (TransformerException e) { 080 throw new ConfigurationException(e); 081 } catch (ParserConfigurationException e) { 082 throw new ConfigurationException(e); 083 } catch (FactoryConfigurationError e) { 084 throw new ConfigurationException(e); 085 } catch (DOMException e) { 086 throw new ConfigurationException(e); 087 } 088 } 089 090 public void toDom(Element holder) { 091 holder.setAttribute("from", new Date(from).toString()); 092 holder.setAttribute("to", new Date().toString()); 093 Iterator it=categories.entrySet().iterator(); 094 while (it.hasNext()) { 095 Map.Entry entry=(Map.Entry) it.next(); 096 MetricSourceRenderer mcr = new MetricSourceRenderer(new RenderRequest(entry.getValue())); 097 try { 098 Element element = mcr.render(holder.getOwnerDocument()); 099 element.setAttribute("category", (String) entry.getKey()); 100 holder.appendChild(element); 101 } catch (RenderingException e) { 102 throw new RuntimeException(e); 103 } 104 } 105 } 106 107 public void setOwner(Object owner) { 108 // Ignore 109 } 110 }