View Javadoc

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