KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > stats > StatsSupport


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.stats;
5
6 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
7
8 import com.tc.stats.statistics.Statistic;
9
10 import java.io.Serializable JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import javax.management.AttributeChangeNotification JavaDoc;
15 import javax.management.MBeanNotificationInfo JavaDoc;
16 import javax.management.NotificationBroadcasterSupport JavaDoc;
17
18 public class StatsSupport extends NotificationBroadcasterSupport JavaDoc implements Serializable JavaDoc {
19   private final Map JavaDoc m_stats = new HashMap JavaDoc();
20   private final SynchronizedLong sequenceNumber = new SynchronizedLong(0L);
21
22   public synchronized void addStatistic(String JavaDoc id, Statistic statistic) {
23     m_stats.put(id, statistic);
24   }
25
26   public synchronized Statistic getStatistic(String JavaDoc id) {
27     return (Statistic) m_stats.get(id);
28   }
29
30   public synchronized String JavaDoc[] getStatisticNames() {
31     return (String JavaDoc[]) m_stats.keySet().toArray(new String JavaDoc[m_stats.size()]);
32   }
33
34   public synchronized Statistic[] getStatistics() {
35     return (Statistic[]) m_stats.values().toArray(new Statistic[m_stats.size()]);
36   }
37
38   public MBeanNotificationInfo JavaDoc[] getNotificationInfo() {
39     String JavaDoc[] types = new String JavaDoc[] { AttributeChangeNotification.ATTRIBUTE_CHANGE };
40     String JavaDoc name = AttributeChangeNotification JavaDoc.class.getName();
41     String JavaDoc description = "An attribute of this MBean has changed";
42
43     return new MBeanNotificationInfo JavaDoc[] { new MBeanNotificationInfo JavaDoc(types, name, description) };
44   }
45
46   protected void sendNotification(String JavaDoc msg, String JavaDoc attr, String JavaDoc type, Object JavaDoc oldVal, Object JavaDoc newVal) {
47     sendNotification(new AttributeChangeNotification JavaDoc(this, sequenceNumber.increment(), System.currentTimeMillis(), msg,
48                                                      attr, type, oldVal, newVal));
49   }
50 }
51
Popular Tags