001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.varia.stats;
023:
024: import java.io.Serializable;
025: import java.util.Map;
026: import java.util.HashMap;
027: import java.util.Iterator;
028:
029: /**
030: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
031: * @version <tt>$Revision: 57210 $</tt>
032: */
033: public class TxReport implements Serializable {
034: static final long serialVersionUID = 1144384523072827681L;
035: private static final String DEFAULT_NAME = "UNKNOWN";
036:
037: private String name = DEFAULT_NAME;
038: private final Map stats = new HashMap();
039: private int count = 1;
040:
041: public String getName() {
042: return name;
043: }
044:
045: public int getCount() {
046: return count;
047: }
048:
049: public Map getStats() {
050: return stats;
051: }
052:
053: public boolean addItem(StatisticalItem item) {
054: if (name == DEFAULT_NAME) {
055: name = item.getValue();
056: }
057:
058: boolean addedNew = false;
059: Map itemMap = (Map) stats.get(item.getName());
060: if (itemMap == null) {
061: itemMap = new HashMap();
062: stats.put(item.getName(), itemMap);
063: addedNew = true;
064: }
065:
066: StatisticalItem curItem = (StatisticalItem) itemMap.get(item
067: .getValue());
068: if (curItem == null) {
069: itemMap.put(item.getValue(), item);
070: } else {
071: curItem.add(item);
072: }
073: return addedNew;
074: }
075:
076: /**
077: * This method destroys txReport parameter!!! Not really a nice implementation.
078: */
079: public void merge(TxReport txReport) {
080: for (Iterator iter = txReport.stats.entrySet().iterator(); iter
081: .hasNext();) {
082: Map.Entry entry = (Map.Entry) iter.next();
083: String itemName = (String) entry.getKey();
084:
085: Map myMap = (Map) stats.get(itemName);
086: Map itemMap = (Map) entry.getValue();
087:
088: if (myMap == null) {
089: stats.put(itemName, itemMap);
090: } else {
091: // first merge common items
092: for (Iterator myItems = myMap.values().iterator(); myItems
093: .hasNext();) {
094: StatisticalItem myItem = (StatisticalItem) myItems
095: .next();
096: StatisticalItem newItem = (StatisticalItem) itemMap
097: .remove(myItem.getValue());
098:
099: if (newItem == null) {
100: myItem.mergeNull();
101: } else {
102: myItem.merge(newItem);
103: }
104: }
105:
106: // add new items
107: if (!itemMap.isEmpty()) {
108: for (Iterator newItems = itemMap.values()
109: .iterator(); newItems.hasNext();) {
110: StatisticalItem newItem = (StatisticalItem) newItems
111: .next();
112: myMap.put(newItem.getValue(), newItem);
113: }
114: }
115: }
116: }
117:
118: count += txReport.count;
119: }
120:
121: // Inner
122:
123: public static class MethodStats extends AbstractStatisticalItem {
124: public static final String NAME = "Method Statistics Per Transaction";
125:
126: public MethodStats(String method) {
127: super (NAME);
128: value = method;
129: }
130: }
131:
132: public static class SqlStats extends AbstractStatisticalItem {
133: public static final String NAME = "SQL Statistics Per Transaction";
134:
135: public SqlStats(String sql) {
136: super(NAME);
137: value = sql;
138: }
139: }
140: }
|