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