1 /*
2 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.jndi.ldap;
27
28 import java.util.Hashtable;
29 import java.util.Vector;
30 import java.util.EventObject;
31
32 import javax.naming.*;
33 import javax.naming.event.*;
34 import javax.naming.directory.SearchControls;
35 import javax.naming.ldap.UnsolicitedNotificationListener;
36 import javax.naming.ldap.UnsolicitedNotificationEvent;
37 import javax.naming.ldap.UnsolicitedNotification;
38
39 /**
40 * This is a utility class that can be used by a context that supports
41 * event notification. You can use an instance of this class as a member field
42 * of your context and delegate various work to it.
43 * It is currently structured so that each context should have its own
44 * EventSupport (instead of static version shared by all contexts
45 * of a service provider).
46 *<p>
47 * This class supports two types of listeners: those that register for
48 * NamingEvents, and those for UnsolicitedNotificationEvents (they can be mixed
49 * into the same listener).
50 * For NamingEvent listeners, it maintains a hashtable that maps
187 if (notifier == null) {
188 notifier = new NamingEventNotifier(this, ctx, args, l);
189 notifiers.put(args, notifier);
190 } else {
191 notifier.addNamingListener(l);
192 }
193 }
194 if (l instanceof UnsolicitedNotificationListener) {
195 // Add listener to this's list of unsolicited notifiers
196 if (unsolicited == null) {
197 unsolicited = new Vector<>(3);
198 }
199 unsolicited.addElement((UnsolicitedNotificationListener)l);
200 }
201 }
202
203 /**
204 * Removes {@code l} from all notifiers in this context.
205 */
206 synchronized void removeNamingListener(NamingListener l) {
207 if (debug) System.err.println("EventSupport removing listener");
208
209 // Go through list of notifiers, remove 'l' from each.
210 // If 'l' is notifier's only listener, remove notifier too.
211 for (NamingEventNotifier notifier : notifiers.values()) {
212 if (notifier != null) {
213 if (debug)
214 System.err.println("EventSupport removing listener from notifier");
215 notifier.removeNamingListener(l);
216 if (!notifier.hasNamingListeners()) {
217 if (debug)
218 System.err.println("EventSupport stopping notifier");
219 notifier.stop();
220 notifiers.remove(notifier.info);
221 }
222 }
223 }
224
225 // Remove from list of unsolicited notifier
226 if (debug) System.err.println("EventSupport removing unsolicited: " +
227 unsolicited);
228 if (unsolicited != null) {
229 unsolicited.removeElement(l);
230 }
231
232 }
233
234 synchronized boolean hasUnsolicited() {
235 return (unsolicited != null && unsolicited.size() > 0);
236 }
237
238 /**
239 * package private;
240 * Called by NamingEventNotifier to remove itself when it encounters
241 * a NamingException.
242 */
243 synchronized void removeDeadNotifier(NotifierArgs info) {
244 if (debug) {
245 System.err.println("EventSupport.removeDeadNotifier: " + info.name);
246 }
247 notifiers.remove(info);
248 }
249
250 /**
251 * Fire an event to unsolicited listeners.
|
1 /*
2 * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.jndi.ldap;
27
28 import java.util.Hashtable;
29 import java.util.Vector;
30 import java.util.EventObject;
31 import java.util.Iterator;
32 import java.util.Map;
33
34 import javax.naming.*;
35 import javax.naming.event.*;
36 import javax.naming.directory.SearchControls;
37 import javax.naming.ldap.UnsolicitedNotificationListener;
38 import javax.naming.ldap.UnsolicitedNotificationEvent;
39 import javax.naming.ldap.UnsolicitedNotification;
40
41 /**
42 * This is a utility class that can be used by a context that supports
43 * event notification. You can use an instance of this class as a member field
44 * of your context and delegate various work to it.
45 * It is currently structured so that each context should have its own
46 * EventSupport (instead of static version shared by all contexts
47 * of a service provider).
48 *<p>
49 * This class supports two types of listeners: those that register for
50 * NamingEvents, and those for UnsolicitedNotificationEvents (they can be mixed
51 * into the same listener).
52 * For NamingEvent listeners, it maintains a hashtable that maps
189 if (notifier == null) {
190 notifier = new NamingEventNotifier(this, ctx, args, l);
191 notifiers.put(args, notifier);
192 } else {
193 notifier.addNamingListener(l);
194 }
195 }
196 if (l instanceof UnsolicitedNotificationListener) {
197 // Add listener to this's list of unsolicited notifiers
198 if (unsolicited == null) {
199 unsolicited = new Vector<>(3);
200 }
201 unsolicited.addElement((UnsolicitedNotificationListener)l);
202 }
203 }
204
205 /**
206 * Removes {@code l} from all notifiers in this context.
207 */
208 synchronized void removeNamingListener(NamingListener l) {
209 if (debug) {
210 System.err.println("EventSupport removing listener");
211 }
212 // Go through list of notifiers, remove 'l' from each.
213 // If 'l' is notifier's only listener, remove notifier too.
214 Iterator<Map.Entry<NotifierArgs, NamingEventNotifier>> allnotifiers;
215 allnotifiers = notifiers.entrySet().iterator();
216 while (allnotifiers.hasNext()) {
217 Map.Entry<NotifierArgs, NamingEventNotifier> entry = allnotifiers.next();
218 NamingEventNotifier notifier = entry.getValue();
219 if (notifier != null) {
220 if (debug) {
221 System.err.println("EventSupport removing listener from notifier");
222 }
223 notifier.removeNamingListener(l);
224 if (!notifier.hasNamingListeners()) {
225 if (debug) {
226 System.err.println("EventSupport stopping notifier");
227 }
228 notifier.stop();
229 allnotifiers.remove();
230 }
231 }
232 }
233 // Remove from list of unsolicited notifier
234 if (debug) {
235 System.err.println("EventSupport removing unsolicited: " + unsolicited);
236 }
237 if (unsolicited != null) {
238 unsolicited.removeElement(l);
239 }
240 }
241
242 synchronized boolean hasUnsolicited() {
243 return (unsolicited != null && unsolicited.size() > 0);
244 }
245
246 /**
247 * package private;
248 * Called by NamingEventNotifier to remove itself when it encounters
249 * a NamingException.
250 */
251 synchronized void removeDeadNotifier(NotifierArgs info) {
252 if (debug) {
253 System.err.println("EventSupport.removeDeadNotifier: " + info.name);
254 }
255 notifiers.remove(info);
256 }
257
258 /**
259 * Fire an event to unsolicited listeners.
|