1. /*
  2. * @(#)DirStateFactory.java 1.11 04/07/16
  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.spi;
  8. import javax.naming.*;
  9. import javax.naming.directory.Attributes;
  10. import java.util.Hashtable;
  11. /**
  12. * This interface represents a factory for obtaining the state of an
  13. * object and corresponding attributes for binding.
  14. *<p>
  15. * The JNDI framework allows for object implementations to
  16. * be loaded in dynamically via <tt>object factories</tt>.
  17. * <p>
  18. * A <tt>DirStateFactory</tt> extends <tt>StateFactory</tt>
  19. * by allowing an <tt>Attributes</tt> instance
  20. * to be supplied to and be returned by the <tt>getStateToBind()</tt> method.
  21. * <tt>DirStateFactory</tt> implementations are intended to be used by
  22. * <tt>DirContext</tt> service providers.
  23. * When a caller binds an object using <tt>DirContext.bind()</tt>,
  24. * he might also specify a set of attributes to be bound with the object.
  25. * The object and attributes to be bound are passed to
  26. * the <tt>getStateToBind()</tt> method of a factory.
  27. * If the factory processes the object and attributes, it returns
  28. * a corresponding pair of object and attributes to be bound.
  29. * If the factory does not process the object, it must return null.
  30. *<p>
  31. * For example, a caller might bind a printer object with some printer-related
  32. * attributes.
  33. *<blockquote><pre>
  34. * ctx.rebind("inky", printer, printerAttrs);
  35. *</pre></blockquote>
  36. * An LDAP service provider for <tt>ctx</tt> uses a <tt>DirStateFactory</tt>
  37. * (indirectly via <tt>DirectoryManager.getStateToBind()</tt>)
  38. * and gives it <tt>printer</tt> and <tt>printerAttrs</tt>. A factory for
  39. * an LDAP directory might turn <tt>printer</tt> into a set of attributes
  40. * and merge that with <tt>printerAttrs</tt>. The service provider then
  41. * uses the resulting attributes to create an LDAP entry and updates
  42. * the directory.
  43. *
  44. * <p> Since <tt>DirStateFactory</tt> extends <tt>StateFactory</tt>, it
  45. * has two <tt>getStateToBind()</tt> methods, where one
  46. * differs from the other by the attributes
  47. * argument. <tt>DirectoryManager.getStateToBind()</tt> will only use
  48. * the form that accepts the attributes argument, while
  49. * <tt>NamingManager.getStateToBind()</tt> will only use the form that
  50. * does not accept the attributes argument.
  51. *
  52. * <p> Either form of the <tt>getStateToBind()</tt> method of a
  53. * DirStateFactory may be invoked multiple times, possibly using different
  54. * parameters. The implementation is thread-safe.
  55. *
  56. * @author Rosanna Lee
  57. * @author Scott Seligman
  58. * @version 1.11 04/07/16
  59. *
  60. * @see DirectoryManager#getStateToBind
  61. * @see DirObjectFactory
  62. * @since 1.3
  63. */
  64. public interface DirStateFactory extends StateFactory {
  65. /**
  66. * Retrieves the state of an object for binding given the object and attributes
  67. * to be transformed.
  68. *<p>
  69. * <tt>DirectoryManager.getStateToBind()</tt>
  70. * successively loads in state factories. If a factory implements
  71. * <tt>DirStateFactory</tt>, <tt>DirectoryManager</tt> invokes this method;
  72. * otherwise, it invokes <tt>StateFactory.getStateToBind()</tt>.
  73. * It does this until a factory produces a non-null answer.
  74. *<p>
  75. * When an exception is thrown by a factory,
  76. * the exception is passed on to the caller
  77. * of <tt>DirectoryManager.getStateToBind()</tt>. The search for other factories
  78. * that may produce a non-null answer is halted.
  79. * A factory should only throw an exception if it is sure that
  80. * it is the only intended factory and that no other factories
  81. * should be tried.
  82. * If this factory cannot create an object using the arguments supplied,
  83. * it should return null.
  84. * <p>
  85. * The <code>name</code> and <code>nameCtx</code> parameters may
  86. * optionally be used to specify the name of the object being created.
  87. * See the description of "Name and Context Parameters" in
  88. * {@link ObjectFactory#getObjectInstance ObjectFactory.getObjectInstance()}
  89. * for details.
  90. * If a factory uses <code>nameCtx</code> it should synchronize its use
  91. * against concurrent access, since context implementations are not
  92. * guaranteed to be thread-safe.
  93. *<p>
  94. * The <tt>name</tt>, <tt>inAttrs</tt>, and <tt>environment</tt> parameters
  95. * are owned by the caller.
  96. * The implementation will not modify these objects or keep references
  97. * to them, although it may keep references to clones or copies.
  98. * The object returned by this method is owned by the caller.
  99. * The implementation will not subsequently modify it.
  100. * It will contain either a new <tt>Attributes</tt> object that is
  101. * likewise owned by the caller, or a reference to the original
  102. * <tt>inAttrs</tt> parameter.
  103. *
  104. * @param obj A possibly null object whose state is to be retrieved.
  105. * @param name The name of this object relative to <code>nameCtx</code>,
  106. * or null if no name is specified.
  107. * @param nameCtx The context relative to which the <code>name</code>
  108. * parameter is specified, or null if <code>name</code> is
  109. * relative to the default initial context.
  110. * @param environment The possibly null environment to
  111. * be used in the creation of the object's state.
  112. * @param inAttrs The possibly null attributes to be bound with the object.
  113. * The factory must not modify <tt>inAttrs</tt>.
  114. * @return A <tt>Result</tt> containing the object's state for binding
  115. * and the corresponding
  116. * attributes to be bound; null if the object don't use this factory.
  117. * @exception NamingException If this factory encountered an exception
  118. * while attempting to get the object's state, and no other factories are
  119. * to be tried.
  120. *
  121. * @see DirectoryManager#getStateToBind
  122. */
  123. public Result getStateToBind(Object obj, Name name, Context nameCtx,
  124. Hashtable<?,?> environment,
  125. Attributes inAttrs)
  126. throws NamingException;
  127. /**
  128. * An object/attributes pair for returning the result of
  129. * DirStateFactory.getStateToBind().
  130. */
  131. public static class Result {
  132. /**
  133. * The possibly null object to be bound.
  134. */
  135. private Object obj;
  136. /**
  137. * The possibly null attributes to be bound.
  138. */
  139. private Attributes attrs;
  140. /**
  141. * Constructs an instance of Result.
  142. *
  143. * @param obj The possibly null object to be bound.
  144. * @param outAttrs The possibly null attributes to be bound.
  145. */
  146. public Result(Object obj, Attributes outAttrs) {
  147. this.obj = obj;
  148. this.attrs = outAttrs;
  149. }
  150. /**
  151. * Retrieves the object to be bound.
  152. * @return The possibly null object to be bound.
  153. */
  154. public Object getObject() { return obj; };
  155. /**
  156. * Retrieves the attributes to be bound.
  157. * @return The possibly null attributes to be bound.
  158. */
  159. public Attributes getAttributes() { return attrs; };
  160. }
  161. }