View Javadoc

1   // ========================================================================
2   // Copyright (c) 1999-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.java;
15  
16  
17  import java.util.Hashtable;
18  
19  import javax.naming.Context;
20  import javax.naming.Name;
21  import javax.naming.NameParser;
22  import javax.naming.NamingEnumeration;
23  import javax.naming.NamingException;
24  import javax.naming.Reference;
25  import javax.naming.StringRefAddr;
26  
27  import org.eclipse.jetty.jndi.ContextFactory;
28  import org.eclipse.jetty.jndi.NamingContext;
29  import org.eclipse.jetty.jndi.NamingUtil;
30  import org.eclipse.jetty.util.log.Logger;
31  
32  
33  
34  
35  /** javaRootURLContext
36   * <p>This is the root of the java: url namespace
37   *
38   * <p><h4>Notes</h4>
39   * <p>Thanks to Rickard Oberg for the idea of binding an ObjectFactory at "comp".
40   *
41   * <p><h4>Usage</h4>
42   * <pre>
43   */
44  /*
45  * </pre>
46  *
47  * @see
48  *
49  * 
50  * @version 1.0
51  */
52  public class javaRootURLContext implements Context
53  {
54      private static Logger __log = NamingUtil.__log;
55      
56      public static final String URL_PREFIX = "java:";
57  
58      protected Hashtable _env;
59  
60      protected static NamingContext __nameRoot;
61  
62      protected static NameParser __javaNameParser;
63  
64      
65      static 
66      {   
67          try
68          {
69              __javaNameParser = new javaNameParser();       
70              __nameRoot = new NamingContext(null,null,null,__javaNameParser);
71            
72              StringRefAddr parserAddr = new StringRefAddr("parser", __javaNameParser.getClass().getName());
73              
74              Reference ref = new Reference ("javax.naming.Context",
75                                             parserAddr,
76                                             ContextFactory.class.getName(),
77                                             (String)null);
78  
79              //bind special object factory at comp
80              __nameRoot.bind ("comp", ref);
81          }
82          catch (Exception e)
83          {
84              __log.warn(e);
85          }
86      }
87  
88  
89  
90      /*------------------------------------------------*/
91      /**
92       * Creates a new <code>javaRootURLContext</code> instance.
93       *
94       * @param env a <code>Hashtable</code> value
95       */
96      public javaRootURLContext(Hashtable env) 
97      {
98          _env = env;
99      } 
100     
101 
102     public Object lookup(Name name) 
103         throws NamingException
104     {
105         return getRoot().lookup(stripProtocol(name));
106     }
107 
108 
109     public Object lookup(String name) 
110         throws NamingException
111     {
112         return getRoot().lookup(stripProtocol(name));
113     }
114 
115     public void bind(Name name, Object obj) 
116         throws NamingException
117     {
118         getRoot().bind(stripProtocol(name), obj);
119     }
120 
121     public void bind(String name, Object obj) 
122         throws NamingException
123     { 
124         getRoot().bind(stripProtocol(name), obj);
125     }
126 
127     public void unbind (String name)
128         throws NamingException
129     {
130         getRoot().unbind(stripProtocol(name));
131     }
132     
133     public void unbind (Name name)
134         throws NamingException
135     {
136         getRoot().unbind(stripProtocol(name));
137     }
138 
139     public void rename (String oldStr, String newStr)
140         throws NamingException
141     {
142         getRoot().rename (stripProtocol(oldStr), stripProtocol(newStr));
143     }
144 
145     public void rename (Name oldName, Name newName)
146         throws NamingException
147     {
148         getRoot().rename (stripProtocol(oldName), stripProtocol(newName));
149     }
150 
151     public void rebind (Name name, Object obj)
152         throws NamingException
153     {
154         getRoot().rebind(stripProtocol(name), obj);
155     }
156 
157     public void rebind (String name, Object obj)
158         throws NamingException
159     {
160         getRoot().rebind(stripProtocol(name), obj);
161     }
162 
163 
164     public Object lookupLink (Name name)
165         throws NamingException
166     {
167         return getRoot().lookupLink(stripProtocol(name));
168     }
169 
170     public Object lookupLink (String name)
171         throws NamingException
172     {
173         return getRoot().lookupLink(stripProtocol(name));
174     }
175    
176 
177     public Context createSubcontext (Name name)
178         throws NamingException
179     {
180         return getRoot().createSubcontext(stripProtocol(name));
181     }
182 
183     public Context createSubcontext (String name)
184         throws NamingException
185     {
186         return getRoot().createSubcontext(stripProtocol(name));
187     }
188 
189 
190     public void destroySubcontext (Name name)
191         throws NamingException    
192     {
193         getRoot().destroySubcontext(stripProtocol(name));
194     }
195 
196     public void destroySubcontext (String name)
197         throws NamingException
198     {
199         getRoot().destroySubcontext(stripProtocol(name));
200     }
201 
202 
203     public NamingEnumeration list(Name name)
204         throws NamingException
205     {
206         return getRoot().list(stripProtocol(name));
207     }
208 
209 
210     public NamingEnumeration list(String name)
211         throws NamingException
212     {
213         return getRoot().list(stripProtocol(name));
214     }
215 
216     public NamingEnumeration listBindings(Name name)
217         throws NamingException
218     {
219         return getRoot().listBindings(stripProtocol(name));
220     }
221 
222     public NamingEnumeration listBindings(String name)
223         throws NamingException
224     {
225         return getRoot().listBindings(stripProtocol(name));
226     }
227 
228     
229     public Name composeName (Name name,
230                              Name prefix)
231         throws NamingException
232     {
233         return getRoot().composeName(name, prefix);
234     }
235 
236     public String composeName (String name,
237                                String prefix)
238         throws NamingException
239     {
240         return getRoot().composeName(name, prefix);
241     }
242 
243 
244     public void close ()       
245         throws NamingException
246     {
247     }
248 
249     public String getNameInNamespace ()
250         throws NamingException
251     {
252         return URL_PREFIX;
253     }
254 
255     public NameParser getNameParser (Name name)
256         throws NamingException
257     {
258         return __javaNameParser;
259     }
260     
261     public NameParser getNameParser (String name) 
262         throws NamingException
263     {
264         return __javaNameParser;
265     }
266 
267 
268     public Object addToEnvironment(String propName,
269                                    Object propVal)
270         throws NamingException
271     {
272        return _env.put (propName,propVal);
273     }
274 
275     public Object removeFromEnvironment(String propName)
276         throws NamingException
277     {
278         return _env.remove (propName);
279     }
280 
281     public Hashtable getEnvironment ()
282     {
283         return _env;
284     }
285 
286     public static NamingContext getRoot ()
287     {
288         return __nameRoot;
289     }
290 
291 
292     protected Name stripProtocol (Name name)
293         throws NamingException
294     {
295         if ((name != null) && (name.size() > 0))
296         {
297             String head = name.get(0);
298             
299             if(__log.isDebugEnabled())__log.debug("Head element of name is: "+head);
300 
301             if (head.startsWith(URL_PREFIX))
302             {
303                 head = head.substring (URL_PREFIX.length());
304                 name.remove(0);
305                 if (head.length() > 0)
306                     name.add(0, head);
307 
308                 if(__log.isDebugEnabled())__log.debug("name modified to "+name.toString());
309             }
310         }
311         
312         return name;
313     }
314 
315 
316 
317     protected String stripProtocol (String name)
318     {
319         String newName = name;
320 
321         if ((name != null) && (!name.equals("")))
322         {
323             if (name.startsWith(URL_PREFIX))
324                newName = name.substring(URL_PREFIX.length());
325         }
326         
327         return newName;
328     }
329 
330 }