VirtualMBeanNotifTest.java 22.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

/*
 * @test VirtualMBeanNotifTest.java
 * @bug 5108776
 * @build VirtualMBeanNotifTest Wombat WombatMBean
28
 * @run main VirtualMBeanNotifTest
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
 * @summary Test that Virtual MBeans can be implemented and emit notifs.
 * @author  Daniel Fuchs
 */
import java.lang.management.ManagementFactory;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import javax.management.Attribute;
import javax.management.DynamicMBean;
import javax.management.InstanceNotFoundException;
import javax.management.JMException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanServer;
import javax.management.Notification;
import javax.management.NotificationBroadcaster;
import javax.management.NotificationEmitter;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.RuntimeOperationsException;
import javax.management.StandardMBean;
import javax.management.event.EventSubscriber;
import javax.management.namespace.VirtualEventManager;
import javax.management.namespace.MBeanServerSupport;

public class VirtualMBeanNotifTest {

    /**
     * An invocation handler that can implement DynamicMBean,
     * NotificationBroadcaster, NotificationEmitter.
     * The invocation handler works by forwarding all calls received from
     * the implemented interfaces through a wrapped MBeanServer object.
     */
    public static class DynamicWrapper
            implements InvocationHandler {

        /**
         * Inserts an additional class at the head of a signature array.
         * @param first The first class in the signature
         * @param rest  The other classes in the signature
         * @return A signature array, of length rest.length+1.
         */
        static Class[] concat(Class first, Class... rest) {
            if (rest == null || rest.length == 0) {
                return new Class[] { first };
            }
            final Class[] sig = new Class[rest.length+1];
            sig[0] = first;
            System.arraycopy(rest, 0, sig, 1, rest.length);
            return sig;
        }

        /**
         * Inserts an additional object at the head of a parameters array.
         * @param first The first object in the parameter array.
         * @param rest  The other objects in the parameter array.
         * @return A parameter array, of length rest.length+1.
         */
        static Object[] concat(Object first, Object... rest) {
            if (rest == null || rest.length == 0) {
                return new Object[] { first };
            }
            final Object[] params = new Object[rest.length+1];
            params[0] = first;
            System.arraycopy(rest, 0, params, 1, rest.length);
            return params;
        }

        /**
         * These two sets are used to check that all methods from
         * implemented interfaces are mapped.
         * unmapped is the set of methods that couldn't be mapped.
         * mapped is the set of methods that could be mapped.
         */
        final static Set<Method> unmapped = new HashSet<Method>();
        final static Set<Method> mapped = new HashSet<Method>();

        /**
         * For each method define in one of the interfaces intf, tries
         * to find a corresponding method in the reference class ref, where
         * the method in ref has the same name, and takes an additional
         * ObjectName as first parameter.
         *
         * So for instance, if ref is MBeanServer and intf is {DynamicMBean}
         * the result map is:
         *     DynamicMBean.getAttribute -> MBeanServer.getAttribute
         *     DynamicMBean.setAttribute -> MBeanServer.setAttribute
         *     etc...
         * If a method was mapped, it is additionally added to 'mapped'
         * If a method couldn't be mapped, it is added to 'unmmapped'.
         * In our example above, DynamicMBean.getNotificationInfo will end
         * up in 'unmapped'.
         *
         * @param ref   The reference class - to which calls will be forwarded
         *              with an additional ObjectName parameter inserted.
         * @param intf  The proxy interface classes - for which we must find an
         *              equivalent in 'ref'
         * @return A map mapping the methods from intfs to the method of ref.
         */
        static Map<Method,Method> makeMapFor(Class<?> ref, Class<?>... intf) {
            final Map<Method,Method> map = new HashMap<Method,Method>();
            for (Class<?> clazz : intf) {
                for (Method m : clazz.getMethods()) {
                    try {
                        final Method m2 =
                            ref.getMethod(m.getName(),
                            concat(ObjectName.class,m.getParameterTypes()));
                        map.put(m,m2);
                        mapped.add(m);
                    } catch (Exception x) {
                        unmapped.add(m);
                    }
                }
            }
            return map;
        }

        /**
         * Tries to map all methods from DynamicMBean.class and
         * NotificationEmitter.class to their equivalent in MBeanServer.
         * This should be all the methods except
         * DynamicMBean.getNotificationInfo.
         */
        static final Map<Method,Method> mbeanmap =
                makeMapFor(MBeanServer.class,DynamicMBean.class,
                NotificationEmitter.class);
        /**
         * Tries to map all methods from DynamicMBean.class and
         * NotificationEmitter.class to an equivalent in DynamicWrapper.
         * This time only DynamicMBean.getNotificationInfo will be mapped.
         */
        static final Map<Method,Method> selfmap =
                makeMapFor(DynamicWrapper.class,DynamicMBean.class,
                NotificationEmitter.class);

        /**
         * Now check that we have mapped all methods.
         */
        static {
            unmapped.removeAll(mapped);
            if (unmapped.size() > 0)
                throw new ExceptionInInitializerError("Couldn't map "+ unmapped);
        }

        /**
         * The wrapped MBeanServer to which everything is delegated.
         */
        private final MBeanServer server;

        /**
         * The name of the MBean we're proxying.
         */
        private final ObjectName name;
        DynamicWrapper(MBeanServer server, ObjectName name) {
            this.server=server;
            this.name=name;
        }

        /**
         * Creates a new proxy for the given MBean. Implements
         * NotificationEmitter/NotificationBroadcaster if the proxied
         * MBean also does.
         * @param name    the name of the proxied MBean
         * @param server  the wrapped server
         * @return a DynamicMBean proxy
         * @throws javax.management.InstanceNotFoundException
         */
        public static DynamicMBean newProxy(ObjectName name, MBeanServer server)
            throws InstanceNotFoundException {
            if (server.isInstanceOf(name,
                    NotificationEmitter.class.getName())) {
                // implements NotificationEmitter
                return (DynamicMBean)
                        Proxy.newProxyInstance(
                        DynamicWrapper.class.getClassLoader(),
                        new Class[] {NotificationEmitter.class,
                        DynamicMBean.class},
                        new DynamicWrapper(server, name));
            }
            if (server.isInstanceOf(name,
                    NotificationBroadcaster.class.getName())) {
                // implements NotificationBroadcaster
                return (DynamicMBean)
                        Proxy.newProxyInstance(
                        DynamicWrapper.class.getClassLoader(),
                        new Class[] {NotificationBroadcaster.class,
                        DynamicMBean.class},
                        new DynamicWrapper(server, name));
            }
            // Only implements DynamicMBean.
            return (DynamicMBean)
                        Proxy.newProxyInstance(
                        DynamicWrapper.class.getClassLoader(),
                        new Class[] {DynamicMBean.class},
                        new DynamicWrapper(server, name));
        }

        public Object invoke(Object proxy, Method method, Object[] args)
                throws Throwable {
            // Look for a method on this class (takes precedence)
            final Method self = selfmap.get(method);
            if (self != null)
                return call(this,self,concat(name,args));

            // no method found on this class, look for the same method
            // on the wrapped MBeanServer
            final Method mbean = mbeanmap.get(method);
            if (mbean != null)
                return call(server,mbean,concat(name,args));

            // This isn't a method that can be forwarded to MBeanServer.
            // If it's a method from Object, call it on this.
            if (method.getDeclaringClass().equals(Object.class))
                return call(this,method,args);
            throw new NoSuchMethodException(method.getName());
        }

        // Call a method using reflection, unwraps invocation target exceptions
        public Object call(Object handle, Method m, Object[] args)
                throws Throwable {
            try {
                return m.invoke(handle, args);
            } catch (InvocationTargetException x) {
               throw x.getCause();
            }
        }

        // this method is called when DynamicMBean.getNotificationInfo() is
        // called. This is the method that should be mapped in
        // 'selfmap'
        public MBeanNotificationInfo[] getNotificationInfo(ObjectName name)
            throws JMException {
            return server.getMBeanInfo(name).getNotifications();
        }
    }

    /**
     * Just so that we can call the same test twice but with two
     * different implementations of VirtualMBeanServerSupport.
     */
    public static interface MBeanServerWrapperFactory {
        public MBeanServer wrapMBeanServer(MBeanServer wrapped);
    }

    /**
     * A VirtualMBeanServerSupport that wrapps an MBeanServer and does not
     * use VirtualEventManager.
     */
    public static class VirtualMBeanServerTest
            extends MBeanServerSupport {

        final MBeanServer wrapped;

        public VirtualMBeanServerTest(MBeanServer wrapped) {
            this.wrapped=wrapped;
        }

        @Override
        public DynamicMBean getDynamicMBeanFor(final ObjectName name)
                throws InstanceNotFoundException {
            if (wrapped.isRegistered(name))
                return DynamicWrapper.newProxy(name,wrapped);
            throw new InstanceNotFoundException(String.valueOf(name));
        }

        @Override
        protected Set<ObjectName> getNames() {
            return wrapped.queryNames(null, null);
        }

        public final static MBeanServerWrapperFactory factory =
                new MBeanServerWrapperFactory() {

            public MBeanServer wrapMBeanServer(MBeanServer wrapped) {
                return new VirtualMBeanServerTest(wrapped);
            }
            @Override
            public String toString() {
                return VirtualMBeanServerTest.class.getName();
            }
        };
    }

     /**
     * A VirtualMBeanServerSupport that wrapps an MBeanServer and
     * uses a VirtualEventManager.
     */
    public static class VirtualMBeanServerTest2
            extends VirtualMBeanServerTest {

        final EventSubscriber sub;
        final NotificationListener nl;
        final VirtualEventManager  mgr;

        /**
         * We use an EventSubscriber to subscribe for all notifications from
         * the wrapped MBeanServer, and publish them through a
         * VirtualEventManager. Not a very efficient way of doing things.
         * @param wrapped
         */
        public VirtualMBeanServerTest2(MBeanServer wrapped) {
            super(wrapped);
            this.sub = EventSubscriber.getEventSubscriber(wrapped);
            this.mgr = new VirtualEventManager();
            this.nl = new NotificationListener() {
                public void handleNotification(Notification notification, Object handback) {
                    mgr.publish((ObjectName)notification.getSource(), notification);
                }
            };
            try {
                sub.subscribe(ObjectName.WILDCARD, nl, null, null);
            } catch (RuntimeException x) {
                throw x;
            } catch (Exception x) {
                throw new IllegalStateException("can't subscribe for notifications!");
            }
        }

        @Override
        public NotificationEmitter
                getNotificationEmitterFor(ObjectName name)
                throws InstanceNotFoundException {
            final DynamicMBean mbean = getDynamicMBeanFor(name);
            if (mbean instanceof NotificationEmitter)
                return mgr.getNotificationEmitterFor(name);
            return null;
        }

        public final static MBeanServerWrapperFactory factory =
                new MBeanServerWrapperFactory() {

            public MBeanServer wrapMBeanServer(MBeanServer wrapped) {
                return new VirtualMBeanServerTest2(wrapped);
            }
            @Override
            public String toString() {
                return VirtualMBeanServerTest2.class.getName();
            }
        };
    }


    public static void test(MBeanServerWrapperFactory factory) throws Exception {
        final MBeanServer server = ManagementFactory.getPlatformMBeanServer();

        // names[] are NotificationEmitters
        final ObjectName[] emitters = new ObjectName[2];
        // shields[] have been shielded by wrapping them in a StandardMBean,
        // so although the resource is an MBean that implements
        // NotificationEmitter, the registered MBean (the wrapper) doesn't.
        final ObjectName[] shielded = new ObjectName[2];

        final List<ObjectName> registered = new ArrayList<ObjectName>(4);

        try {
            // register two MBeans before wrapping
            server.registerMBean(new Wombat(),
                    emitters[0] = new ObjectName("bush:type=Wombat,name=wom"));
            registered.add(emitters[0]);

            // we shield the second MBean in a StandardMBean so that it does
            // not appear as a NotificationEmitter.
            server.registerMBean(
                    new StandardMBean(new Wombat(), WombatMBean.class),
                    shielded[0] = new ObjectName("bush:type=Wombat,name=womshield"));
            registered.add(shielded[0]);

            final MBeanServer vserver = factory.wrapMBeanServer(server);

            // register two other MBeans after wrapping
            server.registerMBean(new Wombat(),
                    emitters[1] = new ObjectName("bush:type=Wombat,name=bat"));
            registered.add(emitters[1]);

            // we shield the second MBean in a StandardMBean so that it does
            // not appear as a NotificationEmitter.
            server.registerMBean(
                    new StandardMBean(new Wombat(), WombatMBean.class),
                    shielded[1] = new ObjectName("bush:type=Wombat,name=batshield"));
            registered.add(shielded[1]);

            // Call test with this config - we have two wombats who broadcast
            // notifs (emitters) and two wombats who don't (shielded).
            test(vserver, emitters, shielded);

            System.out.println("*** Test passed for: " + factory);
        } finally {
            // Clean up the platform mbean server for the next test...
            for (ObjectName n : registered) {
                try {
                    server.unregisterMBean(n);
                } catch (Exception x) {
                    x.printStackTrace();
                }
            }
        }
    }

    /**
     * Perform the actual test.
     * @param vserver    A virtual MBeanServerSupport implementation
     * @param emitters   Names of NotificationBroadcaster MBeans
     * @param shielded   Names of non NotificationBroadcaster MBeans
     * @throws java.lang.Exception
     */
    public static void test(MBeanServer vserver, ObjectName[] emitters,
            ObjectName[] shielded) throws Exception {

        // To catch exception in NotificationListener
        final List<Exception> fail = new CopyOnWriteArrayList<Exception>();

        // A queue of received notifications
        final BlockingQueue<Notification> notifs =
                new ArrayBlockingQueue<Notification>(50);

        // A notification listener that puts the notification it receives
        // in the queue.
        final NotificationListener handler = new NotificationListener() {

            public void handleNotification(Notification notification,
                    Object handback) {
                try {
                    notifs.put(notification);
                } catch (Exception x) {
                    fail.add(x);
                }
            }
        };

        // A list of attribute names for which we might receive an
        // exception. If an exception is received when getting these
        // attributes - the test will not fail.
        final List<String> exceptions = Arrays.asList( new String[] {
           "UsageThresholdCount","UsageThreshold","UsageThresholdExceeded",
           "CollectionUsageThresholdCount","CollectionUsageThreshold",
           "CollectionUsageThresholdExceeded"
        });

        // This is just a sanity check. Get all attributes of all MBeans.
        for (ObjectName n : vserver.queryNames(null, null)) {
            final MBeanInfo m = vserver.getMBeanInfo(n);
            for (MBeanAttributeInfo mba : m.getAttributes()) {
                // System.out.println(n+":");
                Object val;
                try {
                    val = vserver.getAttribute(n, mba.getName());
                } catch (Exception x) {
                    // only accept exception for those attributes that
                    // have a valid reason to fail...
                    if (exceptions.contains(mba.getName())) val = x;
                    else throw new Exception("Failed to get " +
                            mba.getName() + " from " + n,x);
                }
                // System.out.println("\t "+mba.getName()+": "+ val);
            }
        }

        // The actual tests. Register for notifications with notif emitters
        for (ObjectName n : emitters) {
            vserver.addNotificationListener(n, handler, null, n);
        }

        // Trigger the emission of notifications, check that we received them.
        for (ObjectName n : emitters) {
            vserver.setAttribute(n,
                    new Attribute("Caption","I am a new wombat!"));
            final Notification notif = notifs.poll(4, TimeUnit.SECONDS);
            if (!notif.getSource().equals(n))
                throw new Exception("Bad source for "+ notif);
            if (fail.size() > 0)
                throw new Exception("Failed to handle notif",fail.remove(0));
        }

        // Check that we didn't get more notifs than expected
        if (notifs.size() > 0)
            throw new Exception("Extra notifications in queue: "+notifs);

        // Check that if the MBean doesn't exist, we get InstanceNotFound.
        try {
            vserver.addNotificationListener(new ObjectName("toto:toto=toto"),
                    handler, null, null);
            throw new Exception("toto:toto=toto doesn't throw INFE");
        } catch (InstanceNotFoundException x) {
            System.out.println("Received "+x+" as expected.");
        }

        // For those MBeans that shouldn't be NotificationEmitters, check that
        // we get IllegalArgumentException
        for (ObjectName n : shielded) {
            try {
                vserver.addNotificationListener(n, handler, null, n);
            } catch (RuntimeOperationsException x) {
                System.out.println("Received "+x+" as expected.");
                System.out.println("Cause is: "+x.getCause());
                if (!(x.getCause() instanceof IllegalArgumentException))
                    throw new Exception("was expecting IllegalArgumentException cause. Got "+x.getCause(),x);
            }
        }

        // Sanity check. Remove our listeners.
        for (ObjectName n : emitters) {
            vserver.removeNotificationListener(n, handler, null, n);
        }

        // That's it.
        // Sanity check: we shouldn't have received any new notif.
        if (notifs.size() > 0)
            throw new Exception("Extra notifications in queue: "+notifs);
        // The NotifListener shouldn't have logged any new exception.
        if (fail.size() > 0)
                throw new Exception("Failed to handle notif",fail.remove(0));
    }

    public static void main(String[] args) throws Exception {
        // test with a regular MBeanServer (no VirtualMBeanServerSupport)
        final MBeanServerWrapperFactory identity =
                new MBeanServerWrapperFactory() {
            public MBeanServer wrapMBeanServer(MBeanServer wrapped) {
                return wrapped;
            }
        };
        test(identity);
        // test with no EventManager
        test(VirtualMBeanServerTest.factory);
        // test with VirtualEventManager
        test(VirtualMBeanServerTest2.factory);
    }
}