KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > selfmanagement > event > StringStatisticMonitor


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * StringStatisticMonitor.java
26  *
27  * Created on July 11, 2005 3:00 PM
28  */

29
30 package com.sun.enterprise.admin.selfmanagement.event;
31
32 import javax.management.ObjectName JavaDoc;
33 import javax.management.MBeanNotificationInfo JavaDoc;
34 import com.sun.appserv.management.event.StatisticMonitorNotification;
35 import static com.sun.appserv.management.event.StatisticMonitorNotification.*;
36 import java.util.logging.Logger JavaDoc;
37 import java.util.logging.Level JavaDoc;
38 import com.sun.logging.LogDomains;
39
40 /**
41  * Defines a statistic monitor MBean designed to observe the values of a string
42  * attribute.
43  * <P>
44  * A string statistic monitor sends notifications as follows:
45  * <UL>
46  * <LI> if the attribute value matches the string to compare value,
47  * a {@link StatisticMonitorNotification#STRING_TO_COMPARE_VALUE_MATCHED
48  * match notification} is sent.
49  * The notify match flag must be set to <CODE>true</CODE>.
50  * <BR>Subsequent matchings of the string to compare values do not
51  * cause further notifications unless
52  * the attribute value differs from the string to compare value.
53  * <LI> if the attribute value differs from the string to compare value,
54  * a {@link StatisticMonitorNotification#STRING_TO_COMPARE_VALUE_DIFFERED
55  * differ notification} is sent.
56  * The notify differ flag must be set to <CODE>true</CODE>.
57  * <BR>Subsequent differences from the string to compare value do
58  * not cause further notifications unless
59  * the attribute value matches the string to compare value.
60  * </UL>
61  * Used for JDK version greater than 1.5
62  * @author Sun Microsystems, Inc
63  */

64 public class StringStatisticMonitor extends StatisticMonitor implements StringStatisticMonitorMBean {
65
66     //LOGGER
67
//---------------
68
protected static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.SELF_MANAGEMENT_LOGGER);
69
70     /*
71      * ------------------------------------------
72      * PRIVATE VARIABLES
73      * ------------------------------------------
74      */

75
76     /**
77      * String to compare with the observed attribute.
78      * <BR>The default value is an empty character sequence.
79      */

80     private String JavaDoc stringToCompare = "";
81
82     /**
83      * Flag indicating if the string statistic monitor notifies when matching
84      * the string to compare.
85      * <BR>The default value is set to <CODE>false</CODE>.
86      */

87     private boolean notifyMatch = false;
88
89     /**
90      * Flag indicating if the string statistic monitor notifies when differing
91      * from the string to compare.
92      * <BR>The default value is set to <CODE>false</CODE>.
93      */

94     private boolean notifyDiffer = false;
95
96     /**
97      * This attribute is used to handle the matching/differing mechanism.
98      * <BR>Each element in this array corresponds to an observed object
99      * in the list.
100      */

101     private int status[] = new int[capacityIncrement];
102
103     private static final String JavaDoc[] types = {
104         RUNTIME_ERROR,
105         OBSERVED_OBJECT_ERROR,
106         OBSERVED_ATTRIBUTE_ERROR,
107         OBSERVED_ATTRIBUTE_TYPE_ERROR,
108         STRING_TO_COMPARE_VALUE_MATCHED,
109         STRING_TO_COMPARE_VALUE_DIFFERED
110     };
111
112     private static final MBeanNotificationInfo JavaDoc[] notifsInfo = {
113         new MBeanNotificationInfo JavaDoc(
114             types,
115             "com.sun.appserv.management.event.StatisticMonitorNotification",
116             "Notifications sent by the StringStatisticMonitor MBean")
117     };
118
119     // Flags needed to implement the matching/differing mechanism.
120
//
121
private static final int MATCHING = 0;
122     private static final int DIFFERING = 1;
123     private static final int MATCHING_OR_DIFFERING = 2;
124
125     /*
126      * ------------------------------------------
127      * CONSTRUCTORS
128      * ------------------------------------------
129      */

130
131     /**
132      * Default constructor.
133      */

134     public StringStatisticMonitor() {
135     }
136
137     /*
138      * ------------------------------------------
139      * PUBLIC METHODS
140      * ------------------------------------------
141      */

142
143     /**
144      * Starts the string monitor.
145      */

146     public synchronized void start() {
147         // Reset values.
148
//
149
for (int i = 0; i < elementCount; i++) {
150             status[i] = MATCHING_OR_DIFFERING;
151         }
152         doStart();
153     }
154
155     /**
156      * Stops the string monitor.
157      */

158     public synchronized void stop() {
159         doStop();
160     }
161
162     // GETTERS AND SETTERS
163
//--------------------
164

165     /**
166      * Gets the derived gauge of the specified object, if this object is
167      * contained in the set of observed MBeans, or <code>null</code> otherwise.
168      *
169      * @param object the name of the MBean whose derived gauge is required.
170      *
171      * @return The derived gauge of the specified object.
172      *
173      * @since.unbundled JMX 1.2
174      */

175     public synchronized String JavaDoc getDerivedGauge(ObjectName JavaDoc object) {
176         return (String JavaDoc) super.getDerivedGauge(object);
177     }
178
179     /**
180      * Gets the derived gauge timestamp of the specified object, if
181      * this object is contained in the set of observed MBeans, or
182      * <code>null</code> otherwise.
183      *
184      * @param object the name of the object whose derived gauge
185      * timestamp is to be returned.
186      *
187      * @return The derived gauge timestamp of the specified object.
188      *
189      * @since.unbundled JMX 1.2
190      */

191     public synchronized long getDerivedGaugeTimeStamp(ObjectName JavaDoc object) {
192         return super.getDerivedGaugeTimeStamp(object);
193     }
194
195     /**
196      * Returns the derived gauge of the first object in the set of
197      * observed MBeans.
198      *
199      * @return The derived gauge.
200      *
201      * @deprecated As of JMX 1.2, replaced by
202      * {@link #getDerivedGauge(ObjectName)}
203      */

204     @Deprecated JavaDoc
205     public synchronized String JavaDoc getDerivedGauge() {
206         return (String JavaDoc) derivedGauge[0];
207     }
208
209     /**
210      * Gets the derived gauge timestamp of the first object in the set
211      * of observed MBeans.
212      *
213      * @return The derived gauge timestamp.
214      *
215      * @deprecated As of JMX 1.2, replaced by
216      * {@link #getDerivedGaugeTimeStamp(ObjectName)}
217      */

218     @Deprecated JavaDoc
219     public synchronized long getDerivedGaugeTimeStamp() {
220         return derivedGaugeTimestamp[0];
221     }
222
223     /**
224      * Gets the string to compare with the observed attribute common
225      * to all observed MBeans.
226      *
227      * @return The string value.
228      *
229      * @see #setStringToCompare
230      */

231     public synchronized String JavaDoc getStringToCompare() {
232         return stringToCompare;
233     }
234
235     /**
236      * Sets the string to compare with the observed attribute common
237      * to all observed MBeans.
238      *
239      * @param value The string value.
240      *
241      * @exception IllegalArgumentException The specified
242      * string to compare is null.
243      *
244      * @see #getStringToCompare
245      */

246     public synchronized void setStringToCompare(String JavaDoc value)
247             throws IllegalArgumentException JavaDoc {
248
249         if (value == null) {
250             throw new IllegalArgumentException JavaDoc("Null string to compare");
251         }
252
253         stringToCompare = value;
254
255         // Reset values.
256
//
257
for (int i = 0; i < elementCount; i++) {
258           status[i] = MATCHING_OR_DIFFERING;
259         }
260     }
261
262     /**
263      * Gets the matching notification's on/off switch value common to
264      * all observed MBeans.
265      *
266      * @return <CODE>true</CODE> if the string statistic monitor notifies when
267      * matching the string to compare, <CODE>false</CODE> otherwise.
268      *
269      * @see #setNotifyMatch
270      */

271     public synchronized boolean getNotifyMatch() {
272         return notifyMatch;
273     }
274
275     /**
276      * Sets the matching notification's on/off switch value common to
277      * all observed MBeans.
278      *
279      * @param value The matching notification's on/off switch value.
280      *
281      * @see #getNotifyMatch
282      */

283     public synchronized void setNotifyMatch(boolean value) {
284         notifyMatch = value;
285     }
286
287     /**
288      * Gets the differing notification's on/off switch value common to
289      * all observed MBeans.
290      *
291      * @return <CODE>true</CODE> if the string statistic monitor notifies when
292      * differing from the string to compare, <CODE>false</CODE> otherwise.
293      *
294      * @see #setNotifyDiffer
295      */

296     public synchronized boolean getNotifyDiffer() {
297         return notifyDiffer;
298     }
299
300     /**
301      * Sets the differing notification's on/off switch value common to
302      * all observed MBeans.
303      *
304      * @param value The differing notification's on/off switch value.
305      *
306      * @see #getNotifyDiffer
307      */

308     public synchronized void setNotifyDiffer(boolean value) {
309         notifyDiffer = value;
310     }
311
312     /**
313      * Returns a <CODE>NotificationInfo</CODE> object containing the name of
314      * the Java class of the notification and the notification types sent by
315      * the string statistic monitor.
316      */

317     public MBeanNotificationInfo JavaDoc[] getNotificationInfo() {
318         return notifsInfo;
319     }
320
321     /*
322      * ------------------------------------------
323      * PACKAGE METHODS
324      * ------------------------------------------
325      */

326
327     /**
328      * Check that the type of the supplied observed attribute
329      * value is one of the value types supported by this monitor.
330      */

331     boolean isComparableTypeValid(ObjectName JavaDoc object,
332                                   String JavaDoc attribute,
333                                   Comparable JavaDoc<?> value) {
334         // Check that the observed attribute is of type "String".
335
//
336
if (value instanceof String JavaDoc) {
337             return true;
338         }
339         return false;
340     }
341
342     void onErrorNotification(StatisticMonitorNotification notification) {
343         int index = indexOf(notification.getObservedObject());
344         synchronized(this) {
345             // Reset values.
346
//
347
status[index] = MATCHING_OR_DIFFERING;
348         }
349     }
350
351     StatisticMonitorNotification buildAlarmNotification(ObjectName JavaDoc object,
352                                                String JavaDoc attribute,
353                                                Comparable JavaDoc<?> value) {
354         String JavaDoc type = null;
355         String JavaDoc msg = null;
356         Object JavaDoc trigger = null;
357
358         int index = indexOf(object);
359
360         synchronized(this) {
361
362             // Send matching notification if notifyMatch is true.
363
// Send differing notification if notifyDiffer is true.
364
//
365
if (status[index] == MATCHING_OR_DIFFERING) {
366                 if (derivedGauge[index].equals(stringToCompare)) {
367                     if (notifyMatch) {
368                         type = STRING_TO_COMPARE_VALUE_MATCHED;
369                         msg = "";
370                         trigger = stringToCompare;
371                     }
372                     status[index] = DIFFERING;
373                 } else {
374                     if (notifyDiffer) {
375                         type = STRING_TO_COMPARE_VALUE_DIFFERED;
376                         msg = "";
377                         trigger = stringToCompare;
378                     }
379                     status[index] = MATCHING;
380                 }
381             } else {
382                 if (status[index] == MATCHING) {
383                     if (derivedGauge[index].equals(stringToCompare)) {
384                         if (notifyMatch) {
385                             type = STRING_TO_COMPARE_VALUE_MATCHED;
386                             msg = "";
387                             trigger = stringToCompare;
388                         }
389                         status[index] = DIFFERING;
390                     }
391                 } else if (status[index] == DIFFERING) {
392                     if (!derivedGauge[index].equals(stringToCompare)) {
393                         if (notifyDiffer) {
394                             type = STRING_TO_COMPARE_VALUE_DIFFERED;
395                             msg = "";
396                             trigger = stringToCompare;
397                         }
398                         status[index] = MATCHING;
399                     }
400                 }
401             }
402         }
403
404         return new StatisticMonitorNotification(type,
405                                        this,
406                                        0,
407                                        0,
408                                        msg,
409                                        null,
410                                        null,
411                                        null,
412                                        trigger);
413     }
414
415     /**
416      * This method is called when adding a new observed object in the vector.
417      * It updates all the string specific arrays.
418      * @param index The index of the observed object.
419      */

420     synchronized void insertSpecificElementAt(int index) {
421         // Update status array.
422
//
423
if (elementCount >= status.length) {
424             status = expandArray(status);
425         }
426         status[index] = MATCHING_OR_DIFFERING;
427     }
428
429     /**
430      * This method is called when removing an observed object from the vector.
431      * It updates all the string specific arrays.
432      * @param index The index of the observed object.
433      */

434     synchronized void removeSpecificElementAt(int index) {
435         // Update status array.
436
//
437
removeElementAt(status, index);
438     }
439 }
440
Popular Tags