KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > jndi > SimpleContext


1 /*
2  * $Id: SimpleContext.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.impl.jndi;
12
13 import javax.naming.Binding JavaDoc;
14 import javax.naming.CompositeName JavaDoc;
15 import javax.naming.Context JavaDoc;
16 import javax.naming.Name JavaDoc;
17 import javax.naming.NameAlreadyBoundException JavaDoc;
18 import javax.naming.NameClassPair JavaDoc;
19 import javax.naming.NameNotFoundException JavaDoc;
20 import javax.naming.NameParser JavaDoc;
21 import javax.naming.NamingEnumeration JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23 import javax.naming.OperationNotSupportedException JavaDoc;
24
25 import java.util.HashMap JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.jar.Attributes JavaDoc;
30
31 /**
32  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
33  * @version $Revision: 3798 $
34  */

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