01: package com.bm.jndi;
02:
03: import java.util.HashMap;
04: import java.util.Map;
05:
06: import javax.naming.Context;
07: import javax.naming.NameAlreadyBoundException;
08: import javax.naming.NamingException;
09:
10: /**
11: * An in memory naming context.
12: *
13: */
14: public class MemoryContext extends AbstractContext implements Context {
15:
16: private static final String JAVA_COMP_SPACE = "java:/";
17: private final Map<String, Object> bindings = new HashMap<String, Object>();
18:
19: /**
20: * {@inheritDoc}
21: */
22: @Override
23: public void bind(String name, Object obj) throws NamingException {
24: bindings.put(name, obj);
25: }
26:
27: /**
28: * {@inheritDoc}
29: */
30: @Override
31: public Object lookup(String name) throws NamingException {
32: if (name.startsWith(JAVA_COMP_SPACE)) {
33: name = name.substring(JAVA_COMP_SPACE.length());
34: }
35: if (this .bindings.containsKey(name)) {
36: return this .bindings.get(name);
37: }
38: throw new NamingException("Can't find the name (" + name
39: + ") in the JNDI tree Current bindings>(" + bindings
40: + ")");
41: }
42:
43: /**
44: * {@inheritDoc}
45: */
46: @Override
47: public void rebind(String name, Object obj) throws NamingException {
48: bindings.put(name, obj);
49: }
50:
51: /**
52: * {@inheritDoc}
53: */
54: @Override
55: public void rename(String oldName, String newName)
56: throws NamingException {
57: if (this .bindings.containsKey(newName)) {
58: throw new NameAlreadyBoundException("The name (" + newName
59: + ") is already bound");
60: }
61: if (this .bindings.containsKey(oldName)) {
62: final Object tmp = this .bindings.remove(oldName);
63: this .bindings.put(newName, tmp);
64: } else {
65:
66: throw new NamingException("Can't find the name (" + oldName
67: + ") in the JNDI tree");
68: }
69: }
70:
71: /**
72: * {@inheritDoc}
73: */
74: @Override
75: public void unbind(String name) throws NamingException {
76: this.bindings.remove(name);
77: }
78:
79: }
|