KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > jndi > enc > java > JavaURLContext


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JavaURLContext.java,v 1.2 2005/03/15 17:54:52 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.carol.jndi.enc.java;
27
28 import java.util.Hashtable JavaDoc;
29
30 import javax.naming.Context JavaDoc;
31 import javax.naming.Name JavaDoc;
32 import javax.naming.NameNotFoundException JavaDoc;
33 import javax.naming.NameParser JavaDoc;
34 import javax.naming.NamingEnumeration JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36
37 import org.objectweb.carol.util.configuration.TraceCarol;
38
39 /**
40  * Context implementation for the "java:" namespace. <br>
41  * Package must be named .../java (See Initial Context) <br>
42  * Most operations consist of retrieving the actual CompNamingContext and
43  * sending it the operation for processing.
44  * @author Philippe Durieux
45  * @author Philippe Coq
46  * @author Florent Benoit
47  */

48 public class JavaURLContext implements Context JavaDoc {
49
50
51
52     /**
53      * Url prefix, ie : java:
54      */

55     private static final String JavaDoc URL_PREFIX = "java:";
56
57     /**
58      * Environment
59      */

60     private Hashtable JavaDoc myEnv = null;
61
62     /**
63      * Associate a context to a class loader
64      */

65     private static Hashtable JavaDoc clBindings = new Hashtable JavaDoc();
66
67     /**
68      * Naming Context associated with the thread
69      */

70     private static ThreadLocal JavaDoc threadContext = new ThreadLocal JavaDoc();
71
72     /**
73      * Constructor
74      * @param env the JNDI environment
75      */

76     public JavaURLContext(Hashtable JavaDoc env) {
77
78         if (env != null) {
79             // clone env to be able to change it.
80
myEnv = (Hashtable JavaDoc) (env.clone());
81         }
82     }
83
84     /**
85      * Get name without the url prefix.
86      * @param name the absolute name.
87      * @return the relative name (without prefix).
88      * @throws NamingException if the naming failed.
89      */

90     private String JavaDoc getRelativeName(String JavaDoc name) throws NamingException JavaDoc {
91
92         // We suppose that all names must be prefixed as this
93
if (!name.startsWith(URL_PREFIX)) {
94             TraceCarol.error("relative name! :" + name);
95             throw new NameNotFoundException JavaDoc("Invalid name:" + name);
96         }
97         if (name.endsWith("/")) {
98             name = name.substring(URL_PREFIX.length(), name.length() - 1);
99         } else {
100             name = name.substring(URL_PREFIX.length());
101         }
102
103         return name;
104     }
105
106     /**
107      * Get name without the url prefix.
108      * @param name the absolute name.
109      * @return the relative name (without prefix).
110      * @throws NamingException if the naming failed.
111      */

112     private Name JavaDoc getRelativeName(Name JavaDoc name) throws NamingException JavaDoc {
113         if (name.get(0).equals(URL_PREFIX)) {
114             return (name.getSuffix(1));
115         } else {
116             TraceCarol.error("relative name! :" + name);
117             throw new NameNotFoundException JavaDoc("Invalid name:" + name);
118         }
119     }
120
121     /**
122      * Retrieves the named object.
123      * @param name the name of the object to look up
124      * @return the object bound to name
125      * @throws NamingException if a naming exception is encountered
126      */

127     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc {
128         return findContext().lookup(getRelativeName(name));
129     }
130
131     /**
132      * Retrieves the named object.
133      * @param name the name of the object to look up
134      * @return the object bound to name
135      * @throws NamingException if a naming exception is encountered
136      */

137     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
138         return findContext().lookup(getRelativeName(name));
139     }
140
141     /**
142      * Binds a name to an object.
143      * @param name the name to bind; may not be empty
144      * @param obj the object to bind; possibly null
145      * @throws NamingException if a naming exception is encountered
146      */

147     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
148         findContext().bind(getRelativeName(name), obj);
149     }
150
151     /**
152      * Binds a name to an object. All intermediate contexts and the target
153      * context (that named by all but terminal atomic component of the name)
154      * must already exist.
155      * @param name the name to bind; may not be empty
156      * @param obj the object to bind; possibly null
157      * @throws NamingException if a naming exception is encountered
158      */

159     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
160         findContext().bind(getRelativeName(name), obj);
161     }
162
163     /**
164      * Binds a name to an object, overwriting any existing binding. All
165      * intermediate contexts and the target context (that named by all but
166      * terminal atomic component of the name) must already exist. If the object
167      * is a DirContext, any existing attributes associated with the name are
168      * replaced with those of the object. Otherwise, any existing attributes
169      * associated with the name remain unchanged.
170      * @param name the name to bind; may not be empty
171      * @param obj the object to bind; possibly null
172      * @throws NamingException if a naming exception is encountered
173      */

174     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
175         findContext().rebind(getRelativeName(name), obj);
176     }
177
178     /**
179      * Binds a name to an object, overwriting any existing binding. See
180      * {@link #rebind(Name, Object)}for details.
181      * @param name the name to bind; may not be empty
182      * @param obj the object to bind; possibly null
183      * @throws NamingException if a naming exception is encountered
184      */

185     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
186         findContext().rebind(getRelativeName(name), obj);
187     }
188
189     /**
190      * Unbinds the named object. Removes the terminal atomic name in name from
191      * the target context--that named by all but the terminal atomic part of
192      * name. This method is idempotent. It succeeds even if the terminal atomic
193      * name is not bound in the target context, but throws NameNotFoundException
194      * if any of the intermediate contexts do not exist. Any attributes
195      * associated with the name are removed. Intermediate contexts are not
196      * changed.
197      * @param name the name to unbind; may not be empty
198      * @throws NamingException if a naming exception is encountered
199      * @see #unbind(String)
200      */

201     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
202         findContext().unbind(getRelativeName(name));
203     }
204
205     /**
206      * Unbinds the named object. See {@link #unbind(Name)}for details.
207      * @param name the name to unbind; may not be empty
208      * @throws NamingException if a naming exception is encountered
209      */

210     public void unbind(String JavaDoc name) throws NamingException JavaDoc {
211         findContext().unbind(getRelativeName(name));
212     }
213
214     /**
215      * Binds a new name to the object bound to an old name, and unbinds the old
216      * name. This operation is not supported (read only env.)
217      * @param oldName the name of the existing binding; may not be empty
218      * @param newName the name of the new binding; may not be empty
219      * @throws NamingException if a naming exception is encountered
220      */

221     public void rename(Name JavaDoc oldName, Name JavaDoc newName) throws NamingException JavaDoc {
222         findContext().rename(getRelativeName(oldName), getRelativeName(newName));
223     }
224
225     /**
226      * Binds a new name to the object bound to an old name, and unbinds the old
227      * name. Not supported.
228      * @param oldName the name of the existing binding; may not be empty
229      * @param newName the name of the new binding; may not be empty
230      * @throws NamingException if a naming exception is encountered
231      */

232     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException JavaDoc {
233         findContext().rename(getRelativeName(oldName), getRelativeName(newName));
234     }
235
236     /**
237      * Enumerates the names bound in the named context, along with the class
238      * names of objects bound to them. The contents of any subcontexts are not
239      * included. If a binding is added to or removed from this context, its
240      * effect on an enumeration previously returned is undefined.
241      * @param name the name of the context to list
242      * @return an enumeration of the names and class names of the bindings in
243      * this context. Each element of the enumeration is of type
244      * NameClassPair.
245      * @throws NamingException if a naming exception is encountered
246      */

247     public NamingEnumeration JavaDoc list(Name JavaDoc name) throws NamingException JavaDoc {
248         return findContext().list(getRelativeName(name));
249     }
250
251     /**
252      * Enumerates the names bound in the named context, along with the class
253      * names of objects bound to them. See {@link #list(Name)}for details.
254      * @param name the name of the context to list
255      * @return an enumeration of the names and class names of the bindings in
256      * this context. Each element of the enumeration is of type
257      * NameClassPair.
258      * @throws NamingException if a naming exception is encountered
259      */

260     public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc {
261         return findContext().list(getRelativeName(name));
262     }
263
264     /**
265      * Enumerates the names bound in the named context, along with the objects
266      * bound to them. The contents of any subcontexts are not included. If a
267      * binding is added to or removed from this context, its effect on an
268      * enumeration previously returned is undefined.
269      * @param name the name of the context to list
270      * @return an enumeration of the bindings in this context. Each element of
271      * the enumeration is of type Binding.
272      * @throws NamingException if a naming exception is encountered
273      */

274     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc {
275         return findContext().listBindings(getRelativeName(name));
276     }
277
278     /**
279      * Enumerates the names bound in the named context, along with the objects
280      * bound to them. See {@link #listBindings(Name)}for details.
281      * @param name the name of the context to list
282      * @return an enumeration of the bindings in this context. Each element of
283      * the enumeration is of type Binding.
284      * @throws NamingException if a naming exception is encountered
285      */

286     public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc {
287         return findContext().listBindings(getRelativeName(name));
288     }
289
290     /**
291      * Destroys the named context and removes it from the namespace. Any
292      * attributes associated with the name are also removed. Intermediate
293      * contexts are not destroyed. This method is idempotent. It succeeds even
294      * if the terminal atomic name is not bound in the target context, but
295      * throws NameNotFoundException if any of the intermediate contexts do not
296      * exist. In a federated naming system, a context from one naming system may
297      * be bound to a name in another. One can subsequently look up and perform
298      * operations on the foreign context using a composite name. However, an
299      * attempt destroy the context using this composite name will fail with
300      * NotContextException, because the foreign context is not a "subcontext" of
301      * the context in which it is bound. Instead, use unbind() to remove the
302      * binding of the foreign context. Destroying the foreign context requires
303      * that the destroySubcontext() be performed on a context from the foreign
304      * context's "native" naming system.
305      * @param name the name of the context to be destroyed; may not be empty
306      * @throws NamingException if a naming exception is encountered
307      */

308     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
309         findContext().destroySubcontext(getRelativeName(name));
310     }
311
312     /**
313      * Destroys the named context and removes it from the namespace. See
314      * {@link #destroySubcontext(Name)}for details.
315      * @param name the name of the context to be destroyed; may not be empty
316      * @throws NamingException if a naming exception is encountered
317      */

318     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc {
319         findContext().destroySubcontext(getRelativeName(name));
320     }
321
322     /**
323      * Creates and binds a new context. Creates a new context with the given
324      * name and binds it in the target context (that named by all but terminal
325      * atomic component of the name). All intermediate contexts and the target
326      * context must already exist.
327      * @param name the name of the context to create; may not be empty
328      * @return the newly created context
329      * @throws NamingException if a naming exception is encountered
330      */

331     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc {
332         return findContext().createSubcontext(getRelativeName(name));
333     }
334
335     /**
336      * Creates and binds a new context. See {@link #createSubcontext(Name)}for
337      * details.
338      * @param name the name of the context to create; may not be empty
339      * @return the newly created context
340      * @throws NamingException if a naming exception is encountered
341      */

342     public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc {
343         return findContext().createSubcontext(getRelativeName(name));
344     }
345
346     /**
347      * Retrieves the named object, following links except for the terminal
348      * atomic component of the name. If the object bound to name is not a link,
349      * returns the object itself.
350      * @param name the name of the object to look up
351      * @return the object bound to name, not following the terminal link (if
352      * any).
353      * @throws NamingException if a naming exception is encountered
354      */

355     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc {
356         return findContext().lookupLink(getRelativeName(name));
357     }
358
359     /**
360      * Retrieves the named object, following links except for the terminal
361      * atomic component of the name. See {@link #lookupLink(Name)}for details.
362      * @param name the name of the object to look up
363      * @return the object bound to name, not following the terminal link (if
364      * any)
365      * @throws NamingException if a naming exception is encountered
366      */

367     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc {
368         return findContext().lookupLink(getRelativeName(name));
369     }
370
371     /**
372      * Retrieves the parser associated with the named context. In a federation
373      * of namespaces, different naming systems will parse names differently.
374      * This method allows an application to get a parser for parsing names into
375      * their atomic components using the naming convention of a particular
376      * naming system. Within any single naming system, NameParser objects
377      * returned by this method must be equal (using the equals() test).
378      * @param name the name of the context from which to get the parser
379      * @return a name parser that can parse compound names into their atomic
380      * components
381      * @throws NamingException if a naming exception is encountered
382      */

383     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc {
384         return findContext().getNameParser(getRelativeName(name));
385     }
386
387     /**
388      * Retrieves the parser associated with the named context. See
389      * {@link #getNameParser(Name)}for details.
390      * @param name the name of the context from which to get the parser
391      * @return a name parser that can parse compound names into their atomic
392      * components
393      * @throws NamingException if a naming exception is encountered
394      */

395     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc {
396         return findContext().getNameParser(getRelativeName(name));
397     }
398
399     /**
400      * Composes the name of this context with a name relative to this context.
401      * @param name a name relative to this context
402      * @param prefix the name of this context relative to one of its ancestors
403      * @return the composition of prefix and name
404      * @throws NamingException if a naming exception is encountered
405      */

406     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc {
407         prefix = (Name JavaDoc) name.clone();
408         return prefix.addAll(name);
409     }
410
411     /**
412      * Composes the name of this context with a name relative to this context.
413      * @param name a name relative to this context
414      * @param prefix the name of this context relative to one of its ancestors
415      * @return the composition of prefix and name
416      * @throws NamingException if a naming exception is encountered
417      */

418     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix) throws NamingException JavaDoc {
419         return prefix + "/" + name;
420     }
421
422     /**
423      * Adds a new environment property to the environment of this context. If
424      * the property already exists, its value is overwritten. See class
425      * description for more details on environment properties.
426      * @param propName the name of the environment property to add; may not be
427      * null
428      * @param propVal the value of the property to add; may not be null
429      * @return the previous value of the property, or null if the property was
430      * not in the environment before
431      * @throws NamingException if a naming exception is encountered
432      */

433     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal) throws NamingException JavaDoc {
434         return findContext().addToEnvironment(propName, propVal);
435     }
436
437     /**
438      * Removes an environment property from the environment of this context. See
439      * class description for more details on environment properties.
440      * @param propName the name of the environment property to remove; may not
441      * be null
442      * @return the previous value of the property, or null if the property was
443      * not in the environment
444      * @throws NamingException if a naming exception is encountered
445      */

446     public Object JavaDoc removeFromEnvironment(String JavaDoc propName) throws NamingException JavaDoc {
447         return findContext().removeFromEnvironment(propName);
448     }
449
450     /**
451      * Retrieves the environment in effect for this context. See class
452      * description for more details on environment properties. The caller should
453      * not make any changes to the object returned: their effect on the context
454      * is undefined. The environment of this context may be changed using
455      * addToEnvironment() and removeFromEnvironment().
456      * @return the environment of this context; never null
457      * @throws NamingException if a naming exception is encountered
458      */

459     public Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc {
460         return findContext().getEnvironment();
461     }
462
463     /**
464      * Closes this context. This method releases this context's resources
465      * immediately, instead of waiting for them to be released automatically by
466      * the garbage collector. This method is idempotent: invoking it on a
467      * context that has already been closed has no effect. Invoking any other
468      * method on a closed context is not allowed, and results in undefined
469      * behaviour.
470      * @throws NamingException if a naming exception is encountered
471      */

472     public void close() throws NamingException JavaDoc {
473         findContext().close();
474     }
475
476     /**
477      * Retrieves the full name of this context within its own namespace.
478      * @return this context's name in its own namespace; never null
479      * @throws NamingException if a naming exception is encountered
480      */

481     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc {
482         return URL_PREFIX;
483     }
484
485     /**
486      * @return the Context associated with the current thread.
487      */

488     public Context JavaDoc findContext() {
489         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
490
491         // Associate Component to a parent classloader
492
Context JavaDoc ctx = null;
493         if ((cl != null) && (cl.getParent() != null)) {
494             ctx = (Context JavaDoc) clBindings.get(cl.getParent());
495             if (ctx != null) {
496                 return ctx;
497             } else {
498                 // Build a new one
499
ctx = buildNewContext(cl.getParent().toString());
500                 // Now associate them together
501
clBindings.put(cl.getParent(), ctx);
502             }
503         }
504
505         if (ctx == null) {
506             ctx = (Context JavaDoc) threadContext.get();
507             if (ctx == null) {
508                 // build a new one
509
ctx = buildNewContext(threadContext.toString());
510             }
511         }
512
513         return ctx;
514     }
515
516     /**
517      * Build a new CompNamingContext
518      * @param name the name of the context
519      * @return a new context
520      */

521     private Context JavaDoc buildNewContext(String JavaDoc name) {
522         return new CompNamingContext(name, (Hashtable JavaDoc) myEnv.clone());
523     }
524
525 }
Popular Tags