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