001: /**
002: * Library name : Primrose - A Java Database Connection Pool.
003: * Published by Ben Keeping, http://primrose.org.uk .
004: * Copyright (C) 2004 Ben Keeping, primrose.org.uk
005: * Email: Use "Contact Us Form" on website
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */package uk.org.primrose.jndi;
021:
022: import javax.naming.*;
023: import java.util.*;
024:
025: public class PrimroseContext implements Context {
026: Hashtable<String, Object> ht = new Hashtable<String, Object>();
027:
028: public PrimroseContext() throws NamingException {
029: }
030:
031: public PrimroseContext(Hashtable env) throws NamingException {
032: }
033:
034: /**
035: * Retrieves the named object.
036: * If <tt>name</tt> is empty, returns a new instance of this context
037: * (which represents the same naming context as this context, but its
038: * environment may be modified independently and it may be accessed
039: * concurrently).
040: *
041: * @param name
042: * the name of the object to look up
043: * @return the object bound to <tt>name</tt>
044: * @throws NamingException if a naming exception is encountered
045: *
046: * @see #lookup(String)
047: * @see #lookupLink(Name)
048: */
049: public Object lookup(Name name) throws NamingException {
050: return lookup(name.toString());
051: }
052:
053: /**
054: * Retrieves the named object.
055: * See {@link #lookup(Name)} for details.
056: * @param name
057: * the name of the object to look up
058: * @return the object bound to <tt>name</tt>
059: * @throws NamingException if a naming exception is encountered
060: */
061: public Object lookup(String name) throws NamingException {
062: Object o = ht.get(name);
063:
064: // This breaks down "java:comp/env/objectname" into
065: // locating each subcontext until we can finally lookup "objectname"
066: // ... if you see what I mean :)
067: if (o == null) {
068: String[] parts = name.split("/");
069:
070: Context ctx = (Context) ht.get(parts[0]);
071: if (parts.length > 1) {
072: for (int i = 1; i < parts.length - 1; i++) {
073: ctx = (Context) ctx.lookup(parts[i]);
074: }
075:
076: o = ctx.lookup(parts[parts.length - 1]);
077: }
078: }
079:
080: if (o == null) {
081: throw new NamingException(
082: "Cannot find JNDI resource under name : " + name);
083: }
084:
085: return o;
086: }
087:
088: /**
089: * Binds a name to an object.
090: * All intermediate contexts and the target context (that named by all
091: * but terminal atomic component of the name) must already exist.
092: *
093: * @param name
094: * the name to bind {
095: } may not be empty
096: * @param obj
097: * the object to bind {
098: } possibly null
099: * @throws NameAlreadyBoundException if name is already bound
100: * @throws javax.naming.directory.InvalidAttributesException
101: * if object did not supply all mandatory attributes
102: * @throws NamingException if a naming exception is encountered
103: *
104: * @see #bind(String, Object)
105: * @see #rebind(Name, Object)
106: * @see javax.naming.directory.DirContext#bind(Name, Object,
107: * javax.naming.directory.Attributes)
108: */
109: public void bind(Name name, Object obj) throws NamingException {
110: bind(name.toString(), obj);
111: }
112:
113: /**
114: * Binds a name to an object.
115: * See {@link #bind(Name, Object)} for details.
116: *
117: * @param name
118: * the name to bind {
119: } may not be empty
120: * @param obj
121: * the object to bind {
122: } possibly null
123: * @throws NameAlreadyBoundException if name is already bound
124: * @throws javax.naming.directory.InvalidAttributesException
125: * if object did not supply all mandatory attributes
126: * @throws NamingException if a naming exception is encountered
127: */
128: public void bind(String name, Object obj) throws NamingException {
129: if (ht.get(name) != null) {
130: throw new NameAlreadyBoundException(name
131: + " is already bound under this context");
132: }
133: ht.put(name, obj);
134: }
135:
136: /**
137: * Binds a name to an object, overwriting any existing binding.
138: * All intermediate contexts and the target context (that named by all
139: * but terminal atomic component of the name) must already exist.
140: *
141: * <p> If the object is a <tt>DirContext</tt>, any existing attributes
142: * associated with the name are replaced with those of the object.
143: * Otherwise, any existing attributes associated with the name remain
144: * unchanged.
145: *
146: * @param name
147: * the name to bind {
148: } may not be empty
149: * @param obj
150: * the object to bind {
151: } possibly null
152: * @throws javax.naming.directory.InvalidAttributesException
153: * if object did not supply all mandatory attributes
154: * @throws NamingException if a naming exception is encountered
155: *
156: * @see #rebind(String, Object)
157: * @see #bind(Name, Object)
158: * @see javax.naming.directory.DirContext#rebind(Name, Object,
159: * javax.naming.directory.Attributes)
160: * @see javax.naming.directory.DirContext
161: */
162: public void rebind(Name name, Object obj) throws NamingException {
163: rebind(name.toString(), obj);
164: }
165:
166: /**
167: * Binds a name to an object, overwriting any existing binding.
168: * See {@link #rebind(Name, Object)} for details.
169: *
170: * @param name
171: * the name to bind {
172: } may not be empty
173: * @param obj
174: * the object to bind {
175: } possibly null
176: * @throws javax.naming.directory.InvalidAttributesException
177: * if object did not supply all mandatory attributes
178: * @throws NamingException if a naming exception is encountered
179: */
180: public void rebind(String name, Object obj) throws NamingException {
181: ht.remove(obj);
182: ht.put(name, obj);
183:
184: }
185:
186: /**
187: * Unbinds the named object.
188: * Removes the terminal atomic name in <code>name</code>
189: * from the target context--that named by all but the terminal
190: * atomic part of <code>name</code>.
191: *
192: * <p> This method is idempotent.
193: * It succeeds even if the terminal atomic name
194: * is not bound in the target context, but throws
195: * <tt>NameNotFoundException</tt>
196: * if any of the intermediate contexts do not exist.
197: *
198: * <p> Any attributes associated with the name are removed.
199: * Intermediate contexts are not changed.
200: *
201: * @param name
202: * the name to unbind {
203: } may not be empty
204: * @throws NameNotFoundException if an intermediate context does not exist
205: * @throws NamingException if a naming exception is encountered
206: * @see #unbind(String)
207: */
208: public void unbind(Name name) throws NamingException {
209: }
210:
211: /**
212: * Unbinds the named object.
213: * See {@link #unbind(Name)} for details.
214: *
215: * @param name
216: * the name to unbind {
217: } may not be empty
218: * @throws NameNotFoundException if an intermediate context does not exist
219: * @throws NamingException if a naming exception is encountered
220: */
221: public void unbind(String name) throws NamingException {
222: ht.remove(name);
223: }
224:
225: /**
226: * Binds a new name to the object bound to an old name, and unbinds
227: * the old name. Both names are relative to this context.
228: * Any attributes associated with the old name become associated
229: * with the new name.
230: * Intermediate contexts of the old name are not changed.
231: *
232: * @param oldName
233: * the name of the existing binding {
234: } may not be empty
235: * @param newName
236: * the name of the new binding {
237: } may not be empty
238: * @throws NameAlreadyBoundException if <tt>newName</tt> is already bound
239: * @throws NamingException if a naming exception is encountered
240: *
241: * @see #rename(String, String)
242: * @see #bind(Name, Object)
243: * @see #rebind(Name, Object)
244: */
245: public void rename(Name oldName, Name newName)
246: throws NamingException {
247: }
248:
249: /**
250: * Binds a new name to the object bound to an old name, and unbinds
251: * the old name.
252: * See {@link #rename(Name, Name)} for details.
253: *
254: * @param oldName
255: * the name of the existing binding {
256: } may not be empty
257: * @param newName
258: * the name of the new binding {
259: } may not be empty
260: * @throws NameAlreadyBoundException if <tt>newName</tt> is already bound
261: * @throws NamingException if a naming exception is encountered
262: */
263: public void rename(String oldName, String newName)
264: throws NamingException {
265: }
266:
267: /**
268: * Enumerates the names bound in the named context, along with the
269: * class names of objects bound to them.
270: * The contents of any subcontexts are not included.
271: *
272: * <p> If a binding is added to or removed from this context,
273: * its effect on an enumeration previously returned is undefined.
274: *
275: * @param name
276: * the name of the context to list
277: * @return an enumeration of the names and class names of the
278: * bindings in this context. Each element of the
279: * enumeration is of type <tt>NameClassPair</tt>.
280: * @throws NamingException if a naming exception is encountered
281: *
282: * @see #list(String)
283: * @see #listBindings(Name)
284: * @see NameClassPair
285: */
286: public NamingEnumeration list(Name name) throws NamingException {
287: return null;
288: }
289:
290: /**
291: * Enumerates the names bound in the named context, along with the
292: * class names of objects bound to them.
293: * See {@link #list(Name)} for details.
294: *
295: * @param name
296: * the name of the context to list
297: * @return an enumeration of the names and class names of the
298: * bindings in this context. Each element of the
299: * enumeration is of type <tt>NameClassPair</tt>.
300: * @throws NamingException if a naming exception is encountered
301: */
302: @SuppressWarnings("unchecked")
303: public NamingEnumeration list(String name) throws NamingException {
304: return null;
305: }
306:
307: /**
308: * Enumerates the names bound in the named context, along with the
309: * objects bound to them.
310: * The contents of any subcontexts are not included.
311: *
312: * <p> If a binding is added to or removed from this context,
313: * its effect on an enumeration previously returned is undefined.
314: *
315: * @param name
316: * the name of the context to list
317: * @return an enumeration of the bindings in this context.
318: * Each element of the enumeration is of type
319: * <tt>Binding</tt>.
320: * @throws NamingException if a naming exception is encountered
321: *
322: * @see #listBindings(String)
323: * @see #list(Name)
324: * @see Binding
325: */
326: @SuppressWarnings("unchecked")
327: public NamingEnumeration listBindings(Name name)
328: throws NamingException {
329: return null;
330: }
331:
332: /**
333: * Enumerates the names bound in the named context, along with the
334: * objects bound to them.
335: * See {@link #listBindings(Name)} for details.
336: *
337: * @param name
338: * the name of the context to list
339: * @return an enumeration of the bindings in this context.
340: * Each element of the enumeration is of type
341: * <tt>Binding</tt>.
342: * @throws NamingException if a naming exception is encountered
343: */
344: public NamingEnumeration listBindings(String name)
345: throws NamingException {
346: return null;
347: }
348:
349: /**
350: * Destroys the named context and removes it from the namespace.
351: * Any attributes associated with the name are also removed.
352: * Intermediate contexts are not destroyed.
353: *
354: * <p> This method is idempotent.
355: * It succeeds even if the terminal atomic name
356: * is not bound in the target context, but throws
357: * <tt>NameNotFoundException</tt>
358: * if any of the intermediate contexts do not exist.
359: *
360: * <p> In a federated naming system, a context from one naming system
361: * may be bound to a name in another. One can subsequently
362: * look up and perform operations on the foreign context using a
363: * composite name. However, an attempt destroy the context using
364: * this composite name will fail with
365: * <tt>NotContextException</tt>, because the foreign context is not
366: * a "subcontext" of the context in which it is bound.
367: * Instead, use <tt>unbind()</tt> to remove the
368: * binding of the foreign context. Destroying the foreign context
369: * requires that the <tt>destroySubcontext()</tt> be performed
370: * on a context from the foreign context's "native" naming system.
371: *
372: * @param name
373: * the name of the context to be destroyed {
374: } may not be empty
375: * @throws NameNotFoundException if an intermediate context does not exist
376: * @throws NotContextException if the name is bound but does not name a
377: * context, or does not name a context of the appropriate type
378: * @throws ContextNotEmptyException if the named context is not empty
379: * @throws NamingException if a naming exception is encountered
380: *
381: * @see #destroySubcontext(String)
382: */
383: public void destroySubcontext(Name name) throws NamingException {
384: }
385:
386: /**
387: * Destroys the named context and removes it from the namespace.
388: * See {@link #destroySubcontext(Name)} for details.
389: *
390: * @param name
391: * the name of the context to be destroyed {
392: } may not be empty
393: * @throws NameNotFoundException if an intermediate context does not exist
394: * @throws NotContextException if the name is bound but does not name a
395: * context, or does not name a context of the appropriate type
396: * @throws ContextNotEmptyException if the named context is not empty
397: * @throws NamingException if a naming exception is encountered
398: */
399: public void destroySubcontext(String name) throws NamingException {
400: }
401:
402: /**
403: * Creates and binds a new context.
404: * Creates a new context with the given name and binds it in
405: * the target context (that named by all but terminal atomic
406: * component of the name). All intermediate contexts and the
407: * target context must already exist.
408: *
409: * @param name
410: * the name of the context to create {
411: } may not be empty
412: * @return the newly created context
413: *
414: * @throws NameAlreadyBoundException if name is already bound
415: * @throws javax.naming.directory.InvalidAttributesException
416: * if creation of the subcontext requires specification of
417: * mandatory attributes
418: * @throws NamingException if a naming exception is encountered
419: *
420: * @see #createSubcontext(String)
421: * @see javax.naming.directory.DirContext#createSubcontext
422: */
423: public Context createSubcontext(Name name) throws NamingException {
424: return createSubcontext(name.toString());
425: }
426:
427: /**
428: * Creates and binds a new context.
429: * See {@link #createSubcontext(Name)} for details.
430: *
431: * @param name
432: * the name of the context to create {
433: } may not be empty
434: * @return the newly created context
435: *
436: * @throws NameAlreadyBoundException if name is already bound
437: * @throws javax.naming.directory.InvalidAttributesException
438: * if creation of the subcontext requires specification of
439: * mandatory attributes
440: * @throws NamingException if a naming exception is encountered
441: */
442: public Context createSubcontext(String name) throws NamingException {
443: Context ctx = new PrimroseContext();
444: ht.put(name, ctx);
445: return ctx;
446: }
447:
448: /**
449: * Retrieves the named object, following links except
450: * for the terminal atomic component of the name.
451: * If the object bound to <tt>name</tt> is not a link,
452: * returns the object itself.
453: *
454: * @param name
455: * the name of the object to look up
456: * @return the object bound to <tt>name</tt>, not following the
457: * terminal link (if any).
458: * @throws NamingException if a naming exception is encountered
459: *
460: * @see #lookupLink(String)
461: */
462: public Object lookupLink(Name name) throws NamingException {
463: return null;
464: }
465:
466: /**
467: * Retrieves the named object, following links except
468: * for the terminal atomic component of the name.
469: * See {@link #lookupLink(Name)} for details.
470: *
471: * @param name
472: * the name of the object to look up
473: * @return the object bound to <tt>name</tt>, not following the
474: * terminal link (if any)
475: * @throws NamingException if a naming exception is encountered
476: */
477: public Object lookupLink(String name) throws NamingException {
478: return null;
479: }
480:
481: /**
482: * Retrieves the parser associated with the named context.
483: * In a federation of namespaces, different naming systems will
484: * parse names differently. This method allows an application
485: * to get a parser for parsing names into their atomic components
486: * using the naming convention of a particular naming system.
487: * Within any single naming system, <tt>NameParser</tt> objects
488: * returned by this method must be equal (using the <tt>equals()</tt>
489: * test).
490: *
491: * @param name
492: * the name of the context from which to get the parser
493: * @return a name parser that can parse compound names into their atomic
494: * components
495: * @throws NamingException if a naming exception is encountered
496: *
497: * @see #getNameParser(String)
498: * @see CompoundName
499: */
500: public NameParser getNameParser(Name name) throws NamingException {
501: return null;
502: }
503:
504: /**
505: * Retrieves the parser associated with the named context.
506: * See {@link #getNameParser(Name)} for details.
507: *
508: * @param name
509: * the name of the context from which to get the parser
510: * @return a name parser that can parse compound names into their atomic
511: * components
512: * @throws NamingException if a naming exception is encountered
513: */
514: public NameParser getNameParser(String name) throws NamingException {
515: return null;
516: }
517:
518: /**
519: * Composes the name of this context with a name relative to
520: * this context.
521: * Given a name (<code>name</code>) relative to this context, and
522: * the name (<code>prefix</code>) of this context relative to one
523: * of its ancestors, this method returns the composition of the
524: * two names using the syntax appropriate for the naming
525: * system(s) involved. That is, if <code>name</code> names an
526: * object relative to this context, the result is the name of the
527: * same object, but relative to the ancestor context. None of the
528: * names may be null.
529: * <p>
530: * For example, if this context is named "wiz.com" relative
531: * to the initial context, then
532: * <pre>
533: * composeName("east", "wiz.com") </pre>
534: * might return <code>"east.wiz.com"</code>.
535: * If instead this context is named "org/research", then
536: * <pre>
537: * composeName("user/jane", "org/research") </pre>
538: * might return <code>"org/research/user/jane"</code> while
539: * <pre>
540: * composeName("user/jane", "research") </pre>
541: * returns <code>"research/user/jane"</code>.
542: *
543: * @param name
544: * a name relative to this context
545: * @param prefix
546: * the name of this context relative to one of its ancestors
547: * @return the composition of <code>prefix</code> and <code>name</code>
548: * @throws NamingException if a naming exception is encountered
549: *
550: * @see #composeName(String, String)
551: */
552: public Name composeName(Name name, Name prefix)
553: throws NamingException {
554: return null;
555: }
556:
557: /**
558: * Composes the name of this context with a name relative to
559: * this context.
560: * See {@link #composeName(Name, Name)} for details.
561: *
562: * @param name
563: * a name relative to this context
564: * @param prefix
565: * the name of this context relative to one of its ancestors
566: * @return the composition of <code>prefix</code> and <code>name</code>
567: * @throws NamingException if a naming exception is encountered
568: */
569: public String composeName(String name, String prefix)
570: throws NamingException {
571: return null;
572: }
573:
574: /**
575: * Adds a new environment property to the environment of this
576: * context. If the property already exists, its value is overwritten.
577: * See class description for more details on environment properties.
578: *
579: * @param propName
580: * the name of the environment property to add {
581: } may not be null
582: * @param propVal
583: * the value of the property to add {
584: } may not be null
585: * @return the previous value of the property, or null if the property was
586: * not in the environment before
587: * @throws NamingException if a naming exception is encountered
588: *
589: * @see #getEnvironment()
590: * @see #removeFromEnvironment(String)
591: */
592: public Object addToEnvironment(String propName, Object propVal)
593: throws NamingException {
594: return null;
595: }
596:
597: /**
598: * Removes an environment property from the environment of this
599: * context. See class description for more details on environment
600: * properties.
601: *
602: * @param propName
603: * the name of the environment property to remove {
604: } may not be null
605: * @return the previous value of the property, or null if the property was
606: * not in the environment
607: * @throws NamingException if a naming exception is encountered
608: *
609: * @see #getEnvironment()
610: * @see #addToEnvironment(String, Object)
611: */
612: public Object removeFromEnvironment(String propName)
613: throws NamingException {
614: return null;
615: }
616:
617: /**
618: * Retrieves the environment in effect for this context.
619: * See class description for more details on environment properties.
620: *
621: * <p> The caller should not make any changes to the object returned:
622: * their effect on the context is undefined.
623: * The environment of this context may be changed using
624: * <tt>addToEnvironment()</tt> and <tt>removeFromEnvironment()</tt>.
625: *
626: * @return the environment of this context {
627: } never null
628: * @throws NamingException if a naming exception is encountered
629: *
630: * @see #addToEnvironment(String, Object)
631: * @see #removeFromEnvironment(String)
632: */
633: @SuppressWarnings("unchecked")
634: public Hashtable getEnvironment() throws NamingException {
635: return null;
636: }
637:
638: /**
639: * Closes this context.
640: * This method releases this context's resources immediately, instead of
641: * waiting for them to be released automatically by the garbage collector.
642: *
643: * <p> This method is idempotent: invoking it on a context that has
644: * already been closed has no effect. Invoking any other method
645: * on a closed context is not allowed, and results in undefined behaviour.
646: *
647: * @throws NamingException if a naming exception is encountered
648: */
649: public void close() throws NamingException {
650: }
651:
652: /**
653: * Retrieves the full name of this context within its own namespace.
654: *
655: * <p> Many naming services have a notion of a "full name" for objects
656: * in their respective namespaces. For example, an LDAP entry has
657: * a distinguished name, and a DNS record has a fully qualified name.
658: * This method allows the client application to retrieve this name.
659: * The string returned by this method is not a JNDI composite name
660: * and should not be passed directly to context methods.
661: * In naming systems for which the notion of full name does not
662: * make sense, <tt>OperationNotSupportedException</tt> is thrown.
663: *
664: * @return this context's name in its own namespace {
665: } never null
666: * @throws OperationNotSupportedException if the naming system does
667: * not have the notion of a full name
668: * @throws NamingException if a naming exception is encountered
669: *
670: * @since 1.3
671: */
672: public String getNameInNamespace() throws NamingException {
673: return null;
674: }
675:
676: }
|