View Javadoc

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