1. /*
  2. * @(#)NamingEvent.java 1.10 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.naming.event;
  8. import javax.naming.Binding;
  9. /**
  10. * This class represents an event fired by a naming/directory service.
  11. *<p>
  12. * The <tt>NamingEvent</tt>'s state consists of
  13. * <ul>
  14. * <li>The event source: the <tt>EventContext</tt> which fired this event.
  15. * <li>The event type.
  16. * <li>The new binding: information about the object after the change.
  17. * <li>The old binding: information about the object before the change.
  18. * <li>Change information: information about the change
  19. * that triggered this event; usually service provider-specific or server-specific
  20. * information.
  21. * </ul>
  22. * <p>
  23. * Note that the event source is always the same <tt>EventContext</tt>
  24. * <em>instance</em> that the listener has registered with.
  25. * Furthermore, the names of the bindings in
  26. * the <tt>NamingEvent</tt> are always relative to that instance.
  27. * For example, suppose a listener makes the following registration:
  28. *<blockquote><pre>
  29. * NamespaceChangeListener listener = ...;
  30. * src.addNamingListener("x", SUBTREE_SCOPE, listener);
  31. *</pre></blockquote>
  32. * When an object named "x/y" is subsequently deleted, the corresponding
  33. * <tt>NamingEvent</tt> (<tt>evt</tt>) must contain:
  34. *<blockquote><pre>
  35. * evt.getEventContext() == src
  36. * evt.getOldBinding().getName().equals("x/y")
  37. *</pre></blockquote>
  38. *
  39. * Care must be taken when multiple threads are accessing the same
  40. * <tt>EventContext</tt> concurrently.
  41. * See the
  42. * <a href=package-summary.html#THREADING>package description</a>
  43. * for more information on threading issues.
  44. *
  45. * @author Rosanna Lee
  46. * @author Scott Seligman
  47. * @version 1.10 03/12/19
  48. *
  49. * @see NamingListener
  50. * @see EventContext
  51. * @since 1.3
  52. */
  53. public class NamingEvent extends java.util.EventObject {
  54. /**
  55. * Naming event type for indicating that a new object has been added.
  56. * The value of this constant is <tt>0</tt>.
  57. */
  58. public static final int OBJECT_ADDED = 0;
  59. /**
  60. * Naming event type for indicating that an object has been removed.
  61. * The value of this constant is <tt>1</tt>.
  62. */
  63. public static final int OBJECT_REMOVED = 1;
  64. /**
  65. * Naming event type for indicating that an object has been renamed.
  66. * Note that some services might fire multiple events for a single
  67. * logical rename operation. For example, the rename operation might
  68. * be implemented by adding a binding with the new name and removing
  69. * the old binding.
  70. *<p>
  71. * The old/new binding in <tt>NamingEvent</tt> may be null if the old
  72. * name or new name is outside of the scope for which the listener
  73. * has registered.
  74. *<p>
  75. * When an interior node in the namespace tree has been renamed, the
  76. * topmost node which is part of the listener's scope should used to generate
  77. * a rename event. The extent to which this can be supported is
  78. * provider-specific. For example, a service might generate rename
  79. * notifications for all descendants of the changed interior node and the
  80. * corresponding provider might not be able to prevent those
  81. * notifications from being propagated to the listeners.
  82. *<p>
  83. * The value of this constant is <tt>2</tt>.
  84. */
  85. public static final int OBJECT_RENAMED = 2;
  86. /**
  87. * Naming event type for indicating that an object has been changed.
  88. * The changes might include the object's attributes, or the object itself.
  89. * Note that some services might fire multiple events for a single
  90. * modification. For example, the modification might
  91. * be implemented by first removing the old binding and adding
  92. * a new binding containing the same name but a different object.
  93. *<p>
  94. * The value of this constant is <tt>3</tt>.
  95. */
  96. public static final int OBJECT_CHANGED = 3;
  97. /**
  98. * Contains information about the change that generated this event.
  99. * @serial
  100. */
  101. protected Object changeInfo;
  102. /**
  103. * Contains the type of this event.
  104. * @see #OBJECT_ADDED
  105. * @see #OBJECT_REMOVED
  106. * @see #OBJECT_RENAMED
  107. * @see #OBJECT_CHANGED
  108. * @serial
  109. */
  110. protected int type;
  111. /**
  112. * Contains information about the object before the change.
  113. * @serial
  114. */
  115. protected Binding oldBinding;
  116. /**
  117. * Contains information about the object after the change.
  118. * @serial
  119. */
  120. protected Binding newBinding;
  121. /**
  122. * Constructs an instance of <tt>NamingEvent</tt>.
  123. *<p>
  124. * The names in <tt>newBd</tt> and <tt>oldBd</tt> are to be resolved relative
  125. * to the event source <tt>source</tt>.
  126. *
  127. * For an <tt>OBJECT_ADDED</tt> event type, <tt>newBd</tt> must not be null.
  128. * For an <tt>OBJECT_REMOVED</tt> event type, <tt>oldBd</tt> must not be null.
  129. * For an <tt>OBJECT_CHANGED</tt> event type, <tt>newBd</tt> and
  130. * <tt>oldBd</tt> must not be null. For an <tt>OBJECT_RENAMED</tt> event type,
  131. * one of <tt>newBd</tt> or <tt>oldBd</tt> may be null if the new or old
  132. * binding is outside of the scope for which the listener has registered.
  133. *
  134. * @param source The non-null context that fired this event.
  135. * @param type The type of the event.
  136. * @param newBd A possibly null binding before the change. See method description.
  137. * @param oldBd A possibly null binding after the change. See method description.
  138. * @param changeInfo A possibly null object containing information about the change.
  139. * @see #OBJECT_ADDED
  140. * @see #OBJECT_REMOVED
  141. * @see #OBJECT_RENAMED
  142. * @see #OBJECT_CHANGED
  143. */
  144. public NamingEvent(EventContext source, int type,
  145. Binding newBd, Binding oldBd, Object changeInfo) {
  146. super(source);
  147. this.type = type;
  148. oldBinding = oldBd;
  149. newBinding = newBd;
  150. this.changeInfo = changeInfo;
  151. }
  152. /**
  153. * Returns the type of this event.
  154. * @return The type of this event.
  155. * @see #OBJECT_ADDED
  156. * @see #OBJECT_REMOVED
  157. * @see #OBJECT_RENAMED
  158. * @see #OBJECT_CHANGED
  159. */
  160. public int getType() {
  161. return type;
  162. }
  163. /**
  164. * Retrieves the event source that fired this event.
  165. * This returns the same object as <tt>EventObject.getSource()</tt>.
  166. *<p>
  167. * If the result of this method is used to access the
  168. * event source, for example, to look up the object or get its attributes,
  169. * then it needs to be locked because implementations of <tt>Context</tt>
  170. * are not guaranteed to be thread-safe
  171. * (and <tt>EventContext</tt> is a subinterface of <tt>Context</tt>).
  172. * See the
  173. * <a href=package-summary.html#THREADING>package description</a>
  174. * for more information on threading issues.
  175. *
  176. * @return The non-null context that fired this event.
  177. */
  178. public EventContext getEventContext() {
  179. return (EventContext)getSource();
  180. }
  181. /**
  182. * Retrieves the binding of the object before the change.
  183. *<p>
  184. * The binding must be nonnull if the object existed before the change
  185. * relative to the source context (<tt>getEventContext()</tt>).
  186. * That is, it must be nonnull for <tt>OBJECT_REMOVED</tt> and
  187. * <tt>OBJECT_CHANGED</tt>.
  188. * For <tt>OBJECT_RENAMED</tt>, it is null if the object before the rename
  189. * is outside of the scope for which the listener has registered interest;
  190. * it is nonnull if the object is inside the scope before the rename.
  191. *<p>
  192. * The name in the binding is to be resolved relative
  193. * to the event source <tt>getEventContext()</tt>.
  194. * The object returned by <tt>Binding.getObject()</tt> may be null if
  195. * such information is unavailable.
  196. *
  197. * @return The possibly null binding of the object before the change.
  198. */
  199. public Binding getOldBinding() {
  200. return oldBinding;
  201. }
  202. /**
  203. * Retrieves the binding of the object after the change.
  204. *<p>
  205. * The binding must be nonnull if the object existed after the change
  206. * relative to the source context (<tt>getEventContext()</tt>).
  207. * That is, it must be nonnull for <tt>OBJECT_ADDED</tt> and
  208. * <tt>OBJECT_CHANGED</tt>. For <tt>OBJECT_RENAMED</tt>,
  209. * it is null if the object after the rename is outside the scope for
  210. * which the listener registered interest; it is nonnull if the object
  211. * is inside the scope after the rename.
  212. *<p>
  213. * The name in the binding is to be resolved relative
  214. * to the event source <tt>getEventContext()</tt>.
  215. * The object returned by <tt>Binding.getObject()</tt> may be null if
  216. * such information is unavailable.
  217. *
  218. * @return The possibly null binding of the object after the change.
  219. */
  220. public Binding getNewBinding() {
  221. return newBinding;
  222. }
  223. /**
  224. * Retrieves the change information for this event.
  225. * The value of the change information is service-specific. For example,
  226. * it could be an ID that identifies the change in a change log on the server.
  227. *
  228. * @return The possibly null change information of this event.
  229. */
  230. public Object getChangeInfo() {
  231. return changeInfo;
  232. }
  233. /**
  234. * Invokes the appropriate listener method on this event.
  235. * The default implementation of
  236. * this method handles the following event types:
  237. * <tt>OBJECT_ADDED</TT>, <TT>OBJECT_REMOVED</TT>,
  238. * <TT>OBJECT_RENAMED</TT>, <TT>OBJECT_CHANGED</TT>.
  239. *<p>
  240. * The listener method is executed in the same thread
  241. * as this method. See the
  242. * <a href=package-summary.html#THREADING>package description</a>
  243. * for more information on threading issues.
  244. * @param listener The nonnull listener.
  245. */
  246. public void dispatch(NamingListener listener) {
  247. switch (type) {
  248. case OBJECT_ADDED:
  249. ((NamespaceChangeListener)listener).objectAdded(this);
  250. break;
  251. case OBJECT_REMOVED:
  252. ((NamespaceChangeListener)listener).objectRemoved(this);
  253. break;
  254. case OBJECT_RENAMED:
  255. ((NamespaceChangeListener)listener).objectRenamed(this);
  256. break;
  257. case OBJECT_CHANGED:
  258. ((ObjectChangeListener)listener).objectChanged(this);
  259. break;
  260. }
  261. }
  262. private static final long serialVersionUID = -7126752885365133499L;
  263. }