1. /*
  2. * @(#)ContinuationDirContext.java 1.4 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.spi;
  11. import java.util.Hashtable;
  12. import javax.naming.Name;
  13. import javax.naming.NamingEnumeration;
  14. import javax.naming.CompositeName;
  15. import javax.naming.NamingException;
  16. import javax.naming.CannotProceedException;
  17. import javax.naming.OperationNotSupportedException;
  18. import javax.naming.Context;
  19. import javax.naming.directory.DirContext;
  20. import javax.naming.directory.Attributes;
  21. import javax.naming.directory.SearchControls;
  22. import javax.naming.directory.ModificationItem;
  23. /**
  24. * This class is the continuation context for invoking DirContext methods.
  25. *
  26. * @author Rosanna Lee
  27. * @author Scott Seligman
  28. * @version 1.4 00/02/02
  29. * @since 1.3
  30. */
  31. class ContinuationDirContext extends ContinuationContext implements DirContext {
  32. ContinuationDirContext(CannotProceedException cpe) {
  33. super(cpe);
  34. }
  35. protected DirContextNamePair getTargetContext(Name name)
  36. throws NamingException {
  37. if (cpe.getResolvedObj() == null)
  38. throw (NamingException)cpe.fillInStackTrace();
  39. Context ctx = NamingManager.getContext(cpe.getResolvedObj(),
  40. cpe.getAltName(),
  41. cpe.getAltNameCtx(),
  42. cpe.getEnvironment());
  43. if (ctx == null)
  44. throw (NamingException)cpe.fillInStackTrace();
  45. if (ctx instanceof DirContext)
  46. return new DirContextNamePair((DirContext)ctx, name);
  47. if (ctx instanceof Resolver) {
  48. Resolver res = (Resolver)ctx;
  49. ResolveResult rr = res.resolveToClass(name, DirContext.class);
  50. // Reached a DirContext; return result.
  51. DirContext dctx = (DirContext)rr.getResolvedObj();
  52. return (new DirContextNamePair(dctx, rr.getRemainingName()));
  53. }
  54. // Resolve all the way using lookup(). This may allow the operation
  55. // to succeed if it doesn't require the penultimate context.
  56. Object ultimate = ctx.lookup(name);
  57. if (ultimate instanceof DirContext) {
  58. return (new DirContextNamePair((DirContext)ultimate,
  59. new CompositeName()));
  60. }
  61. throw (NamingException)cpe.fillInStackTrace();
  62. }
  63. protected DirContextStringPair getTargetContext(String name)
  64. throws NamingException {
  65. if (cpe.getResolvedObj() == null)
  66. throw (NamingException)cpe.fillInStackTrace();
  67. Context ctx = NamingManager.getContext(cpe.getResolvedObj(),
  68. cpe.getAltName(),
  69. cpe.getAltNameCtx(),
  70. cpe.getEnvironment());
  71. if (ctx instanceof DirContext)
  72. return new DirContextStringPair((DirContext)ctx, name);
  73. if (ctx instanceof Resolver) {
  74. Resolver res = (Resolver)ctx;
  75. ResolveResult rr = res.resolveToClass(name, DirContext.class);
  76. // Reached a DirContext; return result.
  77. DirContext dctx = (DirContext)rr.getResolvedObj();
  78. Name tmp = rr.getRemainingName();
  79. String remains = (tmp != null) ? tmp.toString() : "";
  80. return (new DirContextStringPair(dctx, remains));
  81. }
  82. // Resolve all the way using lookup(). This may allow the operation
  83. // to succeed if it doesn't require the penultimate context.
  84. Object ultimate = ctx.lookup(name);
  85. if (ultimate instanceof DirContext) {
  86. return (new DirContextStringPair((DirContext)ultimate, ""));
  87. }
  88. throw (NamingException)cpe.fillInStackTrace();
  89. }
  90. public Attributes getAttributes(String name) throws NamingException {
  91. DirContextStringPair res = getTargetContext(name);
  92. return res.getDirContext().getAttributes(res.getString());
  93. }
  94. public Attributes getAttributes(String name, String[] attrIds)
  95. throws NamingException {
  96. DirContextStringPair res = getTargetContext(name);
  97. return res.getDirContext().getAttributes(res.getString(), attrIds);
  98. }
  99. public Attributes getAttributes(Name name) throws NamingException {
  100. DirContextNamePair res = getTargetContext(name);
  101. return res.getDirContext().getAttributes(res.getName());
  102. }
  103. public Attributes getAttributes(Name name, String[] attrIds)
  104. throws NamingException {
  105. DirContextNamePair res = getTargetContext(name);
  106. return res.getDirContext().getAttributes(res.getName(), attrIds);
  107. }
  108. public void modifyAttributes(Name name, int mod_op, Attributes attrs)
  109. throws NamingException {
  110. DirContextNamePair res = getTargetContext(name);
  111. res.getDirContext().modifyAttributes(res.getName(), mod_op, attrs);
  112. }
  113. public void modifyAttributes(String name, int mod_op, Attributes attrs)
  114. throws NamingException {
  115. DirContextStringPair res = getTargetContext(name);
  116. res.getDirContext().modifyAttributes(res.getString(), mod_op, attrs);
  117. }
  118. public void modifyAttributes(Name name, ModificationItem[] mods)
  119. throws NamingException {
  120. DirContextNamePair res = getTargetContext(name);
  121. res.getDirContext().modifyAttributes(res.getName(), mods);
  122. }
  123. public void modifyAttributes(String name, ModificationItem[] mods)
  124. throws NamingException {
  125. DirContextStringPair res = getTargetContext(name);
  126. res.getDirContext().modifyAttributes(res.getString(), mods);
  127. }
  128. public void bind(Name name, Object obj, Attributes attrs)
  129. throws NamingException {
  130. DirContextNamePair res = getTargetContext(name);
  131. res.getDirContext().bind(res.getName(), obj, attrs);
  132. }
  133. public void bind(String name, Object obj, Attributes attrs)
  134. throws NamingException {
  135. DirContextStringPair res = getTargetContext(name);
  136. res.getDirContext().bind(res.getString(), obj, attrs);
  137. }
  138. public void rebind(Name name, Object obj, Attributes attrs)
  139. throws NamingException {
  140. DirContextNamePair res = getTargetContext(name);
  141. res.getDirContext().rebind(res.getName(), obj, attrs);
  142. }
  143. public void rebind(String name, Object obj, Attributes attrs)
  144. throws NamingException {
  145. DirContextStringPair res = getTargetContext(name);
  146. res.getDirContext().rebind(res.getString(), obj, attrs);
  147. }
  148. public DirContext createSubcontext(Name name, Attributes attrs)
  149. throws NamingException {
  150. DirContextNamePair res = getTargetContext(name);
  151. return res.getDirContext().createSubcontext(res.getName(), attrs);
  152. }
  153. public DirContext createSubcontext(String name, Attributes attrs)
  154. throws NamingException {
  155. DirContextStringPair res = getTargetContext(name);
  156. return
  157. res.getDirContext().createSubcontext(res.getString(), attrs);
  158. }
  159. public NamingEnumeration search(Name name,
  160. Attributes matchingAttributes,
  161. String[] attributesToReturn)
  162. throws NamingException {
  163. DirContextNamePair res = getTargetContext(name);
  164. return res.getDirContext().search(res.getName(), matchingAttributes,
  165. attributesToReturn);
  166. }
  167. public NamingEnumeration search(String name,
  168. Attributes matchingAttributes,
  169. String[] attributesToReturn)
  170. throws NamingException {
  171. DirContextStringPair res = getTargetContext(name);
  172. return res.getDirContext().search(res.getString(),
  173. matchingAttributes,
  174. attributesToReturn);
  175. }
  176. public NamingEnumeration search(Name name,
  177. Attributes matchingAttributes)
  178. throws NamingException {
  179. DirContextNamePair res = getTargetContext(name);
  180. return res.getDirContext().search(res.getName(), matchingAttributes);
  181. }
  182. public NamingEnumeration search(String name,
  183. Attributes matchingAttributes)
  184. throws NamingException {
  185. DirContextStringPair res = getTargetContext(name);
  186. return res.getDirContext().search(res.getString(),
  187. matchingAttributes);
  188. }
  189. public NamingEnumeration search(Name name,
  190. String filter,
  191. SearchControls cons)
  192. throws NamingException {
  193. DirContextNamePair res = getTargetContext(name);
  194. return res.getDirContext().search(res.getName(), filter, cons);
  195. }
  196. public NamingEnumeration search(String name,
  197. String filter,
  198. SearchControls cons)
  199. throws NamingException {
  200. DirContextStringPair res = getTargetContext(name);
  201. return res.getDirContext().search(res.getString(), filter, cons);
  202. }
  203. public NamingEnumeration search(Name name,
  204. String filterExpr,
  205. Object[] args,
  206. SearchControls cons)
  207. throws NamingException {
  208. DirContextNamePair res = getTargetContext(name);
  209. return res.getDirContext().search(res.getName(), filterExpr, args,
  210. cons);
  211. }
  212. public NamingEnumeration search(String name,
  213. String filterExpr,
  214. Object[] args,
  215. SearchControls cons)
  216. throws NamingException {
  217. DirContextStringPair res = getTargetContext(name);
  218. return res.getDirContext().search(res.getString(), filterExpr, args,
  219. cons);
  220. }
  221. public DirContext getSchema(String name) throws NamingException {
  222. DirContextStringPair res = getTargetContext(name);
  223. return res.getDirContext().getSchema(res.getString());
  224. }
  225. public DirContext getSchema(Name name) throws NamingException {
  226. DirContextNamePair res = getTargetContext(name);
  227. return res.getDirContext().getSchema(res.getName());
  228. }
  229. public DirContext getSchemaClassDefinition(String name)
  230. throws NamingException {
  231. DirContextStringPair res = getTargetContext(name);
  232. return res.getDirContext().getSchemaClassDefinition(res.getString());
  233. }
  234. public DirContext getSchemaClassDefinition(Name name)
  235. throws NamingException {
  236. DirContextNamePair res = getTargetContext(name);
  237. return res.getDirContext().getSchemaClassDefinition(res.getName());
  238. }
  239. }
  240. class DirContextNamePair {
  241. DirContext ctx;
  242. Name name;
  243. DirContextNamePair(DirContext ctx, Name name) {
  244. this.ctx = ctx;
  245. this.name = name;
  246. }
  247. DirContext getDirContext() {
  248. return ctx;
  249. }
  250. Name getName() {
  251. return name;
  252. }
  253. }
  254. class DirContextStringPair {
  255. DirContext ctx;
  256. String str;
  257. DirContextStringPair(DirContext ctx, String str) {
  258. this.ctx = ctx;
  259. this.str = str;
  260. }
  261. DirContext getDirContext() {
  262. return ctx;
  263. }
  264. String getString() {
  265. return str;
  266. }
  267. }