View Javadoc

1   // ========================================================================
2   // Copyright (c) 2000-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.jndi.local;
15  
16  import java.util.Hashtable;
17  import java.util.Properties;
18  
19  import javax.naming.CompoundName;
20  import javax.naming.Context;
21  import javax.naming.Name;
22  import javax.naming.NameParser;
23  import javax.naming.NamingEnumeration;
24  import javax.naming.NamingException;
25  
26  import org.eclipse.jetty.jndi.NamingContext;
27  
28  /**
29   * 
30   * localContext
31   * 
32   * 
33   * @version $Revision: 4780 $ $Date: 2009-03-17 16:36:08 +0100 (Tue, 17 Mar 2009) $
34   * 
35   */
36  public class localContextRoot implements Context
37  {
38      private static final NamingContext __root = new NamingContext();
39      private final Hashtable<String,Object> _env;
40  
41      // make a root for the static namespace local:
42      static
43      {
44          __root.setNameParser(new LocalNameParser());
45      }
46  
47      static class LocalNameParser implements NameParser
48      {
49          Properties syntax = new Properties();
50  
51          LocalNameParser()
52          {
53              syntax.put("jndi.syntax.direction", "left_to_right");
54              syntax.put("jndi.syntax.separator", "/");
55              syntax.put("jndi.syntax.ignorecase", "false");
56          }
57  
58          public Name parse(String name) throws NamingException
59          {
60              return new CompoundName(name, syntax);
61          }
62      }
63      
64      public static NamingContext getRoot()
65      {
66          return __root;
67      }
68  
69      public localContextRoot(Hashtable env)
70      {
71          _env = new Hashtable(env);
72      }
73  
74      /**
75       * 
76       * 
77       * @see javax.naming.Context#close()
78       */
79      public void close() throws NamingException
80      {
81  
82      }
83  
84      /**
85       * 
86       * 
87       * @see javax.naming.Context#getNameInNamespace()
88       */
89      public String getNameInNamespace() throws NamingException
90      {
91          return "";
92      }
93  
94      /**
95       * 
96       * 
97       * @see javax.naming.Context#destroySubcontext(java.lang.String)
98       */
99      public void destroySubcontext(String name) throws NamingException
100     {
101         synchronized (__root)
102         {
103             __root.destroySubcontext(getSuffix(name));
104         }
105     }
106 
107     /**
108      * 
109      * 
110      * @see javax.naming.Context#unbind(java.lang.String)
111      */
112     public void unbind(String name) throws NamingException
113     {
114         synchronized (__root)
115         {
116             __root.unbind(getSuffix(name));
117         }
118     }
119 
120     /**
121      * 
122      * 
123      * @see javax.naming.Context#getEnvironment()
124      */
125     public Hashtable getEnvironment() throws NamingException
126     {
127         return _env;
128     }
129 
130     /**
131      * 
132      * 
133      * @see javax.naming.Context#destroySubcontext(javax.naming.Name)
134      */
135     public void destroySubcontext(Name name) throws NamingException
136     {
137         synchronized (__root)
138         {
139             __root.destroySubcontext(getSuffix(name));
140         }
141     }
142 
143     /**
144      * 
145      * 
146      * @see javax.naming.Context#unbind(javax.naming.Name)
147      */
148     public void unbind(Name name) throws NamingException
149     {
150         synchronized (__root)
151         {
152             __root.unbind(getSuffix(name));
153         }
154     }
155 
156     /**
157      * 
158      * 
159      * @see javax.naming.Context#lookup(java.lang.String)
160      */
161     public Object lookup(String name) throws NamingException
162     {
163         synchronized (__root)
164         {
165             return __root.lookup(getSuffix(name));
166         }
167     }
168 
169     /**
170      * 
171      * 
172      * @see javax.naming.Context#lookupLink(java.lang.String)
173      */
174     public Object lookupLink(String name) throws NamingException
175     {
176         synchronized (__root)
177         {
178             return __root.lookupLink(getSuffix(name));
179         }
180     }
181 
182     /**
183      * 
184      *       
185      * @see javax.naming.Context#removeFromEnvironment(java.lang.String)
186      */
187     public Object removeFromEnvironment(String propName) throws NamingException
188     {
189         return _env.remove(propName);
190     }
191 
192     /**
193      * 
194      * 
195      * @see javax.naming.Context#bind(java.lang.String, java.lang.Object)
196      */
197     public void bind(String name, Object obj) throws NamingException
198     {
199         synchronized (__root)
200         {
201             __root.bind(getSuffix(name), obj);
202         }
203     }
204 
205     /**
206      * 
207      * 
208      * @see javax.naming.Context#rebind(java.lang.String, java.lang.Object)
209      */
210     public void rebind(String name, Object obj) throws NamingException
211     {
212         synchronized (__root)
213         {
214             __root.rebind(getSuffix(name), obj);
215         }
216     }
217 
218     /**
219      * 
220      * 
221      * @see javax.naming.Context#lookup(javax.naming.Name)
222      */
223     public Object lookup(Name name) throws NamingException
224     {
225         synchronized (__root)
226         {
227             return __root.lookup(getSuffix(name));
228         }
229     }
230 
231     /**
232      * 
233      * 
234      * @see javax.naming.Context#lookupLink(javax.naming.Name)
235      */
236     public Object lookupLink(Name name) throws NamingException
237     {
238         synchronized (__root)
239         {
240             return __root.lookupLink(getSuffix(name));
241         }
242     }
243 
244     /**
245      * 
246      * 
247      * @see javax.naming.Context#bind(javax.naming.Name, java.lang.Object)
248      */
249     public void bind(Name name, Object obj) throws NamingException
250     {
251         synchronized (__root)
252         {
253             __root.bind(getSuffix(name), obj);
254         }
255     }
256 
257     /**
258      *
259      * 
260      * @see javax.naming.Context#rebind(javax.naming.Name, java.lang.Object)
261      */
262     public void rebind(Name name, Object obj) throws NamingException
263     {
264         synchronized (__root)
265         {
266             __root.rebind(getSuffix(name), obj);
267         }
268     }
269 
270     /**
271      * 
272      * 
273      * @see javax.naming.Context#rename(java.lang.String, java.lang.String)
274      */
275     public void rename(String oldName, String newName) throws NamingException
276     {
277         synchronized (__root)
278         {
279             __root.rename(getSuffix(oldName), getSuffix(newName));
280         }
281     }
282 
283     /**
284      * 
285      * 
286      * @see javax.naming.Context#createSubcontext(java.lang.String)
287      */
288     public Context createSubcontext(String name) throws NamingException
289     {
290         synchronized (__root)
291         {
292             return __root.createSubcontext(getSuffix(name));
293         }
294     }
295 
296     /**
297      * 
298      * 
299      * @see javax.naming.Context#createSubcontext(javax.naming.Name)
300      */
301     public Context createSubcontext(Name name) throws NamingException
302     {
303         synchronized (__root)
304         {
305             return __root.createSubcontext(getSuffix(name));
306         }
307     }
308 
309     /**
310      * 
311      * 
312      * @see javax.naming.Context#rename(javax.naming.Name, javax.naming.Name)
313      */
314     public void rename(Name oldName, Name newName) throws NamingException
315     {
316         synchronized (__root)
317         {
318             __root.rename(getSuffix(oldName), getSuffix(newName));
319         }
320     }
321 
322     /**
323      *
324      * 
325      * @see javax.naming.Context#getNameParser(java.lang.String)
326      */
327     public NameParser getNameParser(String name) throws NamingException
328     {
329         return __root.getNameParser(name);
330     }
331 
332     /**
333      * 
334      * 
335      * @see javax.naming.Context#getNameParser(javax.naming.Name)
336      */
337     public NameParser getNameParser(Name name) throws NamingException
338     {
339         return __root.getNameParser(name);
340     }
341 
342     /**
343      * 
344      * 
345      * @see javax.naming.Context#list(java.lang.String)
346      */
347     public NamingEnumeration list(String name) throws NamingException
348     {
349         synchronized (__root)
350         {
351             return __root.list(getSuffix(name));
352         }
353     }
354 
355     /**
356      *
357      * 
358      * @see javax.naming.Context#listBindings(java.lang.String)
359      */
360     public NamingEnumeration listBindings(String name) throws NamingException
361     {
362         synchronized (__root)
363         {
364             return __root.listBindings(getSuffix(name));
365         }
366     }
367 
368     /**
369      *
370      * 
371      * @see javax.naming.Context#list(javax.naming.Name)
372      */
373     public NamingEnumeration list(Name name) throws NamingException
374     {
375         synchronized (__root)
376         {
377             return __root.list(getSuffix(name));
378         }
379     }
380 
381     /**
382      *
383      * 
384      * @see javax.naming.Context#listBindings(javax.naming.Name)
385      */
386     public NamingEnumeration listBindings(Name name) throws NamingException
387     {
388         synchronized (__root)
389         {
390             return __root.listBindings(getSuffix(name));
391         }
392     }
393 
394     /**
395      *
396      * 
397      * @see javax.naming.Context#addToEnvironment(java.lang.String,
398      *      java.lang.Object)
399      */
400     public Object addToEnvironment(String propName, Object propVal)
401             throws NamingException
402     {
403         return _env.put(propName, propVal);
404     }
405 
406     /**
407      *
408      * 
409      * @see javax.naming.Context#composeName(java.lang.String, java.lang.String)
410      */
411     public String composeName(String name, String prefix)
412             throws NamingException
413     {
414         return __root.composeName(name, prefix);
415     }
416 
417     /**
418      *
419      * 
420      * @see javax.naming.Context#composeName(javax.naming.Name,
421      *      javax.naming.Name)
422      */
423     public Name composeName(Name name, Name prefix) throws NamingException
424     {
425         return __root.composeName(name, prefix);
426     }
427 
428     protected String getSuffix(String url) throws NamingException
429     {
430         return url;
431     }
432 
433     protected Name getSuffix(Name name) throws NamingException
434     {
435         return name;
436     }
437 
438 }