KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > naming > CompNamingContext


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

25
26 package org.objectweb.jonas.naming;
27
28 import java.util.Enumeration JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.NoSuchElementException JavaDoc;
31
32 import javax.naming.Binding JavaDoc;
33 import javax.naming.CompositeName JavaDoc;
34 import javax.naming.Context JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36 import javax.naming.InvalidNameException JavaDoc;
37 import javax.naming.LinkRef JavaDoc;
38 import javax.naming.Name JavaDoc;
39 import javax.naming.NameAlreadyBoundException JavaDoc;
40 import javax.naming.NameClassPair JavaDoc;
41 import javax.naming.NameNotFoundException JavaDoc;
42 import javax.naming.NameParser JavaDoc;
43 import javax.naming.NamingEnumeration JavaDoc;
44 import javax.naming.NamingException JavaDoc;
45 import javax.naming.NotContextException JavaDoc;
46 import javax.naming.OperationNotSupportedException JavaDoc;
47 import javax.naming.RefAddr JavaDoc;
48 import javax.naming.Reference JavaDoc;
49
50 import org.objectweb.jonas.common.Log;
51 import org.objectweb.util.monolog.api.BasicLevel;
52 import org.objectweb.util.monolog.api.Logger;
53
54 /**
55  * Implementation of Context interface for EJB Environment.
56  * Must handle subContexts (because of jndi/, ejb/, ...)
57  *
58  * @author Philippe Durieux
59  * @author Philippe Coq monolog
60  * @author Florent Benoit 2003.06.13 : handle Reference object for the lookup.
61  */

62 public class CompNamingContext implements Context JavaDoc {
63
64     /**
65      * Logger
66      */

67     private static Logger logger = null;
68
69     /**
70      * Environment
71      */

72     private Hashtable JavaDoc myEnv = null;
73
74     /**
75      * Bindings
76      */

77     private Hashtable JavaDoc bindings = new Hashtable JavaDoc();
78
79     /**
80      * Parser
81      */

82     private static NameParser JavaDoc myParser = new EJBNameParser();
83
84     /**
85      * Naming id
86      */

87     private String JavaDoc compId;
88
89     /**
90      * Constructor
91      * @param id id of the context.
92      * @param env initial environment.
93      */

94     public CompNamingContext(String JavaDoc id, Hashtable JavaDoc env) {
95         if (env != null) {
96             // clone env to be able to change it.
97
myEnv = (Hashtable JavaDoc) (env.clone());
98         }
99         compId = id;
100         logger = Log.getLogger(Log.JONAS_NAMING_PREFIX);
101     }
102
103     /**
104      * Constructor
105      * @param id id of the context.
106      */

107     public CompNamingContext(String JavaDoc id) {
108         myEnv = new Hashtable JavaDoc();
109         compId = id;
110         logger = Log.getLogger(Log.JONAS_NAMING_PREFIX);
111     }
112
113     // ------------------------------------------------------------------
114
// Context implementation
115
// ------------------------------------------------------------------
116

117     /**
118      * Retrieves the named object.
119      * Delegate to the String version.
120      *
121      * @param name the name of the object to look up
122      * @return the object bound to name
123      * @throws NamingException if a naming exception is encountered
124      */

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

137     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
138         if (logger.isLoggable(BasicLevel.DEBUG)) {
139             logger.log(BasicLevel.DEBUG, name);
140         }
141
142         Name JavaDoc n = new CompositeName JavaDoc(name);
143         if (n.size() < 1) {
144             // Empty name means this context
145
if (logger.isLoggable(BasicLevel.DEBUG)) {
146                 logger.log(BasicLevel.DEBUG, "empty name");
147             }
148             return this;
149         }
150
151         if (n.size() == 1) {
152             // leaf in the env tree
153
Object JavaDoc ret = bindings.get(name);
154             if (ret == null) {
155                 if (logger.isLoggable(BasicLevel.DEBUG)) {
156                     logger.log(BasicLevel.DEBUG, " " + name + " not found.");
157                 }
158                 throw new NameNotFoundException JavaDoc(name);
159             }
160             if (ret instanceof LinkRef JavaDoc) {
161                 // Handle special case of the LinkRef since I think
162
// it's not handled by std NamingManager.getObjectInstance().
163
// The name hidden in linkref is in the initial context.
164

165                 InitialContext JavaDoc ictx = NamingManager.getInstance().getInitialContext();
166                 RefAddr JavaDoc ra = ((Reference JavaDoc) ret).get(0);
167                 try {
168                     ret = ictx.lookup((String JavaDoc) ra.getContent());
169                 } catch (Exception JavaDoc e) {
170                     NamingException JavaDoc ne = new NamingException JavaDoc(e.getMessage());
171                     ne.setRootCause(e);
172                     logger.log(BasicLevel.WARN, "unexpected exception " + e.getMessage());
173                     throw ne;
174                 }
175             } else if (ret instanceof Reference JavaDoc) {
176                 //Use NamingManager to build an object
177
try {
178                     Object JavaDoc o = javax.naming.spi.NamingManager.getObjectInstance(ret, n, this, myEnv);
179                     ret = o;
180                 } catch (NamingException JavaDoc e) {
181                     throw e;
182                 } catch (Exception JavaDoc e) {
183                     NamingException JavaDoc ne = new NamingException JavaDoc(e.getMessage());
184                     ne.setRootCause(e);
185                     throw ne;
186                 }
187                 if (ret == null) {
188                     logger.log(BasicLevel.WARN, "Can not build an object with the reference " + name);
189                     throw new NamingException JavaDoc("Can not build an object with the reference '" + name + "'");
190                 }
191             }
192             return ret;
193         } else {
194             // sub context in the env tree
195
String JavaDoc suffix = n.getSuffix(1).toString();
196             // should throw exception if sub context not found!
197
Context JavaDoc subctx = lookupCtx(n.get(0));
198             return subctx.lookup(suffix);
199         }
200     }
201
202     /**
203      * Binds a name to an object.
204      * Delegate to the String version.
205      *
206      * @param name the name to bind; may not be empty
207      * @param obj the object to bind; possibly null
208      * @throws NamingException if a naming exception is encountered
209      * @see javax.naming.directory.InvalidAttributesException
210      * @see javax.naming.NameAlreadyBoundException
211      */

212     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
213         // Just use the string version for now.
214
bind(name.toString(), obj);
215     }
216
217     /**
218      * Binds a name to an object.
219      *
220      * @param name the name to bind; may not be empty
221      * @param obj the object to bind; possibly null
222      * @throws NamingException if a naming exception is encountered
223      * @see javax.naming.directory.InvalidAttributesException
224      * @see javax.naming.NameAlreadyBoundException
225      */

226     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
227
228         if (logger.isLoggable(BasicLevel.DEBUG)) {
229             logger.log(BasicLevel.DEBUG, name);
230         }
231
232         Name JavaDoc n = new CompositeName JavaDoc(name);
233         if (n.size() < 1) {
234             logger.log(BasicLevel.ERROR, "CompNamingContext bind empty name ?");
235             throw new InvalidNameException JavaDoc("CompNamingContext cannot bind empty name");
236         }
237
238         if (n.size() == 1) {
239             // leaf in the env tree
240
if (bindings.get(name) != null) {
241                 logger.log(BasicLevel.ERROR, "CompNamingContext: trying to overbind");
242                 throw new NameAlreadyBoundException JavaDoc("CompNamingContext: Use rebind to bind over a name");
243             }
244             bindings.put(name, obj);
245         } else {
246             // sub context in the env tree
247
String JavaDoc suffix = n.getSuffix(1).toString();
248             // must create the subcontext first if it does not exist yet.
249
Context JavaDoc subctx;
250             try {
251                 subctx = lookupCtx(n.get(0));
252             } catch (NameNotFoundException JavaDoc e) {
253                 subctx = createSubcontext(n.get(0));
254             }
255             subctx.bind(suffix, obj);
256         }
257     }
258
259     /**
260      * Binds a name to an object, overwriting any existing binding.
261      *
262      * @param name
263      * the name to bind; may not be empty
264      * @param obj
265      * the object to bind; possibly null
266      * @throws NamingException if a naming exception is encountered
267      * @see javax.naming.directory.InvalidAttributesException
268      */

269     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
270         // Just use the string version for now.
271
rebind(name.toString(), obj);
272     }
273
274     /**
275      * Binds a name to an object, overwriting any existing binding.
276      *
277      * @param name
278      * the name to bind; may not be empty
279      * @param obj
280      * the object to bind; possibly null
281      * @throws NamingException if a naming exception is encountered
282      * @see javax.naming.directory.InvalidAttributesException
283      * @see javax.naming.InvalidNameException
284      */

285     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
286
287         if (logger.isLoggable(BasicLevel.DEBUG)) {
288             logger.log(BasicLevel.DEBUG, name);
289         }
290
291         Name JavaDoc n = new CompositeName JavaDoc(name);
292         if (n.size() < 1) {
293             logger.log(BasicLevel.ERROR, "CompNamingContext rebind empty name ?");
294             throw new InvalidNameException JavaDoc("CompNamingContext cannot rebind empty name");
295         }
296
297         if (n.size() == 1) {
298             // leaf in the env tree
299
bindings.put(name, obj);
300         } else {
301             // sub context in the env tree
302
String JavaDoc suffix = n.getSuffix(1).toString();
303             // must create the subcontext first if it does not exist yet.
304
Context JavaDoc subctx;
305             try {
306                 subctx = lookupCtx(n.get(0));
307             } catch (NameNotFoundException JavaDoc e) {
308                 subctx = createSubcontext(n.get(0));
309             }
310             subctx.rebind(suffix, obj);
311         }
312     }
313
314     /**
315      * Unbinds the named object.
316      * @param name
317      * the name to unbind; may not be empty
318      * @throws NamingException if a naming exception is encountered
319      * @see javax.naming.NameNotFoundException
320      */

321     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
322         // Just use the string version for now.
323
unbind(name.toString());
324     }
325
326     /**
327      * Unbinds the named object.
328      * @param name
329      * the name to unbind; may not be empty
330      * @throws NamingException if a naming exception is encountered
331      * @see javax.naming.NameNotFoundException
332      * @see javax.naming.InvalidNameException
333      */

334     public void unbind(String JavaDoc name) throws NamingException JavaDoc {
335
336         if (logger.isLoggable(BasicLevel.DEBUG)) {
337             logger.log(BasicLevel.DEBUG, name);
338         }
339
340         Name JavaDoc n = new CompositeName JavaDoc(name);
341         if (n.size() < 1) {
342             logger.log(BasicLevel.ERROR, "CompNamingContext unbind empty name ?");
343             throw new InvalidNameException JavaDoc("CompNamingContext cannot unbind empty name");
344         }
345
346         if (n.size() == 1) {
347             // leaf in the env tree
348
if (bindings.get(name) == null) {
349                 logger.log(BasicLevel.ERROR, "CompNamingContext nothing to unbind");
350                 throw new NameNotFoundException JavaDoc(name);
351             }
352             bindings.remove(name);
353         } else {
354             // sub context in the env tree
355
String JavaDoc suffix = n.getSuffix(1).toString();
356             // should throw exception if sub context not found!
357
Context JavaDoc subctx = lookupCtx(n.get(0));
358             subctx.unbind(suffix);
359         }
360     }
361
362     /**
363      * Binds a new name to the object bound to an old name, and unbinds
364      * the old name.
365      *
366      * @param oldName
367      * the name of the existing binding; may not be empty
368      * @param newName
369      * the name of the new binding; may not be empty
370      * @throws NamingException if a naming exception is encountered
371      */

372     public void rename(Name JavaDoc oldName, Name JavaDoc newName) throws NamingException JavaDoc {
373         // Just use the string version for now.
374
rename(oldName.toString(), newName.toString());
375     }
376
377     /**
378      * Binds a new name to the object bound to an old name, and unbinds
379      * the old name.
380      *
381      * @param oldName the name of the existing binding; may not be empty
382      * @param newName the name of the new binding; may not be empty
383      * @throws NamingException if a naming exception is encountered
384      */

385     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException JavaDoc {
386
387         logger.log(BasicLevel.ERROR, "CompNamingContext rename " + oldName + " in " + newName);
388
389         Object JavaDoc obj = lookup(oldName);
390         rebind(newName, obj);
391         unbind(oldName);
392     }
393
394     /**
395      * Enumerates the names bound in the named context, along with the
396      * class names of objects bound to them.
397      * The contents of any subcontexts are not included.
398      *
399      * @param name the name of the context to list
400      * @return an enumeration of the names and class names of the
401      * bindings in this context. Each element of the
402      * enumeration is of type NameClassPair.
403      * @throws NamingException if a naming exception is encountered
404      */

405     public NamingEnumeration JavaDoc list(Name JavaDoc name) throws NamingException JavaDoc {
406         // Just use the string version for now.
407
return list(name.toString());
408     }
409
410     /**
411      * Enumerates the names bound in the named context, along with the
412      * class names of objects bound to them.
413      *
414      * @param name the name of the context to list
415      * @return an enumeration of the names and class names of the
416      * bindings in this context. Each element of the
417      * enumeration is of type NameClassPair.
418      * @throws NamingException if a naming exception is encountered
419      */

420     public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc {
421
422         if (logger.isLoggable(BasicLevel.DEBUG)) {
423             logger.log(BasicLevel.DEBUG, name);
424         }
425
426         if (name.length() == 0) {
427             // List this context
428
return new ListOfNames(bindings);
429         }
430         Object JavaDoc obj = lookup(name);
431         if (obj instanceof Context JavaDoc) {
432             return ((Context JavaDoc) obj).list("");
433         } else {
434             logger.log(BasicLevel.ERROR, "CompNamingContext: can only list a Context");
435             throw new NotContextException JavaDoc(name);
436         }
437     }
438
439     /**
440      * Enumerates the names bound in the named context, along with the
441      * objects bound to them.
442      * The contents of any subcontexts are not included.
443      *
444      * If a binding is added to or removed from this context,
445      * its effect on an enumeration previously returned is undefined.
446      *
447      * @param name
448      * the name of the context to list
449      * @return an enumeration of the bindings in this context.
450      * Each element of the enumeration is of type
451      * Binding.
452      * @throws NamingException if a naming exception is encountered
453      *
454      */

455     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc {
456         // Just use the string version for now.
457
return listBindings(name.toString());
458     }
459
460     /**
461      * Enumerates the names bound in the named context, along with the
462      * objects bound to them.
463      *
464      * @param name the name of the context to list
465      * @return an enumeration of the bindings in this context.
466      * Each element of the enumeration is of type
467      * Binding.
468      * @throws NamingException if a naming exception is encountered
469      */

470     public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc {
471
472         if (logger.isLoggable(BasicLevel.DEBUG)) {
473             logger.log(BasicLevel.DEBUG, name);
474         }
475
476         if (name.length() == 0) {
477             // List this context
478
return new ListOfBindings(bindings);
479         }
480         Object JavaDoc obj = lookup(name);
481         if (obj instanceof Context JavaDoc) {
482             return ((Context JavaDoc) obj).listBindings("");
483         } else {
484             logger.log(BasicLevel.ERROR, "CompNamingContext: can only list a Context");
485             throw new NotContextException JavaDoc(name);
486         }
487     }
488
489     /**
490      * Destroys the named context and removes it from the namespace.
491      * Not supported yet.
492      * @param name the name of the context to be destroyed; may not be empty
493      * @throws NamingException if a naming exception is encountered
494      */

495     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
496         // Just use the string version for now.
497
destroySubcontext(name.toString());
498     }
499
500     /**
501      * Destroys the named context and removes it from the namespace.
502      * Not supported yet.
503      * @param name the name of the context to be destroyed; may not be empty
504      * @throws NamingException if a naming exception is encountered
505      */

506     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc {
507
508         logger.log(BasicLevel.ERROR, "CompNamingContext try to destroySubcontext " + name);
509
510         throw new OperationNotSupportedException JavaDoc("CompNamingContext: destroySubcontext");
511     }
512
513     /**
514      * Creates and binds a new context.
515      * Creates a new context with the given name and binds it in
516      * the target context.
517      *
518      * @param name the name of the context to create; may not be empty
519      * @return the newly created context
520      *
521      * @throws NamingException if a naming exception is encountered
522      * @see javax.naming.directory.InvalidAttributesException
523      * @see javax.naming.NameAlreadyBoundException
524      */

525     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc {
526         // Just use the string version for now.
527
return createSubcontext(name.toString());
528     }
529
530     /**
531      * Creates and binds a new context.
532      *
533      * @param name the name of the context to create; may not be empty
534      * @return the newly created context
535      *
536      * @throws NamingException if a naming exception is encountered
537      * @see javax.naming.directory.InvalidAttributesException
538      * @see javax.naming.NameAlreadyBoundException
539      */

540     public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc {
541
542         if (logger.isLoggable(BasicLevel.DEBUG)) {
543             logger.log(BasicLevel.DEBUG, name);
544         }
545
546         Name JavaDoc n = new CompositeName JavaDoc(name);
547         if (n.size() < 1) {
548             logger.log(BasicLevel.ERROR, "CompNamingContext createSubcontext with empty name ?");
549             throw new InvalidNameException JavaDoc("CompNamingContext cannot create empty Subcontext");
550         }
551
552         Context JavaDoc ctx = null; // returned ctx
553
if (n.size() == 1) {
554             // leaf in the env tree: create ctx and bind it in parent.
555
ctx = new CompNamingContext(compId, myEnv);
556             bindings.put(name, ctx);
557         } else {
558             // as for bind, we must create first all the subcontexts
559
// if they don't exist yet.
560
String JavaDoc suffix = n.getSuffix(1).toString();
561             Context JavaDoc subctx;
562             name = n.get(0);
563             try {
564                 subctx = lookupCtx(name);
565             } catch (NameNotFoundException JavaDoc e) {
566                 subctx = createSubcontext(name);
567             }
568             ctx = subctx.createSubcontext(suffix);
569         }
570         return ctx;
571     }
572
573     /**
574      * Retrieves the named object, following links except
575      * for the terminal atomic component of the name.
576      * If the object bound to name is not a link,
577      * returns the object itself.
578      *
579      * @param name the name of the object to look up
580      * @return the object bound to name, not following the
581      * terminal link (if any).
582      * @throws NamingException if a naming exception is encountered
583      */

584     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc {
585         // Just use the string version for now.
586
return lookupLink(name.toString());
587     }
588
589     /**
590      * Retrieves the named object, following links except
591      * for the terminal atomic component of the name.
592      * If the object bound to name is not a link,
593      * returns the object itself.
594      *
595      * @param name
596      * the name of the object to look up
597      * @return the object bound to name, not following the
598      * terminal link (if any)
599      * @throws NamingException if a naming exception is encountered
600      */

601     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc {
602
603         if (logger.isLoggable(BasicLevel.DEBUG)) {
604             logger.log(BasicLevel.DEBUG, name);
605         }
606
607         // To be done. For now: just return the object
608
logger.log(BasicLevel.ERROR, "CompNamingContext lookupLink not implemented yet!");
609         return lookup(name);
610     }
611
612     /**
613      * Retrieves the parser associated with the named context.
614      *
615      * @param name
616      * the name of the context from which to get the parser
617      * @return a name parser that can parse compound names into their atomic
618      * components
619      * @throws NamingException if a naming exception is encountered
620      */

621     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc {
622         return myParser;
623     }
624
625     /**
626      * Retrieves the parser associated with the named context.
627      *
628      * @param name
629      * the name of the context from which to get the parser
630      * @return a name parser that can parse compound names into their atomic
631      * components
632      * @throws NamingException if a naming exception is encountered
633      */

634     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc {
635         return myParser;
636     }
637
638     /**
639      * Composes the name of this context with a name relative to
640      * this context.
641      *
642      * @param name a name relative to this context
643      * @param prefix the name of this context relative to one of its ancestors
644      * @return the composition of prefix and name
645      * @throws NamingException if a naming exception is encountered
646      */

647     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc {
648
649         logger.log(BasicLevel.ERROR, "CompNamingContext composeName not implemented!");
650         throw new OperationNotSupportedException JavaDoc("CompNamingContext composeName");
651     }
652
653     /**
654      * Composes the name of this context with a name relative to
655      * this context: Not supported.
656      *
657      * @param name a name relative to this context
658      * @param prefix the name of this context relative to one of its ancestors
659      * @return the composition of prefix and name
660      * @throws NamingException if a naming exception is encountered
661      */

662     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix) throws NamingException JavaDoc {
663
664         logger.log(BasicLevel.ERROR, "CompNamingContext composeName " + name + " " + prefix);
665
666         throw new OperationNotSupportedException JavaDoc("CompNamingContext composeName");
667     }
668
669     /**
670      * Adds a new environment property to the environment of this
671      * context. If the property already exists, its value is overwritten.
672      *
673      * @param propName
674      * the name of the environment property to add; may not be null
675      * @param propVal
676      * the value of the property to add; may not be null
677      * @return the previous value of the property, or null if the property was
678      * not in the environment before
679      * @throws NamingException if a naming exception is encountered
680      */

681     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal) throws NamingException JavaDoc {
682
683         if (logger.isLoggable(BasicLevel.DEBUG)) {
684             logger.log(BasicLevel.DEBUG, propName);
685         }
686
687         if (myEnv == null) {
688             myEnv = new Hashtable JavaDoc();
689         }
690         return myEnv.put(propName, propVal);
691     }
692
693     /**
694      * Removes an environment property from the environment of this
695      * context.
696      *
697      * @param propName the name of the environment property to remove; may not be null
698      * @return the previous value of the property, or null if the property was
699      * not in the environment
700      * @throws NamingException if a naming exception is encountered
701      */

702     public Object JavaDoc removeFromEnvironment(String JavaDoc propName) throws NamingException JavaDoc {
703
704         if (logger.isLoggable(BasicLevel.DEBUG)) {
705             logger.log(BasicLevel.DEBUG, propName);
706         }
707
708         if (myEnv == null) {
709             return null;
710         }
711         return myEnv.remove(propName);
712     }
713
714     /**
715      * Retrieves the environment in effect for this context.
716      *
717      * @return the environment of this context; never null
718      * @throws NamingException if a naming exception is encountered
719      */

720     public Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc {
721
722         if (logger.isLoggable(BasicLevel.DEBUG)) {
723             logger.log(BasicLevel.DEBUG, "");
724         }
725
726         if (myEnv == null) {
727             myEnv = new Hashtable JavaDoc();
728         }
729         return myEnv;
730     }
731
732     /**
733      * Closes this context.
734      *
735      * @throws NamingException if a naming exception is encountered
736      */

737     public void close() throws NamingException JavaDoc {
738         myEnv = null;
739     }
740
741     /**
742      * Retrieves the full name of this context within its own namespace.
743      *
744      * @return this context's name in its own namespace; never null
745      */

746     public String JavaDoc getNameInNamespace() {
747         // this is used today for debug only.
748
return compId;
749     }
750
751     // ------------------------------------------------------------------
752
// Private Methods
753
// ------------------------------------------------------------------
754

755     /**
756      * Find if this name is a sub context.
757      * @param name the sub context name
758      * @return the named Context
759      * @throws NamingException When nam?ing fails
760      * @see javax.naming.NameNotFoundException
761      * @see javax.naming.NameAlreadyBoundException
762      */

763     private Context JavaDoc lookupCtx(String JavaDoc name) throws NamingException JavaDoc {
764         Object JavaDoc obj = bindings.get(name);
765         if (obj == null) {
766             throw new NameNotFoundException JavaDoc();
767         }
768         if (obj instanceof CompNamingContext) {
769             return (Context JavaDoc) obj;
770         } else {
771             throw new NameAlreadyBoundException JavaDoc(name);
772         }
773     }
774
775     // ------------------------------------------------------------------
776
// Inner classes for enumerating lists of bindings
777
// ------------------------------------------------------------------
778

779     /**
780      * Implementation of the NamingEnumeration for list operations
781      * Each element is of type NameClassPair.
782      */

783     class ListOfNames implements NamingEnumeration JavaDoc {
784         protected Enumeration JavaDoc names;
785         protected Hashtable JavaDoc bindings;
786
787         // Constructor. Called by list()
788
// copy bindings locally in this object and build an
789
// enumeration of the keys.
790

791         ListOfNames (Hashtable JavaDoc bindings) {
792             this.bindings = bindings;
793             this.names = bindings.keys();
794         }
795
796         // Methods implementing NamingEnumeration interface:
797
// - hasMore
798
// - next
799
// - close
800
public boolean hasMore() throws NamingException JavaDoc {
801             return names.hasMoreElements();
802         }
803
804         public Object JavaDoc next() throws NamingException JavaDoc {
805             String JavaDoc name = (String JavaDoc) names.nextElement();
806             String JavaDoc className = bindings.get(name).getClass().getName();
807             return new NameClassPair JavaDoc(name, className);
808         }
809
810         public void close() {
811         }
812
813         // Methods inherited from Enumeration:
814
// - nextElement
815
// - hasMoreElements
816

817         public Object JavaDoc nextElement() {
818             try {
819                 return next();
820             } catch (NamingException JavaDoc e) {
821                 throw new NoSuchElementException JavaDoc(e.toString());
822             }
823         }
824
825         public boolean hasMoreElements() {
826             try {
827                 return hasMore();
828             } catch (NamingException JavaDoc e) {
829                 return false;
830             }
831         }
832
833     }
834
835     /**
836      * Implementation of the NamingEnumeration for listBindings operations
837      */

838     class ListOfBindings extends ListOfNames {
839
840         ListOfBindings (Hashtable JavaDoc bindings) {
841             super(bindings);
842         }
843
844         // next() is the only different method.
845
// It returns a Binding instead of a NameClassPair
846
public Object JavaDoc next() throws NamingException JavaDoc {
847             String JavaDoc name = (String JavaDoc) names.nextElement();
848             return new Binding JavaDoc(name, this.bindings.get(name));
849         }
850     }
851 }
852
Popular Tags