View Javadoc

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