0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6205072
0N/A * @summary Test that the jmx.monitor.error.runtime monitor notification
0N/A * is emitted when getAttribute throws ReflectionException.
0N/A * @author Luis-Miguel Alventosa
0N/A * @run clean ReflectionExceptionTest MBeanServerBuilderImpl
0N/A * MBeanServerForwarderInvocationHandler
0N/A * @run build ReflectionExceptionTest MBeanServerBuilderImpl
0N/A * MBeanServerForwarderInvocationHandler
0N/A * @run main ReflectionExceptionTest
0N/A */
0N/A
0N/Aimport java.lang.reflect.Proxy;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.MBeanServerFactory;
0N/Aimport javax.management.Notification;
0N/Aimport javax.management.NotificationListener;
0N/Aimport javax.management.ObjectName;
0N/Aimport javax.management.ReflectionException;
0N/Aimport javax.management.monitor.CounterMonitor;
0N/Aimport javax.management.monitor.GaugeMonitor;
0N/Aimport javax.management.monitor.MonitorNotification;
0N/Aimport javax.management.monitor.StringMonitor;
0N/A
0N/Apublic class ReflectionExceptionTest implements NotificationListener {
0N/A
0N/A // MBean class
0N/A public class ObservedObject implements ObservedObjectMBean {
0N/A public Integer getIntegerAttribute() {
0N/A return i;
0N/A }
0N/A public void setIntegerAttribute(Integer i) {
0N/A this.i = i;
0N/A }
0N/A public String getStringAttribute() {
0N/A return s;
0N/A }
0N/A public void setStringAttribute(String s) {
0N/A this.s = s;
0N/A }
0N/A private Integer i = 1;
0N/A private String s = "dummy";
0N/A }
0N/A
0N/A // MBean interface
0N/A public interface ObservedObjectMBean {
0N/A public Integer getIntegerAttribute();
0N/A public void setIntegerAttribute(Integer i);
0N/A public String getStringAttribute();
0N/A public void setStringAttribute(String s);
0N/A }
0N/A
0N/A // Notification handler
0N/A public void handleNotification(Notification notification, Object handback) {
0N/A echo(">>> Received notification: " + notification);
0N/A if (notification instanceof MonitorNotification) {
0N/A String type = notification.getType();
0N/A if (type.equals(MonitorNotification.RUNTIME_ERROR)) {
0N/A MonitorNotification mn = (MonitorNotification) notification;
0N/A echo("\tType: " + mn.getType());
0N/A echo("\tTimeStamp: " + mn.getTimeStamp());
0N/A echo("\tObservedObject: " + mn.getObservedObject());
0N/A echo("\tObservedAttribute: " + mn.getObservedAttribute());
0N/A echo("\tDerivedGauge: " + mn.getDerivedGauge());
0N/A echo("\tTrigger: " + mn.getTrigger());
0N/A messageReceived = true;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Update the counter and check for notifications
0N/A */
0N/A public int counterMonitorNotification() throws Exception {
0N/A
0N/A CounterMonitor counterMonitor = new CounterMonitor();
0N/A try {
0N/A // Create a new CounterMonitor MBean and add it to the MBeanServer.
0N/A //
0N/A echo(">>> CREATE a new CounterMonitor MBean");
0N/A ObjectName counterMonitorName = new ObjectName(
0N/A domain + ":type=" + CounterMonitor.class.getName());
0N/A server.registerMBean(counterMonitor, counterMonitorName);
0N/A
0N/A echo(">>> ADD a listener to the CounterMonitor");
0N/A counterMonitor.addNotificationListener(this, null, null);
0N/A
0N/A //
0N/A // MANAGEMENT OF A STANDARD MBEAN
0N/A //
0N/A
0N/A echo(">>> SET the attributes of the CounterMonitor:");
0N/A
0N/A counterMonitor.addObservedObject(obsObjName);
0N/A echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);
0N/A
0N/A counterMonitor.setObservedAttribute("IntegerAttribute");
0N/A echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
0N/A
0N/A counterMonitor.setNotify(false);
0N/A echo("\tATTRIBUTE \"NotifyFlag\" = false");
0N/A
0N/A Integer threshold = 2;
0N/A counterMonitor.setInitThreshold(threshold);
0N/A echo("\tATTRIBUTE \"Threshold\" = " + threshold);
0N/A
0N/A int granularityperiod = 500;
0N/A counterMonitor.setGranularityPeriod(granularityperiod);
0N/A echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
0N/A
0N/A echo(">>> START the CounterMonitor");
0N/A counterMonitor.start();
0N/A
0N/A // Wait for granularity period (multiplied by 2 for sure)
0N/A //
0N/A Thread.sleep(granularityperiod * 2);
0N/A
0N/A // Check if notification was received
0N/A //
0N/A if (messageReceived) {
0N/A echo("\tOK: CounterMonitor got RUNTIME_ERROR notification!");
0N/A } else {
0N/A echo("\tKO: CounterMonitor did not get " +
0N/A "RUNTIME_ERROR notification!");
0N/A return 1;
0N/A }
0N/A } finally {
0N/A messageReceived = false;
0N/A if (counterMonitor != null)
0N/A counterMonitor.stop();
0N/A }
0N/A
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Update the gauge and check for notifications
0N/A */
0N/A public int gaugeMonitorNotification() throws Exception {
0N/A
0N/A GaugeMonitor gaugeMonitor = new GaugeMonitor();
0N/A try {
0N/A // Create a new GaugeMonitor MBean and add it to the MBeanServer.
0N/A //
0N/A echo(">>> CREATE a new GaugeMonitor MBean");
0N/A ObjectName gaugeMonitorName = new ObjectName(
0N/A domain + ":type=" + GaugeMonitor.class.getName());
0N/A server.registerMBean(gaugeMonitor, gaugeMonitorName);
0N/A
0N/A echo(">>> ADD a listener to the GaugeMonitor");
0N/A gaugeMonitor.addNotificationListener(this, null, null);
0N/A
0N/A //
0N/A // MANAGEMENT OF A STANDARD MBEAN
0N/A //
0N/A
0N/A echo(">>> SET the attributes of the GaugeMonitor:");
0N/A
0N/A gaugeMonitor.addObservedObject(obsObjName);
0N/A echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);
0N/A
0N/A gaugeMonitor.setObservedAttribute("IntegerAttribute");
0N/A echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
0N/A
0N/A gaugeMonitor.setNotifyLow(false);
0N/A gaugeMonitor.setNotifyHigh(false);
0N/A echo("\tATTRIBUTE \"Notify Low Flag\" = false");
0N/A echo("\tATTRIBUTE \"Notify High Flag\" = false");
0N/A
0N/A Integer highThreshold = 3, lowThreshold = 2;
0N/A gaugeMonitor.setThresholds(highThreshold, lowThreshold);
0N/A echo("\tATTRIBUTE \"Low Threshold\" = " + lowThreshold);
0N/A echo("\tATTRIBUTE \"High Threshold\" = " + highThreshold);
0N/A
0N/A int granularityperiod = 500;
0N/A gaugeMonitor.setGranularityPeriod(granularityperiod);
0N/A echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
0N/A
0N/A echo(">>> START the GaugeMonitor");
0N/A gaugeMonitor.start();
0N/A
0N/A // Wait for granularity period (multiplied by 2 for sure)
0N/A //
0N/A Thread.sleep(granularityperiod * 2);
0N/A
0N/A // Check if notification was received
0N/A //
0N/A if (messageReceived) {
0N/A echo("\tOK: GaugeMonitor got RUNTIME_ERROR notification!");
0N/A } else {
0N/A echo("\tKO: GaugeMonitor did not get " +
0N/A "RUNTIME_ERROR notification!");
0N/A return 1;
0N/A }
0N/A } finally {
0N/A messageReceived = false;
0N/A if (gaugeMonitor != null)
0N/A gaugeMonitor.stop();
0N/A }
0N/A
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Update the string and check for notifications
0N/A */
0N/A public int stringMonitorNotification() throws Exception {
0N/A
0N/A StringMonitor stringMonitor = new StringMonitor();
0N/A try {
0N/A // Create a new StringMonitor MBean and add it to the MBeanServer.
0N/A //
0N/A echo(">>> CREATE a new StringMonitor MBean");
0N/A ObjectName stringMonitorName = new ObjectName(
0N/A domain + ":type=" + StringMonitor.class.getName());
0N/A server.registerMBean(stringMonitor, stringMonitorName);
0N/A
0N/A echo(">>> ADD a listener to the StringMonitor");
0N/A stringMonitor.addNotificationListener(this, null, null);
0N/A
0N/A //
0N/A // MANAGEMENT OF A STANDARD MBEAN
0N/A //
0N/A
0N/A echo(">>> SET the attributes of the StringMonitor:");
0N/A
0N/A stringMonitor.addObservedObject(obsObjName);
0N/A echo("\tATTRIBUTE \"ObservedObject\" = " + obsObjName);
0N/A
0N/A stringMonitor.setObservedAttribute("StringAttribute");
0N/A echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute");
0N/A
0N/A stringMonitor.setNotifyMatch(false);
0N/A echo("\tATTRIBUTE \"NotifyMatch\" = false");
0N/A
0N/A stringMonitor.setNotifyDiffer(false);
0N/A echo("\tATTRIBUTE \"NotifyDiffer\" = false");
0N/A
0N/A stringMonitor.setStringToCompare("dummy");
0N/A echo("\tATTRIBUTE \"StringToCompare\" = \"dummy\"");
0N/A
0N/A int granularityperiod = 500;
0N/A stringMonitor.setGranularityPeriod(granularityperiod);
0N/A echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
0N/A
0N/A echo(">>> START the StringMonitor");
0N/A stringMonitor.start();
0N/A
0N/A // Wait for granularity period (multiplied by 2 for sure)
0N/A //
0N/A Thread.sleep(granularityperiod * 2);
0N/A
0N/A // Check if notification was received
0N/A //
0N/A if (messageReceived) {
0N/A echo("\tOK: StringMonitor got RUNTIME_ERROR notification!");
0N/A } else {
0N/A echo("\tKO: StringMonitor did not get " +
0N/A "RUNTIME_ERROR notification!");
0N/A return 1;
0N/A }
0N/A } finally {
0N/A messageReceived = false;
0N/A if (stringMonitor != null)
0N/A stringMonitor.stop();
0N/A }
0N/A
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Test the monitor notifications.
0N/A */
0N/A public int monitorNotifications() throws Exception {
0N/A
0N/A server = MBeanServerFactory.newMBeanServer();
0N/A
0N/A MBeanServerForwarderInvocationHandler mbsfih =
0N/A (MBeanServerForwarderInvocationHandler)
0N/A Proxy.getInvocationHandler(server);
0N/A
0N/A mbsfih.setGetAttributeException(
0N/A new ReflectionException(new RuntimeException(),
0N/A "Test ReflectionException"));
0N/A
0N/A domain = server.getDefaultDomain();
0N/A
0N/A obsObjName = ObjectName.getInstance(domain + ":type=ObservedObject");
0N/A server.registerMBean(new ObservedObject(), obsObjName);
0N/A
0N/A echo(">>> ----------------------------------------");
0N/A int error = counterMonitorNotification();
0N/A echo(">>> ----------------------------------------");
0N/A error += gaugeMonitorNotification();
0N/A echo(">>> ----------------------------------------");
0N/A error += stringMonitorNotification();
0N/A echo(">>> ----------------------------------------");
0N/A return error;
0N/A }
0N/A
0N/A /*
0N/A * Print message
0N/A */
0N/A private static void echo(String message) {
0N/A System.out.println(message);
0N/A }
0N/A
0N/A /*
0N/A * Standalone entry point.
0N/A *
0N/A * Run the test and report to stdout.
0N/A */
0N/A public static void main (String args[]) throws Exception {
0N/A System.setProperty("javax.management.builder.initial",
0N/A MBeanServerBuilderImpl.class.getName());
0N/A ReflectionExceptionTest test = new ReflectionExceptionTest();
0N/A int error = test.monitorNotifications();
0N/A if (error > 0) {
0N/A echo(">>> Unhappy Bye, Bye!");
0N/A throw new IllegalStateException("Test FAILED: Didn't get all " +
0N/A "the notifications that were " +
0N/A "expected by the test!");
0N/A } else {
0N/A echo(">>> Happy Bye, Bye!");
0N/A }
0N/A }
0N/A
0N/A // Flag to notify that a message has been received
0N/A private boolean messageReceived = false;
0N/A
0N/A private MBeanServer server;
0N/A private ObjectName obsObjName;
0N/A private String domain;
0N/A}