1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.jndi; |
8 | |
|
9 | |
import java.util.HashMap; |
10 | |
import java.util.Hashtable; |
11 | |
import java.util.Iterator; |
12 | |
import java.util.Map; |
13 | |
import java.util.jar.Attributes; |
14 | |
|
15 | |
import javax.naming.Binding; |
16 | |
import javax.naming.CompositeName; |
17 | |
import javax.naming.Context; |
18 | |
import javax.naming.Name; |
19 | |
import javax.naming.NameAlreadyBoundException; |
20 | |
import javax.naming.NameClassPair; |
21 | |
import javax.naming.NameNotFoundException; |
22 | |
import javax.naming.NameParser; |
23 | |
import javax.naming.NamingEnumeration; |
24 | |
import javax.naming.NamingException; |
25 | |
import javax.naming.OperationNotSupportedException; |
26 | |
|
27 | |
public class SimpleContext implements Context |
28 | |
{ |
29 | |
|
30 | 0 | protected Map bindings = new HashMap(); |
31 | |
|
32 | |
|
33 | |
private Hashtable environment; |
34 | |
|
35 | |
public SimpleContext() |
36 | |
{ |
37 | 0 | super(); |
38 | 0 | } |
39 | |
|
40 | |
public Object lookupLink(Name name) throws NamingException |
41 | |
{ |
42 | |
|
43 | 0 | return null; |
44 | |
} |
45 | |
|
46 | |
public void rename(Name oldName, Name newName) throws NamingException |
47 | |
{ |
48 | |
|
49 | 0 | } |
50 | |
|
51 | |
public NameParser getNameParser(Name name) throws NamingException |
52 | |
{ |
53 | |
|
54 | 0 | return null; |
55 | |
} |
56 | |
|
57 | |
public NamingEnumeration list(Name name) throws NamingException |
58 | |
{ |
59 | |
|
60 | 0 | return null; |
61 | |
} |
62 | |
|
63 | |
public Object lookup(Name name) throws NamingException |
64 | |
{ |
65 | 0 | return lookup(name.toString()); |
66 | |
} |
67 | |
|
68 | |
public Object lookup(String name) throws NamingException |
69 | |
{ |
70 | 0 | Object rc = bindings.get(name); |
71 | 0 | if (rc == null) |
72 | |
{ |
73 | 0 | throw new NameNotFoundException(name); |
74 | |
} |
75 | 0 | return rc; |
76 | |
} |
77 | |
|
78 | |
public void bind(Name name, Object obj) throws NamingException |
79 | |
{ |
80 | 0 | bind(name.toString(), obj); |
81 | 0 | } |
82 | |
|
83 | |
public void bind(String name, Object obj) throws NamingException |
84 | |
{ |
85 | 0 | if (bindings.containsKey(name)) |
86 | |
{ |
87 | 0 | throw new NameAlreadyBoundException(name); |
88 | |
} |
89 | 0 | bindings.put(name, obj); |
90 | 0 | } |
91 | |
|
92 | |
public void rebind(Name name, Object obj) throws NamingException |
93 | |
{ |
94 | 0 | rebind(name.toString(), obj); |
95 | 0 | } |
96 | |
|
97 | |
public void rebind(String name, Object obj) throws NamingException |
98 | |
{ |
99 | 0 | bindings.put(name, obj); |
100 | 0 | } |
101 | |
|
102 | |
public void unbind(Name name) throws NamingException |
103 | |
{ |
104 | 0 | unbind(name.toString()); |
105 | 0 | } |
106 | |
|
107 | |
public void unbind(String name) throws NamingException |
108 | |
{ |
109 | 0 | if (bindings.remove(name) == null) |
110 | |
{ |
111 | 0 | throw new NameNotFoundException(name); |
112 | |
} |
113 | 0 | } |
114 | |
|
115 | |
public void rename(Attributes.Name oldName, Attributes.Name newName) throws NamingException |
116 | |
{ |
117 | 0 | rename(oldName.toString(), newName.toString()); |
118 | 0 | } |
119 | |
|
120 | |
public void rename(String oldName, String newName) throws NamingException |
121 | |
{ |
122 | 0 | if (!bindings.containsKey(oldName)) |
123 | |
{ |
124 | 0 | throw new NameNotFoundException(oldName); |
125 | |
} |
126 | 0 | if (bindings.containsKey(newName)) |
127 | |
{ |
128 | 0 | throw new NameAlreadyBoundException(newName); |
129 | |
} |
130 | |
|
131 | 0 | bindings.put(newName, bindings.remove(oldName)); |
132 | 0 | } |
133 | |
|
134 | |
public NamingEnumeration list(Attributes.Name name) throws NamingException |
135 | |
{ |
136 | 0 | return list(name.toString()); |
137 | |
} |
138 | |
|
139 | |
public NamingEnumeration list(String name) throws NamingException |
140 | |
{ |
141 | 0 | if (name.length() > 0) |
142 | |
{ |
143 | 0 | throw new OperationNotSupportedException("subcontexts not supported"); |
144 | |
} |
145 | 0 | final Iterator i = bindings.entrySet().iterator(); |
146 | 0 | return new NamingEnumeration() |
147 | 0 | { |
148 | |
public Object next() |
149 | |
{ |
150 | 0 | Map.Entry e = (Map.Entry) i.next(); |
151 | 0 | return new NameClassPair((String) e.getKey(), e.getValue().getClass().getName()); |
152 | |
} |
153 | |
|
154 | |
public boolean hasMore() |
155 | |
{ |
156 | 0 | return i.hasNext(); |
157 | |
} |
158 | |
|
159 | |
public void close() |
160 | |
{ |
161 | |
|
162 | 0 | } |
163 | |
|
164 | |
public boolean hasMoreElements() |
165 | |
{ |
166 | 0 | return hasMore(); |
167 | |
} |
168 | |
|
169 | |
public Object nextElement() |
170 | |
{ |
171 | 0 | return next(); |
172 | |
} |
173 | |
}; |
174 | |
} |
175 | |
|
176 | |
public NamingEnumeration listBindings(Name name) throws NamingException |
177 | |
{ |
178 | 0 | return listBindings(name.toString()); |
179 | |
} |
180 | |
|
181 | |
public NamingEnumeration listBindings(String name) throws NamingException |
182 | |
{ |
183 | 0 | if (name.length() > 0) |
184 | |
{ |
185 | 0 | throw new OperationNotSupportedException("subcontexts not supported"); |
186 | |
} |
187 | 0 | final Iterator i = bindings.entrySet().iterator(); |
188 | 0 | return new NamingEnumeration() |
189 | 0 | { |
190 | |
public Object next() |
191 | |
{ |
192 | 0 | Map.Entry e = (Map.Entry) i.next(); |
193 | 0 | return new Binding((String) e.getKey(), e.getValue()); |
194 | |
} |
195 | |
|
196 | |
public boolean hasMore() |
197 | |
{ |
198 | 0 | return i.hasNext(); |
199 | |
} |
200 | |
|
201 | |
public void close() |
202 | |
{ |
203 | |
|
204 | 0 | } |
205 | |
|
206 | |
public boolean hasMoreElements() |
207 | |
{ |
208 | 0 | return hasMore(); |
209 | |
} |
210 | |
|
211 | |
public Object nextElement() |
212 | |
{ |
213 | 0 | return next(); |
214 | |
} |
215 | |
|
216 | |
}; |
217 | |
} |
218 | |
|
219 | |
public void destroySubcontext(Name name) throws NamingException |
220 | |
{ |
221 | 0 | destroySubcontext(name.toString()); |
222 | 0 | } |
223 | |
|
224 | |
public void destroySubcontext(String name) throws NamingException |
225 | |
{ |
226 | 0 | throw new OperationNotSupportedException("subcontexts not supported"); |
227 | |
} |
228 | |
|
229 | |
public Context createSubcontext(Name name) throws NamingException |
230 | |
{ |
231 | 0 | return createSubcontext(name.toString()); |
232 | |
} |
233 | |
|
234 | |
public Context createSubcontext(String name) throws NamingException |
235 | |
{ |
236 | 0 | throw new OperationNotSupportedException("subcontexts not supported"); |
237 | |
} |
238 | |
|
239 | |
public Object lookupLink(Attributes.Name name) throws NamingException |
240 | |
{ |
241 | 0 | return lookupLink(name.toString()); |
242 | |
} |
243 | |
|
244 | |
public Object lookupLink(String name) throws NamingException |
245 | |
{ |
246 | 0 | return lookup(name); |
247 | |
} |
248 | |
|
249 | |
public NameParser getNameParser(Attributes.Name name) |
250 | |
{ |
251 | 0 | return getNameParser(name.toString()); |
252 | |
} |
253 | |
|
254 | |
public NameParser getNameParser(String name) |
255 | |
{ |
256 | 0 | throw new UnsupportedOperationException("getNameParser"); |
257 | |
} |
258 | |
|
259 | |
public String composeName(String name, String prefix) throws NamingException |
260 | |
{ |
261 | 0 | return composeName(new CompositeName(name), new CompositeName(prefix)).toString(); |
262 | |
} |
263 | |
|
264 | |
public Name composeName(Name name, Name prefix) throws NamingException |
265 | |
{ |
266 | 0 | Name result = (Name) prefix.clone(); |
267 | 0 | result.addAll(name); |
268 | 0 | return result; |
269 | |
} |
270 | |
|
271 | |
public Object addToEnvironment(String key, Object val) |
272 | |
{ |
273 | 0 | if (environment == null) |
274 | |
{ |
275 | 0 | environment = new Hashtable(); |
276 | |
} |
277 | 0 | return environment.put(key, val); |
278 | |
} |
279 | |
|
280 | |
public Object removeFromEnvironment(String key) |
281 | |
{ |
282 | 0 | if (environment == null) |
283 | |
{ |
284 | 0 | environment = new Hashtable(); |
285 | |
} |
286 | 0 | return environment.remove(key); |
287 | |
} |
288 | |
|
289 | |
public Hashtable getEnvironment() |
290 | |
{ |
291 | 0 | if (environment == null) |
292 | |
{ |
293 | 0 | environment = new Hashtable(); |
294 | |
} |
295 | 0 | return environment; |
296 | |
} |
297 | |
|
298 | |
public void close() |
299 | |
{ |
300 | |
|
301 | 0 | } |
302 | |
|
303 | |
public String getNameInNamespace() |
304 | |
{ |
305 | 0 | return ""; |
306 | |
} |
307 | |
|
308 | |
} |