View Javadoc

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