jdk/src/share/classes/com/sun/jndi/ldap/EventQueue.java
author prappo
Fri, 01 Aug 2014 22:32:51 +0100
changeset 25808 e113d0a0fde0
parent 10324 e28265130e4f
permissions -rw-r--r--
8054158: Fix typos in JNDI-related packages Reviewed-by: rriggs, vinnie
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.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.EventObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.naming.event.NamingEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.naming.event.NamingExceptionEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.naming.event.NamingListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.naming.ldap.UnsolicitedNotificationEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.naming.ldap.UnsolicitedNotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Package private class used by EventSupport to dispatch events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class implements an event queue, and a dispatcher thread that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * dequeues and dispatches events from the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Pieces stolen from sun.misc.Queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author      Bill Shannon (from javax.mail.event)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author      Rosanna Lee (modified for JNDI-related events)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
final class EventQueue implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    final static private boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static class QueueElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        QueueElement next = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        QueueElement prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        EventObject event = null;
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    54
        Vector<NamingListener> vector = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    56
        QueueElement(EventObject event, Vector<NamingListener> vector) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            this.event = event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            this.vector = vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private QueueElement head = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private QueueElement tail = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private Thread qThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // package private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    EventQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        qThread = Obj.helper.createThread(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        qThread.setDaemon(true);  // not a user thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        qThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // package private;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Enqueue an event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param event Either a <tt>NamingExceptionEvent</tt> or a subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *              of <tt>NamingEvent</tt> or
25808
e113d0a0fde0 8054158: Fix typos in JNDI-related packages
prappo
parents: 10324
diff changeset
    78
     * <tt>UnsolicitedNotificationEvent</tt>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * If it is a subclass of <tt>NamingEvent</tt>, all listeners must implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * the corresponding subinterface of <tt>NamingListener</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * For example, for a <tt>ObjectAddedEvent</tt>, all listeners <em>must</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * implement the <tt>ObjectAddedListener</tt> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * <em>The current implementation does not check this before dispatching
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * the event.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * If the event is a <tt>NamingExceptionEvent</tt>, then all listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * are notified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param vector List of NamingListeners that will be notified of event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    89
    synchronized void enqueue(EventObject event, Vector<NamingListener> vector) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        QueueElement newElt = new QueueElement(event, vector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (head == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            head = newElt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            tail = newElt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            newElt.next = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            head.prev = newElt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            head = newElt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Dequeue the oldest object on the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Used only by the run() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @return    the oldest object on the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @exception java.lang.InterruptedException if any thread has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *              interrupted this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private synchronized QueueElement dequeue()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        while (tail == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        QueueElement elt = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        tail = elt.prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (tail == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            head = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            tail.next = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        elt.prev = elt.next = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return elt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Pull events off the queue and dispatch them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        QueueElement qe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            while ((qe = dequeue()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                EventObject e = qe.event;
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   135
                Vector<NamingListener> v = qe.vector;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                for (int i = 0; i < v.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    // Dispatch to corresponding NamingListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    // The listener should only be getting the event that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    // it is interested in. (No need to check mask or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    // instanceof subinterfaces.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    // It is the responsibility of the enqueuer to
25808
e113d0a0fde0 8054158: Fix typos in JNDI-related packages
prappo
parents: 10324
diff changeset
   144
                    // only enqueue events with listeners of the correct type.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    if (e instanceof NamingEvent) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   147
                        ((NamingEvent)e).dispatch(v.elementAt(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    // An exception occurred: if notify all naming listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    } else if (e instanceof NamingExceptionEvent) {
10324
e28265130e4f 7072353: JNDI libraries do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
   151
                        ((NamingExceptionEvent)e).dispatch(v.elementAt(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    } else if (e instanceof UnsolicitedNotificationEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        ((UnsolicitedNotificationEvent)e).dispatch(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                            (UnsolicitedNotificationListener)v.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                qe = null; e = null; v = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            // just die
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // package private; used by EventSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Stop the dispatcher so we can be destroyed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    void stop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (debug) System.err.println("EventQueue stopping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (qThread != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            qThread.interrupt();        // kill our thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            qThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
}