1. /*
  2. * @(#)InitialDirContext.java 1.8 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.naming.directory;
  8. import java.util.Hashtable;
  9. import javax.naming.spi.NamingManager;
  10. import javax.naming.*;
  11. /**
  12. * This class is the starting context for performing
  13. * directory operations. The documentation in the class description
  14. * of InitialContext (including those for synchronization) apply here.
  15. *
  16. *
  17. * @author Rosanna Lee
  18. * @author Scott Seligman
  19. * @version 1.8 03/01/23
  20. *
  21. * @see javax.naming.InitialContext
  22. * @since 1.3
  23. */
  24. public class InitialDirContext extends InitialContext implements DirContext {
  25. /**
  26. * Constructs an initial DirContext with the option of not
  27. * initializing it. This may be used by a constructor in
  28. * a subclass when the value of the environment parameter
  29. * is not yet known at the time the <tt>InitialDirContext</tt>
  30. * constructor is called. The subclass's constructor will
  31. * call this constructor, compute the value of the environment,
  32. * and then call <tt>init()</tt> before returning.
  33. *
  34. * @param lazy
  35. * true means do not initialize the initial DirContext; false
  36. * is equivalent to calling <tt>new InitialDirContext()</tt>
  37. * @throws NamingException if a naming exception is encountered
  38. *
  39. * @see InitialContext#init(Hashtable)
  40. * @since 1.3
  41. */
  42. protected InitialDirContext(boolean lazy) throws NamingException {
  43. super(lazy);
  44. }
  45. /**
  46. * Constructs an initial DirContext.
  47. * No environment properties are supplied.
  48. * Equivalent to <tt>new InitialDirContext(null)</tt>.
  49. *
  50. * @throws NamingException if a naming exception is encountered
  51. *
  52. * @see #InitialDirContext(Hashtable)
  53. */
  54. public InitialDirContext() throws NamingException {
  55. super();
  56. }
  57. /**
  58. * Constructs an initial DirContext using the supplied environment.
  59. * Environment properties are discussed in the
  60. * <tt>javax.naming.InitialContext</tt> class description.
  61. *
  62. * <p> This constructor will not modify <tt>environment</tt>
  63. * or save a reference to it, but may save a clone.
  64. *
  65. * @param environment
  66. * environment used to create the initial DirContext.
  67. * Null indicates an empty environment.
  68. *
  69. * @throws NamingException if a naming exception is encountered
  70. */
  71. public InitialDirContext(Hashtable environment) throws NamingException {
  72. super(environment);
  73. }
  74. private DirContext getURLOrDefaultInitDirCtx(String name)
  75. throws NamingException {
  76. Context answer = getURLOrDefaultInitCtx(name);
  77. if (!(answer instanceof DirContext)) {
  78. if (answer == null) {
  79. throw new NoInitialContextException();
  80. } else {
  81. throw new NotContextException(
  82. "Not an instance of DirContext");
  83. }
  84. }
  85. return (DirContext)answer;
  86. }
  87. private DirContext getURLOrDefaultInitDirCtx(Name name)
  88. throws NamingException {
  89. Context answer = getURLOrDefaultInitCtx(name);
  90. if (!(answer instanceof DirContext)) {
  91. if (answer == null) {
  92. throw new NoInitialContextException();
  93. } else {
  94. throw new NotContextException(
  95. "Not an instance of DirContext");
  96. }
  97. }
  98. return (DirContext)answer;
  99. }
  100. // DirContext methods
  101. // Most Javadoc is deferred to the DirContext interface.
  102. public Attributes getAttributes(String name)
  103. throws NamingException {
  104. return getAttributes(name, null);
  105. }
  106. public Attributes getAttributes(String name, String[] attrIds)
  107. throws NamingException {
  108. return getURLOrDefaultInitDirCtx(name).getAttributes(name, attrIds);
  109. }
  110. public Attributes getAttributes(Name name)
  111. throws NamingException {
  112. return getAttributes(name, null);
  113. }
  114. public Attributes getAttributes(Name name, String[] attrIds)
  115. throws NamingException {
  116. return getURLOrDefaultInitDirCtx(name).getAttributes(name, attrIds);
  117. }
  118. public void modifyAttributes(String name, int mod_op, Attributes attrs)
  119. throws NamingException {
  120. getURLOrDefaultInitDirCtx(name).modifyAttributes(name, mod_op, attrs);
  121. }
  122. public void modifyAttributes(Name name, int mod_op, Attributes attrs)
  123. throws NamingException {
  124. getURLOrDefaultInitDirCtx(name).modifyAttributes(name, mod_op, attrs);
  125. }
  126. public void modifyAttributes(String name, ModificationItem[] mods)
  127. throws NamingException {
  128. getURLOrDefaultInitDirCtx(name).modifyAttributes(name, mods);
  129. }
  130. public void modifyAttributes(Name name, ModificationItem[] mods)
  131. throws NamingException {
  132. getURLOrDefaultInitDirCtx(name).modifyAttributes(name, mods);
  133. }
  134. public void bind(String name, Object obj, Attributes attrs)
  135. throws NamingException {
  136. getURLOrDefaultInitDirCtx(name).bind(name, obj, attrs);
  137. }
  138. public void bind(Name name, Object obj, Attributes attrs)
  139. throws NamingException {
  140. getURLOrDefaultInitDirCtx(name).bind(name, obj, attrs);
  141. }
  142. public void rebind(String name, Object obj, Attributes attrs)
  143. throws NamingException {
  144. getURLOrDefaultInitDirCtx(name).rebind(name, obj, attrs);
  145. }
  146. public void rebind(Name name, Object obj, Attributes attrs)
  147. throws NamingException {
  148. getURLOrDefaultInitDirCtx(name).rebind(name, obj, attrs);
  149. }
  150. public DirContext createSubcontext(String name, Attributes attrs)
  151. throws NamingException {
  152. return getURLOrDefaultInitDirCtx(name).createSubcontext(name, attrs);
  153. }
  154. public DirContext createSubcontext(Name name, Attributes attrs)
  155. throws NamingException {
  156. return getURLOrDefaultInitDirCtx(name).createSubcontext(name, attrs);
  157. }
  158. public DirContext getSchema(String name) throws NamingException {
  159. return getURLOrDefaultInitDirCtx(name).getSchema(name);
  160. }
  161. public DirContext getSchema(Name name) throws NamingException {
  162. return getURLOrDefaultInitDirCtx(name).getSchema(name);
  163. }
  164. public DirContext getSchemaClassDefinition(String name)
  165. throws NamingException {
  166. return getURLOrDefaultInitDirCtx(name).getSchemaClassDefinition(name);
  167. }
  168. public DirContext getSchemaClassDefinition(Name name)
  169. throws NamingException {
  170. return getURLOrDefaultInitDirCtx(name).getSchemaClassDefinition(name);
  171. }
  172. // -------------------- search operations
  173. public NamingEnumeration search(String name,
  174. Attributes matchingAttributes)
  175. throws NamingException {
  176. return getURLOrDefaultInitDirCtx(name).search(name, matchingAttributes);
  177. }
  178. public NamingEnumeration search(Name name,
  179. Attributes matchingAttributes)
  180. throws NamingException {
  181. return getURLOrDefaultInitDirCtx(name).search(name, matchingAttributes);
  182. }
  183. public NamingEnumeration search(String name,
  184. Attributes matchingAttributes,
  185. String[] attributesToReturn)
  186. throws NamingException {
  187. return getURLOrDefaultInitDirCtx(name).search(name,
  188. matchingAttributes,
  189. attributesToReturn);
  190. }
  191. public NamingEnumeration search(Name name,
  192. Attributes matchingAttributes,
  193. String[] attributesToReturn)
  194. throws NamingException {
  195. return getURLOrDefaultInitDirCtx(name).search(name,
  196. matchingAttributes,
  197. attributesToReturn);
  198. }
  199. public NamingEnumeration search(String name,
  200. String filter,
  201. SearchControls cons)
  202. throws NamingException {
  203. return getURLOrDefaultInitDirCtx(name).search(name, filter, cons);
  204. }
  205. public NamingEnumeration search(Name name,
  206. String filter,
  207. SearchControls cons)
  208. throws NamingException {
  209. return getURLOrDefaultInitDirCtx(name).search(name, filter, cons);
  210. }
  211. public NamingEnumeration search(String name,
  212. String filterExpr,
  213. Object[] filterArgs,
  214. SearchControls cons)
  215. throws NamingException {
  216. return getURLOrDefaultInitDirCtx(name).search(name, filterExpr,
  217. filterArgs, cons);
  218. }
  219. public NamingEnumeration search(Name name,
  220. String filterExpr,
  221. Object[] filterArgs,
  222. SearchControls cons)
  223. throws NamingException {
  224. return getURLOrDefaultInitDirCtx(name).search(name, filterExpr,
  225. filterArgs, cons);
  226. }
  227. }