Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/naming/event/NamingEvent.java
38830 views
/*1* Copyright (c) 1999, 2000, 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.Binding;2829/**30* This class represents an event fired by a naming/directory service.31*<p>32* The <tt>NamingEvent</tt>'s state consists of33* <ul>34* <li>The event source: the <tt>EventContext</tt> which fired this event.35* <li>The event type.36* <li>The new binding: information about the object after the change.37* <li>The old binding: information about the object before the change.38* <li>Change information: information about the change39* that triggered this event; usually service provider-specific or server-specific40* information.41* </ul>42* <p>43* Note that the event source is always the same <tt>EventContext</tt>44* <em>instance</em> that the listener has registered with.45* Furthermore, the names of the bindings in46* the <tt>NamingEvent</tt> are always relative to that instance.47* For example, suppose a listener makes the following registration:48*<blockquote><pre>49* NamespaceChangeListener listener = ...;50* src.addNamingListener("x", SUBTREE_SCOPE, listener);51*</pre></blockquote>52* When an object named "x/y" is subsequently deleted, the corresponding53* <tt>NamingEvent</tt> (<tt>evt</tt>) must contain:54*<blockquote><pre>55* evt.getEventContext() == src56* evt.getOldBinding().getName().equals("x/y")57*</pre></blockquote>58*59* Care must be taken when multiple threads are accessing the same60* <tt>EventContext</tt> concurrently.61* See the62* <a href=package-summary.html#THREADING>package description</a>63* for more information on threading issues.64*65* @author Rosanna Lee66* @author Scott Seligman67*68* @see NamingListener69* @see EventContext70* @since 1.371*/72public class NamingEvent extends java.util.EventObject {73/**74* Naming event type for indicating that a new object has been added.75* The value of this constant is <tt>0</tt>.76*/77public static final int OBJECT_ADDED = 0;7879/**80* Naming event type for indicating that an object has been removed.81* The value of this constant is <tt>1</tt>.82*/83public static final int OBJECT_REMOVED = 1;8485/**86* Naming event type for indicating that an object has been renamed.87* Note that some services might fire multiple events for a single88* logical rename operation. For example, the rename operation might89* be implemented by adding a binding with the new name and removing90* the old binding.91*<p>92* The old/new binding in <tt>NamingEvent</tt> may be null if the old93* name or new name is outside of the scope for which the listener94* has registered.95*<p>96* When an interior node in the namespace tree has been renamed, the97* topmost node which is part of the listener's scope should used to generate98* a rename event. The extent to which this can be supported is99* provider-specific. For example, a service might generate rename100* notifications for all descendants of the changed interior node and the101* corresponding provider might not be able to prevent those102* notifications from being propagated to the listeners.103*<p>104* The value of this constant is <tt>2</tt>.105*/106public static final int OBJECT_RENAMED = 2;107108/**109* Naming event type for indicating that an object has been changed.110* The changes might include the object's attributes, or the object itself.111* Note that some services might fire multiple events for a single112* modification. For example, the modification might113* be implemented by first removing the old binding and adding114* a new binding containing the same name but a different object.115*<p>116* The value of this constant is <tt>3</tt>.117*/118public static final int OBJECT_CHANGED = 3;119120/**121* Contains information about the change that generated this event.122* @serial123*/124protected Object changeInfo;125126/**127* Contains the type of this event.128* @see #OBJECT_ADDED129* @see #OBJECT_REMOVED130* @see #OBJECT_RENAMED131* @see #OBJECT_CHANGED132* @serial133*/134protected int type;135136/**137* Contains information about the object before the change.138* @serial139*/140protected Binding oldBinding;141142/**143* Contains information about the object after the change.144* @serial145*/146protected Binding newBinding;147148/**149* Constructs an instance of <tt>NamingEvent</tt>.150*<p>151* The names in <tt>newBd</tt> and <tt>oldBd</tt> are to be resolved relative152* to the event source <tt>source</tt>.153*154* For an <tt>OBJECT_ADDED</tt> event type, <tt>newBd</tt> must not be null.155* For an <tt>OBJECT_REMOVED</tt> event type, <tt>oldBd</tt> must not be null.156* For an <tt>OBJECT_CHANGED</tt> event type, <tt>newBd</tt> and157* <tt>oldBd</tt> must not be null. For an <tt>OBJECT_RENAMED</tt> event type,158* one of <tt>newBd</tt> or <tt>oldBd</tt> may be null if the new or old159* binding is outside of the scope for which the listener has registered.160*161* @param source The non-null context that fired this event.162* @param type The type of the event.163* @param newBd A possibly null binding before the change. See method description.164* @param oldBd A possibly null binding after the change. See method description.165* @param changeInfo A possibly null object containing information about the change.166* @see #OBJECT_ADDED167* @see #OBJECT_REMOVED168* @see #OBJECT_RENAMED169* @see #OBJECT_CHANGED170*/171public NamingEvent(EventContext source, int type,172Binding newBd, Binding oldBd, Object changeInfo) {173super(source);174this.type = type;175oldBinding = oldBd;176newBinding = newBd;177this.changeInfo = changeInfo;178}179180/**181* Returns the type of this event.182* @return The type of this event.183* @see #OBJECT_ADDED184* @see #OBJECT_REMOVED185* @see #OBJECT_RENAMED186* @see #OBJECT_CHANGED187*/188public int getType() {189return type;190}191192/**193* Retrieves the event source that fired this event.194* This returns the same object as <tt>EventObject.getSource()</tt>.195*<p>196* If the result of this method is used to access the197* event source, for example, to look up the object or get its attributes,198* then it needs to be locked because implementations of <tt>Context</tt>199* are not guaranteed to be thread-safe200* (and <tt>EventContext</tt> is a subinterface of <tt>Context</tt>).201* See the202* <a href=package-summary.html#THREADING>package description</a>203* for more information on threading issues.204*205* @return The non-null context that fired this event.206*/207public EventContext getEventContext() {208return (EventContext)getSource();209}210211/**212* Retrieves the binding of the object before the change.213*<p>214* The binding must be nonnull if the object existed before the change215* relative to the source context (<tt>getEventContext()</tt>).216* That is, it must be nonnull for <tt>OBJECT_REMOVED</tt> and217* <tt>OBJECT_CHANGED</tt>.218* For <tt>OBJECT_RENAMED</tt>, it is null if the object before the rename219* is outside of the scope for which the listener has registered interest;220* it is nonnull if the object is inside the scope before the rename.221*<p>222* The name in the binding is to be resolved relative223* to the event source <tt>getEventContext()</tt>.224* The object returned by <tt>Binding.getObject()</tt> may be null if225* such information is unavailable.226*227* @return The possibly null binding of the object before the change.228*/229public Binding getOldBinding() {230return oldBinding;231}232233/**234* Retrieves the binding of the object after the change.235*<p>236* The binding must be nonnull if the object existed after the change237* relative to the source context (<tt>getEventContext()</tt>).238* That is, it must be nonnull for <tt>OBJECT_ADDED</tt> and239* <tt>OBJECT_CHANGED</tt>. For <tt>OBJECT_RENAMED</tt>,240* it is null if the object after the rename is outside the scope for241* which the listener registered interest; it is nonnull if the object242* is inside the scope after the rename.243*<p>244* The name in the binding is to be resolved relative245* to the event source <tt>getEventContext()</tt>.246* The object returned by <tt>Binding.getObject()</tt> may be null if247* such information is unavailable.248*249* @return The possibly null binding of the object after the change.250*/251public Binding getNewBinding() {252return newBinding;253}254255/**256* Retrieves the change information for this event.257* The value of the change information is service-specific. For example,258* it could be an ID that identifies the change in a change log on the server.259*260* @return The possibly null change information of this event.261*/262public Object getChangeInfo() {263return changeInfo;264}265266/**267* Invokes the appropriate listener method on this event.268* The default implementation of269* this method handles the following event types:270* <tt>OBJECT_ADDED</TT>, <TT>OBJECT_REMOVED</TT>,271* <TT>OBJECT_RENAMED</TT>, <TT>OBJECT_CHANGED</TT>.272*<p>273* The listener method is executed in the same thread274* as this method. See the275* <a href=package-summary.html#THREADING>package description</a>276* for more information on threading issues.277* @param listener The nonnull listener.278*/279public void dispatch(NamingListener listener) {280switch (type) {281case OBJECT_ADDED:282((NamespaceChangeListener)listener).objectAdded(this);283break;284285case OBJECT_REMOVED:286((NamespaceChangeListener)listener).objectRemoved(this);287break;288289case OBJECT_RENAMED:290((NamespaceChangeListener)listener).objectRenamed(this);291break;292293case OBJECT_CHANGED:294((ObjectChangeListener)listener).objectChanged(this);295break;296}297}298private static final long serialVersionUID = -7126752885365133499L;299}300301302