001: /**
002: * AnagramsStats.java
003: *
004: * Created on Wed May 11 09:37:26 MEST 2005
005: */package com.toy.anagrams.mbeans;
006:
007: import javax.management.*;
008:
009: /**
010: * Class AnagramsStats
011: * Anagrams MBean
012: */
013: public class AnagramsStats implements AnagramsStatsMBean,
014: NotificationEmitter {
015: /** Attribute : LastThinkingTime */
016: private int lastThinkingTime = 0;
017:
018: /** Attribute : MaxThinkingTime */
019: private int maxThinkingTime = 0;
020:
021: /** Attribute : MinThinkingTime */
022: private int minThinkingTime = 0;
023:
024: /** Attribute : NumResolvedAnagrams */
025: private int NumResolvedAnagrams = 0;
026:
027: /** Attribute : CurrentAnagram */
028: private String currentAnagram = null;
029:
030: /* Creates a new instance of AnagramsStats */
031: public AnagramsStats() {
032: }
033:
034: /**
035: * Get The time it tooks for a user to resolve the last anagram
036: */
037: public int getLastThinkingTime() {
038: return lastThinkingTime;
039: }
040:
041: /**
042: * Get The maximum time it tooks for a user to resolve an anagram
043: */
044: public int getMaxThinkingTime() {
045: return maxThinkingTime;
046: }
047:
048: /**
049: * Get The minimum time it tooks for a user to resolve an anagram
050: */
051: public int getMinThinkingTime() {
052: return minThinkingTime;
053: }
054:
055: /**
056: * Get Number of resolved anagrams
057: */
058: public int getNumResolvedAnagrams() {
059: return NumResolvedAnagrams;
060: }
061:
062: /**
063: * Get The current anagram
064: */
065: public String getCurrentAnagram() {
066: return currentAnagram;
067: }
068:
069: /**
070: * Resety all thinking related counters
071: */
072: public void resetThinkingTimes() {
073: //TODO Add the operation implementation
074: minThinkingTime = 0;
075: maxThinkingTime = 0;
076: lastThinkingTime = 0;
077: }
078:
079: /**
080: * Methods exposed to Anagrams UI components to feed management with data.
081: */
082:
083: /*
084: * A new Anagram is porposed to the user. He starts thinking...
085: */
086: public void startThinking() {
087: startTime = System.currentTimeMillis();
088: }
089:
090: /*
091: * An Anagram has been resolved.
092: */
093: public void stopThinking() {
094:
095: //Update the number of resolved anagrams
096: NumResolvedAnagrams++;
097:
098: // Computes max and min
099: long stopTime = System.currentTimeMillis();
100: long oldMin = minThinkingTime;
101: lastThinkingTime = (int) (stopTime - startTime) / 1000;
102: minThinkingTime = (lastThinkingTime < minThinkingTime)
103: || minThinkingTime == 0 ? lastThinkingTime
104: : minThinkingTime;
105: maxThinkingTime = lastThinkingTime > maxThinkingTime ? lastThinkingTime
106: : maxThinkingTime;
107:
108: //Create a JMX Notification
109: Notification notification = new Notification(
110: AttributeChangeNotification.ATTRIBUTE_CHANGE, this ,
111: getNextSeqNumber(), "Anagram resolved!");
112:
113: // Send a JMX notification.
114: broadcaster.sendNotification(notification);
115: }
116:
117: /**
118: * Updates the current anagram coputed by the application
119: */
120: public void setCurrentAnagram(String currentAnagram) {
121: this .currentAnagram = currentAnagram;
122: }
123:
124: /**
125: * MBean Notification support
126: * You shouldn't update these methods
127: */
128: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
129: public void addNotificationListener(NotificationListener listener,
130: NotificationFilter filter, Object handback)
131: throws IllegalArgumentException {
132: broadcaster.addNotificationListener(listener, filter, handback);
133: }
134:
135: public MBeanNotificationInfo[] getNotificationInfo() {
136: return new MBeanNotificationInfo[] { new MBeanNotificationInfo(
137: new String[] { AttributeChangeNotification.ATTRIBUTE_CHANGE },
138: javax.management.Notification.class.getName(),
139: "When an anagram is resolved time") };
140: }
141:
142: public void removeNotificationListener(NotificationListener listener)
143: throws ListenerNotFoundException {
144: broadcaster.removeNotificationListener(listener);
145: }
146:
147: public void removeNotificationListener(
148: NotificationListener listener, NotificationFilter filter,
149: Object handback) throws ListenerNotFoundException {
150: broadcaster.removeNotificationListener(listener, filter,
151: handback);
152: }
153:
154: private synchronized long getNextSeqNumber() {
155: return seqNumber++;
156: }
157:
158: private long seqNumber;
159: // </editor-fold>
160:
161: private final NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
162:
163: //Stores the time a new anagram is proposed to the user.
164: private long startTime;
165: }
|