KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > modules > java > SelectorContext


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.naming.modules.java;
18
19 import java.util.Hashtable JavaDoc;
20
21 import javax.naming.Context JavaDoc;
22 import javax.naming.Name JavaDoc;
23 import javax.naming.NameParser JavaDoc;
24 import javax.naming.NamingEnumeration JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26
27 import org.apache.naming.core.BaseContext;
28 import org.apache.naming.modules.memory.MemoryNamingContext;
29 import org.apache.tomcat.util.res.StringManager;
30
31 /* This delegates to another context, removing a prefix.
32    XXX make it generic, move to core. The context thread can be
33    selected only once - in the java: factory, to avoid overhead.
34 */

35
36 /**
37  * Per thread context, implementing java: like contexts.
38  *
39  * @author Remy Maucherat
40  */

41 public class SelectorContext extends BaseContext {
42
43     // -------------------------------------------------------------- Constants
44

45
46     /**
47      * Namespace URL.
48      */

49     public static final String JavaDoc prefix = "java:";
50
51
52     /**
53      * Namespace URL length.
54      */

55     public static final int prefixLength = prefix.length();
56
57
58     /**
59      * Initial context prefix.
60      */

61     public static final String JavaDoc IC_PREFIX = "IC_";
62
63
64     // ----------------------------------------------------------- Constructors
65

66
67     /**
68      * Builds a Catalina selector context using the given environment.
69      */

70     public SelectorContext(Hashtable JavaDoc env) {
71         super( env );
72     }
73
74
75     /**
76      * Builds a Catalina selector context using the given environment.
77      */

78     public SelectorContext(Hashtable JavaDoc env, boolean initialContext) {
79         this(env);
80         this.initialContext = initialContext;
81     }
82
83
84     // ----------------------------------------------------- Instance Variables
85

86
87     /**
88      * Environment.
89      */

90     protected Hashtable JavaDoc env;
91
92
93     /**
94      * The string manager for this package.
95      */

96     protected StringManager sm = StringManager.getManager("org.apache.naming.res");
97
98
99     /**
100      * Request for an initial context.
101      */

102     protected boolean initialContext = false;
103
104
105     // --------------------------------------------------------- Public Methods
106

107
108     // -------------------------------------------------------- Context Methods
109

110
111     public Object JavaDoc lookup(Name JavaDoc name)
112         throws NamingException JavaDoc {
113         // Strip the URL header
114
// Find the appropriate NamingContext according to the current bindings
115
// Execute the lookup on that context
116
return getBoundContext().lookup(parseName(name));
117     }
118
119
120     /**
121      * Retrieves the named object.
122      *
123      * @param name the name of the object to look up
124      * @return the object bound to name
125      * @exception NamingException if a naming exception is encountered
126      */

127     public Object JavaDoc lookup(String JavaDoc name)
128         throws NamingException JavaDoc {
129         // Strip the URL header
130
// Find the appropriate NamingContext according to the current bindings
131
// Execute the lookup on that context
132
return getBoundContext().lookup(parseName(name));
133     }
134
135
136     /**
137      * Binds a name to an object. All intermediate contexts and the target
138      * context (that named by all but terminal atomic component of the name)
139      * must already exist.
140      *
141      * @param name the name to bind; may not be empty
142      * @param obj the object to bind; possibly null
143      * @exception NameAlreadyBoundException if name is already bound
144      * @exception InvalidAttributesException if object did not supply all
145      * mandatory attributes
146      * @exception NamingException if a naming exception is encountered
147      */

148     public void bind(Name JavaDoc name, Object JavaDoc obj)
149         throws NamingException JavaDoc {
150         getBoundContext().bind(parseName(name), obj);
151     }
152
153
154     /**
155      * Binds a name to an object.
156      *
157      * @param name the name to bind; may not be empty
158      * @param obj the object to bind; possibly null
159      * @exception NameAlreadyBoundException if name is already bound
160      * @exception InvalidAttributesException if object did not supply all
161      * mandatory attributes
162      * @exception NamingException if a naming exception is encountered
163      */

164     public void bind(String JavaDoc name, Object JavaDoc obj)
165         throws NamingException JavaDoc {
166         getBoundContext().bind(parseName(name), obj);
167     }
168
169
170     /**
171      * Binds a name to an object, overwriting any existing binding. All
172      * intermediate contexts and the target context (that named by all but
173      * terminal atomic component of the name) must already exist.
174      * <p>
175      * If the object is a DirContext, any existing attributes associated with
176      * the name are replaced with those of the object. Otherwise, any
177      * existing attributes associated with the name remain unchanged.
178      *
179      * @param name the name to bind; may not be empty
180      * @param obj the object to bind; possibly null
181      * @exception InvalidAttributesException if object did not supply all
182      * mandatory attributes
183      * @exception NamingException if a naming exception is encountered
184      */

185     public void rebind(Name JavaDoc name, Object JavaDoc obj)
186         throws NamingException JavaDoc {
187         getBoundContext().rebind(parseName(name), obj);
188     }
189
190
191     /**
192      * Binds a name to an object, overwriting any existing binding.
193      *
194      * @param name the name to bind; may not be empty
195      * @param obj the object to bind; possibly null
196      * @exception InvalidAttributesException if object did not supply all
197      * mandatory attributes
198      * @exception NamingException if a naming exception is encountered
199      */

200     public void rebind(String JavaDoc name, Object JavaDoc obj)
201         throws NamingException JavaDoc {
202         getBoundContext().rebind(parseName(name), obj);
203     }
204
205
206     /**
207      * Unbinds the named object. Removes the terminal atomic name in name
208      * from the target context--that named by all but the terminal atomic
209      * part of name.
210      * <p>
211      * This method is idempotent. It succeeds even if the terminal atomic
212      * name is not bound in the target context, but throws
213      * NameNotFoundException if any of the intermediate contexts do not exist.
214      *
215      * @param name the name to bind; may not be empty
216      * @exception NameNotFoundException if an intermediate context does not
217      * exist
218      * @exception NamingException if a naming exception is encountered
219      */

220     public void unbind(Name JavaDoc name)
221         throws NamingException JavaDoc {
222         getBoundContext().unbind(parseName(name));
223     }
224
225
226     /**
227      * Unbinds the named object.
228      *
229      * @param name the name to bind; may not be empty
230      * @exception NameNotFoundException if an intermediate context does not
231      * exist
232      * @exception NamingException if a naming exception is encountered
233      */

234     public void unbind(String JavaDoc name)
235         throws NamingException JavaDoc {
236         getBoundContext().unbind(parseName(name));
237     }
238
239
240     /**
241      * Binds a new name to the object bound to an old name, and unbinds the
242      * old name. Both names are relative to this context. Any attributes
243      * associated with the old name become associated with the new name.
244      * Intermediate contexts of the old name are not changed.
245      *
246      * @param oldName the name of the existing binding; may not be empty
247      * @param newName the name of the new binding; may not be empty
248      * @exception NameAlreadyBoundException if newName is already bound
249      * @exception NamingException if a naming exception is encountered
250      */

251     public void rename(Name JavaDoc oldName, Name JavaDoc newName)
252         throws NamingException JavaDoc {
253         getBoundContext().rename(parseName(oldName), parseName(newName));
254     }
255
256
257     /**
258      * Binds a new name to the object bound to an old name, and unbinds the
259      * old name.
260      *
261      * @param oldName the name of the existing binding; may not be empty
262      * @param newName the name of the new binding; may not be empty
263      * @exception NameAlreadyBoundException if newName is already bound
264      * @exception NamingException if a naming exception is encountered
265      */

266     public void rename(String JavaDoc oldName, String JavaDoc newName)
267         throws NamingException JavaDoc {
268         getBoundContext().rename(parseName(oldName), parseName(newName));
269     }
270
271
272     /**
273      * Enumerates the names bound in the named context, along with the class
274      * names of objects bound to them. The contents of any subcontexts are
275      * not included.
276      * <p>
277      * If a binding is added to or removed from this context, its effect on
278      * an enumeration previously returned is undefined.
279      *
280      * @param name the name of the context to list
281      * @return an enumeration of the names and class names of the bindings in
282      * this context. Each element of the enumeration is of type NameClassPair.
283      * @exception NamingException if a naming exception is encountered
284      */

285     public NamingEnumeration JavaDoc list(Name JavaDoc name)
286         throws NamingException JavaDoc {
287         return getBoundContext().list(parseName(name));
288     }
289
290
291     /**
292      * Enumerates the names bound in the named context, along with the class
293      * names of objects bound to them.
294      *
295      * @param name the name of the context to list
296      * @return an enumeration of the names and class names of the bindings in
297      * this context. Each element of the enumeration is of type NameClassPair.
298      * @exception NamingException if a naming exception is encountered
299      */

300     public NamingEnumeration JavaDoc list(String JavaDoc name)
301         throws NamingException JavaDoc {
302         return getBoundContext().list(parseName(name));
303     }
304
305
306     /**
307      * Enumerates the names bound in the named context, along with the
308      * objects bound to them. The contents of any subcontexts are not
309      * included.
310      * <p>
311      * If a binding is added to or removed from this context, its effect on
312      * an enumeration previously returned is undefined.
313      *
314      * @param name the name of the context to list
315      * @return an enumeration of the bindings in this context.
316      * Each element of the enumeration is of type Binding.
317      * @exception NamingException if a naming exception is encountered
318      */

319     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name)
320         throws NamingException JavaDoc {
321         return getBoundContext().listBindings(parseName(name));
322     }
323
324
325     /**
326      * Enumerates the names bound in the named context, along with the
327      * objects bound to them.
328      *
329      * @param name the name of the context to list
330      * @return an enumeration of the bindings in this context.
331      * Each element of the enumeration is of type Binding.
332      * @exception NamingException if a naming exception is encountered
333      */

334     public NamingEnumeration JavaDoc listBindings(String JavaDoc name)
335         throws NamingException JavaDoc {
336         return getBoundContext().listBindings(parseName(name));
337     }
338
339
340     /**
341      * Destroys the named context and removes it from the namespace. Any
342      * attributes associated with the name are also removed. Intermediate
343      * contexts are not destroyed.
344      * <p>
345      * This method is idempotent. It succeeds even if the terminal atomic
346      * name is not bound in the target context, but throws
347      * NameNotFoundException if any of the intermediate contexts do not exist.
348      *
349      * In a federated naming system, a context from one naming system may be
350      * bound to a name in another. One can subsequently look up and perform
351      * operations on the foreign context using a composite name. However, an
352      * attempt destroy the context using this composite name will fail with
353      * NotContextException, because the foreign context is not a "subcontext"
354      * of the context in which it is bound. Instead, use unbind() to remove
355      * the binding of the foreign context. Destroying the foreign context
356      * requires that the destroySubcontext() be performed on a context from
357      * the foreign context's "native" naming system.
358      *
359      * @param name the name of the context to be destroyed; may not be empty
360      * @exception NameNotFoundException if an intermediate context does not
361      * exist
362      * @exception NotContextException if the name is bound but does not name
363      * a context, or does not name a context of the appropriate type
364      */

365     public void destroySubcontext(Name JavaDoc name)
366         throws NamingException JavaDoc {
367         getBoundContext().destroySubcontext(parseName(name));
368     }
369
370
371     /**
372      * Destroys the named context and removes it from the namespace.
373      *
374      * @param name the name of the context to be destroyed; may not be empty
375      * @exception NameNotFoundException if an intermediate context does not
376      * exist
377      * @exception NotContextException if the name is bound but does not name
378      * a context, or does not name a context of the appropriate type
379      */

380     public void destroySubcontext(String JavaDoc name)
381         throws NamingException JavaDoc {
382         getBoundContext().destroySubcontext(parseName(name));
383     }
384
385
386     /**
387      * Creates and binds a new context. Creates a new context with the given
388      * name and binds it in the target context (that named by all but
389      * terminal atomic component of the name). All intermediate contexts and
390      * the target context must already exist.
391      *
392      * @param name the name of the context to create; may not be empty
393      * @return the newly created context
394      * @exception NameAlreadyBoundException if name is already bound
395      * @exception InvalidAttributesException if creation of the subcontext
396      * requires specification of mandatory attributes
397      * @exception NamingException if a naming exception is encountered
398      */

399     public Context JavaDoc createSubcontext(Name JavaDoc name)
400         throws NamingException JavaDoc {
401         return getBoundContext().createSubcontext(parseName(name));
402     }
403
404
405     /**
406      * Creates and binds a new context.
407      *
408      * @param name the name of the context to create; may not be empty
409      * @return the newly created context
410      * @exception NameAlreadyBoundException if name is already bound
411      * @exception InvalidAttributesException if creation of the subcontext
412      * requires specification of mandatory attributes
413      * @exception NamingException if a naming exception is encountered
414      */

415     public Context JavaDoc createSubcontext(String JavaDoc name)
416         throws NamingException JavaDoc {
417         return getBoundContext().createSubcontext(parseName(name));
418     }
419
420
421     /**
422      * Retrieves the named object, following links except for the terminal
423      * atomic component of the name. If the object bound to name is not a
424      * link, returns the object itself.
425      *
426      * @param name the name of the object to look up
427      * @return the object bound to name, not following the terminal link
428      * (if any).
429      * @exception NamingException if a naming exception is encountered
430      */

431     public Object JavaDoc lookupLink(Name JavaDoc name)
432         throws NamingException JavaDoc {
433         return getBoundContext().lookupLink(parseName(name));
434     }
435
436
437     /**
438      * Retrieves the named object, following links except for the terminal
439      * atomic component of the name.
440      *
441      * @param name the name of the object to look up
442      * @return the object bound to name, not following the terminal link
443      * (if any).
444      * @exception NamingException if a naming exception is encountered
445      */

446     public Object JavaDoc lookupLink(String JavaDoc name)
447         throws NamingException JavaDoc {
448         return getBoundContext().lookupLink(parseName(name));
449     }
450
451
452     /**
453      * Retrieves the parser associated with the named context. In a
454      * federation of namespaces, different naming systems will parse names
455      * differently. This method allows an application to get a parser for
456      * parsing names into their atomic components using the naming convention
457      * of a particular naming system. Within any single naming system,
458      * NameParser objects returned by this method must be equal (using the
459      * equals() test).
460      *
461      * @param name the name of the context from which to get the parser
462      * @return a name parser that can parse compound names into their atomic
463      * components
464      * @exception NamingException if a naming exception is encountered
465      */

466     public NameParser JavaDoc getNameParser(Name JavaDoc name)
467         throws NamingException JavaDoc {
468         return getBoundContext().getNameParser(parseName(name));
469     }
470
471
472     /**
473      * Retrieves the parser associated with the named context.
474      *
475      * @param name the name of the context from which to get the parser
476      * @return a name parser that can parse compound names into their atomic
477      * components
478      * @exception NamingException if a naming exception is encountered
479      */

480     public NameParser JavaDoc getNameParser(String JavaDoc name)
481         throws NamingException JavaDoc {
482         return getBoundContext().getNameParser(parseName(name));
483     }
484
485
486     /**
487      * Composes the name of this context with a name relative to this context.
488      * <p>
489      * Given a name (name) relative to this context, and the name (prefix)
490      * of this context relative to one of its ancestors, this method returns
491      * the composition of the two names using the syntax appropriate for the
492      * naming system(s) involved. That is, if name names an object relative
493      * to this context, the result is the name of the same object, but
494      * relative to the ancestor context. None of the names may be null.
495      *
496      * @param name a name relative to this context
497      * @param prefix the name of this context relative to one of its ancestors
498      * @return the composition of prefix and name
499      * @exception NamingException if a naming exception is encountered
500      */

501     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix)
502         throws NamingException JavaDoc {
503     prefix = (Name JavaDoc) name.clone();
504     return prefix.addAll(name);
505     }
506
507
508     /**
509      * Composes the name of this context with a name relative to this context.
510      *
511      * @param name a name relative to this context
512      * @param prefix the name of this context relative to one of its ancestors
513      * @return the composition of prefix and name
514      * @exception NamingException if a naming exception is encountered
515      */

516     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix)
517         throws NamingException JavaDoc {
518         return prefix + "/" + name;
519     }
520
521
522     /**
523      * Adds a new environment property to the environment of this context. If
524      * the property already exists, its value is overwritten.
525      *
526      * @param propName the name of the environment property to add; may not
527      * be null
528      * @param propVal the value of the property to add; may not be null
529      * @exception NamingException if a naming exception is encountered
530      */

531     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal)
532         throws NamingException JavaDoc {
533         return getBoundContext().addToEnvironment(propName, propVal);
534     }
535
536
537     /**
538      * Removes an environment property from the environment of this context.
539      *
540      * @param propName the name of the environment property to remove;
541      * may not be null
542      * @exception NamingException if a naming exception is encountered
543      */

544     public Object JavaDoc removeFromEnvironment(String JavaDoc propName)
545         throws NamingException JavaDoc {
546         return getBoundContext().removeFromEnvironment(propName);
547     }
548
549
550     /**
551      * Retrieves the environment in effect for this context. See class
552      * description for more details on environment properties.
553      * The caller should not make any changes to the object returned: their
554      * effect on the context is undefined. The environment of this context
555      * may be changed using addToEnvironment() and removeFromEnvironment().
556      *
557      * @return the environment of this context; never null
558      * @exception NamingException if a naming exception is encountered
559      */

560     public Hashtable JavaDoc getEnvironment()
561         throws NamingException JavaDoc {
562         return getBoundContext().getEnvironment();
563     }
564
565
566     /**
567      * Closes this context. This method releases this context's resources
568      * immediately, instead of waiting for them to be released automatically
569      * by the garbage collector.
570      * This method is idempotent: invoking it on a context that has already
571      * been closed has no effect. Invoking any other method on a closed
572      * context is not allowed, and results in undefined behaviour.
573      *
574      * @exception NamingException if a naming exception is encountered
575      */

576     public void close()
577         throws NamingException JavaDoc {
578         getBoundContext().close();
579     }
580
581
582     /**
583      * Retrieves the full name of this context within its own namespace.
584      * <p>
585      * Many naming services have a notion of a "full name" for objects in
586      * their respective namespaces. For example, an LDAP entry has a
587      * distinguished name, and a DNS record has a fully qualified name. This
588      * method allows the client application to retrieve this name. The string
589      * returned by this method is not a JNDI composite name and should not be
590      * passed directly to context methods. In naming systems for which the
591      * notion of full name does not make sense,
592      * OperationNotSupportedException is thrown.
593      *
594      * @return this context's name in its own namespace; never null
595      * @exception OperationNotSupportedException if the naming system does
596      * not have the notion of a full name
597      * @exception NamingException if a naming exception is encountered
598      */

599     public String JavaDoc getNameInNamespace()
600         throws NamingException JavaDoc {
601         return prefix;
602     }
603
604
605     // ------------------------------------------------------ Protected Methods
606

607
608     /**
609      * Get the bound context.
610      */

611     protected Context JavaDoc getBoundContext()
612         throws NamingException JavaDoc {
613
614         if (initialContext) {
615             String JavaDoc ICName = IC_PREFIX;
616             if (ContextBindings.isThreadBound()) {
617                 ICName += ContextBindings.getThreadName();
618             } else if (ContextBindings.isClassLoaderBound()) {
619                 ICName += ContextBindings.getClassLoaderName();
620             }
621             Context JavaDoc initialContext = ContextBindings.getContext(ICName);
622             if (initialContext == null) {
623                 // Allocating a new context and binding it to the appropriate
624
// name
625
// XXX Should return null, let the caller create something
626
// Or use a different constructor.
627
initialContext = new MemoryNamingContext(env);
628                 ContextBindings.bindContext(ICName, initialContext);
629             }
630             return initialContext;
631         } else {
632             if (ContextBindings.isThreadBound()) {
633                 return ContextBindings.getThread();
634             } else {
635                 return ContextBindings.getClassLoader();
636             }
637         }
638
639     }
640
641
642     /**
643      * Strips the URL header.
644      *
645      * @return the parsed name
646      * @exception NamingException if there is no "java:" header or if no
647      * naming context has been bound to this thread
648      */

649     protected String JavaDoc parseName(String JavaDoc name)
650         throws NamingException JavaDoc {
651         
652     if ((!initialContext) && (name.startsWith(prefix))) {
653             return (name.substring(prefixLength));
654         } else {
655             if (initialContext) {
656                 return (name);
657             } else {
658                 throw new NamingException JavaDoc
659                     (sm.getString("selectorContext.noJavaUrl"));
660             }
661         }
662         
663     }
664
665
666     /**
667      * Strips the URL header.
668      *
669      * @return the parsed name
670      * @exception NamingException if there is no "java:" header or if no
671      * naming context has been bound to this thread
672      */

673     protected Name JavaDoc parseName(Name JavaDoc name)
674         throws NamingException JavaDoc {
675
676     if ((!initialContext) && (!name.isEmpty())
677             && (name.get(0).equals(prefix))) {
678             return (name.getSuffix(1));
679         } else {
680             if (initialContext) {
681                 return (name);
682             } else {
683                 throw new NamingException JavaDoc
684                     (sm.getString("selectorContext.noJavaUrl"));
685             }
686         }
687
688     }
689 }
690
691
Popular Tags