KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jboss.tm.TransactionLocal;
25
26 import javax.transaction.Transaction JavaDoc;
27 import javax.transaction.Synchronization JavaDoc;
28 import javax.transaction.Status JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.HashSet JavaDoc;
34
35 /**
36  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
37  * @version <tt>$Revision: 37459 $</tt>
38  */

39 public class TxStatistics
40 {
41    private final Map JavaDoc reports = new HashMap JavaDoc();
42    private final Set JavaDoc collectedItemNames = new HashSet JavaDoc();
43
44    private final TransactionLocal txReport = new TransactionLocal()
45    {
46       protected Object JavaDoc initialValue()
47       {
48          Transaction JavaDoc tx = getTransaction();
49          TxReport report;
50          if(tx != null)
51          {
52             report = new TxReport();
53             try
54             {
55                tx.registerSynchronization(new TxSynchronization(report));
56             }
57             catch(Exception JavaDoc e)
58             {
59                e.printStackTrace();
60                throw new IllegalStateException JavaDoc("Failed to register tx synchronization: " + e.getMessage());
61             }
62          }
63          else
64          {
65             report = null;
66          }
67          return report;
68       }
69    };
70
71    public String JavaDoc[] getCollectedItemNames()
72    {
73       return (String JavaDoc[])collectedItemNames.toArray(new String JavaDoc[collectedItemNames.size()]);
74    }
75
76    public Iterator JavaDoc getReports()
77    {
78       return reports.values().iterator();
79    }
80
81    public synchronized void clear()
82    {
83       reports.clear();
84       collectedItemNames.clear();
85    }
86
87    public synchronized void addStatisticalItem(StatisticalItem item)
88    {
89       TxReport report = (TxReport) txReport.get();
90       if(report != null)
91       {
92          boolean addedNew = report.addItem(item);
93          if(addedNew)
94          {
95             collectedItemNames.add(item.getName());
96          }
97       }
98    }
99
100    private synchronized void addReport(TxReport report)
101    {
102       TxReport oldReport = (TxReport)reports.get(report.getName());
103       if(oldReport == null)
104       {
105          reports.put(report.getName(), report);
106       }
107       else
108       {
109          oldReport.merge(report);
110       }
111    }
112
113    public TxReport getReports(String JavaDoc reportName)
114    {
115       return (TxReport)reports.get(reportName);
116    }
117
118    // Inner
119

120    private class TxSynchronization implements Synchronization JavaDoc
121    {
122       private final TxReport report;
123
124       public TxSynchronization(TxReport report)
125       {
126          this.report = report;
127       }
128
129       public void beforeCompletion()
130       {
131       }
132
133       public void afterCompletion(int status)
134       {
135          if(status != Status.STATUS_ROLLEDBACK)
136          {
137             try
138             {
139                addReport(report);
140             }
141             catch(Exception JavaDoc e)
142             {
143                e.printStackTrace();
144             }
145          }
146       }
147    }
148 }
149
Popular Tags