Path: blob/master/src/java.naming/share/classes/javax/naming/event/EventContext.java
41159 views
/*1* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.naming.event;2627import javax.naming.Name;28import javax.naming.Context;29import javax.naming.NamingException;303132/**33* Contains methods for registering/deregistering listeners to be notified of34* events fired when objects named in a context changes.35*36*<h2>Target</h2>37* The name parameter in the {@code addNamingListener()} methods is referred38* to as the <em>target</em>. The target, along with the scope, identify39* the object(s) that the listener is interested in.40* It is possible to register interest in a target that does not exist, but41* there might be limitations in the extent to which this can be42* supported by the service provider and underlying protocol/service.43*<p>44* If a service only supports registration for existing45* targets, an attempt to register for a nonexistent target46* results in a {@code NameNotFoundException} being thrown as early as possible,47* preferably at the time {@code addNamingListener()} is called, or if that is48* not possible, the listener will receive the exception through the49* {@code NamingExceptionEvent}.50*<p>51* Also, for service providers that only support registration for existing52* targets, when the target that a listener has registered for is53* subsequently removed from the namespace, the listener is notified54* via a {@code NamingExceptionEvent} (containing a55*{@code NameNotFoundException}).56*<p>57* An application can use the method {@code targetMustExist()} to check58* whether an {@code EventContext} supports registration59* of nonexistent targets.60*61*<h2>Event Source</h2>62* The {@code EventContext} instance on which you invoke the63* registration methods is the <em>event source</em> of the events that are64* (potentially) generated.65* The source is <em>not necessarily</em> the object named by the target.66* Only when the target is the empty name is the object named by the target67* the source.68* In other words, the target,69* along with the scope parameter, are used to identify70* the object(s) that the listener is interested in, but the event source71* is the {@code EventContext} instance with which the listener72* has registered.73*<p>74* For example, suppose a listener makes the following registration:75*<blockquote><pre>76* NamespaceChangeListener listener = ...;77* src.addNamingListener("x", SUBTREE_SCOPE, listener);78*</pre></blockquote>79* When an object named "x/y" is subsequently deleted, the corresponding80* {@code NamingEvent} ({@code evt}) must contain:81*<blockquote><pre>82* evt.getEventContext() == src83* evt.getOldBinding().getName().equals("x/y")84*</pre></blockquote>85*<p>86* Furthermore, listener registration/deregistration is with87* the {@code EventContext}88* <em>instance</em>, and not with the corresponding object in the namespace.89* If the program intends at some point to remove a listener, then it needs to90* keep a reference to the {@code EventContext} instance on91* which it invoked {@code addNamingListener()} (just as92* it needs to keep a reference to the listener in order to remove it93* later). It cannot expect to do a {@code lookup()} and get another instance of94* an {@code EventContext} on which to perform the deregistration.95*<h2>Lifetime of Registration</h2>96* A registered listener becomes deregistered when:97*<ul>98*<li>It is removed using {@code removeNamingListener()}.99*<li>An exception is thrown while collecting information about the events.100* That is, when the listener receives a {@code NamingExceptionEvent}.101*<li>{@code Context.close()} is invoked on the {@code EventContext}102* instance with which it has registered.103</ul>104* Until that point, an {@code EventContext} instance that has outstanding105* listeners will continue to exist and be maintained by the service provider.106*107*<h2>Listener Implementations</h2>108* The registration/deregistration methods accept an instance of109* {@code NamingListener}. There are subinterfaces of {@code NamingListener}110* for different of event types of {@code NamingEvent}.111* For example, the {@code ObjectChangeListener}112* interface is for the {@code NamingEvent.OBJECT_CHANGED} event type.113* To register interest in multiple event types, the listener implementation114* should implement multiple {@code NamingListener} subinterfaces and use a115* single invocation of {@code addNamingListener()}.116* In addition to reducing the number of method calls and possibly the code size117* of the listeners, this allows some service providers to optimize the118* registration.119*120*<h2>Threading Issues</h2>121*122* Like {@code Context} instances in general, instances of123* {@code EventContext} are not guaranteed to be thread-safe.124* Care must be taken when multiple threads are accessing the same125* {@code EventContext} concurrently.126* See the127* <a href=package-summary.html#THREADING>package description</a>128* for more information on threading issues.129*130* @author Rosanna Lee131* @author Scott Seligman132* @since 1.3133*/134135public interface EventContext extends Context {136/**137* Constant for expressing interest in events concerning the object named138* by the target.139*<p>140* The value of this constant is {@code 0}.141*/142public static final int OBJECT_SCOPE = 0;143144/**145* Constant for expressing interest in events concerning objects146* in the context named by the target,147* excluding the context named by the target.148*<p>149* The value of this constant is {@code 1}.150*/151public static final int ONELEVEL_SCOPE = 1;152153/**154* Constant for expressing interest in events concerning objects155* in the subtree of the object named by the target, including the object156* named by the target.157*<p>158* The value of this constant is {@code 2}.159*/160public static final int SUBTREE_SCOPE = 2;161162163/**164* Adds a listener for receiving naming events fired165* when the object(s) identified by a target and scope changes.166*167* The event source of those events is this context. See the168* class description for a discussion on event source and target.169* See the descriptions of the constants {@code OBJECT_SCOPE},170* {@code ONELEVEL_SCOPE}, and {@code SUBTREE_SCOPE} to see how171* {@code scope} affects the registration.172*<p>173* {@code target} needs to name a context only when {@code scope} is174* {@code ONELEVEL_SCOPE}.175* {@code target} may name a non-context if {@code scope} is either176* {@code OBJECT_SCOPE} or {@code SUBTREE_SCOPE}. Using177* {@code SUBTREE_SCOPE} for a non-context might be useful,178* for example, if the caller does not know in advance whether {@code target}179* is a context and just wants to register interest in the (possibly180* degenerate subtree) rooted at {@code target}.181*<p>182* When the listener is notified of an event, the listener may183* in invoked in a thread other than the one in which184* {@code addNamingListener()} is executed.185* Care must be taken when multiple threads are accessing the same186* {@code EventContext} concurrently.187* See the188* <a href=package-summary.html#THREADING>package description</a>189* for more information on threading issues.190*191* @param target A nonnull name to be resolved relative to this context.192* @param scope One of {@code OBJECT_SCOPE}, {@code ONELEVEL_SCOPE}, or193* {@code SUBTREE_SCOPE}.194* @param l The nonnull listener.195* @throws NamingException If a problem was encountered while196* adding the listener.197* @see #removeNamingListener198*/199void addNamingListener(Name target, int scope, NamingListener l)200throws NamingException;201202/**203* Adds a listener for receiving naming events fired204* when the object named by the string target name and scope changes.205*206* See the overload that accepts a {@code Name} for details.207*208* @param target The nonnull string name of the object resolved relative209* to this context.210* @param scope One of {@code OBJECT_SCOPE}, {@code ONELEVEL_SCOPE}, or211* {@code SUBTREE_SCOPE}.212* @param l The nonnull listener.213* @throws NamingException If a problem was encountered while214* adding the listener.215* @see #removeNamingListener216*/217void addNamingListener(String target, int scope, NamingListener l)218throws NamingException;219220/**221* Removes a listener from receiving naming events fired222* by this {@code EventContext}.223* The listener may have registered more than once with this224* {@code EventContext}, perhaps with different target/scope arguments.225* After this method is invoked, the listener will no longer226* receive events with this {@code EventContext} instance227* as the event source (except for those events already in the process of228* being dispatched).229* If the listener was not, or is no longer, registered with230* this {@code EventContext} instance, this method does not do anything.231*232* @param l The nonnull listener.233* @throws NamingException If a problem was encountered while234* removing the listener.235* @see #addNamingListener236*/237void removeNamingListener(NamingListener l) throws NamingException;238239/**240* Determines whether a listener can register interest in a target241* that does not exist.242*243* @return true if the target must exist; false if the target need not exist.244* @throws NamingException If the context's behavior in this regard cannot245* be determined.246*/247boolean targetMustExist() throws NamingException;248}249250251