jdk/src/java.naming/share/classes/com/sun/jndi/ldap/EventSupport.java
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 25859 3317bb8137f4
child 30655 d83f50188ca9
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.jndi.ldap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.EventObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.naming.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.naming.directory.SearchControls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.naming.ldap.UnsolicitedNotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.naming.ldap.UnsolicitedNotificationEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.naming.ldap.UnsolicitedNotification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This is a utility class that can be used by a context that supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * event notification.  You can use an instance of this class as a member field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * of your context and delegate various work to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * It is currently structured so that each context should have its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * EventSupport (instead of static version shared by all contexts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * of a service provider).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This class supports two types of listeners: those that register for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * NamingEvents, and those for UnsolicitedNotificationEvents (they can be mixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * into the same listener).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * For NamingEvent listeners, it maintains a hashtable that maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * registration requests--the key--to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <em>notifiers</em>--the value. Each registration request consists of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *<li>The name argument of the registration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *<li>The filter (default is "(objectclass=*)").
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *<li>The search controls (default is null SearchControls).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *<li>The events that the listener is interested in. This is determined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * finding out which <tt>NamingListener</tt> interface the listener supports.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *A notifier (<tt>NamingEventNotifier</tt>) is a worker thread that is responsible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *for gathering information for generating events requested by its listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *Each notifier maintains its own list of listeners; these listeners have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *all made the same registration request (at different times) and implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *the same <tt>NamingListener</tt> interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *For unsolicited listeners, this class maintains a vector, unsolicited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *When an unsolicited listener is registered, this class adds itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *to the context's LdapClient. When LdapClient receives an unsolicited
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *notification, it notifies this EventSupport to fire an event to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *the listeners. Special handling in LdapClient is done for the DISCONNECT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *notification. [It results in the EventSupport firing also a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *NamingExceptionEvent to the unsolicited listeners.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *When a context no longer needs this EventSupport, it should invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *cleanup() on it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *<h4>Registration</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *When a registration request is made, this class attempts to find an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *existing notifier that's already working on the request. If one is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *found, the listener is added to the notifier's list. If one is not found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *a new notifier is created for the listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *<h4>Deregistration</h4>
25808
e113d0a0fde0 8054158: Fix typos in JNDI-related packages
prappo
parents: 10324
diff changeset
    86
 *When a deregistration request is made, this class attempts to find its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *corresponding notifier. If the notifier is found, the listener is removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *from the notifier's list. If the listener is the last listener on the list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *the notifier's thread is terminated and removed from this class's hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *Nothing happens if the notifier is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *<h4>Event Dispatching</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *The notifiers are responsible for gather information for generating events
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *requested by their respective listeners. When a notifier gets sufficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *information to generate an event, it creates invokes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *appropriate <tt>fireXXXEvent</tt> on this class with the information and list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *listeners. This causes an event and the list of listeners to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *to the <em>event queue</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *This class maintains an event queue and a dispatching thread that dequeues
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *events from the queue and dispatches them to the listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *<h4>Synchronization</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *This class is used by the main thread (LdapCtx) to add/remove listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *It is also used asynchronously by NamingEventNotifiers threads and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *the context's Connection thread. It is used by the notifier threads to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *queue events and to update the notifiers list when the notifiers exit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *It is used by the Connection thread to fire unsolicited notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *Methods that access/update the 'unsolicited' and 'notifiers' lists are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
final class EventSupport {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    final static private boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private LdapCtx ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * NamingEventNotifiers; hashed by search arguments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   121
    private Hashtable<NotifierArgs, NamingEventNotifier> notifiers =
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   122
            new Hashtable<>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * List of unsolicited notification listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   127
    private Vector<UnsolicitedNotificationListener> unsolicited = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Constructs EventSupport for ctx.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <em>Do we need to record the name of the target context?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Or can we assume that EventSupport is called on a resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * context? Do we need other add/remove-NamingListener methods?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * package private;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    EventSupport(LdapCtx ctx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        this.ctx = ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Adds <tt>l</tt> to list of listeners interested in <tt>nm</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Make the add/removeNamingListeners synchronized to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * 1. protect usage of 'unsolicited', which may be read by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *    the Connection thread when dispatching unsolicited notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * 2. ensure that NamingEventNotifier thread's access to 'notifiers'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *    is safe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    synchronized void addNamingListener(String nm, int scope,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        NamingListener l) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (l instanceof ObjectChangeListener ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            l instanceof NamespaceChangeListener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            NotifierArgs args = new NotifierArgs(nm, scope, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   157
            NamingEventNotifier notifier = notifiers.get(args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (notifier == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                notifier = new NamingEventNotifier(this, ctx, args, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                notifiers.put(args, notifier);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                notifier.addNamingListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (l instanceof UnsolicitedNotificationListener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // Add listener to this's list of unsolicited notifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if (unsolicited == null) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   168
                unsolicited = new Vector<>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   171
            unsolicited.addElement((UnsolicitedNotificationListener)l);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Adds <tt>l</tt> to list of listeners interested in <tt>nm</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * and filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    synchronized void addNamingListener(String nm, String filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        SearchControls ctls, NamingListener l) throws NamingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (l instanceof ObjectChangeListener ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            l instanceof NamespaceChangeListener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            NotifierArgs args = new NotifierArgs(nm, filter, ctls, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   186
            NamingEventNotifier notifier = notifiers.get(args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (notifier == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                notifier = new NamingEventNotifier(this, ctx, args, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                notifiers.put(args, notifier);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                notifier.addNamingListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (l instanceof UnsolicitedNotificationListener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            // Add listener to this's list of unsolicited notifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if (unsolicited == null) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   197
                unsolicited = new Vector<>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   199
            unsolicited.addElement((UnsolicitedNotificationListener)l);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Removes <tt>l</tt> from all notifiers in this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    synchronized void removeNamingListener(NamingListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (debug) System.err.println("EventSupport removing listener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        // Go through list of notifiers, remove 'l' from each.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // If 'l' is notifier's only listener, remove notifier too.
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   211
        for (NamingEventNotifier notifier : notifiers.values()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (notifier != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    System.err.println("EventSupport removing listener from notifier");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                notifier.removeNamingListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                if (!notifier.hasNamingListeners()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        System.err.println("EventSupport stopping notifier");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    notifier.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    notifiers.remove(notifier.info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        // Remove from list of unsolicited notifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (debug) System.err.println("EventSupport removing unsolicited: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            unsolicited);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (unsolicited != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            unsolicited.removeElement(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    synchronized boolean hasUnsolicited() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return (unsolicited != null && unsolicited.size() > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
      * package private;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
      * Called by NamingEventNotifier to remove itself when it encounters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
      * a NamingException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    synchronized void removeDeadNotifier(NotifierArgs info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            System.err.println("EventSupport.removeDeadNotifier: " + info.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        notifiers.remove(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Fire an event to unsolicited listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * package private;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Called by LdapCtx when its clnt receives an unsolicited notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    synchronized void fireUnsolicited(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            System.err.println("EventSupport.fireUnsolicited: " + obj + " "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                + unsolicited);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (unsolicited == null || unsolicited.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            // This shouldn't really happen, but might in case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            // there is a timing problem that removes a listener
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   263
            // before a fired event reaches here.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (obj instanceof UnsolicitedNotification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            // Fire UnsolicitedNotification to unsolicited listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            UnsolicitedNotificationEvent evt =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                new UnsolicitedNotificationEvent(ctx, (UnsolicitedNotification)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            queueEvent(evt, unsolicited);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        } else if (obj instanceof NamingException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            // Fire NamingExceptionEvent to unsolicited listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            NamingExceptionEvent evt =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                new NamingExceptionEvent(ctx, (NamingException)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            queueEvent(evt, unsolicited);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            // When an exception occurs, the unsolicited listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            // are automatically deregistered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            // When LdapClient.processUnsolicited() fires a NamingException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // it will update its listener list so we don't have to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            // Likewise for LdapCtx.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            unsolicited = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Stops notifier threads that are collecting event data and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * stops the event queue from dispatching events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Package private; used by LdapCtx.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    synchronized void cleanup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (debug) System.err.println("EventSupport clean up");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (notifiers != null) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   301
            for (NamingEventNotifier notifier : notifiers.values()) {
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   302
                notifier.stop();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            notifiers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (eventQueue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            eventQueue.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            eventQueue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        // %%% Should we fire NamingExceptionEvents to unsolicited listeners?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * The queue of events to be delivered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    private EventQueue eventQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Add the event and vector of listeners to the queue to be delivered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * An event dispatcher thread dequeues events from the queue and dispatches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * them to the registered listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Package private; used by NamingEventNotifier to fire events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   324
    synchronized void queueEvent(EventObject event,
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   325
                                 Vector<? extends NamingListener> vector) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (eventQueue == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            eventQueue = new EventQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         * Copy the vector in order to freeze the state of the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         * of EventListeners the event should be delivered to prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         * to delivery.  This ensures that any changes made to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         * Vector from a target listener's method during the delivery
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         * of this event will not take effect until after the event is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         * delivered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   337
        @SuppressWarnings("unchecked") // clone()
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   338
        Vector<NamingListener> v =
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   339
                (Vector<NamingListener>)vector.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        eventQueue.enqueue(event, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    // No finalize() needed because EventSupport is always owned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    // an LdapCtx. LdapCtx's finalize() and close() always call cleanup() so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    // there is no need for EventSupport to have a finalize().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
}