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