KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > statistic > pool > JBossXmlSubPoolStatisticFormatter


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.resource.statistic.pool;
23
24 import java.io.StringWriter JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
29 import javax.xml.parsers.ParserConfigurationException JavaDoc;
30 import javax.xml.transform.Source JavaDoc;
31 import javax.xml.transform.TransformerException JavaDoc;
32 import javax.xml.transform.TransformerFactory JavaDoc;
33 import javax.xml.transform.dom.DOMSource JavaDoc;
34 import javax.xml.transform.stream.StreamResult JavaDoc;
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 JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41 import org.w3c.dom.Text JavaDoc;
42
43 /**
44  * A XmlStatisticsFormatter.
45  *
46  * @author <a HREF="weston.price@jboss.com">Weston Price</a>
47  * @version $Revision: 44968 $
48  */

49 public class JBossXmlSubPoolStatisticFormatter implements StatisticsFormatter
50 {
51
52    public Object JavaDoc formatSubPoolStatistics(Collection JavaDoc subPoolStatistics)
53    {
54       final Document JavaDoc doc = createDocument();
55       final Element JavaDoc root = doc.createElement("subpool-statistics");
56       doc.appendChild(root);
57       
58       for(Iterator JavaDoc 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 JavaDoc formatSubPoolStatistics(ManagedConnectionPoolStatistics stats)
69    {
70       return formatSubPoolStatistics(stats.getSubPools());
71    }
72    
73    private void createChildNode(Document JavaDoc doc, Element JavaDoc 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 JavaDoc createTextNode(Document JavaDoc doc, String JavaDoc name, String JavaDoc value){
84       
85       Text JavaDoc node = doc.createTextNode(name);
86       node.setNodeValue(String.valueOf(value));
87       Element JavaDoc child = doc.createElement(name);
88       child.appendChild(node);
89       return child;
90       
91       
92    }
93   
94    private Document JavaDoc createDocument(){
95       
96       try
97       {
98          Document JavaDoc doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
99          return doc;
100       }
101       catch (ParserConfigurationException JavaDoc e)
102       {
103          throw new StatisticsFormatterException(e.getMessage());
104          
105       }
106       
107    }
108    
109    private String JavaDoc getDocumentAsString(Document JavaDoc doc){
110       
111       try
112       {
113          final Source JavaDoc source = new DOMSource JavaDoc(doc);
114          final StringWriter JavaDoc writer = new StringWriter JavaDoc();
115          final StreamResult JavaDoc result = new StreamResult JavaDoc(writer);
116          TransformerFactory.newInstance().newTransformer().transform(source, result);
117          return writer.toString();
118       }
119       catch (TransformerException JavaDoc e)
120       {
121          throw new StatisticsFormatterException(e.getMessage());
122          
123       }
124             
125    }
126
127    public Object JavaDoc formatStatistics(JBossStatistics stats)
128    {
129       if(!(stats instanceof ManagedConnectionPoolStatistics)){
130
131          throw new IllegalArgumentException JavaDoc("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