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