KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > varia > stats > TxReport


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.varia.stats;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 /**
30  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
31  * @version <tt>$Revision: 37459 $</tt>
32  */

33 public class TxReport
34    implements Serializable JavaDoc
35 {
36    private static final String JavaDoc DEFAULT_NAME = "UNKNOWN";
37
38    private String JavaDoc name = DEFAULT_NAME;
39    private final Map JavaDoc stats = new HashMap JavaDoc();
40    private int count = 1;
41
42    public String JavaDoc getName()
43    {
44       return name;
45    }
46
47    public int getCount()
48    {
49       return count;
50    }
51
52    public Map JavaDoc getStats()
53    {
54       return stats;
55    }
56
57    public boolean addItem(StatisticalItem item)
58    {
59       if(name == DEFAULT_NAME)
60       {
61          name = item.getValue();
62       }
63
64       boolean addedNew = false;
65       Map JavaDoc itemMap = (Map JavaDoc) stats.get(item.getName());
66       if(itemMap == null)
67       {
68          itemMap = new HashMap JavaDoc();
69          stats.put(item.getName(), itemMap);
70          addedNew = true;
71       }
72
73       StatisticalItem curItem = (StatisticalItem) itemMap.get(item.getValue());
74       if(curItem == null)
75       {
76          itemMap.put(item.getValue(), item);
77       }
78       else
79       {
80          curItem.add(item);
81       }
82       return addedNew;
83    }
84
85    /**
86     * This method destroys txReport parameter!!! Not really a nice implementation.
87     */

88    public void merge(TxReport txReport)
89    {
90       for(Iterator JavaDoc iter = txReport.stats.entrySet().iterator(); iter.hasNext();)
91       {
92          Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
93          String JavaDoc itemName = (String JavaDoc) entry.getKey();
94
95          Map JavaDoc myMap = (Map JavaDoc) stats.get(itemName);
96          Map JavaDoc itemMap = (Map JavaDoc) entry.getValue();
97
98          if(myMap == null)
99          {
100             stats.put(itemName, itemMap);
101          }
102          else
103          {
104             // first merge common items
105
for(Iterator JavaDoc myItems = myMap.values().iterator(); myItems.hasNext();)
106             {
107                StatisticalItem myItem = (StatisticalItem) myItems.next();
108                StatisticalItem newItem = (StatisticalItem) itemMap.remove(myItem.getValue());
109
110                if(newItem == null)
111                {
112                   myItem.mergeNull();
113                }
114                else
115                {
116                   myItem.merge(newItem);
117                }
118             }
119
120             // add new items
121
if(!itemMap.isEmpty())
122             {
123                for(Iterator JavaDoc newItems = itemMap.values().iterator(); newItems.hasNext();)
124                {
125                   StatisticalItem newItem = (StatisticalItem) newItems.next();
126                   myMap.put(newItem.getValue(), newItem);
127                }
128             }
129          }
130       }
131
132       count += txReport.count;
133    }
134
135    // Inner
136

137    public static class MethodStats extends AbstractStatisticalItem
138    {
139       public static final String JavaDoc NAME = "Method Statistics Per Transaction";
140
141       public MethodStats(String JavaDoc method)
142       {
143          super(NAME);
144          value = method;
145       }
146    }
147
148    public static class SqlStats extends AbstractStatisticalItem
149    {
150       public static final String JavaDoc NAME = "SQL Statistics Per Transaction";
151
152       public SqlStats(String JavaDoc sql)
153       {
154          super(NAME);
155          value = sql;
156       }
157    }
158 }
159
Popular Tags