001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/SelectorContext.java,v 1.2 2000/11/11 01:47:47 remm Exp $
003: * $Revision: 1.2 $
004: * $Date: 2000/11/11 01:47:47 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.naming;
065:
066: import java.util.Hashtable;
067: import javax.naming.Context;
068: import javax.naming.Name;
069: import javax.naming.NameParser;
070: import javax.naming.NamingEnumeration;
071: import javax.naming.NamingException;
072:
073: /**
074: * Catalina JNDI Context implementation.
075: *
076: * @author Remy Maucherat
077: * @version $Revision: 1.2 $ $Date: 2000/11/11 01:47:47 $
078: */
079:
080: public class SelectorContext implements Context {
081:
082: // -------------------------------------------------------------- Constants
083:
084: /**
085: * Namespace URL.
086: */
087: public static final String prefix = "java:";
088:
089: /**
090: * Namespace URL length.
091: */
092: public static final int prefixLength = prefix.length();
093:
094: /**
095: * Initial context prefix.
096: */
097: public static final String IC_PREFIX = "IC_";
098:
099: // ----------------------------------------------------------- Constructors
100:
101: /**
102: * Builds a Catalina selector context using the given environment.
103: */
104: public SelectorContext(Hashtable env) {
105: this .env = env;
106: }
107:
108: /**
109: * Builds a Catalina selector context using the given environment.
110: */
111: public SelectorContext(Hashtable env, boolean initialContext) {
112: this (env);
113: this .initialContext = initialContext;
114: }
115:
116: // ----------------------------------------------------- Instance Variables
117:
118: /**
119: * Environment.
120: */
121: protected Hashtable env;
122:
123: /**
124: * The string manager for this package.
125: */
126: protected StringManager sm = StringManager
127: .getManager(Constants.Package);
128:
129: /**
130: * Request for an initial context.
131: */
132: protected boolean initialContext = false;
133:
134: // --------------------------------------------------------- Public Methods
135:
136: // -------------------------------------------------------- Context Methods
137:
138: /**
139: * Retrieves the named object. If name is empty, returns a new instance
140: * of this context (which represents the same naming context as this
141: * context, but its environment may be modified independently and it may
142: * be accessed concurrently).
143: *
144: * @param name the name of the object to look up
145: * @return the object bound to name
146: * @exception NamingException if a naming exception is encountered
147: */
148: public Object lookup(Name name) throws NamingException {
149: // Strip the URL header
150: // Find the appropriate NamingContext according to the current bindings
151: // Execute the lookup on that context
152: return getBoundContext().lookup(parseName(name));
153: }
154:
155: /**
156: * Retrieves the named object.
157: *
158: * @param name the name of the object to look up
159: * @return the object bound to name
160: * @exception NamingException if a naming exception is encountered
161: */
162: public Object lookup(String name) throws NamingException {
163: // Strip the URL header
164: // Find the appropriate NamingContext according to the current bindings
165: // Execute the lookup on that context
166: return getBoundContext().lookup(parseName(name));
167: }
168:
169: /**
170: * Binds a name to an object. All intermediate contexts and the target
171: * context (that named by all but terminal atomic component of the name)
172: * must already exist.
173: *
174: * @param name the name to bind; may not be empty
175: * @param obj the object to bind; possibly null
176: * @exception NameAlreadyBoundException if name is already bound
177: * @exception InvalidAttributesException if object did not supply all
178: * mandatory attributes
179: * @exception NamingException if a naming exception is encountered
180: */
181: public void bind(Name name, Object obj) throws NamingException {
182: getBoundContext().bind(parseName(name), obj);
183: }
184:
185: /**
186: * Binds a name to an object.
187: *
188: * @param name the name to bind; may not be empty
189: * @param obj the object to bind; possibly null
190: * @exception NameAlreadyBoundException if name is already bound
191: * @exception InvalidAttributesException if object did not supply all
192: * mandatory attributes
193: * @exception NamingException if a naming exception is encountered
194: */
195: public void bind(String name, Object obj) throws NamingException {
196: getBoundContext().bind(parseName(name), obj);
197: }
198:
199: /**
200: * Binds a name to an object, overwriting any existing binding. All
201: * intermediate contexts and the target context (that named by all but
202: * terminal atomic component of the name) must already exist.
203: * <p>
204: * If the object is a DirContext, any existing attributes associated with
205: * the name are replaced with those of the object. Otherwise, any
206: * existing attributes associated with the name remain unchanged.
207: *
208: * @param name the name to bind; may not be empty
209: * @param obj the object to bind; possibly null
210: * @exception InvalidAttributesException if object did not supply all
211: * mandatory attributes
212: * @exception NamingException if a naming exception is encountered
213: */
214: public void rebind(Name name, Object obj) throws NamingException {
215: getBoundContext().rebind(parseName(name), obj);
216: }
217:
218: /**
219: * Binds a name to an object, overwriting any existing binding.
220: *
221: * @param name the name to bind; may not be empty
222: * @param obj the object to bind; possibly null
223: * @exception InvalidAttributesException if object did not supply all
224: * mandatory attributes
225: * @exception NamingException if a naming exception is encountered
226: */
227: public void rebind(String name, Object obj) throws NamingException {
228: getBoundContext().rebind(parseName(name), obj);
229: }
230:
231: /**
232: * Unbinds the named object. Removes the terminal atomic name in name
233: * from the target context--that named by all but the terminal atomic
234: * part of name.
235: * <p>
236: * This method is idempotent. It succeeds even if the terminal atomic
237: * name is not bound in the target context, but throws
238: * NameNotFoundException if any of the intermediate contexts do not exist.
239: *
240: * @param name the name to bind; may not be empty
241: * @exception NameNotFoundException if an intermediate context does not
242: * exist
243: * @exception NamingException if a naming exception is encountered
244: */
245: public void unbind(Name name) throws NamingException {
246: getBoundContext().unbind(parseName(name));
247: }
248:
249: /**
250: * Unbinds the named object.
251: *
252: * @param name the name to bind; may not be empty
253: * @exception NameNotFoundException if an intermediate context does not
254: * exist
255: * @exception NamingException if a naming exception is encountered
256: */
257: public void unbind(String name) throws NamingException {
258: getBoundContext().unbind(parseName(name));
259: }
260:
261: /**
262: * Binds a new name to the object bound to an old name, and unbinds the
263: * old name. Both names are relative to this context. Any attributes
264: * associated with the old name become associated with the new name.
265: * Intermediate contexts of the old name are not changed.
266: *
267: * @param oldName the name of the existing binding; may not be empty
268: * @param newName the name of the new binding; may not be empty
269: * @exception NameAlreadyBoundException if newName is already bound
270: * @exception NamingException if a naming exception is encountered
271: */
272: public void rename(Name oldName, Name newName)
273: throws NamingException {
274: getBoundContext()
275: .rename(parseName(oldName), parseName(newName));
276: }
277:
278: /**
279: * Binds a new name to the object bound to an old name, and unbinds the
280: * old name.
281: *
282: * @param oldName the name of the existing binding; may not be empty
283: * @param newName the name of the new binding; may not be empty
284: * @exception NameAlreadyBoundException if newName is already bound
285: * @exception NamingException if a naming exception is encountered
286: */
287: public void rename(String oldName, String newName)
288: throws NamingException {
289: getBoundContext()
290: .rename(parseName(oldName), parseName(newName));
291: }
292:
293: /**
294: * Enumerates the names bound in the named context, along with the class
295: * names of objects bound to them. The contents of any subcontexts are
296: * not included.
297: * <p>
298: * If a binding is added to or removed from this context, its effect on
299: * an enumeration previously returned is undefined.
300: *
301: * @param name the name of the context to list
302: * @return an enumeration of the names and class names of the bindings in
303: * this context. Each element of the enumeration is of type NameClassPair.
304: * @exception NamingException if a naming exception is encountered
305: */
306: public NamingEnumeration list(Name name) throws NamingException {
307: return getBoundContext().list(parseName(name));
308: }
309:
310: /**
311: * Enumerates the names bound in the named context, along with the class
312: * names of objects bound to them.
313: *
314: * @param name the name of the context to list
315: * @return an enumeration of the names and class names of the bindings in
316: * this context. Each element of the enumeration is of type NameClassPair.
317: * @exception NamingException if a naming exception is encountered
318: */
319: public NamingEnumeration list(String name) throws NamingException {
320: return getBoundContext().list(parseName(name));
321: }
322:
323: /**
324: * Enumerates the names bound in the named context, along with the
325: * objects bound to them. The contents of any subcontexts are not
326: * included.
327: * <p>
328: * If a binding is added to or removed from this context, its effect on
329: * an enumeration previously returned is undefined.
330: *
331: * @param name the name of the context to list
332: * @return an enumeration of the bindings in this context.
333: * Each element of the enumeration is of type Binding.
334: * @exception NamingException if a naming exception is encountered
335: */
336: public NamingEnumeration listBindings(Name name)
337: throws NamingException {
338: return getBoundContext().listBindings(parseName(name));
339: }
340:
341: /**
342: * Enumerates the names bound in the named context, along with the
343: * objects bound to them.
344: *
345: * @param name the name of the context to list
346: * @return an enumeration of the bindings in this context.
347: * Each element of the enumeration is of type Binding.
348: * @exception NamingException if a naming exception is encountered
349: */
350: public NamingEnumeration listBindings(String name)
351: throws NamingException {
352: return getBoundContext().listBindings(parseName(name));
353: }
354:
355: /**
356: * Destroys the named context and removes it from the namespace. Any
357: * attributes associated with the name are also removed. Intermediate
358: * contexts are not destroyed.
359: * <p>
360: * This method is idempotent. It succeeds even if the terminal atomic
361: * name is not bound in the target context, but throws
362: * NameNotFoundException if any of the intermediate contexts do not exist.
363: *
364: * In a federated naming system, a context from one naming system may be
365: * bound to a name in another. One can subsequently look up and perform
366: * operations on the foreign context using a composite name. However, an
367: * attempt destroy the context using this composite name will fail with
368: * NotContextException, because the foreign context is not a "subcontext"
369: * of the context in which it is bound. Instead, use unbind() to remove
370: * the binding of the foreign context. Destroying the foreign context
371: * requires that the destroySubcontext() be performed on a context from
372: * the foreign context's "native" naming system.
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(Name name) throws NamingException {
381: getBoundContext().destroySubcontext(parseName(name));
382: }
383:
384: /**
385: * Destroys the named context and removes it from the namespace.
386: *
387: * @param name the name of the context to be destroyed; may not be empty
388: * @exception NameNotFoundException if an intermediate context does not
389: * exist
390: * @exception NotContextException if the name is bound but does not name
391: * a context, or does not name a context of the appropriate type
392: */
393: public void destroySubcontext(String name) throws NamingException {
394: getBoundContext().destroySubcontext(parseName(name));
395: }
396:
397: /**
398: * Creates and binds a new context. Creates a new context with the given
399: * name and binds it in the target context (that named by all but
400: * terminal atomic component of the name). All intermediate contexts and
401: * the target context must already exist.
402: *
403: * @param name the name of the context to create; may not be empty
404: * @return the newly created context
405: * @exception NameAlreadyBoundException if name is already bound
406: * @exception InvalidAttributesException if creation of the subcontext
407: * requires specification of mandatory attributes
408: * @exception NamingException if a naming exception is encountered
409: */
410: public Context createSubcontext(Name name) throws NamingException {
411: return getBoundContext().createSubcontext(parseName(name));
412: }
413:
414: /**
415: * Creates and binds a new context.
416: *
417: * @param name the name of the context to create; may not be empty
418: * @return the newly created context
419: * @exception NameAlreadyBoundException if name is already bound
420: * @exception InvalidAttributesException if creation of the subcontext
421: * requires specification of mandatory attributes
422: * @exception NamingException if a naming exception is encountered
423: */
424: public Context createSubcontext(String name) throws NamingException {
425: return getBoundContext().createSubcontext(parseName(name));
426: }
427:
428: /**
429: * Retrieves the named object, following links except for the terminal
430: * atomic component of the name. If the object bound to name is not a
431: * link, returns the object itself.
432: *
433: * @param name the name of the object to look up
434: * @return the object bound to name, not following the terminal link
435: * (if any).
436: * @exception NamingException if a naming exception is encountered
437: */
438: public Object lookupLink(Name name) throws NamingException {
439: return getBoundContext().lookupLink(parseName(name));
440: }
441:
442: /**
443: * Retrieves the named object, following links except for the terminal
444: * atomic component of the name.
445: *
446: * @param name the name of the object to look up
447: * @return the object bound to name, not following the terminal link
448: * (if any).
449: * @exception NamingException if a naming exception is encountered
450: */
451: public Object lookupLink(String name) throws NamingException {
452: return getBoundContext().lookupLink(parseName(name));
453: }
454:
455: /**
456: * Retrieves the parser associated with the named context. In a
457: * federation of namespaces, different naming systems will parse names
458: * differently. This method allows an application to get a parser for
459: * parsing names into their atomic components using the naming convention
460: * of a particular naming system. Within any single naming system,
461: * NameParser objects returned by this method must be equal (using the
462: * equals() test).
463: *
464: * @param name the name of the context from which to get the parser
465: * @return a name parser that can parse compound names into their atomic
466: * components
467: * @exception NamingException if a naming exception is encountered
468: */
469: public NameParser getNameParser(Name name) throws NamingException {
470: return getBoundContext().getNameParser(parseName(name));
471: }
472:
473: /**
474: * Retrieves the parser associated with the named context.
475: *
476: * @param name the name of the context from which to get the parser
477: * @return a name parser that can parse compound names into their atomic
478: * components
479: * @exception NamingException if a naming exception is encountered
480: */
481: public NameParser getNameParser(String name) throws NamingException {
482: return getBoundContext().getNameParser(parseName(name));
483: }
484:
485: /**
486: * Composes the name of this context with a name relative to this context.
487: * <p>
488: * Given a name (name) relative to this context, and the name (prefix)
489: * of this context relative to one of its ancestors, this method returns
490: * the composition of the two names using the syntax appropriate for the
491: * naming system(s) involved. That is, if name names an object relative
492: * to this context, the result is the name of the same object, but
493: * relative to the ancestor context. None of the names may be null.
494: *
495: * @param name a name relative to this context
496: * @param prefix the name of this context relative to one of its ancestors
497: * @return the composition of prefix and name
498: * @exception NamingException if a naming exception is encountered
499: */
500: public Name composeName(Name name, Name prefix)
501: throws NamingException {
502: prefix = (Name) name.clone();
503: return prefix.addAll(name);
504: }
505:
506: /**
507: * Composes the name of this context with a name relative to this context.
508: *
509: * @param name a name relative to this context
510: * @param prefix the name of this context relative to one of its ancestors
511: * @return the composition of prefix and name
512: * @exception NamingException if a naming exception is encountered
513: */
514: public String composeName(String name, String prefix)
515: throws NamingException {
516: return prefix + "/" + name;
517: }
518:
519: /**
520: * Adds a new environment property to the environment of this context. If
521: * the property already exists, its value is overwritten.
522: *
523: * @param propName the name of the environment property to add; may not
524: * be null
525: * @param propVal the value of the property to add; may not be null
526: * @exception NamingException if a naming exception is encountered
527: */
528: public Object addToEnvironment(String propName, Object propVal)
529: throws NamingException {
530: return getBoundContext().addToEnvironment(propName, propVal);
531: }
532:
533: /**
534: * Removes an environment property from the environment of this context.
535: *
536: * @param propName the name of the environment property to remove;
537: * may not be null
538: * @exception NamingException if a naming exception is encountered
539: */
540: public Object removeFromEnvironment(String propName)
541: throws NamingException {
542: return getBoundContext().removeFromEnvironment(propName);
543: }
544:
545: /**
546: * Retrieves the environment in effect for this context. See class
547: * description for more details on environment properties.
548: * The caller should not make any changes to the object returned: their
549: * effect on the context is undefined. The environment of this context
550: * may be changed using addToEnvironment() and removeFromEnvironment().
551: *
552: * @return the environment of this context; never null
553: * @exception NamingException if a naming exception is encountered
554: */
555: public Hashtable getEnvironment() throws NamingException {
556: return getBoundContext().getEnvironment();
557: }
558:
559: /**
560: * Closes this context. This method releases this context's resources
561: * immediately, instead of waiting for them to be released automatically
562: * by the garbage collector.
563: * This method is idempotent: invoking it on a context that has already
564: * been closed has no effect. Invoking any other method on a closed
565: * context is not allowed, and results in undefined behaviour.
566: *
567: * @exception NamingException if a naming exception is encountered
568: */
569: public void close() throws NamingException {
570: getBoundContext().close();
571: }
572:
573: /**
574: * Retrieves the full name of this context within its own namespace.
575: * <p>
576: * Many naming services have a notion of a "full name" for objects in
577: * their respective namespaces. For example, an LDAP entry has a
578: * distinguished name, and a DNS record has a fully qualified name. This
579: * method allows the client application to retrieve this name. The string
580: * returned by this method is not a JNDI composite name and should not be
581: * passed directly to context methods. In naming systems for which the
582: * notion of full name does not make sense,
583: * OperationNotSupportedException is thrown.
584: *
585: * @return this context's name in its own namespace; never null
586: * @exception OperationNotSupportedException if the naming system does
587: * not have the notion of a full name
588: * @exception NamingException if a naming exception is encountered
589: */
590: public String getNameInNamespace() throws NamingException {
591: return prefix;
592: }
593:
594: // ------------------------------------------------------ Protected Methods
595:
596: /**
597: * Get the bound context.
598: */
599: protected Context getBoundContext() throws NamingException {
600:
601: if (initialContext) {
602: String ICName = IC_PREFIX;
603: if (ContextBindings.isThreadBound()) {
604: ICName += ContextBindings.getThreadName();
605: } else if (ContextBindings.isClassLoaderBound()) {
606: ICName += ContextBindings.getClassLoaderName();
607: }
608: Context initialContext = ContextBindings.getContext(ICName);
609: if (initialContext == null) {
610: // Allocating a new context and binding it to the appropriate
611: // name
612: initialContext = new NamingContext(env, ICName);
613: ContextBindings.bindContext(ICName, initialContext);
614: }
615: return initialContext;
616: } else {
617: if (ContextBindings.isThreadBound()) {
618: return ContextBindings.getThread();
619: } else {
620: return ContextBindings.getClassLoader();
621: }
622: }
623:
624: }
625:
626: /**
627: * Strips the URL header.
628: *
629: * @return the parsed name
630: * @exception NamingException if there is no "java:" header or if no
631: * naming context has been bound to this thread
632: */
633: protected String parseName(String name) throws NamingException {
634:
635: if ((!initialContext) && (name.startsWith(prefix))) {
636: return (name.substring(prefixLength));
637: } else {
638: if (initialContext) {
639: return (name);
640: } else {
641: throw new NamingException(sm
642: .getString("selectorContext.noJavaUrl"));
643: }
644: }
645:
646: }
647:
648: /**
649: * Strips the URL header.
650: *
651: * @return the parsed name
652: * @exception NamingException if there is no "java:" header or if no
653: * naming context has been bound to this thread
654: */
655: protected Name parseName(Name name) throws NamingException {
656:
657: if ((!initialContext) && (!name.isEmpty())
658: && (name.get(0).equals(prefix))) {
659: return (name.getSuffix(1));
660: } else {
661: if (initialContext) {
662: return (name);
663: } else {
664: throw new NamingException(sm
665: .getString("selectorContext.noJavaUrl"));
666: }
667: }
668:
669: }
670:
671: }
|