1 22 package org.jboss.resource.statistic.pool; 23 24 import java.io.StringWriter ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.parsers.ParserConfigurationException ; 30 import javax.xml.transform.Source ; 31 import javax.xml.transform.TransformerException ; 32 import javax.xml.transform.TransformerFactory ; 33 import javax.xml.transform.dom.DOMSource ; 34 import javax.xml.transform.stream.StreamResult ; 35 36 import org.jboss.resource.statistic.JBossStatistics; 37 import org.jboss.resource.statistic.formatter.StatisticsFormatter; 38 import org.jboss.resource.statistic.formatter.StatisticsFormatterException; 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.Element ; 41 import org.w3c.dom.Text ; 42 43 49 public class JBossXmlSubPoolStatisticFormatter implements StatisticsFormatter 50 { 51 52 public Object formatSubPoolStatistics(Collection subPoolStatistics) 53 { 54 final Document doc = createDocument(); 55 final Element root = doc.createElement("subpool-statistics"); 56 doc.appendChild(root); 57 58 for(Iterator iter = subPoolStatistics.iterator(); iter.hasNext();){ 59 60 JBossSubPoolStatistics stat = (JBossSubPoolStatistics)iter.next(); 61 createChildNode(doc, root, stat); 62 63 } 64 65 return getDocumentAsString(doc); 66 } 67 68 public Object formatSubPoolStatistics(ManagedConnectionPoolStatistics stats) 69 { 70 return formatSubPoolStatistics(stats.getSubPools()); 71 } 72 73 private void createChildNode(Document doc, Element root, JBossSubPoolStatistics stat){ 74 75 root.appendChild(createTextNode(doc, "max-connections-in-use", String.valueOf(stat.getMaxConnectionsInUse()))); 76 root.appendChild(createTextNode(doc, "track-by-txn", String.valueOf(stat.getTrackByTxn()))); 77 root.appendChild(createTextNode(doc, "connections-in-use", String.valueOf(stat.getConnectionsInUse()))); 78 root.appendChild(createTextNode(doc, "connections-destroyed", String.valueOf(stat.getConnectionsDestroyed()))); 79 root.appendChild(createTextNode(doc, "available-connections", String.valueOf(stat.getAvailableConnections()))); 80 81 } 82 83 private Element createTextNode(Document doc, String name, String value){ 84 85 Text node = doc.createTextNode(name); 86 node.setNodeValue(String.valueOf(value)); 87 Element child = doc.createElement(name); 88 child.appendChild(node); 89 return child; 90 91 92 } 93 94 private Document createDocument(){ 95 96 try 97 { 98 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 99 return doc; 100 } 101 catch (ParserConfigurationException e) 102 { 103 throw new StatisticsFormatterException(e.getMessage()); 104 105 } 106 107 } 108 109 private String getDocumentAsString(Document doc){ 110 111 try 112 { 113 final Source source = new DOMSource (doc); 114 final StringWriter writer = new StringWriter (); 115 final StreamResult result = new StreamResult (writer); 116 TransformerFactory.newInstance().newTransformer().transform(source, result); 117 return writer.toString(); 118 } 119 catch (TransformerException e) 120 { 121 throw new StatisticsFormatterException(e.getMessage()); 122 123 } 124 125 } 126 127 public Object formatStatistics(JBossStatistics stats) 128 { 129 if(!(stats instanceof ManagedConnectionPoolStatistics)){ 130 131 throw new IllegalArgumentException ("Error: invalid statistics implementaiton for formatter."); 132 133 } 134 135 final ManagedConnectionPoolStatistics poolStats = (ManagedConnectionPoolStatistics)stats; 136 return formatSubPoolStatistics(poolStats); 137 } 138 139 } 140 | Popular Tags |