01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.stats;
05:
06: import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
07:
08: import com.tc.stats.statistics.Statistic;
09:
10: import java.io.Serializable;
11: import java.util.HashMap;
12: import java.util.Map;
13:
14: import javax.management.AttributeChangeNotification;
15: import javax.management.MBeanNotificationInfo;
16: import javax.management.NotificationBroadcasterSupport;
17:
18: public class StatsSupport extends NotificationBroadcasterSupport
19: implements Serializable {
20: private final Map m_stats = new HashMap();
21: private final SynchronizedLong sequenceNumber = new SynchronizedLong(
22: 0L);
23:
24: public synchronized void addStatistic(String id, Statistic statistic) {
25: m_stats.put(id, statistic);
26: }
27:
28: public synchronized Statistic getStatistic(String id) {
29: return (Statistic) m_stats.get(id);
30: }
31:
32: public synchronized String[] getStatisticNames() {
33: return (String[]) m_stats.keySet().toArray(
34: new String[m_stats.size()]);
35: }
36:
37: public synchronized Statistic[] getStatistics() {
38: return (Statistic[]) m_stats.values().toArray(
39: new Statistic[m_stats.size()]);
40: }
41:
42: public MBeanNotificationInfo[] getNotificationInfo() {
43: String[] types = new String[] { AttributeChangeNotification.ATTRIBUTE_CHANGE };
44: String name = AttributeChangeNotification.class.getName();
45: String description = "An attribute of this MBean has changed";
46:
47: return new MBeanNotificationInfo[] { new MBeanNotificationInfo(
48: types, name, description) };
49: }
50:
51: protected void sendNotification(String msg, String attr,
52: String type, Object oldVal, Object newVal) {
53: sendNotification(new AttributeChangeNotification(this,
54: sequenceNumber.increment(), System.currentTimeMillis(),
55: msg, attr, type, oldVal, newVal));
56: }
57: }
|