View Javadoc

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