001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.naming.modules.java;
018:
019: import java.util.Hashtable;
020:
021: import javax.naming.Context;
022: import javax.naming.Name;
023: import javax.naming.NameParser;
024: import javax.naming.NamingEnumeration;
025: import javax.naming.NamingException;
026:
027: import org.apache.naming.core.BaseContext;
028: import org.apache.naming.modules.memory.MemoryNamingContext;
029: import org.apache.tomcat.util.res.StringManager;
030:
031: /* This delegates to another context, removing a prefix.
032: XXX make it generic, move to core. The context thread can be
033: selected only once - in the java: factory, to avoid overhead.
034: */
035:
036: /**
037: * Per thread context, implementing java: like contexts.
038: *
039: * @author Remy Maucherat
040: */
041: public class SelectorContext extends BaseContext {
042:
043: // -------------------------------------------------------------- Constants
044:
045: /**
046: * Namespace URL.
047: */
048: public static final String prefix = "java:";
049:
050: /**
051: * Namespace URL length.
052: */
053: public static final int prefixLength = prefix.length();
054:
055: /**
056: * Initial context prefix.
057: */
058: public static final String IC_PREFIX = "IC_";
059:
060: // ----------------------------------------------------------- Constructors
061:
062: /**
063: * Builds a Catalina selector context using the given environment.
064: */
065: public SelectorContext(Hashtable env) {
066: super (env);
067: }
068:
069: /**
070: * Builds a Catalina selector context using the given environment.
071: */
072: public SelectorContext(Hashtable env, boolean initialContext) {
073: this (env);
074: this .initialContext = initialContext;
075: }
076:
077: // ----------------------------------------------------- Instance Variables
078:
079: /**
080: * Environment.
081: */
082: protected Hashtable env;
083:
084: /**
085: * The string manager for this package.
086: */
087: protected StringManager sm = StringManager
088: .getManager("org.apache.naming.res");
089:
090: /**
091: * Request for an initial context.
092: */
093: protected boolean initialContext = false;
094:
095: // --------------------------------------------------------- Public Methods
096:
097: // -------------------------------------------------------- Context Methods
098:
099: public Object lookup(Name name) throws NamingException {
100: // Strip the URL header
101: // Find the appropriate NamingContext according to the current bindings
102: // Execute the lookup on that context
103: return getBoundContext().lookup(parseName(name));
104: }
105:
106: /**
107: * Retrieves the named object.
108: *
109: * @param name the name of the object to look up
110: * @return the object bound to name
111: * @exception NamingException if a naming exception is encountered
112: */
113: public Object lookup(String name) throws NamingException {
114: // Strip the URL header
115: // Find the appropriate NamingContext according to the current bindings
116: // Execute the lookup on that context
117: return getBoundContext().lookup(parseName(name));
118: }
119:
120: /**
121: * Binds a name to an object. All intermediate contexts and the target
122: * context (that named by all but terminal atomic component of the name)
123: * must already exist.
124: *
125: * @param name the name to bind; may not be empty
126: * @param obj the object to bind; possibly null
127: * @exception NameAlreadyBoundException if name is already bound
128: * @exception InvalidAttributesException if object did not supply all
129: * mandatory attributes
130: * @exception NamingException if a naming exception is encountered
131: */
132: public void bind(Name name, Object obj) throws NamingException {
133: getBoundContext().bind(parseName(name), obj);
134: }
135:
136: /**
137: * Binds a name to an object.
138: *
139: * @param name the name to bind; may not be empty
140: * @param obj the object to bind; possibly null
141: * @exception NameAlreadyBoundException if name is already bound
142: * @exception InvalidAttributesException if object did not supply all
143: * mandatory attributes
144: * @exception NamingException if a naming exception is encountered
145: */
146: public void bind(String name, Object obj) throws NamingException {
147: getBoundContext().bind(parseName(name), obj);
148: }
149:
150: /**
151: * Binds a name to an object, overwriting any existing binding. All
152: * intermediate contexts and the target context (that named by all but
153: * terminal atomic component of the name) must already exist.
154: * <p>
155: * If the object is a DirContext, any existing attributes associated with
156: * the name are replaced with those of the object. Otherwise, any
157: * existing attributes associated with the name remain unchanged.
158: *
159: * @param name the name to bind; may not be empty
160: * @param obj the object to bind; possibly null
161: * @exception InvalidAttributesException if object did not supply all
162: * mandatory attributes
163: * @exception NamingException if a naming exception is encountered
164: */
165: public void rebind(Name name, Object obj) throws NamingException {
166: getBoundContext().rebind(parseName(name), obj);
167: }
168:
169: /**
170: * Binds a name to an object, overwriting any existing binding.
171: *
172: * @param name the name to bind; may not be empty
173: * @param obj the object to bind; possibly null
174: * @exception InvalidAttributesException if object did not supply all
175: * mandatory attributes
176: * @exception NamingException if a naming exception is encountered
177: */
178: public void rebind(String name, Object obj) throws NamingException {
179: getBoundContext().rebind(parseName(name), obj);
180: }
181:
182: /**
183: * Unbinds the named object. Removes the terminal atomic name in name
184: * from the target context--that named by all but the terminal atomic
185: * part of name.
186: * <p>
187: * This method is idempotent. It succeeds even if the terminal atomic
188: * name is not bound in the target context, but throws
189: * NameNotFoundException if any of the intermediate contexts do not exist.
190: *
191: * @param name the name to bind; may not be empty
192: * @exception NameNotFoundException if an intermediate context does not
193: * exist
194: * @exception NamingException if a naming exception is encountered
195: */
196: public void unbind(Name name) throws NamingException {
197: getBoundContext().unbind(parseName(name));
198: }
199:
200: /**
201: * Unbinds the named object.
202: *
203: * @param name the name to bind; may not be empty
204: * @exception NameNotFoundException if an intermediate context does not
205: * exist
206: * @exception NamingException if a naming exception is encountered
207: */
208: public void unbind(String name) throws NamingException {
209: getBoundContext().unbind(parseName(name));
210: }
211:
212: /**
213: * Binds a new name to the object bound to an old name, and unbinds the
214: * old name. Both names are relative to this context. Any attributes
215: * associated with the old name become associated with the new name.
216: * Intermediate contexts of the old name are not changed.
217: *
218: * @param oldName the name of the existing binding; may not be empty
219: * @param newName the name of the new binding; may not be empty
220: * @exception NameAlreadyBoundException if newName is already bound
221: * @exception NamingException if a naming exception is encountered
222: */
223: public void rename(Name oldName, Name newName)
224: throws NamingException {
225: getBoundContext()
226: .rename(parseName(oldName), parseName(newName));
227: }
228:
229: /**
230: * Binds a new name to the object bound to an old name, and unbinds the
231: * old name.
232: *
233: * @param oldName the name of the existing binding; may not be empty
234: * @param newName the name of the new binding; may not be empty
235: * @exception NameAlreadyBoundException if newName is already bound
236: * @exception NamingException if a naming exception is encountered
237: */
238: public void rename(String oldName, String newName)
239: throws NamingException {
240: getBoundContext()
241: .rename(parseName(oldName), parseName(newName));
242: }
243:
244: /**
245: * Enumerates the names bound in the named context, along with the class
246: * names of objects bound to them. The contents of any subcontexts are
247: * not included.
248: * <p>
249: * If a binding is added to or removed from this context, its effect on
250: * an enumeration previously returned is undefined.
251: *
252: * @param name the name of the context to list
253: * @return an enumeration of the names and class names of the bindings in
254: * this context. Each element of the enumeration is of type NameClassPair.
255: * @exception NamingException if a naming exception is encountered
256: */
257: public NamingEnumeration list(Name name) throws NamingException {
258: return getBoundContext().list(parseName(name));
259: }
260:
261: /**
262: * Enumerates the names bound in the named context, along with the class
263: * names of objects bound to them.
264: *
265: * @param name the name of the context to list
266: * @return an enumeration of the names and class names of the bindings in
267: * this context. Each element of the enumeration is of type NameClassPair.
268: * @exception NamingException if a naming exception is encountered
269: */
270: public NamingEnumeration list(String name) throws NamingException {
271: return getBoundContext().list(parseName(name));
272: }
273:
274: /**
275: * Enumerates the names bound in the named context, along with the
276: * objects bound to them. The contents of any subcontexts are not
277: * included.
278: * <p>
279: * If a binding is added to or removed from this context, its effect on
280: * an enumeration previously returned is undefined.
281: *
282: * @param name the name of the context to list
283: * @return an enumeration of the bindings in this context.
284: * Each element of the enumeration is of type Binding.
285: * @exception NamingException if a naming exception is encountered
286: */
287: public NamingEnumeration listBindings(Name name)
288: throws NamingException {
289: return getBoundContext().listBindings(parseName(name));
290: }
291:
292: /**
293: * Enumerates the names bound in the named context, along with the
294: * objects bound to them.
295: *
296: * @param name the name of the context to list
297: * @return an enumeration of the bindings in this context.
298: * Each element of the enumeration is of type Binding.
299: * @exception NamingException if a naming exception is encountered
300: */
301: public NamingEnumeration listBindings(String name)
302: throws NamingException {
303: return getBoundContext().listBindings(parseName(name));
304: }
305:
306: /**
307: * Destroys the named context and removes it from the namespace. Any
308: * attributes associated with the name are also removed. Intermediate
309: * contexts are not destroyed.
310: * <p>
311: * This method is idempotent. It succeeds even if the terminal atomic
312: * name is not bound in the target context, but throws
313: * NameNotFoundException if any of the intermediate contexts do not exist.
314: *
315: * In a federated naming system, a context from one naming system may be
316: * bound to a name in another. One can subsequently look up and perform
317: * operations on the foreign context using a composite name. However, an
318: * attempt destroy the context using this composite name will fail with
319: * NotContextException, because the foreign context is not a "subcontext"
320: * of the context in which it is bound. Instead, use unbind() to remove
321: * the binding of the foreign context. Destroying the foreign context
322: * requires that the destroySubcontext() be performed on a context from
323: * the foreign context's "native" naming system.
324: *
325: * @param name the name of the context to be destroyed; may not be empty
326: * @exception NameNotFoundException if an intermediate context does not
327: * exist
328: * @exception NotContextException if the name is bound but does not name
329: * a context, or does not name a context of the appropriate type
330: */
331: public void destroySubcontext(Name name) throws NamingException {
332: getBoundContext().destroySubcontext(parseName(name));
333: }
334:
335: /**
336: * Destroys the named context and removes it from the namespace.
337: *
338: * @param name the name of the context to be destroyed; may not be empty
339: * @exception NameNotFoundException if an intermediate context does not
340: * exist
341: * @exception NotContextException if the name is bound but does not name
342: * a context, or does not name a context of the appropriate type
343: */
344: public void destroySubcontext(String name) throws NamingException {
345: getBoundContext().destroySubcontext(parseName(name));
346: }
347:
348: /**
349: * Creates and binds a new context. Creates a new context with the given
350: * name and binds it in the target context (that named by all but
351: * terminal atomic component of the name). All intermediate contexts and
352: * the target context must already exist.
353: *
354: * @param name the name of the context to create; may not be empty
355: * @return the newly created context
356: * @exception NameAlreadyBoundException if name is already bound
357: * @exception InvalidAttributesException if creation of the subcontext
358: * requires specification of mandatory attributes
359: * @exception NamingException if a naming exception is encountered
360: */
361: public Context createSubcontext(Name name) throws NamingException {
362: return getBoundContext().createSubcontext(parseName(name));
363: }
364:
365: /**
366: * Creates and binds a new context.
367: *
368: * @param name the name of the context to create; may not be empty
369: * @return the newly created context
370: * @exception NameAlreadyBoundException if name is already bound
371: * @exception InvalidAttributesException if creation of the subcontext
372: * requires specification of mandatory attributes
373: * @exception NamingException if a naming exception is encountered
374: */
375: public Context createSubcontext(String name) throws NamingException {
376: return getBoundContext().createSubcontext(parseName(name));
377: }
378:
379: /**
380: * Retrieves the named object, following links except for the terminal
381: * atomic component of the name. If the object bound to name is not a
382: * link, returns the object itself.
383: *
384: * @param name the name of the object to look up
385: * @return the object bound to name, not following the terminal link
386: * (if any).
387: * @exception NamingException if a naming exception is encountered
388: */
389: public Object lookupLink(Name name) throws NamingException {
390: return getBoundContext().lookupLink(parseName(name));
391: }
392:
393: /**
394: * Retrieves the named object, following links except for the terminal
395: * atomic component of the name.
396: *
397: * @param name the name of the object to look up
398: * @return the object bound to name, not following the terminal link
399: * (if any).
400: * @exception NamingException if a naming exception is encountered
401: */
402: public Object lookupLink(String name) throws NamingException {
403: return getBoundContext().lookupLink(parseName(name));
404: }
405:
406: /**
407: * Retrieves the parser associated with the named context. In a
408: * federation of namespaces, different naming systems will parse names
409: * differently. This method allows an application to get a parser for
410: * parsing names into their atomic components using the naming convention
411: * of a particular naming system. Within any single naming system,
412: * NameParser objects returned by this method must be equal (using the
413: * equals() test).
414: *
415: * @param name the name of the context from which to get the parser
416: * @return a name parser that can parse compound names into their atomic
417: * components
418: * @exception NamingException if a naming exception is encountered
419: */
420: public NameParser getNameParser(Name name) throws NamingException {
421: return getBoundContext().getNameParser(parseName(name));
422: }
423:
424: /**
425: * Retrieves the parser associated with the named context.
426: *
427: * @param name the name of the context from which to get the parser
428: * @return a name parser that can parse compound names into their atomic
429: * components
430: * @exception NamingException if a naming exception is encountered
431: */
432: public NameParser getNameParser(String name) throws NamingException {
433: return getBoundContext().getNameParser(parseName(name));
434: }
435:
436: /**
437: * Composes the name of this context with a name relative to this context.
438: * <p>
439: * Given a name (name) relative to this context, and the name (prefix)
440: * of this context relative to one of its ancestors, this method returns
441: * the composition of the two names using the syntax appropriate for the
442: * naming system(s) involved. That is, if name names an object relative
443: * to this context, the result is the name of the same object, but
444: * relative to the ancestor context. None of the names may be null.
445: *
446: * @param name a name relative to this context
447: * @param prefix the name of this context relative to one of its ancestors
448: * @return the composition of prefix and name
449: * @exception NamingException if a naming exception is encountered
450: */
451: public Name composeName(Name name, Name prefix)
452: throws NamingException {
453: prefix = (Name) name.clone();
454: return prefix.addAll(name);
455: }
456:
457: /**
458: * Composes the name of this context with a name relative to this context.
459: *
460: * @param name a name relative to this context
461: * @param prefix the name of this context relative to one of its ancestors
462: * @return the composition of prefix and name
463: * @exception NamingException if a naming exception is encountered
464: */
465: public String composeName(String name, String prefix)
466: throws NamingException {
467: return prefix + "/" + name;
468: }
469:
470: /**
471: * Adds a new environment property to the environment of this context. If
472: * the property already exists, its value is overwritten.
473: *
474: * @param propName the name of the environment property to add; may not
475: * be null
476: * @param propVal the value of the property to add; may not be null
477: * @exception NamingException if a naming exception is encountered
478: */
479: public Object addToEnvironment(String propName, Object propVal)
480: throws NamingException {
481: return getBoundContext().addToEnvironment(propName, propVal);
482: }
483:
484: /**
485: * Removes an environment property from the environment of this context.
486: *
487: * @param propName the name of the environment property to remove;
488: * may not be null
489: * @exception NamingException if a naming exception is encountered
490: */
491: public Object removeFromEnvironment(String propName)
492: throws NamingException {
493: return getBoundContext().removeFromEnvironment(propName);
494: }
495:
496: /**
497: * Retrieves the environment in effect for this context. See class
498: * description for more details on environment properties.
499: * The caller should not make any changes to the object returned: their
500: * effect on the context is undefined. The environment of this context
501: * may be changed using addToEnvironment() and removeFromEnvironment().
502: *
503: * @return the environment of this context; never null
504: * @exception NamingException if a naming exception is encountered
505: */
506: public Hashtable getEnvironment() throws NamingException {
507: return getBoundContext().getEnvironment();
508: }
509:
510: /**
511: * Closes this context. This method releases this context's resources
512: * immediately, instead of waiting for them to be released automatically
513: * by the garbage collector.
514: * This method is idempotent: invoking it on a context that has already
515: * been closed has no effect. Invoking any other method on a closed
516: * context is not allowed, and results in undefined behaviour.
517: *
518: * @exception NamingException if a naming exception is encountered
519: */
520: public void close() throws NamingException {
521: getBoundContext().close();
522: }
523:
524: /**
525: * Retrieves the full name of this context within its own namespace.
526: * <p>
527: * Many naming services have a notion of a "full name" for objects in
528: * their respective namespaces. For example, an LDAP entry has a
529: * distinguished name, and a DNS record has a fully qualified name. This
530: * method allows the client application to retrieve this name. The string
531: * returned by this method is not a JNDI composite name and should not be
532: * passed directly to context methods. In naming systems for which the
533: * notion of full name does not make sense,
534: * OperationNotSupportedException is thrown.
535: *
536: * @return this context's name in its own namespace; never null
537: * @exception OperationNotSupportedException if the naming system does
538: * not have the notion of a full name
539: * @exception NamingException if a naming exception is encountered
540: */
541: public String getNameInNamespace() throws NamingException {
542: return prefix;
543: }
544:
545: // ------------------------------------------------------ Protected Methods
546:
547: /**
548: * Get the bound context.
549: */
550: protected Context getBoundContext() throws NamingException {
551:
552: if (initialContext) {
553: String ICName = IC_PREFIX;
554: if (ContextBindings.isThreadBound()) {
555: ICName += ContextBindings.getThreadName();
556: } else if (ContextBindings.isClassLoaderBound()) {
557: ICName += ContextBindings.getClassLoaderName();
558: }
559: Context initialContext = ContextBindings.getContext(ICName);
560: if (initialContext == null) {
561: // Allocating a new context and binding it to the appropriate
562: // name
563: // XXX Should return null, let the caller create something
564: // Or use a different constructor.
565: initialContext = new MemoryNamingContext(env);
566: ContextBindings.bindContext(ICName, initialContext);
567: }
568: return initialContext;
569: } else {
570: if (ContextBindings.isThreadBound()) {
571: return ContextBindings.getThread();
572: } else {
573: return ContextBindings.getClassLoader();
574: }
575: }
576:
577: }
578:
579: /**
580: * Strips the URL header.
581: *
582: * @return the parsed name
583: * @exception NamingException if there is no "java:" header or if no
584: * naming context has been bound to this thread
585: */
586: protected String parseName(String name) throws NamingException {
587:
588: if ((!initialContext) && (name.startsWith(prefix))) {
589: return (name.substring(prefixLength));
590: } else {
591: if (initialContext) {
592: return (name);
593: } else {
594: throw new NamingException(sm
595: .getString("selectorContext.noJavaUrl"));
596: }
597: }
598:
599: }
600:
601: /**
602: * Strips the URL header.
603: *
604: * @return the parsed name
605: * @exception NamingException if there is no "java:" header or if no
606: * naming context has been bound to this thread
607: */
608: protected Name parseName(Name name) throws NamingException {
609:
610: if ((!initialContext) && (!name.isEmpty())
611: && (name.get(0).equals(prefix))) {
612: return (name.getSuffix(1));
613: } else {
614: if (initialContext) {
615: return (name);
616: } else {
617: throw new NamingException(sm
618: .getString("selectorContext.noJavaUrl"));
619: }
620: }
621:
622: }
623: }
|