blob: 2d12ab38c0db4613a323dac24695669346cf8d50 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999-2004 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package javax.naming.spi;
27
28import java.util.Hashtable;
29
30import javax.naming.Name;
31import javax.naming.NamingEnumeration;
32import javax.naming.CompositeName;
33import javax.naming.NamingException;
34import javax.naming.CannotProceedException;
35import javax.naming.OperationNotSupportedException;
36import javax.naming.Context;
37
38import javax.naming.directory.DirContext;
39import javax.naming.directory.Attributes;
40import javax.naming.directory.SearchControls;
41import javax.naming.directory.ModificationItem;
42
43/**
44 * This class is the continuation context for invoking DirContext methods.
45 *
46 * @author Rosanna Lee
47 * @author Scott Seligman
48 * @since 1.3
49 */
50
51class ContinuationDirContext extends ContinuationContext implements DirContext {
52
53 ContinuationDirContext(CannotProceedException cpe, Hashtable env) {
54 super(cpe, env);
55 }
56
57 protected DirContextNamePair getTargetContext(Name name)
58 throws NamingException {
59
60 if (cpe.getResolvedObj() == null)
61 throw (NamingException)cpe.fillInStackTrace();
62
63 Context ctx = NamingManager.getContext(cpe.getResolvedObj(),
64 cpe.getAltName(),
65 cpe.getAltNameCtx(),
66 env);
67 if (ctx == null)
68 throw (NamingException)cpe.fillInStackTrace();
69
70 if (ctx instanceof DirContext)
71 return new DirContextNamePair((DirContext)ctx, name);
72
73 if (ctx instanceof Resolver) {
74 Resolver res = (Resolver)ctx;
75 ResolveResult rr = res.resolveToClass(name, DirContext.class);
76
77 // Reached a DirContext; return result.
78 DirContext dctx = (DirContext)rr.getResolvedObj();
79 return (new DirContextNamePair(dctx, rr.getRemainingName()));
80 }
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 DirContextNamePair((DirContext)ultimate,
87 new CompositeName()));
88 }
89
90 throw (NamingException)cpe.fillInStackTrace();
91 }
92
93 protected DirContextStringPair getTargetContext(String name)
94 throws NamingException {
95
96 if (cpe.getResolvedObj() == null)
97 throw (NamingException)cpe.fillInStackTrace();
98
99 Context ctx = NamingManager.getContext(cpe.getResolvedObj(),
100 cpe.getAltName(),
101 cpe.getAltNameCtx(),
102 env);
103
104 if (ctx instanceof DirContext)
105 return new DirContextStringPair((DirContext)ctx, name);
106
107 if (ctx instanceof Resolver) {
108 Resolver res = (Resolver)ctx;
109 ResolveResult rr = res.resolveToClass(name, DirContext.class);
110
111 // Reached a DirContext; return result.
112 DirContext dctx = (DirContext)rr.getResolvedObj();
113 Name tmp = rr.getRemainingName();
114 String remains = (tmp != null) ? tmp.toString() : "";
115 return (new DirContextStringPair(dctx, remains));
116 }
117
118 // Resolve all the way using lookup(). This may allow the operation
119 // to succeed if it doesn't require the penultimate context.
120 Object ultimate = ctx.lookup(name);
121 if (ultimate instanceof DirContext) {
122 return (new DirContextStringPair((DirContext)ultimate, ""));
123 }
124
125 throw (NamingException)cpe.fillInStackTrace();
126 }
127
128 public Attributes getAttributes(String name) throws NamingException {
129 DirContextStringPair res = getTargetContext(name);
130 return res.getDirContext().getAttributes(res.getString());
131 }
132
133 public Attributes getAttributes(String name, String[] attrIds)
134 throws NamingException {
135 DirContextStringPair res = getTargetContext(name);
136 return res.getDirContext().getAttributes(res.getString(), attrIds);
137 }
138
139 public Attributes getAttributes(Name name) throws NamingException {
140 DirContextNamePair res = getTargetContext(name);
141 return res.getDirContext().getAttributes(res.getName());
142 }
143
144 public Attributes getAttributes(Name name, String[] attrIds)
145 throws NamingException {
146 DirContextNamePair res = getTargetContext(name);
147 return res.getDirContext().getAttributes(res.getName(), attrIds);
148 }
149
150 public void modifyAttributes(Name name, int mod_op, Attributes attrs)
151 throws NamingException {
152 DirContextNamePair res = getTargetContext(name);
153 res.getDirContext().modifyAttributes(res.getName(), mod_op, attrs);
154 }
155 public void modifyAttributes(String name, int mod_op, Attributes attrs)
156 throws NamingException {
157 DirContextStringPair res = getTargetContext(name);
158 res.getDirContext().modifyAttributes(res.getString(), mod_op, attrs);
159 }
160
161 public void modifyAttributes(Name name, ModificationItem[] mods)
162 throws NamingException {
163 DirContextNamePair res = getTargetContext(name);
164 res.getDirContext().modifyAttributes(res.getName(), mods);
165 }
166 public void modifyAttributes(String name, ModificationItem[] mods)
167 throws NamingException {
168 DirContextStringPair res = getTargetContext(name);
169 res.getDirContext().modifyAttributes(res.getString(), mods);
170 }
171
172 public void bind(Name name, Object obj, Attributes attrs)
173 throws NamingException {
174 DirContextNamePair res = getTargetContext(name);
175 res.getDirContext().bind(res.getName(), obj, attrs);
176 }
177 public void bind(String name, Object obj, Attributes attrs)
178 throws NamingException {
179 DirContextStringPair res = getTargetContext(name);
180 res.getDirContext().bind(res.getString(), obj, attrs);
181 }
182
183 public void rebind(Name name, Object obj, Attributes attrs)
184 throws NamingException {
185 DirContextNamePair res = getTargetContext(name);
186 res.getDirContext().rebind(res.getName(), obj, attrs);
187 }
188 public void rebind(String name, Object obj, Attributes attrs)
189 throws NamingException {
190 DirContextStringPair res = getTargetContext(name);
191 res.getDirContext().rebind(res.getString(), obj, attrs);
192 }
193
194 public DirContext createSubcontext(Name name, Attributes attrs)
195 throws NamingException {
196 DirContextNamePair res = getTargetContext(name);
197 return res.getDirContext().createSubcontext(res.getName(), attrs);
198 }
199
200 public DirContext createSubcontext(String name, Attributes attrs)
201 throws NamingException {
202 DirContextStringPair res = getTargetContext(name);
203 return
204 res.getDirContext().createSubcontext(res.getString(), attrs);
205 }
206
207 public NamingEnumeration search(Name name,
208 Attributes matchingAttributes,
209 String[] attributesToReturn)
210 throws NamingException {
211 DirContextNamePair res = getTargetContext(name);
212 return res.getDirContext().search(res.getName(), matchingAttributes,
213 attributesToReturn);
214 }
215
216 public NamingEnumeration search(String name,
217 Attributes matchingAttributes,
218 String[] attributesToReturn)
219 throws NamingException {
220 DirContextStringPair res = getTargetContext(name);
221 return res.getDirContext().search(res.getString(),
222 matchingAttributes,
223 attributesToReturn);
224 }
225
226 public NamingEnumeration search(Name name,
227 Attributes matchingAttributes)
228 throws NamingException {
229 DirContextNamePair res = getTargetContext(name);
230 return res.getDirContext().search(res.getName(), matchingAttributes);
231 }
232 public NamingEnumeration search(String name,
233 Attributes matchingAttributes)
234 throws NamingException {
235 DirContextStringPair res = getTargetContext(name);
236 return res.getDirContext().search(res.getString(),
237 matchingAttributes);
238 }
239
240 public NamingEnumeration search(Name name,
241 String filter,
242 SearchControls cons)
243 throws NamingException {
244 DirContextNamePair res = getTargetContext(name);
245 return res.getDirContext().search(res.getName(), filter, cons);
246 }
247
248 public NamingEnumeration search(String name,
249 String filter,
250 SearchControls cons)
251 throws NamingException {
252 DirContextStringPair res = getTargetContext(name);
253 return res.getDirContext().search(res.getString(), filter, cons);
254 }
255
256 public NamingEnumeration search(Name name,
257 String filterExpr,
258 Object[] args,
259 SearchControls cons)
260 throws NamingException {
261 DirContextNamePair res = getTargetContext(name);
262 return res.getDirContext().search(res.getName(), filterExpr, args,
263 cons);
264 }
265
266 public NamingEnumeration search(String name,
267 String filterExpr,
268 Object[] args,
269 SearchControls cons)
270 throws NamingException {
271 DirContextStringPair res = getTargetContext(name);
272 return res.getDirContext().search(res.getString(), filterExpr, args,
273 cons);
274 }
275
276 public DirContext getSchema(String name) throws NamingException {
277 DirContextStringPair res = getTargetContext(name);
278 return res.getDirContext().getSchema(res.getString());
279 }
280
281 public DirContext getSchema(Name name) throws NamingException {
282 DirContextNamePair res = getTargetContext(name);
283 return res.getDirContext().getSchema(res.getName());
284 }
285
286 public DirContext getSchemaClassDefinition(String name)
287 throws NamingException {
288 DirContextStringPair res = getTargetContext(name);
289 return res.getDirContext().getSchemaClassDefinition(res.getString());
290 }
291
292 public DirContext getSchemaClassDefinition(Name name)
293 throws NamingException {
294 DirContextNamePair res = getTargetContext(name);
295 return res.getDirContext().getSchemaClassDefinition(res.getName());
296 }
297}
298
299class DirContextNamePair {
300 DirContext ctx;
301 Name name;
302
303 DirContextNamePair(DirContext ctx, Name name) {
304 this.ctx = ctx;
305 this.name = name;
306 }
307
308 DirContext getDirContext() {
309 return ctx;
310 }
311
312 Name getName() {
313 return name;
314 }
315}
316
317class DirContextStringPair {
318 DirContext ctx;
319 String str;
320
321 DirContextStringPair(DirContext ctx, String str) {
322 this.ctx = ctx;
323 this.str = str;
324 }
325
326 DirContext getDirContext() {
327 return ctx;
328 }
329
330 String getString() {
331 return str;
332 }
333}