1 /* 2 * Copyright (c) 2005, 2015, 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 * 32 * @run clean NonComparableAttributeValueTest 33 * @run build NonComparableAttributeValueTest 34 * @run main NonComparableAttributeValueTest 35 */ 36 37 import javax.management.*; 38 import javax.management.monitor.*; 39 40 public class NonComparableAttributeValueTest implements NotificationListener { 41 42 // Flag to notify that a message has been received 43 private volatile boolean messageReceived = false; 44 45 // MBean class 46 public class ObservedObject implements ObservedObjectMBean { getIntegerAttribute()47 public Object getIntegerAttribute() { 48 return new Object(); 49 } getStringAttribute()50 public Object getStringAttribute() { 51 return new Object(); 52 } 53 } 54 55 // MBean interface 56 public interface ObservedObjectMBean { getIntegerAttribute()57 public Object getIntegerAttribute(); getStringAttribute()58 public Object getStringAttribute(); 59 } 60 61 // Notification handler handleNotification(Notification notification, Object handback)62 public void handleNotification(Notification notification, 63 Object handback) { 64 MonitorNotification n = (MonitorNotification) notification; 65 echo("\tInside handleNotification..."); 66 String type = n.getType(); 67 try { 68 if (type.equals( 69 MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR)) { 70 echo("\t\t" + n.getObservedAttribute() + " is null"); 71 echo("\t\tDerived Gauge = " + n.getDerivedGauge()); 72 echo("\t\tTrigger = " + n.getTrigger()); 73 74 synchronized (this) { 75 messageReceived = true; 76 notifyAll(); 77 } 78 } else { 79 echo("\t\tSkipping notification of type: " + type); 80 } 81 } catch (Exception e) { 82 echo("\tError in handleNotification!"); 83 e.printStackTrace(System.out); 84 } 85 } 86 87 /** 88 * Update the counter and check for notifications 89 */ counterMonitorNotification()90 public int counterMonitorNotification() throws Exception { 91 92 CounterMonitor counterMonitor = null; 93 try { 94 MBeanServer server = MBeanServerFactory.newMBeanServer(); 95 96 String domain = server.getDefaultDomain(); 97 98 // Create a new CounterMonitor MBean and add it to the MBeanServer. 99 // 100 echo(">>> CREATE a new CounterMonitor MBean"); 101 ObjectName counterMonitorName = new ObjectName( 102 domain + ":type=" + CounterMonitor.class.getName()); 103 counterMonitor = new CounterMonitor(); 104 server.registerMBean(counterMonitor, counterMonitorName); 105 106 echo(">>> ADD a listener to the CounterMonitor"); 107 counterMonitor.addNotificationListener(this, null, null); 108 109 // 110 // MANAGEMENT OF A STANDARD MBEAN 111 // 112 113 echo(">>> CREATE a new ObservedObject MBean"); 114 115 ObjectName obsObjName = 116 ObjectName.getInstance(domain + ":type=ObservedObject"); 117 ObservedObject obsObj = new ObservedObject(); 118 server.registerMBean(obsObj, obsObjName); 119 120 echo(">>> SET the attributes of the CounterMonitor:"); 121 122 counterMonitor.addObservedObject(obsObjName); 123 echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName); 124 125 counterMonitor.setObservedAttribute("IntegerAttribute"); 126 echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute"); 127 128 counterMonitor.setNotify(true); 129 echo("\tATTRIBUTE \"NotifyFlag\" = true"); 130 131 Integer threshold = 2; 132 counterMonitor.setInitThreshold(threshold); 133 echo("\tATTRIBUTE \"Threshold\" = " + threshold); 134 135 int granularityperiod = 500; 136 counterMonitor.setGranularityPeriod(granularityperiod); 137 echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod); 138 139 echo(">>> START the CounterMonitor"); 140 counterMonitor.start(); 141 142 // Check if notification was received 143 // 144 doWait(); 145 if (messageReceived) { 146 echo("\tOK: CounterMonitor notification received"); 147 } else { 148 echo("\tKO: CounterMonitor notification missed or not emitted"); 149 return 1; 150 } 151 } finally { 152 if (counterMonitor != null) 153 counterMonitor.stop(); 154 } 155 156 return 0; 157 } 158 159 /** 160 * Update the gauge and check for notifications 161 */ gaugeMonitorNotification()162 public int gaugeMonitorNotification() throws Exception { 163 164 GaugeMonitor gaugeMonitor = null; 165 try { 166 MBeanServer server = MBeanServerFactory.newMBeanServer(); 167 168 String domain = server.getDefaultDomain(); 169 170 // Create a new GaugeMonitor MBean and add it to the MBeanServer. 171 // 172 echo(">>> CREATE a new GaugeMonitor MBean"); 173 ObjectName gaugeMonitorName = new ObjectName( 174 domain + ":type=" + GaugeMonitor.class.getName()); 175 gaugeMonitor = new GaugeMonitor(); 176 server.registerMBean(gaugeMonitor, gaugeMonitorName); 177 178 echo(">>> ADD a listener to the GaugeMonitor"); 179 gaugeMonitor.addNotificationListener(this, null, null); 180 181 // 182 // MANAGEMENT OF A STANDARD MBEAN 183 // 184 185 echo(">>> CREATE a new ObservedObject MBean"); 186 187 ObjectName obsObjName = 188 ObjectName.getInstance(domain + ":type=ObservedObject"); 189 ObservedObject obsObj = new ObservedObject(); 190 server.registerMBean(obsObj, obsObjName); 191 192 echo(">>> SET the attributes of the GaugeMonitor:"); 193 194 gaugeMonitor.addObservedObject(obsObjName); 195 echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName); 196 197 gaugeMonitor.setObservedAttribute("IntegerAttribute"); 198 echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute"); 199 200 gaugeMonitor.setNotifyLow(false); 201 gaugeMonitor.setNotifyHigh(true); 202 echo("\tATTRIBUTE \"Notify Low Flag\" = false"); 203 echo("\tATTRIBUTE \"Notify High Flag\" = true"); 204 205 Double highThreshold = 3.0, lowThreshold = 2.5; 206 gaugeMonitor.setThresholds(highThreshold, lowThreshold); 207 echo("\tATTRIBUTE \"Low Threshold\" = " + lowThreshold); 208 echo("\tATTRIBUTE \"High Threshold\" = " + highThreshold); 209 210 int granularityperiod = 500; 211 gaugeMonitor.setGranularityPeriod(granularityperiod); 212 echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod); 213 214 echo(">>> START the GaugeMonitor"); 215 gaugeMonitor.start(); 216 217 // Check if notification was received 218 // 219 doWait(); 220 if (messageReceived) { 221 echo("\tOK: GaugeMonitor notification received"); 222 } else { 223 echo("\tKO: GaugeMonitor notification missed or not emitted"); 224 return 1; 225 } 226 } finally { 227 if (gaugeMonitor != null) 228 gaugeMonitor.stop(); 229 } 230 231 return 0; 232 } 233 234 /** 235 * Update the string and check for notifications 236 */ stringMonitorNotification()237 public int stringMonitorNotification() throws Exception { 238 239 StringMonitor stringMonitor = null; 240 try { 241 MBeanServer server = MBeanServerFactory.newMBeanServer(); 242 243 String domain = server.getDefaultDomain(); 244 245 // Create a new StringMonitor MBean and add it to the MBeanServer. 246 // 247 echo(">>> CREATE a new StringMonitor MBean"); 248 ObjectName stringMonitorName = new ObjectName( 249 domain + ":type=" + StringMonitor.class.getName()); 250 stringMonitor = new StringMonitor(); 251 server.registerMBean(stringMonitor, stringMonitorName); 252 253 echo(">>> ADD a listener to the StringMonitor"); 254 stringMonitor.addNotificationListener(this, null, null); 255 256 // 257 // MANAGEMENT OF A STANDARD MBEAN 258 // 259 260 echo(">>> CREATE a new ObservedObject MBean"); 261 262 ObjectName obsObjName = 263 ObjectName.getInstance(domain + ":type=ObservedObject"); 264 ObservedObject obsObj = new ObservedObject(); 265 server.registerMBean(obsObj, obsObjName); 266 267 echo(">>> SET the attributes of the StringMonitor:"); 268 269 stringMonitor.addObservedObject(obsObjName); 270 echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName); 271 272 stringMonitor.setObservedAttribute("StringAttribute"); 273 echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute"); 274 275 stringMonitor.setNotifyMatch(true); 276 echo("\tATTRIBUTE \"NotifyMatch\" = true"); 277 278 stringMonitor.setNotifyDiffer(false); 279 echo("\tATTRIBUTE \"NotifyDiffer\" = false"); 280 281 stringMonitor.setStringToCompare("do_match_now"); 282 echo("\tATTRIBUTE \"StringToCompare\" = \"do_match_now\""); 283 284 int granularityperiod = 500; 285 stringMonitor.setGranularityPeriod(granularityperiod); 286 echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod); 287 288 echo(">>> START the StringMonitor"); 289 stringMonitor.start(); 290 291 // Check if notification was received 292 // 293 doWait(); 294 if (messageReceived) { 295 echo("\tOK: StringMonitor notification received"); 296 } else { 297 echo("\tKO: StringMonitor notification missed or not emitted"); 298 return 1; 299 } 300 } finally { 301 if (stringMonitor != null) 302 stringMonitor.stop(); 303 } 304 305 return 0; 306 } 307 308 /** 309 * Test the monitor notifications. 310 */ monitorNotifications()311 public int monitorNotifications() throws Exception { 312 echo(">>> ----------------------------------------"); 313 messageReceived = false; 314 int error = counterMonitorNotification(); 315 echo(">>> ----------------------------------------"); 316 messageReceived = false; 317 error += gaugeMonitorNotification(); 318 echo(">>> ----------------------------------------"); 319 messageReceived = false; 320 error += stringMonitorNotification(); 321 echo(">>> ----------------------------------------"); 322 return error; 323 } 324 325 /* 326 * Print message 327 */ echo(String message)328 private static void echo(String message) { 329 System.out.println(message); 330 } 331 332 /* 333 * Wait messageReceived to be true 334 */ doWait()335 synchronized void doWait() { 336 while (!messageReceived) { 337 try { 338 wait(); 339 } catch (InterruptedException e) { 340 System.err.println("Got unexpected exception: " + e); 341 e.printStackTrace(); 342 break; 343 } 344 } 345 } 346 347 /* 348 * Standalone entry point. 349 * 350 * Run the test and report to stdout. 351 */ main(String args[])352 public static void main (String args[]) throws Exception { 353 NonComparableAttributeValueTest test = new NonComparableAttributeValueTest(); 354 int error = test.monitorNotifications(); 355 if (error > 0) { 356 echo(">>> Unhappy Bye, Bye!"); 357 throw new IllegalStateException("Test FAILED: Didn't get all " + 358 "the notifications that were " + 359 "expected by the test!"); 360 } else { 361 echo(">>> Happy Bye, Bye!"); 362 } 363 } 364 } 365