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