1. /*
  2. * @(#)EventContext.java 1.9 01/02/09
  3. *
  4. * Copyright 1999-2001 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.naming.event;
  11. import javax.naming.Name;
  12. import javax.naming.Context;
  13. import javax.naming.NamingException;
  14. /**
  15. * Contains methods for registering/deregistering listeners to be notified of
  16. * events fired when objects named in a context changes.
  17. *<p>
  18. *<h4>Target</h4>
  19. * The name parameter in the <tt>addNamingListener()</tt> methods is referred
  20. * to as the <em>target</em>. The target, along with the scope, identify
  21. * the object(s) that the listener is interested in.
  22. * It is possible to register interest in a target that does not exist, but
  23. * there might be limitations in the extent to which this can be
  24. * supported by the service provider and underlying protocol/service.
  25. *<p>
  26. * If a service only supports registration for existing
  27. * targets, an attempt to register for a nonexistent target
  28. * results in a <tt>NameNotFoundException</tt> being thrown as early as possible,
  29. * preferably at the time <tt>addNamingListener()</tt> is called, or if that is
  30. * not possible, the listener will receive the exception through the
  31. * <tt>NamingExceptionEvent</tt>.
  32. *<p>
  33. * Also, for service providers that only support registration for existing
  34. * targets, when the target that a listener has registered for is
  35. * subsequently removed from the namespace, the listener is notified
  36. * via a <tt>NamingExceptionEvent</tt> (containing a
  37. *<tt>NameNotFoundException</tt>).
  38. *<p>
  39. * An application can use the method <tt>targetMustExist()</tt> to check
  40. * whether a <tt>EventContext</tt> supports registration
  41. * of nonexistent targets.
  42. *<p>
  43. *<h4>Event Source</h4>
  44. * The <tt>EventContext</tt> instance on which you invoke the
  45. * registration methods is the <em>event source</em> of the events that are
  46. * (potentially) generated.
  47. * The source is <em>not necessarily</em> the object named by the target.
  48. * Only when the target is the empty name is the object named by the target
  49. * the source.
  50. * In other words, the target,
  51. * along with the scope parameter, are used to identify
  52. * the object(s) that the listener is interested in, but the event source
  53. * is the <tt>EventContext</tt> instance with which the listener
  54. * has registered.
  55. *<p>
  56. * For example, suppose a listener makes the following registration:
  57. *<blockquote><pre>
  58. * NamespaceChangeListener listener = ...;
  59. * src.addNamingListener("x", SUBTREE_SCOPE, listener);
  60. *</pre></blockquote>
  61. * When an object named "x/y" is subsequently deleted, the corresponding
  62. * <tt>NamingEvent</tt> (<tt>evt</tt>) must contain:
  63. *<blockquote><pre>
  64. * evt.getEventContext() == src
  65. * evt.getOldBinding().getName().equals("x/y")
  66. *</pre></blockquote>
  67. *<p>
  68. * Furthermore, listener registration/deregistration is with
  69. * the <tt>EventContext</tt>
  70. * <em>instance</em>, and not with the corresponding object in the namespace.
  71. * If the program intends at some point to remove a listener, then it needs to
  72. * keep a reference to the <tt>EventContext</tt> instance on
  73. * which it invoked <tt>addNamingListener()</tt> (just as
  74. * it needs to keep a reference to the listener in order to remove it
  75. * later). It cannot expect to do a <tt>lookup()</tt> and get another instance of
  76. * a <tt>EventContext</tt> on which to perform the deregistration.
  77. *<h4>Lifetime of Registration</h4>
  78. * A registered listener becomes deregistered when:
  79. *<ul>
  80. *<li>It is removed using <tt>removeNamingListener()</tt>.
  81. *<li>An exception is thrown while collecting information about the events.
  82. * That is, when the listener receives a <tt>NamingExceptionEvent</tt>.
  83. *<li><tt>Context.close()</tt> is invoked on the <tt>EventContext</tt>
  84. * instance with which it has registered.
  85. </ul>
  86. * Until that point, a <tt>EventContext</tt> instance that has outstanding
  87. * listeners will continue to exist and be maintained by the service provider.
  88. *
  89. *<h4>Listener Implementations</h4>
  90. * The registration/deregistration methods accept an instance of
  91. * <tt>NamingListener</tt>. There are subinterfaces of <tt>NamingListener</tt>
  92. * for different of event types of <tt>NamingEvent</tt>.
  93. * For example, the <tt>ObjectChangeListener</tt>
  94. * interface is for the <tt>NamingEvent.OBJECT_CHANGED</tt> event type.
  95. * To register interest in multiple event types, the listener implementation
  96. * should implement multiple <tt>NamingListener</tt> subinterfaces and use a
  97. * single invocation of <tt>addNamingListener()</tt>.
  98. * In addition to reducing the number of method calls and possibly the code size
  99. * of the listeners, this allows some service providers to optimize the
  100. * registration.
  101. *
  102. *<h4>Threading Issues</h4>
  103. *
  104. * Like <tt>Context</tt> instances in general, instances of
  105. * <tt>EventContext</tt> are not guaranteed to be thread-safe.
  106. * Care must be taken when multiple threads are accessing the same
  107. * <tt>EventContext</tt> concurrently.
  108. * See the
  109. * <a href=package-summary.html#THREADING>package description</a>
  110. * for more information on threading issues.
  111. *
  112. * @author Rosanna Lee
  113. * @author Scott Seligman
  114. * @version 1.9 01/02/09
  115. * @since 1.3
  116. */
  117. public interface EventContext extends Context {
  118. /**
  119. * Constant for expressing interest in events concerning the object named
  120. * by the target.
  121. *<p>
  122. * The value of this constant is <tt>0</tt>.
  123. */
  124. public final static int OBJECT_SCOPE = 0;
  125. /**
  126. * Constant for expressing interest in events concerning objects
  127. * in the context named by the target,
  128. * excluding the context named by the target.
  129. *<p>
  130. * The value of this constant is <tt>1</tt>.
  131. */
  132. public final static int ONELEVEL_SCOPE = 1;
  133. /**
  134. * Constant for expressing interest in events concerning objects
  135. * in the subtree of the object named by the target, including the object
  136. * named by the target.
  137. *<p>
  138. * The value of this constant is <tt>2</tt>.
  139. */
  140. public final static int SUBTREE_SCOPE = 2;
  141. /**
  142. * Adds a listener for receiving naming events fired
  143. * when the object(s) identified by a target and scope changes.
  144. *
  145. * The event source of those events is this context. See the
  146. * class description for a discussion on event source and target.
  147. * See the descriptions of the constants <tt>OBJECT_SCOPE</tt>,
  148. * <tt>ONELEVEL_SCOPE</tt>, and <tt>SUBTREE_SCOPE</tt> to see how
  149. * <tt>scope</tt> affects the registration.
  150. *<p>
  151. * <tt>target</tt> needs to name a context only when <tt>scope</tt> is
  152. * <tt>ONELEVEL_SCOPE</tt>.
  153. * <tt>target</tt> may name a non-context if <tt>scope</tt> is either
  154. * <tt>OBJECT_SCOPE</tt> or <tt>SUBTREE_SCOPE</tt>. Using
  155. * <tt>SUBTREE_SCOPE</tt> for a non-context might be useful,
  156. * for example, if the caller does not know in advance whether <tt>target</tt>
  157. * is a context and just wants to register interest in the (possibly
  158. * degenerate subtree) rooted at <tt>target</tt>.
  159. *<p>
  160. * When the listener is notified of an event, the listener may
  161. * in invoked in a thread other than the one in which
  162. * <tt>addNamingListener()</tt> is executed.
  163. * Care must be taken when multiple threads are accessing the same
  164. * <tt>EventContext</tt> concurrently.
  165. * See the
  166. * <a href=package-summary.html#THREADING>package description</a>
  167. * for more information on threading issues.
  168. *
  169. * @param target A nonnull name to be resolved relative to this context.
  170. * @param scope One of <tt>OBJECT_SCOPE</tt>, <tt>ONELEVEL_SCOPE</tt>, or
  171. * <tt>SUBTREE_SCOPE</tt>.
  172. * @param l The nonnull listener.
  173. * @exception NamingException If a problem was encountered while
  174. * adding the listener.
  175. * @see #removeNamingListener
  176. */
  177. void addNamingListener(Name target, int scope, NamingListener l)
  178. throws NamingException;
  179. /**
  180. * Adds a listener for receiving naming events fired
  181. * when the object named by the string target name and scope changes.
  182. *
  183. * See the overload that accepts a <tt>Name</tt> for details.
  184. *
  185. * @param target The nonnull string name of the object resolved relative
  186. * to this context.
  187. * @param scope One of <tt>OBJECT_SCOPE</tt>, <tt>ONELEVEL_SCOPE</tt>, or
  188. * <tt>SUBTREE_SCOPE</tt>.
  189. * @param l The nonnull listener.
  190. * @exception NamingException If a problem was encountered while
  191. * adding the listener.
  192. * @see #removeNamingListener
  193. */
  194. void addNamingListener(String target, int scope, NamingListener l)
  195. throws NamingException;
  196. /**
  197. * Removes a listener from receiving naming events fired
  198. * by this <tt>EventContext</tt>.
  199. * The listener may have registered more than once with this
  200. * <tt>EventContext</tt>, perhaps with different target/scope arguments.
  201. * After this method is invoked, the listener will no longer
  202. * receive events with this <tt>EventContext</tt> instance
  203. * as the event source (except for those events already in the process of
  204. * being dispatched).
  205. * If the listener was not, or is no longer, registered with
  206. * this <tt>EventContext</tt> instance, this method does not do anything.
  207. *
  208. * @param l The nonnull listener.
  209. * @exception NamingException If a problem was encountered while
  210. * removing the listener.
  211. * @see #addNamingListener
  212. */
  213. void removeNamingListener(NamingListener l) throws NamingException;
  214. /**
  215. * Determines whether a listener can register interest in a target
  216. * that does not exist.
  217. *
  218. * @return true if the target must exist; false if the target need not exist.
  219. * @exception NamingException If the context's behavior in this regard cannot
  220. * be determined.
  221. */
  222. boolean targetMustExist() throws NamingException;
  223. }