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