1 /*
2  * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 6273541
27  * @summary Test that the counter/gauge/string monitors emit a
28  *          jmx.monitor.error.type notification when the attribute
29  *          being monitored returns a non comparable value.
30  * @author Luis-Miguel Alventosa
31  * @run clean NonComparableAttributeValueTest
32  * @run build NonComparableAttributeValueTest
33  * @run main NonComparableAttributeValueTest
34  */
35 
36 import javax.management.*;
37 import javax.management.monitor.*;
38 
39 public class NonComparableAttributeValueTest implements NotificationListener {
40 
41     // Flag to notify that a message has been received
42     private volatile boolean messageReceived = false;
43 
44     // MBean class
45     public class ObservedObject implements ObservedObjectMBean {
getIntegerAttribute()46         public Object getIntegerAttribute() {
47             return new Object();
48         }
getStringAttribute()49         public Object getStringAttribute() {
50             return new Object();
51         }
52     }
53 
54     // MBean interface
55     public interface ObservedObjectMBean {
getIntegerAttribute()56         public Object getIntegerAttribute();
getStringAttribute()57         public Object getStringAttribute();
58     }
59 
60     // Notification handler
handleNotification(Notification notification, Object handback)61     public void handleNotification(Notification notification,
62                                    Object handback) {
63         MonitorNotification n = (MonitorNotification) notification;
64         echo("\tInside handleNotification...");
65         String type = n.getType();
66         try {
67             if (type.equals(
68                     MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR)) {
69                 echo("\t\t" + n.getObservedAttribute() + " is null");
70                 echo("\t\tDerived Gauge = " + n.getDerivedGauge());
71                 echo("\t\tTrigger = " + n.getTrigger());
72 
73                 synchronized (this) {
74                     messageReceived = true;
75                     notifyAll();
76                 }
77             } else {
78                 echo("\t\tSkipping notification of type: " + type);
79             }
80         } catch (Exception e) {
81             echo("\tError in handleNotification!");
82             e.printStackTrace(System.out);
83         }
84     }
85 
86     /**
87      * Update the counter and check for notifications
88      */
counterMonitorNotification()89     public int counterMonitorNotification() throws Exception {
90 
91         CounterMonitor counterMonitor = null;
92         try {
93             MBeanServer server = MBeanServerFactory.newMBeanServer();
94 
95             String domain = server.getDefaultDomain();
96 
97             // Create a new CounterMonitor MBean and add it to the MBeanServer.
98             //
99             echo(">>> CREATE a new CounterMonitor MBean");
100             ObjectName counterMonitorName = new ObjectName(
101                             domain + ":type=" + CounterMonitor.class.getName());
102             counterMonitor = new CounterMonitor();
103             server.registerMBean(counterMonitor, counterMonitorName);
104 
105             echo(">>> ADD a listener to the CounterMonitor");
106             counterMonitor.addNotificationListener(this, null, null);
107 
108             //
109             // MANAGEMENT OF A STANDARD MBEAN
110             //
111 
112             echo(">>> CREATE a new ObservedObject MBean");
113 
114             ObjectName obsObjName =
115                 ObjectName.getInstance(domain + ":type=ObservedObject");
116             ObservedObject obsObj = new ObservedObject();
117             server.registerMBean(obsObj, obsObjName);
118 
119             echo(">>> SET the attributes of the CounterMonitor:");
120 
121             counterMonitor.addObservedObject(obsObjName);
122             echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
123 
124             counterMonitor.setObservedAttribute("IntegerAttribute");
125             echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
126 
127             counterMonitor.setNotify(true);
128             echo("\tATTRIBUTE \"NotifyFlag\"        = true");
129 
130             Integer threshold = 2;
131             counterMonitor.setInitThreshold(threshold);
132             echo("\tATTRIBUTE \"Threshold\"         = " + threshold);
133 
134             int granularityperiod = 500;
135             counterMonitor.setGranularityPeriod(granularityperiod);
136             echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
137 
138             echo(">>> START the CounterMonitor");
139             counterMonitor.start();
140 
141             // Check if notification was received
142             //
143             doWait();
144             if (messageReceived) {
145                 echo("\tOK: CounterMonitor notification received");
146             } else {
147                 echo("\tKO: CounterMonitor notification missed or not emitted");
148                 return 1;
149             }
150         } finally {
151             if (counterMonitor != null)
152                 counterMonitor.stop();
153         }
154 
155         return 0;
156     }
157 
158     /**
159      * Update the gauge and check for notifications
160      */
gaugeMonitorNotification()161     public int gaugeMonitorNotification() throws Exception {
162 
163         GaugeMonitor gaugeMonitor = null;
164         try {
165             MBeanServer server = MBeanServerFactory.newMBeanServer();
166 
167             String domain = server.getDefaultDomain();
168 
169             // Create a new GaugeMonitor MBean and add it to the MBeanServer.
170             //
171             echo(">>> CREATE a new GaugeMonitor MBean");
172             ObjectName gaugeMonitorName = new ObjectName(
173                             domain + ":type=" + GaugeMonitor.class.getName());
174             gaugeMonitor = new GaugeMonitor();
175             server.registerMBean(gaugeMonitor, gaugeMonitorName);
176 
177             echo(">>> ADD a listener to the GaugeMonitor");
178             gaugeMonitor.addNotificationListener(this, null, null);
179 
180             //
181             // MANAGEMENT OF A STANDARD MBEAN
182             //
183 
184             echo(">>> CREATE a new ObservedObject MBean");
185 
186             ObjectName obsObjName =
187                 ObjectName.getInstance(domain + ":type=ObservedObject");
188             ObservedObject obsObj = new ObservedObject();
189             server.registerMBean(obsObj, obsObjName);
190 
191             echo(">>> SET the attributes of the GaugeMonitor:");
192 
193             gaugeMonitor.addObservedObject(obsObjName);
194             echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
195 
196             gaugeMonitor.setObservedAttribute("IntegerAttribute");
197             echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
198 
199             gaugeMonitor.setNotifyLow(false);
200             gaugeMonitor.setNotifyHigh(true);
201             echo("\tATTRIBUTE \"Notify Low  Flag\"  = false");
202             echo("\tATTRIBUTE \"Notify High Flag\"  = true");
203 
204             Double highThreshold = 3.0, lowThreshold = 2.5;
205             gaugeMonitor.setThresholds(highThreshold, lowThreshold);
206             echo("\tATTRIBUTE \"Low  Threshold\"    = " + lowThreshold);
207             echo("\tATTRIBUTE \"High Threshold\"    = " + highThreshold);
208 
209             int granularityperiod = 500;
210             gaugeMonitor.setGranularityPeriod(granularityperiod);
211             echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
212 
213             echo(">>> START the GaugeMonitor");
214             gaugeMonitor.start();
215 
216             // Check if notification was received
217             //
218             doWait();
219             if (messageReceived) {
220                 echo("\tOK: GaugeMonitor notification received");
221             } else {
222                 echo("\tKO: GaugeMonitor notification missed or not emitted");
223                 return 1;
224             }
225         } finally {
226             if (gaugeMonitor != null)
227                 gaugeMonitor.stop();
228         }
229 
230         return 0;
231     }
232 
233     /**
234      * Update the string and check for notifications
235      */
stringMonitorNotification()236     public int stringMonitorNotification() throws Exception {
237 
238         StringMonitor stringMonitor = null;
239         try {
240             MBeanServer server = MBeanServerFactory.newMBeanServer();
241 
242             String domain = server.getDefaultDomain();
243 
244             // Create a new StringMonitor MBean and add it to the MBeanServer.
245             //
246             echo(">>> CREATE a new StringMonitor MBean");
247             ObjectName stringMonitorName = new ObjectName(
248                             domain + ":type=" + StringMonitor.class.getName());
249             stringMonitor = new StringMonitor();
250             server.registerMBean(stringMonitor, stringMonitorName);
251 
252             echo(">>> ADD a listener to the StringMonitor");
253             stringMonitor.addNotificationListener(this, null, null);
254 
255             //
256             // MANAGEMENT OF A STANDARD MBEAN
257             //
258 
259             echo(">>> CREATE a new ObservedObject MBean");
260 
261             ObjectName obsObjName =
262                 ObjectName.getInstance(domain + ":type=ObservedObject");
263             ObservedObject obsObj = new ObservedObject();
264             server.registerMBean(obsObj, obsObjName);
265 
266             echo(">>> SET the attributes of the StringMonitor:");
267 
268             stringMonitor.addObservedObject(obsObjName);
269             echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
270 
271             stringMonitor.setObservedAttribute("StringAttribute");
272             echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute");
273 
274             stringMonitor.setNotifyMatch(true);
275             echo("\tATTRIBUTE \"NotifyMatch\"       = true");
276 
277             stringMonitor.setNotifyDiffer(false);
278             echo("\tATTRIBUTE \"NotifyDiffer\"      = false");
279 
280             stringMonitor.setStringToCompare("do_match_now");
281             echo("\tATTRIBUTE \"StringToCompare\"   = \"do_match_now\"");
282 
283             int granularityperiod = 500;
284             stringMonitor.setGranularityPeriod(granularityperiod);
285             echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
286 
287             echo(">>> START the StringMonitor");
288             stringMonitor.start();
289 
290             // Check if notification was received
291             //
292             doWait();
293             if (messageReceived) {
294                 echo("\tOK: StringMonitor notification received");
295             } else {
296                 echo("\tKO: StringMonitor notification missed or not emitted");
297                 return 1;
298             }
299         } finally {
300             if (stringMonitor != null)
301                 stringMonitor.stop();
302         }
303 
304         return 0;
305     }
306 
307     /**
308      * Test the monitor notifications.
309      */
monitorNotifications()310     public int monitorNotifications() throws Exception {
311         echo(">>> ----------------------------------------");
312         messageReceived = false;
313         int error = counterMonitorNotification();
314         echo(">>> ----------------------------------------");
315         messageReceived = false;
316         error += gaugeMonitorNotification();
317         echo(">>> ----------------------------------------");
318         messageReceived = false;
319         error += stringMonitorNotification();
320         echo(">>> ----------------------------------------");
321         return error;
322     }
323 
324     /*
325      * Print message
326      */
echo(String message)327     private static void echo(String message) {
328         System.out.println(message);
329     }
330 
331     /*
332      * Wait messageReceived to be true
333      */
doWait()334     synchronized void doWait() {
335         while (!messageReceived) {
336             try {
337                 wait();
338             } catch (InterruptedException e) {
339                 System.err.println("Got unexpected exception: " + e);
340                 e.printStackTrace();
341                 break;
342             }
343         }
344     }
345 
346     /*
347      * Standalone entry point.
348      *
349      * Run the test and report to stdout.
350      */
main(String args[])351     public static void main (String args[]) throws Exception {
352         NonComparableAttributeValueTest test = new NonComparableAttributeValueTest();
353         int error = test.monitorNotifications();
354         if (error > 0) {
355             echo(">>> Unhappy Bye, Bye!");
356             throw new IllegalStateException("Test FAILED: Didn't get all " +
357                                             "the notifications that were " +
358                                             "expected by the test!");
359         } else {
360             echo(">>> Happy Bye, Bye!");
361         }
362     }
363 }
364