Path: blob/master/src/java.naming/share/classes/javax/naming/event/NamingEvent.java
41159 views
/*1* Copyright (c) 1999, 2019, 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 {@code NamingEvent}'s state consists of33* <ul>34* <li>The event source: the {@code EventContext} 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 {@code EventContext}44* <em>instance</em> that the listener has registered with.45* Furthermore, the names of the bindings in46* the {@code NamingEvent} 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* {@code NamingEvent} ({@code evt}) 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* {@code EventContext} 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 {@code 0}.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 {@code 1}.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 {@code NamingEvent} 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 {@code 2}.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 {@code 3}.117*/118public static final int OBJECT_CHANGED = 3;119120/**121* Contains information about the change that generated this event.122* @serial123*/124@SuppressWarnings("serial") // Not statically typed as Serializable125protected Object changeInfo;126127/**128* Contains the type of this event.129* @see #OBJECT_ADDED130* @see #OBJECT_REMOVED131* @see #OBJECT_RENAMED132* @see #OBJECT_CHANGED133* @serial134*/135protected int type;136137/**138* Contains information about the object before the change.139* @serial140*/141protected Binding oldBinding;142143/**144* Contains information about the object after the change.145* @serial146*/147protected Binding newBinding;148149/**150* Constructs an instance of {@code NamingEvent}.151*<p>152* The names in {@code newBd} and {@code oldBd} are to be resolved relative153* to the event source {@code source}.154*155* For an {@code OBJECT_ADDED} event type, {@code newBd} must not be null.156* For an {@code OBJECT_REMOVED} event type, {@code oldBd} must not be null.157* For an {@code OBJECT_CHANGED} event type, {@code newBd} and158* {@code oldBd} must not be null. For an {@code OBJECT_RENAMED} event type,159* one of {@code newBd} or {@code oldBd} may be null if the new or old160* binding is outside of the scope for which the listener has registered.161*162* @param source The non-null context that fired this event.163* @param type The type of the event.164* @param newBd A possibly null binding before the change. See method description.165* @param oldBd A possibly null binding after the change. See method description.166* @param changeInfo A possibly null object containing information about the change.167* @see #OBJECT_ADDED168* @see #OBJECT_REMOVED169* @see #OBJECT_RENAMED170* @see #OBJECT_CHANGED171*/172public NamingEvent(EventContext source, int type,173Binding newBd, Binding oldBd, Object changeInfo) {174super(source);175this.type = type;176oldBinding = oldBd;177newBinding = newBd;178this.changeInfo = changeInfo;179}180181/**182* Returns the type of this event.183* @return The type of this event.184* @see #OBJECT_ADDED185* @see #OBJECT_REMOVED186* @see #OBJECT_RENAMED187* @see #OBJECT_CHANGED188*/189public int getType() {190return type;191}192193/**194* Retrieves the event source that fired this event.195* This returns the same object as {@code EventObject.getSource()}.196*<p>197* If the result of this method is used to access the198* event source, for example, to look up the object or get its attributes,199* then it needs to be locked because implementations of {@code Context}200* are not guaranteed to be thread-safe201* (and {@code EventContext} is a subinterface of {@code Context}).202* See the203* <a href=package-summary.html#THREADING>package description</a>204* for more information on threading issues.205*206* @return The non-null context that fired this event.207*/208public EventContext getEventContext() {209return (EventContext)getSource();210}211212/**213* Retrieves the binding of the object before the change.214*<p>215* The binding must be nonnull if the object existed before the change216* relative to the source context ({@code getEventContext()}).217* That is, it must be nonnull for {@code OBJECT_REMOVED} and218* {@code OBJECT_CHANGED}.219* For {@code OBJECT_RENAMED}, it is null if the object before the rename220* is outside of the scope for which the listener has registered interest;221* it is nonnull if the object is inside the scope before the rename.222*<p>223* The name in the binding is to be resolved relative224* to the event source {@code getEventContext()}.225* The object returned by {@code Binding.getObject()} may be null if226* such information is unavailable.227*228* @return The possibly null binding of the object before the change.229*/230public Binding getOldBinding() {231return oldBinding;232}233234/**235* Retrieves the binding of the object after the change.236*<p>237* The binding must be nonnull if the object existed after the change238* relative to the source context ({@code getEventContext()}).239* That is, it must be nonnull for {@code OBJECT_ADDED} and240* {@code OBJECT_CHANGED}. For {@code OBJECT_RENAMED},241* it is null if the object after the rename is outside the scope for242* which the listener registered interest; it is nonnull if the object243* is inside the scope after the rename.244*<p>245* The name in the binding is to be resolved relative246* to the event source {@code getEventContext()}.247* The object returned by {@code Binding.getObject()} may be null if248* such information is unavailable.249*250* @return The possibly null binding of the object after the change.251*/252public Binding getNewBinding() {253return newBinding;254}255256/**257* Retrieves the change information for this event.258* The value of the change information is service-specific. For example,259* it could be an ID that identifies the change in a change log on the server.260*261* @return The possibly null change information of this event.262*/263public Object getChangeInfo() {264return changeInfo;265}266267/**268* Invokes the appropriate listener method on this event.269* The default implementation of270* this method handles the following event types:271* {@code OBJECT_ADDED, OBJECT_REMOVED,272* OBJECT_RENAMED, OBJECT_CHANGED}.273*<p>274* The listener method is executed in the same thread275* as this method. See the276* <a href=package-summary.html#THREADING>package description</a>277* for more information on threading issues.278* @param listener The nonnull listener.279*/280public void dispatch(NamingListener listener) {281switch (type) {282case OBJECT_ADDED:283((NamespaceChangeListener)listener).objectAdded(this);284break;285286case OBJECT_REMOVED:287((NamespaceChangeListener)listener).objectRemoved(this);288break;289290case OBJECT_RENAMED:291((NamespaceChangeListener)listener).objectRenamed(this);292break;293294case OBJECT_CHANGED:295((ObjectChangeListener)listener).objectChanged(this);296break;297}298}299private static final long serialVersionUID = -7126752885365133499L;300}301302303