1. /*
  2. * @(#)InitialDirContext.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.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.11 04/07/16
  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)
  72. throws NamingException
  73. {
  74. super(environment);
  75. }
  76. private DirContext getURLOrDefaultInitDirCtx(String name)
  77. throws NamingException {
  78. Context answer = getURLOrDefaultInitCtx(name);
  79. if (!(answer instanceof DirContext)) {
  80. if (answer == null) {
  81. throw new NoInitialContextException();
  82. } else {
  83. throw new NotContextException(
  84. "Not an instance of DirContext");
  85. }
  86. }
  87. return (DirContext)answer;
  88. }
  89. private DirContext getURLOrDefaultInitDirCtx(Name name)
  90. throws NamingException {
  91. Context answer = getURLOrDefaultInitCtx(name);
  92. if (!(answer instanceof DirContext)) {
  93. if (answer == null) {
  94. throw new NoInitialContextException();
  95. } else {
  96. throw new NotContextException(
  97. "Not an instance of DirContext");
  98. }
  99. }
  100. return (DirContext)answer;
  101. }
  102. // DirContext methods
  103. // Most Javadoc is deferred to the DirContext interface.
  104. public Attributes getAttributes(String name)
  105. throws NamingException {
  106. return getAttributes(name, null);
  107. }
  108. public Attributes getAttributes(String name, String[] attrIds)
  109. throws NamingException {
  110. return getURLOrDefaultInitDirCtx(name).getAttributes(name, attrIds);
  111. }
  112. public Attributes getAttributes(Name name)
  113. throws NamingException {
  114. return getAttributes(name, null);
  115. }
  116. public Attributes getAttributes(Name name, String[] attrIds)
  117. throws NamingException {
  118. return getURLOrDefaultInitDirCtx(name).getAttributes(name, attrIds);
  119. }
  120. public void modifyAttributes(String name, int mod_op, Attributes attrs)
  121. throws NamingException {
  122. getURLOrDefaultInitDirCtx(name).modifyAttributes(name, mod_op, attrs);
  123. }
  124. public void modifyAttributes(Name name, int mod_op, Attributes attrs)
  125. throws NamingException {
  126. getURLOrDefaultInitDirCtx(name).modifyAttributes(name, mod_op, attrs);
  127. }
  128. public void modifyAttributes(String name, ModificationItem[] mods)
  129. throws NamingException {
  130. getURLOrDefaultInitDirCtx(name).modifyAttributes(name, mods);
  131. }
  132. public void modifyAttributes(Name name, ModificationItem[] mods)
  133. throws NamingException {
  134. getURLOrDefaultInitDirCtx(name).modifyAttributes(name, mods);
  135. }
  136. public void bind(String name, Object obj, Attributes attrs)
  137. throws NamingException {
  138. getURLOrDefaultInitDirCtx(name).bind(name, obj, attrs);
  139. }
  140. public void bind(Name name, Object obj, Attributes attrs)
  141. throws NamingException {
  142. getURLOrDefaultInitDirCtx(name).bind(name, obj, attrs);
  143. }
  144. public void rebind(String name, Object obj, Attributes attrs)
  145. throws NamingException {
  146. getURLOrDefaultInitDirCtx(name).rebind(name, obj, attrs);
  147. }
  148. public void rebind(Name name, Object obj, Attributes attrs)
  149. throws NamingException {
  150. getURLOrDefaultInitDirCtx(name).rebind(name, obj, attrs);
  151. }
  152. public DirContext createSubcontext(String name, Attributes attrs)
  153. throws NamingException {
  154. return getURLOrDefaultInitDirCtx(name).createSubcontext(name, attrs);
  155. }
  156. public DirContext createSubcontext(Name name, Attributes attrs)
  157. throws NamingException {
  158. return getURLOrDefaultInitDirCtx(name).createSubcontext(name, attrs);
  159. }
  160. public DirContext getSchema(String name) throws NamingException {
  161. return getURLOrDefaultInitDirCtx(name).getSchema(name);
  162. }
  163. public DirContext getSchema(Name name) throws NamingException {
  164. return getURLOrDefaultInitDirCtx(name).getSchema(name);
  165. }
  166. public DirContext getSchemaClassDefinition(String name)
  167. throws NamingException {
  168. return getURLOrDefaultInitDirCtx(name).getSchemaClassDefinition(name);
  169. }
  170. public DirContext getSchemaClassDefinition(Name name)
  171. throws NamingException {
  172. return getURLOrDefaultInitDirCtx(name).getSchemaClassDefinition(name);
  173. }
  174. // -------------------- search operations
  175. public NamingEnumeration<SearchResult>
  176. search(String name, Attributes matchingAttributes)
  177. throws NamingException
  178. {
  179. return getURLOrDefaultInitDirCtx(name).search(name, matchingAttributes);
  180. }
  181. public NamingEnumeration<SearchResult>
  182. search(Name name, Attributes matchingAttributes)
  183. throws NamingException
  184. {
  185. return getURLOrDefaultInitDirCtx(name).search(name, matchingAttributes);
  186. }
  187. public NamingEnumeration<SearchResult>
  188. search(String name,
  189. Attributes matchingAttributes,
  190. String[] attributesToReturn)
  191. throws NamingException
  192. {
  193. return getURLOrDefaultInitDirCtx(name).search(name,
  194. matchingAttributes,
  195. attributesToReturn);
  196. }
  197. public NamingEnumeration<SearchResult>
  198. search(Name name,
  199. Attributes matchingAttributes,
  200. String[] attributesToReturn)
  201. throws NamingException
  202. {
  203. return getURLOrDefaultInitDirCtx(name).search(name,
  204. matchingAttributes,
  205. attributesToReturn);
  206. }
  207. public NamingEnumeration<SearchResult>
  208. search(String name,
  209. String filter,
  210. SearchControls cons)
  211. throws NamingException
  212. {
  213. return getURLOrDefaultInitDirCtx(name).search(name, filter, cons);
  214. }
  215. public NamingEnumeration<SearchResult>
  216. search(Name name,
  217. String filter,
  218. SearchControls cons)
  219. throws NamingException
  220. {
  221. return getURLOrDefaultInitDirCtx(name).search(name, filter, cons);
  222. }
  223. public NamingEnumeration<SearchResult>
  224. search(String name,
  225. String filterExpr,
  226. Object[] filterArgs,
  227. SearchControls cons)
  228. throws NamingException
  229. {
  230. return getURLOrDefaultInitDirCtx(name).search(name, filterExpr,
  231. filterArgs, cons);
  232. }
  233. public NamingEnumeration<SearchResult>
  234. search(Name name,
  235. String filterExpr,
  236. Object[] filterArgs,
  237. SearchControls cons)
  238. throws NamingException
  239. {
  240. return getURLOrDefaultInitDirCtx(name).search(name, filterExpr,
  241. filterArgs, cons);
  242. }
  243. }