EventClient.java 39.2 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 28 29 30 31
/*
 * Copyright 2007 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.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * 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.
 */

package javax.management.event;

import com.sun.jmx.event.DaemonThreadFactory;
import com.sun.jmx.event.LeaseRenewer;
import com.sun.jmx.event.ReceiverBuffer;
import com.sun.jmx.event.RepeatedSingletonJob;
32
import com.sun.jmx.namespace.JMXNamespaceUtils;
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
import com.sun.jmx.mbeanserver.PerThreadGroupPool;
import com.sun.jmx.remote.util.ClassLogger;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import javax.management.InstanceNotFoundException;
import javax.management.ListenerNotFoundException;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanServerConnection;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.NotificationResult;
import javax.management.remote.TargetedNotification;

/**
 * <p>This class is used to manage its notification listeners on the client
 * side in the same way as on the MBean server side. This class needs to work
 * with an {@link EventClientDelegateMBean} on the server side.</p>
 *
 * <P>A user can specify an {@link EventRelay} object to specify how to receive
 * notifications forwarded by the {@link EventClientDelegateMBean}. By default,
 * the class {@link FetchingEventRelay} is used.</p>
 *
 * <p>A user can specify an {@link java.util.concurrent.Executor Executor}
 * to distribute notifications to local listeners. If no executor is
 * specified, the thread in the {@link EventRelay} which calls {@link
 * EventReceiver#receive EventReceiver.receive} will be reused to distribute
 * the notifications (in other words, to call the {@link
 * NotificationListener#handleNotification handleNotification} method of the
 * appropriate listeners). It is useful to make a separate thread do this
 * distribution in some cases. For example, if network communication is slow,
 * the forwarding thread can concentrate on communication while, locally,
 * the distributing thread distributes the received notifications. Another
 * usage is to share a thread pool between many clients, for scalability.
 * Note, though, that if the {@code Executor} can create more than one thread
 * then it is possible that listeners will see notifications in a different
 * order from the order in which they were sent.</p>
 *
 * <p>An object of this class sends notifications to listeners added with
 * {@link #addEventClientListener}.  The {@linkplain Notification#getType()
 * type} of each such notification is one of {@link #FAILED}, {@link #NONFATAL},
 * or {@link #NOTIFS_LOST}.</p>
 *
 * @since JMX 2.0
 */
public class EventClient implements EventConsumer, NotificationManager {

    /**
     * <p>A notification string type used by an {@code EventClient} object
     * to inform a listener added by {@link #addEventClientListener} that
     * it failed to get notifications from a remote server, and that it is
     * possible that no more notifications will be delivered.</p>
     *
     * @see #addEventClientListener
     * @see EventReceiver#failed
     */
    public static final String FAILED = "jmx.event.service.failed";

    /**
     * <p>Reports that an unexpected exception has been received by the {@link
     * EventRelay} object but that it is non-fatal. For example, a notification
     * received is not serializable or its class is not found.</p>
     *
     * @see #addEventClientListener
     * @see EventReceiver#nonFatal
     */
    public static final String NONFATAL = "jmx.event.service.nonfatal";

    /**
     * <p>A notification string type used by an {@code EventClient} object to
     * inform a listener added by {@code #addEventClientListener} that it
     * has detected that notifications have been lost.  The {@link
     * Notification#getUserData() userData} of the notification is a Long which
     * is an upper bound on the number of lost notifications that have just
     * been detected.</p>
     *
     * @see #addEventClientListener
     */
    public static final String NOTIFS_LOST = "jmx.event.service.notifs.lost";

    /**
     * The default lease time, {@value}, in milliseconds.
     *
     * @see EventClientDelegateMBean#lease
     */
    public static final long DEFAULT_LEASE_TIMEOUT = 300000;

    /**
     * <p>Constructs a default {@code EventClient} object.</p>
     *
     * <p>This object creates a {@link FetchingEventRelay} object to
     * receive notifications forwarded by the {@link EventClientDelegateMBean}.
     * The {@link EventClientDelegateMBean} that it works with is the
     * one registered with the {@linkplain EventClientDelegate#OBJECT_NAME
     * default ObjectName}.  The thread from the {@link FetchingEventRelay}
     * object that fetches the notifications is also used to distribute them.
     *
     * @param conn An {@link MBeanServerConnection} object used to communicate
     * with an {@link EventClientDelegateMBean} MBean.
     *
     * @throws IllegalArgumentException If {@code conn} is null.
     * @throws IOException If an I/O error occurs when communicating with the
     * {@code EventClientDelegateMBean}.
     */
    public EventClient(MBeanServerConnection conn) throws IOException {
        this(EventClientDelegate.getProxy(conn));
    }

    /**
     * Constructs an {@code EventClient} object with a specified
     * {@link EventClientDelegateMBean}.
     *
     * <p>This object creates a {@link FetchingEventRelay} object to receive
     * notifications forwarded by the {@link EventClientDelegateMBean}.  The
     * thread from the {@link FetchingEventRelay} object that fetches the
     * notifications is also used to distribute them.
     *
     * @param delegate An {@link EventClientDelegateMBean} object to work with.
     *
     * @throws IllegalArgumentException If {@code delegate} is null.
     * @throws IOException If an I/O error occurs when communicating with the
     * the {@link EventClientDelegateMBean}.
     */
    public EventClient(EventClientDelegateMBean delegate)
    throws IOException {
        this(delegate, null, null, null, DEFAULT_LEASE_TIMEOUT);
    }

    /**
     * Constructs an {@code EventClient} object with the specified
     * {@link EventClientDelegateMBean}, {@link EventRelay}
     * object, and distributing thread.
     *
     * @param delegate An {@link EventClientDelegateMBean} object to work with.
     * Usually, this will be a proxy constructed using
     * {@link EventClientDelegate#getProxy}.
     * @param eventRelay An object used to receive notifications
     * forwarded by the {@link EventClientDelegateMBean}. If {@code null}, a
     * {@link FetchingEventRelay} object will be used.
     * @param distributingExecutor Used to distribute notifications to local
     * listeners. If {@code null}, the thread that calls {@link
     * EventReceiver#receive EventReceiver.receive} from the {@link EventRelay}
     * object is used.
     * @param leaseScheduler An object that will be used to schedule the
     * periodic {@linkplain EventClientDelegateMBean#lease lease updates}.
     * If {@code null}, a default scheduler will be used.
     * @param requestedLeaseTime The lease time used to keep this client alive
     * in the {@link EventClientDelegateMBean}.  A value of zero is equivalent
     * to the {@linkplain #DEFAULT_LEASE_TIMEOUT default value}.
     *
     * @throws IllegalArgumentException If {@code delegate} is null.
     * @throws IOException If an I/O error occurs when communicating with the
     * {@link EventClientDelegateMBean}.
     */
    public EventClient(EventClientDelegateMBean delegate,
            EventRelay eventRelay,
            Executor distributingExecutor,
            ScheduledExecutorService leaseScheduler,
            long requestedLeaseTime)
            throws IOException {
        if (delegate == null) {
            throw new IllegalArgumentException("Null EventClientDelegateMBean");
        }

        if (requestedLeaseTime == 0)
            requestedLeaseTime = DEFAULT_LEASE_TIMEOUT;
        else if (requestedLeaseTime < 0) {
            throw new IllegalArgumentException(
                    "Negative lease time: " + requestedLeaseTime);
        }

        eventClientDelegate = delegate;

        if (eventRelay != null) {
            this.eventRelay = eventRelay;
        } else {
            try {
                this.eventRelay = new FetchingEventRelay(delegate);
            } catch (IOException ioe) {
                throw ioe;
            } catch (Exception e) {
                // impossible?
                final IOException ioee = new IOException(e.toString());
                ioee.initCause(e);
                throw ioee;
            }
        }

        if (distributingExecutor == null)
            distributingExecutor = callerExecutor;
        this.distributingExecutor = distributingExecutor;
        this.dispatchingJob = new DispatchingJob();

        clientId = this.eventRelay.getClientId();

        this.requestedLeaseTime = requestedLeaseTime;
        if (leaseScheduler == null)
            leaseScheduler = defaultLeaseScheduler();
        leaseRenewer = new LeaseRenewer(leaseScheduler, renewLease);

        if (logger.traceOn()) {
            logger.trace("init", "New EventClient: "+clientId);
        }
    }

    private static ScheduledExecutorService defaultLeaseScheduler() {
        // The default lease scheduler uses a ScheduledThreadPoolExecutor
        // with a maximum of 20 threads.  This means that if you have many
        // EventClient instances and some of them get blocked (because of an
        // unresponsive network, for example), then even the instances that
        // are connected to responsive servers may have their leases expire.
        // XXX check if the above is true and possibly fix.
        PerThreadGroupPool.Create<ScheduledThreadPoolExecutor> create =
                new PerThreadGroupPool.Create<ScheduledThreadPoolExecutor>() {
            public ScheduledThreadPoolExecutor createThreadPool(ThreadGroup group) {
                ThreadFactory daemonThreadFactory = new DaemonThreadFactory(
267
                        "JMX EventClient lease renewer %d");
268 269
                ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(
                        20, daemonThreadFactory);
270
                exec.setKeepAliveTime(1, TimeUnit.SECONDS);
271
                exec.allowCoreThreadTimeOut(true);
272
                exec.setRemoveOnCancelPolicy(true);
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 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
                return exec;
            }
        };
        return leaseRenewerThreadPool.getThreadPoolExecutor(create);

    }

    /**
     * <p>Closes this EventClient, removes all listeners and stops receiving
     * notifications.</p>
     *
     * <p>This method calls {@link
     * EventClientDelegateMBean#removeClient(String)} and {@link
     * EventRelay#stop}.  Both operations occur even if one of them
     * throws an {@code IOException}.
     *
     * @throws IOException if an I/O error occurs when communicating with
     * {@link EventClientDelegateMBean}, or if {@link EventRelay#stop}
     * throws an {@code IOException}.
     */
    public void close() throws IOException {
        if (logger.traceOn()) {
            logger.trace("close", clientId);
        }

        synchronized(listenerInfoMap) {
            if (closed) {
                return;
            }

            closed = true;
            listenerInfoMap.clear();
        }

        if (leaseRenewer != null)
            leaseRenewer.close();

        IOException ioe = null;
        try {
            eventRelay.stop();
        } catch (IOException e) {
            ioe = e;
            logger.debug("close", "EventRelay.stop", e);
        }

        try {
            eventClientDelegate.removeClient(clientId);
        } catch (Exception e) {
            if (e instanceof IOException)
                ioe = (IOException) e;
            else
                ioe = new IOException(e);
            logger.debug("close",
                    "Got exception when removing "+clientId, e);
        }

        if (ioe != null)
            throw ioe;
    }

    /**
     * <p>Determine if this {@code EventClient} is closed.</p>
     *
     * @return True if the {@code EventClient} is closed.
     */
    public boolean closed() {
        return closed;
    }

    /**
     * <p>Return the {@link EventRelay} associated with this
     * {@code EventClient}.</p>
     *
     * @return The {@link EventRelay} object used.
     */
    public EventRelay getEventRelay() {
        return eventRelay;
    }

    /**
     * <p>Return the lease time that this {@code EventClient} requests
     * on every lease renewal.</p>
     *
     * @return The requested lease time.
     *
     * @see EventClientDelegateMBean#lease
     */
    public long getRequestedLeaseTime() {
        return requestedLeaseTime;
    }

    /**
     * @see javax.management.MBeanServerConnection#addNotificationListener(
     * ObjectName, NotificationListener, NotificationFilter, Object).
     */
    public void addNotificationListener(ObjectName name,
            NotificationListener listener,
            NotificationFilter filter,
            Object handback)
            throws InstanceNotFoundException, IOException {
        if (logger.traceOn()) {
            logger.trace("addNotificationListener", "");
        }

        checkState();

        Integer listenerId;
        try {
            listenerId =
                    eventClientDelegate.addListener(clientId, name, filter);
        } catch (EventClientNotFoundException ecnfe) {
            final IOException ioe = new IOException();
            ioe.initCause(ecnfe);
            throw ioe;
        }

        synchronized(listenerInfoMap) {
            listenerInfoMap.put(listenerId,  new ListenerInfo(
                    name,
                    listener,
                    filter,
                    handback,
                    false));
        }

        startListening();
    }

    /**
     * @see javax.management.MBeanServerConnection#removeNotificationListener(
     * ObjectName, NotificationListener).
     */
    public void removeNotificationListener(ObjectName name,
            NotificationListener listener)
            throws InstanceNotFoundException,
            ListenerNotFoundException,
            IOException {
        if (logger.traceOn()) {
            logger.trace("removeNotificationListener", "");
        }
        checkState();

        for (Integer id : getListenerInfo(name, listener, false)) {
            removeListener(id);
        }
    }

    /**
     * @see javax.management.MBeanServerConnection#removeNotificationListener(
     * ObjectName, NotificationListener, NotificationFilter, Object).
     */
    public void removeNotificationListener(ObjectName name,
            NotificationListener listener,
            NotificationFilter filter,
            Object handback)
            throws InstanceNotFoundException,
            ListenerNotFoundException,
            IOException {
        if (logger.traceOn()) {
            logger.trace("removeNotificationListener", "with all arguments.");
        }
        checkState();
        final Integer listenerId =
                getListenerInfo(name, listener, filter, handback, false);

        removeListener(listenerId);
    }

    /**
     * @see javax.management.event.EventConsumer#unsubscribe(
     * ObjectName, NotificationListener).
     */
    public void unsubscribe(ObjectName name,
            NotificationListener listener)
            throws ListenerNotFoundException, IOException {
        if (logger.traceOn()) {
            logger.trace("unsubscribe", "");
        }
        checkState();
        final Integer listenerId =
                getMatchedListenerInfo(name, listener, true);

        synchronized(listenerInfoMap) {
            if (listenerInfoMap.remove(listenerId) == null) {
                throw new ListenerNotFoundException();
            }
        }

        stopListening();

        try {
            eventClientDelegate.removeListenerOrSubscriber(clientId, listenerId);
        } catch (InstanceNotFoundException e) {
            logger.trace("unsubscribe", "removeSubscriber", e);
        } catch (EventClientNotFoundException cnfe) {
            logger.trace("unsubscribe", "removeSubscriber", cnfe);
        }
    }

    /**
     * @see javax.management.event.EventConsumer#subscribe(
     * ObjectName, NotificationListener, NotificationFilter, Object).
     */
    public void subscribe(ObjectName name,
            NotificationListener listener,
            NotificationFilter filter,
            Object handback) throws IOException {
        if (logger.traceOn()) {
            logger.trace("subscribe", "");
        }

        checkState();

        Integer listenerId;
        try {
            listenerId =
                    eventClientDelegate.addSubscriber(clientId, name, filter);
        } catch (EventClientNotFoundException ecnfe) {
            final IOException ioe = new IOException();
            ioe.initCause(ecnfe);
            throw ioe;
        }

        synchronized(listenerInfoMap) {
            listenerInfoMap.put(listenerId,  new ListenerInfo(
                    name,
                    listener,
                    filter,
                    handback,
                    true));
        }

        startListening();
    }

    /**
     * <p>Adds a set of listeners to the remote MBeanServer.  This method can
     * be used to copy the listeners from one {@code EventClient} to another.</p>
     *
     * <p>A listener is represented by a {@link ListenerInfo} object. The listener
     * is added by calling {@link #subscribe(ObjectName,
     * NotificationListener, NotificationFilter, Object)} if the method
     * {@link ListenerInfo#isSubscription() isSubscription}
     * returns {@code true}; otherwise it is added by calling
     * {@link #addNotificationListener(ObjectName, NotificationListener,
     * NotificationFilter, Object)}.</p>
     *
     * <P>The method returns the listeners which were added successfully. The
     * elements in the returned collection are a subset of the elements in
     * {@code infoList}. If all listeners were added successfully, the two
     * collections are the same. If no listener was added successfully, the
     * returned collection is empty.</p>
     *
     * @param listeners the listeners to add.
     *
     * @return The listeners that were added successfully.
     *
     * @throws IOException If an I/O error occurs.
     *
     * @see #getListeners()
     */
    public Collection<ListenerInfo> addListeners(Collection<ListenerInfo> listeners)
    throws IOException {
        if (logger.traceOn()) {
            logger.trace("addListeners", "");
        }

        checkState();

        if (listeners == null || listeners.isEmpty())
            return Collections.emptySet();

        final List<ListenerInfo> list = new ArrayList<ListenerInfo>();
        for (ListenerInfo l : listeners) {
            try {
                if (l.isSubscription()) {
                    subscribe(l.getObjectName(),
                            l.getListener(),
                            l.getFilter(),
                            l.getHandback());
                } else {
                    addNotificationListener(l.getObjectName(),
                            l.getListener(),
                            l.getFilter(),
                            l.getHandback());
                }

                list.add(l);
            } catch (Exception e) {
                if (logger.traceOn()) {
                    logger.trace("addListeners", "failed to add: "+l, e);
                }
            }
        }

        return list;
    }

    /**
     * Returns the set of listeners that have been added through
     * this {@code EventClient} and not subsequently removed.
     *
     * @return A collection of listener information. Empty if there are no
     * current listeners or if this {@code EventClient} has been {@linkplain
     * #close closed}.
     *
     * @see #addListeners
     */
    public Collection<ListenerInfo> getListeners() {
        if (logger.traceOn()) {
            logger.trace("getListeners", "");
        }

        synchronized(listenerInfoMap) {
            return Collections.unmodifiableCollection(listenerInfoMap.values());
        }
    }

    /**
     * Adds a listener to receive the {@code EventClient} notifications specified in
     * {@link #getEventClientNotificationInfo}.
     *
     * @param listener A listener to receive {@code EventClient} notifications.
     * @param filter A filter to select which notifications are to be delivered
     * to the listener, or {@code null} if all notifications are to be delivered.
     * @param handback An object to be given to the listener along with each
     * notification. Can be null.
     * @throws NullPointerException If listener is null.
     * @see #removeEventClientListener
     */
    public void addEventClientListener(NotificationListener listener,
            NotificationFilter filter,
            Object handback) {
        if (logger.traceOn()) {
            logger.trace("addEventClientListener", "");
        }
        broadcaster.addNotificationListener(listener, filter, handback);
    }

    /**
     * Removes a listener added to receive {@code EventClient} notifications specified in
     * {@link #getEventClientNotificationInfo}.
     *
     * @param listener A listener to receive {@code EventClient} notifications.
     * @throws NullPointerException If listener is null.
     * @throws ListenerNotFoundException If the listener is not added to
     * this {@code EventClient}.
     */
    public void removeEventClientListener(NotificationListener listener)
    throws ListenerNotFoundException {
        if (logger.traceOn()) {
            logger.trace("removeEventClientListener", "");
        }
        broadcaster.removeNotificationListener(listener);
    }

    /**
     * <p>Get the types of notification that an {@code EventClient} can send
     * to listeners added with {@link #addEventClientListener
     * addEventClientListener}.</p>
     *
     * @return Types of notification emitted by this {@code EventClient}.
     *
     * @see #FAILED
     * @see #NONFATAL
     * @see #NOTIFS_LOST
     */
    public MBeanNotificationInfo[] getEventClientNotificationInfo() {
        return myInfo.clone();
    }

    private static boolean match(ListenerInfo li,
            ObjectName name,
            NotificationListener listener,
            boolean subscribed) {
        return li.getObjectName().equals(name) &&
                li.getListener() == listener &&
                li.isSubscription() == subscribed;
    }

    private static boolean match(ListenerInfo li,
            ObjectName name,
            NotificationListener listener,
            NotificationFilter filter,
            Object handback,
            boolean subscribed) {
        return li.getObjectName().equals(name) &&
                li.getFilter() == filter &&
                li.getListener() == listener &&
                li.getHandback() == handback &&
                li.isSubscription() == subscribed;
    }

// ---------------------------------------------------
// private classes
// ---------------------------------------------------
    private class DispatchingJob extends RepeatedSingletonJob {
        public DispatchingJob() {
            super(distributingExecutor);
        }

        public boolean isSuspended() {
            return closed || buffer.size() == 0;
        }

        public void task() {
            TargetedNotification[] tns ;
            int lost = 0;

            synchronized(buffer) {
                tns = buffer.removeNotifs();
                lost = buffer.removeLost();
            }

            if ((tns == null || tns.length == 0)
            && lost == 0) {
                return;
            }

            // forwarding
            if (tns != null && tns.length > 0) {
                if (logger.traceOn()) {
                    logger.trace("DispatchingJob-task",
                            "Forwarding: "+tns.length);
                }
                for (TargetedNotification tn : tns) {
                    final ListenerInfo li = listenerInfoMap.get(tn.getListenerID());
                    try {
                        li.getListener().handleNotification(tn.getNotification(),
                                li.getHandback());
                    } catch (Exception e) {
                        logger.fine(
                                "DispatchingJob.task", "listener got exception", e);
                    }
                }
            }

            if (lost > 0) {
                if (logger.traceOn()) {
                    logger.trace("DispatchingJob-task",
                            "lost: "+lost);
                }
                final Notification n = new Notification(NOTIFS_LOST,
                        EventClient.this,
                        myNotifCounter.getAndIncrement(),
                        System.currentTimeMillis(),
                        "Lost notifications.");
                n.setUserData(new Long(lost));
                broadcaster.sendNotification(n);
            }
        }
    }


    private class EventReceiverImpl implements EventReceiver {
        public void receive(NotificationResult nr) {
            if (logger.traceOn()) {
                logger.trace("MyEventReceiver-receive", "");
            }

            synchronized(buffer) {
                buffer.addNotifs(nr);

                dispatchingJob.resume();
            }
        }

        public void failed(Throwable t) {
            if (logger.traceOn()) {
                logger.trace("MyEventReceiver-failed", "", t);
            }
            final Notification n = new Notification(FAILED,
                    this,
                    myNotifCounter.getAndIncrement(),
                    System.currentTimeMillis());
            n.setSource(t);
            broadcaster.sendNotification(n);
        }

        public void nonFatal(Exception e) {
            if (logger.traceOn()) {
                logger.trace("MyEventReceiver-nonFatal", "", e);
            }

            final Notification n = new Notification(NONFATAL,
                    this,
                    myNotifCounter.getAndIncrement(),
                    System.currentTimeMillis());
            n.setSource(e);
            broadcaster.sendNotification(n);
        }
    }

// ----------------------------------------------------
// private class
// ----------------------------------------------------


// ----------------------------------------------------
// private methods
// ----------------------------------------------------
    private Integer getListenerInfo(ObjectName name,
            NotificationListener listener,
            NotificationFilter filter,
            Object handback,
            boolean subscribed) throws ListenerNotFoundException {

        synchronized(listenerInfoMap) {
            for (Map.Entry<Integer, ListenerInfo> entry :
                    listenerInfoMap.entrySet()) {
                ListenerInfo li = entry.getValue();
                if (match(li, name, listener, filter, handback, subscribed)) {
                    return entry.getKey();
                }
            }
        }

        throw new ListenerNotFoundException();
    }

    private Integer getMatchedListenerInfo(ObjectName name,
            NotificationListener listener,
            boolean subscribed) throws ListenerNotFoundException {

        synchronized(listenerInfoMap) {
            for (Map.Entry<Integer, ListenerInfo> entry :
                    listenerInfoMap.entrySet()) {
                ListenerInfo li = entry.getValue();
                if (li.getObjectName().equals(name) &&
                        li.getListener() == listener &&
                        li.isSubscription() == subscribed) {
                    return entry.getKey();
                }
            }
        }

        throw new ListenerNotFoundException();
    }

    private Collection<Integer> getListenerInfo(ObjectName name,
            NotificationListener listener,
            boolean subscribed) throws ListenerNotFoundException {

        final ArrayList<Integer> ids = new ArrayList<Integer>();
        synchronized(listenerInfoMap) {
            for (Map.Entry<Integer, ListenerInfo> entry :
                    listenerInfoMap.entrySet()) {
                ListenerInfo li = entry.getValue();
                if (match(li, name, listener, subscribed)) {
                    ids.add(entry.getKey());
                }
            }
        }

        if (ids.isEmpty()) {
            throw new ListenerNotFoundException();
        }

        return ids;
    }

    private void checkState() throws IOException {
        synchronized(listenerInfoMap) {
            if (closed) {
                throw new IOException("Ended!");
            }
        }
    }

    private void startListening() throws IOException {
        synchronized(listenerInfoMap) {
            if (!startedListening && listenerInfoMap.size() > 0) {
                eventRelay.setEventReceiver(myReceiver);
            }

            startedListening = true;

            if (logger.traceOn()) {
                logger.trace("startListening", "listening");
            }
        }
    }

    private void stopListening() throws IOException {
        synchronized(listenerInfoMap) {
            if (listenerInfoMap.size() == 0 && startedListening) {
                eventRelay.setEventReceiver(null);

                startedListening = false;

                if (logger.traceOn()) {
                    logger.trace("stopListening", "non listening");
                }
            }
        }
    }

    private void removeListener(Integer id)
    throws InstanceNotFoundException,
            ListenerNotFoundException,
            IOException {
        synchronized(listenerInfoMap) {
            if (listenerInfoMap.remove(id) == null) {
                throw new ListenerNotFoundException();
            }

            stopListening();
        }

        try {
            eventClientDelegate.removeListenerOrSubscriber(clientId, id);
        } catch (EventClientNotFoundException cnfe) {
            logger.trace("removeListener", "ecd.removeListener", cnfe);
        }
    }


// ----------------------------------------------------
// private variables
// ----------------------------------------------------
    private static final ClassLogger logger =
            new ClassLogger("javax.management.event", "EventClient");

    private final Executor distributingExecutor;
    private final EventClientDelegateMBean eventClientDelegate;
    private final EventRelay eventRelay;
    private volatile String clientId = null;
    private final long requestedLeaseTime;

    private final ReceiverBuffer buffer = new ReceiverBuffer();

    private final EventReceiverImpl myReceiver =
            new EventReceiverImpl();
    private final DispatchingJob dispatchingJob;

    private final HashMap<Integer, ListenerInfo> listenerInfoMap =
            new HashMap<Integer, ListenerInfo>();

    private volatile boolean closed = false;

    private volatile boolean startedListening = false;

    // Could change synchronization here. But at worst a race will mean
    // sequence numbers are not contiguous, which may not matter much.
    private final AtomicLong myNotifCounter = new AtomicLong();

    private final static MBeanNotificationInfo[] myInfo =
            new MBeanNotificationInfo[] {
        new MBeanNotificationInfo(
                new String[] {FAILED, NOTIFS_LOST},
                Notification.class.getName(), "")};

    private final NotificationBroadcasterSupport broadcaster =
            new NotificationBroadcasterSupport();

    private final static Executor callerExecutor = new Executor() {
        // DirectExecutor using caller thread
        public void execute(Runnable r) {
            r.run();
        }
    };

    private static void checkInit(final MBeanServerConnection conn,
            final ObjectName delegateName)
            throws IOException {
        if (conn == null) {
            throw new IllegalArgumentException("No connection specified");
        }
        if (delegateName != null &&
                (!conn.isRegistered(delegateName))) {
            throw new IllegalArgumentException(
                    delegateName +
                    ": not found");
        }
        if (delegateName == null &&
                (!conn.isRegistered(
                EventClientDelegate.OBJECT_NAME))) {
            throw new IllegalArgumentException(
                    EventClientDelegate.OBJECT_NAME +
                    ": not found");
        }
    }

// ----------------------------------------------------
// private event lease issues
// ----------------------------------------------------
    private Callable<Long> renewLease = new Callable<Long>() {
        public Long call() throws IOException, EventClientNotFoundException {
            return eventClientDelegate.lease(clientId, requestedLeaseTime);
        }
    };

    private final LeaseRenewer leaseRenewer;

// ------------------------------------------------------------------------
    /**
     * Constructs an {@code MBeanServerConnection} that uses an {@code EventClient} object,
     * if the underlying connection has an {@link EventClientDelegateMBean}.
     * <P> The {@code EventClient} object creates a default
     * {@link FetchingEventRelay} object to
     * receive notifications forwarded by the {@link EventClientDelegateMBean}.
     * The {@link EventClientDelegateMBean} it works with is the
     * default one registered with the ObjectName
     * {@link EventClientDelegate#OBJECT_NAME
     * OBJECT_NAME}.
     * The thread from the {@link FetchingEventRelay} object that fetches the
     * notifications is also used to distribute them.
     *
     * @param conn An {@link MBeanServerConnection} object used to communicate
     * with an {@link EventClientDelegateMBean}.
     * @throws IllegalArgumentException If the value of {@code conn} is null,
     *         or the default {@link EventClientDelegateMBean} is not registered.
     * @throws IOException If an I/O error occurs.
     */
    public static MBeanServerConnection getEventClientConnection(
            final MBeanServerConnection conn)
            throws IOException {
        return getEventClientConnection(conn, null);
    }

    /**
     * Constructs an MBeanServerConnection that uses an {@code EventClient}
     * object with a user-specific {@link EventRelay}
     * object.
     * <P>
     * The {@link EventClientDelegateMBean} which it works with is the
     * default one registered with the ObjectName
     * {@link EventClientDelegate#OBJECT_NAME
     * OBJECT_NAME}
     * The thread that calls {@link EventReceiver#receive
     * EventReceiver.receive} from the {@link EventRelay} object is used
     * to distribute notifications to their listeners.
     *
     * @param conn An {@link MBeanServerConnection} object used to communicate
     * with an {@link EventClientDelegateMBean}.
     * @param eventRelay A user-specific object used to receive notifications
     * forwarded by the {@link EventClientDelegateMBean}. If null, the default
     * {@link FetchingEventRelay} object is used.
     * @throws IllegalArgumentException If the value of {@code conn} is null,
     *         or the default {@link EventClientDelegateMBean} is not registered.
     * @throws IOException If an I/O error occurs.
     */
    public static MBeanServerConnection getEventClientConnection(
            final MBeanServerConnection conn,
            final EventRelay eventRelay)
            throws IOException {

        if (newEventConn == null) {
            throw new IllegalArgumentException(
                    "Class not found: EventClientConnection");
        }

        checkInit(conn,null);
        final Callable<EventClient> factory = new Callable<EventClient>() {
            final public EventClient call() throws Exception {
                EventClientDelegateMBean ecd = EventClientDelegate.getProxy(conn);
                return new EventClient(ecd, eventRelay, null, null,
                        DEFAULT_LEASE_TIMEOUT);
            }
        };

        try {
            return (MBeanServerConnection)newEventConn.invoke(null,
                    conn, factory);
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }

    private static Method newEventConn = null;
    static {
        try {
            Class<?> c = Class.forName(
                    "com.sun.jmx.remote.util.EventClientConnection",
                    false, Thread.currentThread().getContextClassLoader());
            newEventConn = c.getMethod("getEventConnectionFor",
                    MBeanServerConnection.class, Callable.class);
        } catch (Exception e) {
            // OK: we're running in a subset of our classes
        }
    }

    /**
     * <p>Get the client id of this {@code EventClient} in the
     * {@link EventClientDelegateMBean}.
     *
     * @return the client id.
     *
     * @see EventClientDelegateMBean#addClient(String, Object[], String[])
     * EventClientDelegateMBean.addClient
     */
    public String getClientId() {
        return clientId;
    }

1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
    /**
     * Returns a JMX Connector that will use an {@link EventClient}
     * to subscribe for notifications. If the server doesn't have
     * an {@link EventClientDelegateMBean}, then the connector will
     * use the legacy notification mechanism instead.
     *
     * @param wrapped The underlying JMX Connector wrapped by the returned
     *               connector.
     *
     * @return A JMX Connector that will uses an {@link EventClient}, if
     *         available.
     *
     * @see EventClient#getEventClientConnection(MBeanServerConnection)
     */
    public static JMXConnector withEventClient(final JMXConnector wrapped) {
        return JMXNamespaceUtils.withEventClient(wrapped);
    }

1086 1087 1088
    private static final PerThreadGroupPool<ScheduledThreadPoolExecutor>
            leaseRenewerThreadPool = PerThreadGroupPool.make();
}