View Javadoc

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