1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.config.spring.jndi; |
12 | |
|
13 | |
import java.io.Serializable; |
14 | |
import java.util.HashMap; |
15 | |
import java.util.Hashtable; |
16 | |
import java.util.Iterator; |
17 | |
import java.util.Map; |
18 | |
|
19 | |
import javax.naming.Binding; |
20 | |
import javax.naming.CompositeName; |
21 | |
import javax.naming.Context; |
22 | |
import javax.naming.LinkRef; |
23 | |
import javax.naming.Name; |
24 | |
import javax.naming.NameClassPair; |
25 | |
import javax.naming.NameNotFoundException; |
26 | |
import javax.naming.NameParser; |
27 | |
import javax.naming.NamingEnumeration; |
28 | |
import javax.naming.NamingException; |
29 | |
import javax.naming.NotContextException; |
30 | |
import javax.naming.OperationNotSupportedException; |
31 | |
import javax.naming.Reference; |
32 | |
import javax.naming.spi.NamingManager; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public class DefaultSpringJndiContext implements Context, Serializable |
43 | |
{ |
44 | |
|
45 | |
private static final long serialVersionUID = -5754338187296859149L; |
46 | 0 | protected static final NameParser nameParser = new DefaultNameParser(); |
47 | |
|
48 | 0 | private boolean freeze = false; |
49 | |
|
50 | |
protected final Hashtable environment; |
51 | |
protected final Map bindings; |
52 | |
protected final Map treeBindings; |
53 | |
|
54 | 0 | private boolean frozen = false; |
55 | 0 | private String nameInNamespace = ""; |
56 | |
public static final String SEPARATOR = "/"; |
57 | |
|
58 | |
public DefaultSpringJndiContext() |
59 | 0 | { |
60 | 0 | environment = new Hashtable(); |
61 | 0 | bindings = new HashMap(); |
62 | 0 | treeBindings = new HashMap(); |
63 | 0 | } |
64 | |
|
65 | |
public DefaultSpringJndiContext(Hashtable env) |
66 | 0 | { |
67 | 0 | if (env == null) |
68 | |
{ |
69 | 0 | this.environment = new Hashtable(); |
70 | |
} |
71 | |
else |
72 | |
{ |
73 | 0 | this.environment = new Hashtable(env); |
74 | |
} |
75 | 0 | this.bindings = new HashMap(); |
76 | 0 | this.treeBindings = new HashMap(); |
77 | 0 | } |
78 | |
|
79 | |
public DefaultSpringJndiContext(Hashtable environment, Map bindings) |
80 | 0 | { |
81 | 0 | if (environment == null) |
82 | |
{ |
83 | 0 | this.environment = new Hashtable(); |
84 | |
} |
85 | |
else |
86 | |
{ |
87 | 0 | this.environment = new Hashtable(environment); |
88 | |
} |
89 | 0 | this.bindings = bindings; |
90 | 0 | treeBindings = new HashMap(); |
91 | 0 | frozen = true; |
92 | 0 | } |
93 | |
|
94 | |
public DefaultSpringJndiContext(Hashtable environment, Map bindings, String nameInNamespace) |
95 | |
{ |
96 | 0 | this(environment, bindings); |
97 | 0 | this.nameInNamespace = nameInNamespace; |
98 | 0 | } |
99 | |
|
100 | |
protected DefaultSpringJndiContext(DefaultSpringJndiContext clone, Hashtable env) |
101 | 0 | { |
102 | 0 | this.bindings = clone.bindings; |
103 | 0 | this.treeBindings = clone.treeBindings; |
104 | 0 | this.environment = new Hashtable(env); |
105 | 0 | } |
106 | |
|
107 | |
protected DefaultSpringJndiContext(DefaultSpringJndiContext clone, Hashtable env, String nameInNamespace) |
108 | |
{ |
109 | 0 | this(clone, env); |
110 | 0 | this.nameInNamespace = nameInNamespace; |
111 | 0 | } |
112 | |
|
113 | |
public Object addToEnvironment(String propName, Object propVal) throws NamingException |
114 | |
{ |
115 | 0 | return environment.put(propName, propVal); |
116 | |
} |
117 | |
|
118 | |
public Hashtable getEnvironment() throws NamingException |
119 | |
{ |
120 | 0 | return (Hashtable) environment.clone(); |
121 | |
} |
122 | |
|
123 | |
public Object removeFromEnvironment(String propName) throws NamingException |
124 | |
{ |
125 | 0 | return environment.remove(propName); |
126 | |
} |
127 | |
|
128 | |
public Object lookup(String name) throws NamingException |
129 | |
{ |
130 | 0 | if (name.length() == 0) |
131 | |
{ |
132 | 0 | return this; |
133 | |
} |
134 | 0 | Object result = treeBindings.get(name); |
135 | 0 | if (result == null) |
136 | |
{ |
137 | 0 | result = bindings.get(name); |
138 | |
} |
139 | 0 | if (result == null) |
140 | |
{ |
141 | 0 | int pos = name.indexOf(':'); |
142 | 0 | if (pos > 0) |
143 | |
{ |
144 | 0 | String scheme = name.substring(0, pos); |
145 | 0 | Context ctx = NamingManager.getURLContext(scheme, environment); |
146 | 0 | if (ctx == null) |
147 | |
{ |
148 | 0 | throw new NamingException("scheme " + scheme + " not recognized"); |
149 | |
} |
150 | 0 | return ctx.lookup(name); |
151 | |
} |
152 | |
else |
153 | |
{ |
154 | |
|
155 | |
|
156 | 0 | CompositeName path = new CompositeName(name); |
157 | |
|
158 | 0 | if (path.size() == 0) |
159 | |
{ |
160 | 0 | return this; |
161 | |
} |
162 | |
else |
163 | |
{ |
164 | 0 | String first = path.get(0); |
165 | 0 | Object obj = bindings.get(first); |
166 | 0 | if (obj == null) |
167 | |
{ |
168 | 0 | throw new NameNotFoundException(name); |
169 | |
} |
170 | 0 | else if (obj instanceof Context && path.size() > 1) |
171 | |
{ |
172 | 0 | Context subContext = (Context) obj; |
173 | 0 | obj = subContext.lookup(path.getSuffix(1)); |
174 | |
} |
175 | 0 | return obj; |
176 | |
} |
177 | |
} |
178 | |
} |
179 | 0 | if (result instanceof LinkRef) |
180 | |
{ |
181 | 0 | LinkRef ref = (LinkRef) result; |
182 | 0 | result = lookup(ref.getLinkName()); |
183 | |
} |
184 | 0 | if (result instanceof Reference) |
185 | |
{ |
186 | |
try |
187 | |
{ |
188 | 0 | result = NamingManager.getObjectInstance(result, null, null, this.environment); |
189 | |
} |
190 | 0 | catch (NamingException e) |
191 | |
{ |
192 | 0 | throw e; |
193 | |
} |
194 | 0 | catch (Exception e) |
195 | |
{ |
196 | 0 | throw (NamingException) new NamingException("could not look up : " + name).initCause(e); |
197 | 0 | } |
198 | |
} |
199 | 0 | if (result instanceof DefaultSpringJndiContext) |
200 | |
{ |
201 | 0 | String prefix = getNameInNamespace(); |
202 | 0 | if (prefix.length() > 0) |
203 | |
{ |
204 | 0 | prefix = prefix + SEPARATOR; |
205 | |
} |
206 | 0 | result = new DefaultSpringJndiContext((DefaultSpringJndiContext) result, environment, prefix + name); |
207 | |
} |
208 | 0 | return result; |
209 | |
} |
210 | |
|
211 | |
public Object lookup(Name name) throws NamingException |
212 | |
{ |
213 | 0 | return lookup(name.toString()); |
214 | |
} |
215 | |
|
216 | |
public Object lookupLink(String name) throws NamingException |
217 | |
{ |
218 | 0 | return lookup(name); |
219 | |
} |
220 | |
|
221 | |
public Name composeName(Name name, Name prefix) throws NamingException |
222 | |
{ |
223 | 0 | Name result = (Name) prefix.clone(); |
224 | 0 | result.addAll(name); |
225 | 0 | return result; |
226 | |
} |
227 | |
|
228 | |
public String composeName(String name, String prefix) throws NamingException |
229 | |
{ |
230 | 0 | CompositeName result = new CompositeName(prefix); |
231 | 0 | result.addAll(new CompositeName(name)); |
232 | 0 | return result.toString(); |
233 | |
} |
234 | |
|
235 | |
public NamingEnumeration list(String name) throws NamingException |
236 | |
{ |
237 | 0 | Object o = lookup(name); |
238 | 0 | if (o == this) |
239 | |
{ |
240 | 0 | return new DefaultSpringJndiContext.ListEnumeration(); |
241 | |
} |
242 | 0 | else if (o instanceof Context) |
243 | |
{ |
244 | 0 | return ((Context) o).list(""); |
245 | |
} |
246 | |
else |
247 | |
{ |
248 | 0 | throw new NotContextException(); |
249 | |
} |
250 | |
} |
251 | |
|
252 | |
public NamingEnumeration listBindings(String name) throws NamingException |
253 | |
{ |
254 | 0 | Object o = lookup(name); |
255 | 0 | if (o == this) |
256 | |
{ |
257 | 0 | return new DefaultSpringJndiContext.ListBindingEnumeration(); |
258 | |
} |
259 | 0 | else if (o instanceof Context) |
260 | |
{ |
261 | 0 | return ((Context) o).listBindings(""); |
262 | |
} |
263 | |
else |
264 | |
{ |
265 | 0 | throw new NotContextException(); |
266 | |
} |
267 | |
} |
268 | |
|
269 | |
public Object lookupLink(Name name) throws NamingException |
270 | |
{ |
271 | 0 | return lookupLink(name.toString()); |
272 | |
} |
273 | |
|
274 | |
public NamingEnumeration list(Name name) throws NamingException |
275 | |
{ |
276 | 0 | return list(name.toString()); |
277 | |
} |
278 | |
|
279 | |
public NamingEnumeration listBindings(Name name) throws NamingException |
280 | |
{ |
281 | 0 | return listBindings(name.toString()); |
282 | |
} |
283 | |
|
284 | |
public void bind(Name name, Object value) throws NamingException |
285 | |
{ |
286 | 0 | bind(name.toString(), value); |
287 | 0 | } |
288 | |
|
289 | |
public void bind(String name, Object value) throws NamingException |
290 | |
{ |
291 | 0 | checkFrozen(); |
292 | 0 | internalBind(name, value); |
293 | 0 | } |
294 | |
|
295 | |
public void close() throws NamingException |
296 | |
{ |
297 | |
|
298 | 0 | } |
299 | |
|
300 | |
public Context createSubcontext(Name name) throws NamingException |
301 | |
{ |
302 | 0 | throw new OperationNotSupportedException(); |
303 | |
} |
304 | |
|
305 | |
public Context createSubcontext(String name) throws NamingException |
306 | |
{ |
307 | 0 | throw new OperationNotSupportedException(); |
308 | |
} |
309 | |
|
310 | |
public void destroySubcontext(Name name) throws NamingException |
311 | |
{ |
312 | 0 | throw new OperationNotSupportedException(); |
313 | |
} |
314 | |
|
315 | |
public void destroySubcontext(String name) throws NamingException |
316 | |
{ |
317 | 0 | throw new OperationNotSupportedException(); |
318 | |
} |
319 | |
|
320 | |
public String getNameInNamespace() throws NamingException |
321 | |
{ |
322 | 0 | return nameInNamespace; |
323 | |
} |
324 | |
|
325 | |
public NameParser getNameParser(Name name) throws NamingException |
326 | |
{ |
327 | 0 | return nameParser; |
328 | |
} |
329 | |
|
330 | |
public NameParser getNameParser(String name) throws NamingException |
331 | |
{ |
332 | 0 | return nameParser; |
333 | |
} |
334 | |
|
335 | |
public void rebind(Name name, Object value) throws NamingException |
336 | |
{ |
337 | 0 | rebind(name.toString(), value); |
338 | 0 | } |
339 | |
|
340 | |
public void rebind(String name, Object value) throws NamingException |
341 | |
{ |
342 | 0 | checkFrozen(); |
343 | 0 | internalBind(name, value, true); |
344 | 0 | } |
345 | |
|
346 | |
public void rename(Name oldName, Name newName) throws NamingException |
347 | |
{ |
348 | 0 | checkFrozen(); |
349 | 0 | Object value = lookup(oldName); |
350 | 0 | unbind(oldName); |
351 | 0 | bind(newName, value); |
352 | 0 | } |
353 | |
|
354 | |
public void rename(String oldName, String newName) throws NamingException |
355 | |
{ |
356 | 0 | Object value = lookup(oldName); |
357 | 0 | unbind(oldName); |
358 | 0 | bind(newName, value); |
359 | 0 | } |
360 | |
|
361 | |
public void unbind(Name name) throws NamingException |
362 | |
{ |
363 | 0 | unbind(name.toString()); |
364 | 0 | } |
365 | |
|
366 | |
public void unbind(String name) throws NamingException |
367 | |
{ |
368 | 0 | checkFrozen(); |
369 | 0 | internalBind(name, null, true); |
370 | 0 | } |
371 | |
|
372 | 0 | private abstract class AbstractLocalNamingEnumeration implements NamingEnumeration |
373 | |
{ |
374 | |
|
375 | 0 | private Iterator i = bindings.entrySet().iterator(); |
376 | |
|
377 | |
public boolean hasMore() throws NamingException |
378 | |
{ |
379 | 0 | return i.hasNext(); |
380 | |
} |
381 | |
|
382 | |
public boolean hasMoreElements() |
383 | |
{ |
384 | 0 | return i.hasNext(); |
385 | |
} |
386 | |
|
387 | |
protected Map.Entry getNext() |
388 | |
{ |
389 | 0 | return (Map.Entry) i.next(); |
390 | |
} |
391 | |
|
392 | |
public void close() throws NamingException |
393 | |
{ |
394 | 0 | } |
395 | |
} |
396 | |
|
397 | 0 | private class ListEnumeration extends AbstractLocalNamingEnumeration |
398 | |
{ |
399 | |
|
400 | |
public Object next() throws NamingException |
401 | |
{ |
402 | 0 | return nextElement(); |
403 | |
} |
404 | |
|
405 | |
public Object nextElement() |
406 | |
{ |
407 | 0 | Map.Entry entry = getNext(); |
408 | 0 | return new NameClassPair((String) entry.getKey(), entry.getValue().getClass().getName()); |
409 | |
} |
410 | |
} |
411 | |
|
412 | 0 | private class ListBindingEnumeration extends AbstractLocalNamingEnumeration |
413 | |
{ |
414 | |
|
415 | |
public Object next() throws NamingException |
416 | |
{ |
417 | 0 | return nextElement(); |
418 | |
} |
419 | |
|
420 | |
public Object nextElement() |
421 | |
{ |
422 | 0 | Map.Entry entry = getNext(); |
423 | 0 | return new Binding((String) entry.getKey(), entry.getValue()); |
424 | |
} |
425 | |
} |
426 | |
|
427 | |
public Map getEntries() |
428 | |
{ |
429 | 0 | return new HashMap(bindings); |
430 | |
} |
431 | |
|
432 | |
public void setEntries(Map entries) throws NamingException |
433 | |
{ |
434 | 0 | if (entries != null) |
435 | |
{ |
436 | 0 | for (Iterator iter = entries.entrySet().iterator(); iter.hasNext();) |
437 | |
{ |
438 | 0 | Map.Entry entry = (Map.Entry) iter.next(); |
439 | 0 | String name = (String) entry.getKey(); |
440 | 0 | Object value = entry.getValue(); |
441 | 0 | internalBind(name, value); |
442 | 0 | } |
443 | |
} |
444 | 0 | } |
445 | |
|
446 | |
public boolean isFreeze() |
447 | |
{ |
448 | 0 | return freeze; |
449 | |
} |
450 | |
|
451 | |
public void setFreeze(boolean freeze) |
452 | |
{ |
453 | 0 | this.freeze = freeze; |
454 | 0 | } |
455 | |
|
456 | |
|
457 | |
|
458 | |
|
459 | |
|
460 | |
|
461 | |
|
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | |
protected Map internalBind(String name, Object value) throws NamingException |
469 | |
{ |
470 | 0 | return internalBind(name, value, false); |
471 | |
|
472 | |
} |
473 | |
|
474 | |
protected Map internalBind(String name, Object value, boolean allowRebind) throws NamingException |
475 | |
{ |
476 | 0 | if (name == null || name.length() == 0) |
477 | |
{ |
478 | 0 | throw new NamingException("Invalid Name " + name); |
479 | |
} |
480 | 0 | if (frozen) |
481 | |
{ |
482 | 0 | throw new NamingException("Read only"); |
483 | |
} |
484 | |
|
485 | 0 | Map newBindings = new HashMap(); |
486 | 0 | int pos = name.indexOf('/'); |
487 | 0 | if (pos == -1) |
488 | |
{ |
489 | 0 | Object oldValue = treeBindings.put(name, value); |
490 | 0 | if (!allowRebind && oldValue != null) |
491 | |
{ |
492 | 0 | throw new NamingException("Something already bound at " + name); |
493 | |
} |
494 | 0 | bindings.put(name, value); |
495 | 0 | newBindings.put(name, value); |
496 | 0 | } |
497 | |
else |
498 | |
{ |
499 | 0 | String segment = name.substring(0, pos); |
500 | |
|
501 | 0 | if (segment == null || segment.length() == 0) |
502 | |
{ |
503 | 0 | throw new NamingException("Invalid segment " + segment); |
504 | |
} |
505 | 0 | Object o = treeBindings.get(segment); |
506 | 0 | if (o == null) |
507 | |
{ |
508 | 0 | o = newContext(); |
509 | 0 | treeBindings.put(segment, o); |
510 | 0 | bindings.put(segment, o); |
511 | 0 | newBindings.put(segment, o); |
512 | |
} |
513 | 0 | else if (!(o instanceof DefaultSpringJndiContext)) |
514 | |
{ |
515 | 0 | throw new NamingException("Something already bound where a subcontext should go"); |
516 | |
} |
517 | 0 | DefaultSpringJndiContext defaultContext = (DefaultSpringJndiContext) o; |
518 | 0 | String remainder = name.substring(pos + 1); |
519 | 0 | Map subBindings = defaultContext.internalBind(remainder, value, allowRebind); |
520 | 0 | for (Iterator iterator = subBindings.entrySet().iterator(); iterator.hasNext();) |
521 | |
{ |
522 | 0 | Map.Entry entry = (Map.Entry) iterator.next(); |
523 | 0 | String subName = segment + "/" + (String) entry.getKey(); |
524 | 0 | Object bound = entry.getValue(); |
525 | 0 | treeBindings.put(subName, bound); |
526 | 0 | newBindings.put(subName, bound); |
527 | 0 | } |
528 | |
} |
529 | 0 | return newBindings; |
530 | |
} |
531 | |
|
532 | |
protected void checkFrozen() throws OperationNotSupportedException |
533 | |
{ |
534 | 0 | if (isFreeze()) |
535 | |
{ |
536 | 0 | throw new OperationNotSupportedException("JNDI context is frozen!"); |
537 | |
} |
538 | 0 | } |
539 | |
|
540 | |
protected DefaultSpringJndiContext newContext() |
541 | |
{ |
542 | 0 | return new DefaultSpringJndiContext(); |
543 | |
} |
544 | |
|
545 | |
} |
546 | |
|