KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > jndi > enc > java > CompNamingContext


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 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.5 2005/03/15 17:54:52 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.carol.jndi.enc.java;
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.carol.util.configuration.TraceCarol;
51
52 /**
53  * Implementation of Context interface for EJB Environment. Must handle
54  * subContexts (because of jndi/, ejb/, ...)
55  * @author Philippe Durieux
56  * @author Philippe Coq monolog
57  * @author Florent Benoit handle Reference object for the lookup.
58  */

59 public class CompNamingContext implements Context JavaDoc {
60
61     /**
62      * Environment
63      */

64     private Hashtable JavaDoc myEnv = null;
65
66     /**
67      * Bindings
68      */

69     private Hashtable JavaDoc bindings = new Hashtable JavaDoc();
70
71     /**
72      * Parser
73      */

74     private static NameParser JavaDoc myParser = new JavaNameParser();
75
76     /**
77      * Naming id
78      */

79     private String JavaDoc compId;
80
81     /**
82      * Constructor
83      * @param id id of the context.
84      * @param env initial environment.
85      */

86     public CompNamingContext(String JavaDoc id, Hashtable JavaDoc env) {
87         if (env != null) {
88             // clone env to be able to change it.
89
myEnv = (Hashtable JavaDoc) (env.clone());
90         }
91         compId = id;
92     }
93
94     /**
95      * Constructor
96      * @param id id of the context.
97      */

98     public CompNamingContext(String JavaDoc id) {
99         this(id, new Hashtable JavaDoc());
100     }
101
102     /**
103      * Retrieves the named object.
104      * @param name the name of the object to look up
105      * @return the object bound to <tt>name</tt>
106      * @throws NamingException if a naming exception is encountered
107      */

108     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc {
109         // Just use the string version for now.
110
return lookup(name.toString());
111     }
112
113     /**
114      * Retrieves the named object.
115      * @param name the name of the object to look up
116      * @return the object bound to name
117      * @throws NamingException if a naming exception is encountered
118      */

119     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
120         if (TraceCarol.isDebugjndiEncCarol()) {
121             TraceCarol.debugjndiEncCarol(name);
122         }
123
124         Name JavaDoc n = new CompositeName JavaDoc(name);
125         if (n.size() < 1) {
126             // Empty name means this context
127
if (TraceCarol.isDebugjndiEncCarol()) {
128                 TraceCarol.debugjndiEncCarol("empty name");
129             }
130             return this;
131         }
132
133         if (n.size() == 1) {
134             // leaf in the env tree
135
Object JavaDoc ret = bindings.get(name);
136             if (ret == null) {
137                 if (TraceCarol.isDebugjndiEncCarol()) {
138                     TraceCarol.debugjndiEncCarol(" " + name + " not found.");
139                 }
140                 throw new NameNotFoundException JavaDoc(name);
141             }
142             if (ret instanceof LinkRef JavaDoc) {
143                 // Handle special case of the LinkRef since I think
144
// it's not handled by std NamingManager.getObjectInstance().
145
// The name hidden in linkref is in the initial context.
146

147                 InitialContext JavaDoc ictx = new InitialContext JavaDoc();
148                 RefAddr JavaDoc ra = ((Reference JavaDoc) ret).get(0);
149                 try {
150                     ret = ictx.lookup((String JavaDoc) ra.getContent());
151                 } catch (Exception JavaDoc e) {
152                     NamingException JavaDoc ne = new NamingException JavaDoc(e.getMessage());
153                     ne.setRootCause(e);
154                     TraceCarol.error("unexpected exception " + e.getMessage(), e);
155                     throw ne;
156                 }
157             } else if (ret instanceof Reference JavaDoc) {
158                 // Use NamingManager to build an object
159
try {
160                     Object JavaDoc o = javax.naming.spi.NamingManager.getObjectInstance(ret, n, this, myEnv);
161                     ret = o;
162                 } catch (NamingException JavaDoc e) {
163                     throw e;
164                 } catch (Exception JavaDoc e) {
165                     NamingException JavaDoc ne = new NamingException JavaDoc(e.getMessage());
166                     ne.setRootCause(e);
167                     throw ne;
168                 }
169                 if (ret == null) {
170                     TraceCarol.error("Can not build an object with the reference " + name);
171                     throw new NamingException JavaDoc("Can not build an object with the reference '" + name + "'");
172                 }
173             }
174             return ret;
175         } else {
176             // sub context in the env tree
177
String JavaDoc suffix = n.getSuffix(1).toString();
178             // should throw exception if sub context not found!
179
Context JavaDoc subctx = lookupCtx(n.get(0));
180             return subctx.lookup(suffix);
181         }
182     }
183
184     /**
185      * Binds a name to an object. Delegate to the String version.
186      * @param name the name to bind; may not be empty
187      * @param obj the object to bind; possibly null
188      * @throws NamingException if a naming exception is encountered
189      * @see javax.naming.directory.InvalidAttributesException
190      * @see javax.naming.NameAlreadyBoundException
191      */

192     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
193         // Just use the string version for now.
194
bind(name.toString(), obj);
195     }
196
197     /**
198      * Binds a name to an object.
199      * @param name the name to bind; may not be empty
200      * @param obj the object to bind; possibly null
201      * @throws NamingException if a naming exception is encountered
202      * @see javax.naming.directory.InvalidAttributesException
203      * @see javax.naming.NameAlreadyBoundException
204      */

205     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
206         if (TraceCarol.isDebugjndiEncCarol()) {
207             TraceCarol.debugjndiEncCarol(name);
208         }
209
210         Name JavaDoc n = new CompositeName JavaDoc(name);
211         if (n.size() < 1) {
212             TraceCarol.error("CompNamingContext bind empty name ?");
213
214             throw new InvalidNameException JavaDoc("CompNamingContext cannot bind empty name");
215         }
216
217         if (n.size() == 1) {
218             // leaf in the env tree
219
if (bindings.get(name) != null) {
220                 TraceCarol.error("CompNamingContext: trying to overbind");
221                 throw new NameAlreadyBoundException JavaDoc("CompNamingContext: Use rebind to bind over a name");
222             }
223             bindings.put(name, obj);
224         } else {
225             // sub context in the env tree
226
String JavaDoc suffix = n.getSuffix(1).toString();
227             // must create the subcontext first if it does not exist yet.
228
Context JavaDoc subctx;
229             try {
230                 subctx = lookupCtx(n.get(0));
231             } catch (NameNotFoundException JavaDoc e) {
232                 subctx = createSubcontext(n.get(0));
233             }
234             subctx.bind(suffix, obj);
235         }
236     }
237
238     /**
239      * Binds a name to an object, overwriting any existing binding.
240      * @param name the name to bind; may not be empty
241      * @param obj the object to bind; possibly null
242      * @throws NamingException if a naming exception is encountered
243      * @see javax.naming.directory.InvalidAttributesException
244      */

245     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
246         // Just use the string version for now.
247
rebind(name.toString(), obj);
248     }
249
250     /**
251      * Binds a name to an object, overwriting any existing binding.
252      * @param name the name to bind; may not be empty
253      * @param obj the object to bind; possibly null
254      * @throws NamingException if a naming exception is encountered
255      * @see javax.naming.directory.InvalidAttributesException
256      * @see javax.naming.InvalidNameException
257      */

258     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
259
260         if (TraceCarol.isDebugjndiEncCarol()) {
261             TraceCarol.debugjndiEncCarol(name);
262         }
263
264         Name JavaDoc n = new CompositeName JavaDoc(name);
265         if (n.size() < 1) {
266             TraceCarol.error("CompNamingContext rebind empty name ?");
267             throw new InvalidNameException JavaDoc("CompNamingContext cannot rebind empty name");
268         }
269
270         if (n.size() == 1) {
271             // leaf in the env tree
272
bindings.put(name, obj);
273         } else {
274             // sub context in the env tree
275
String JavaDoc suffix = n.getSuffix(1).toString();
276             // must create the subcontext first if it does not exist yet.
277
Context JavaDoc subctx;
278             try {
279                 subctx = lookupCtx(n.get(0));
280             } catch (NameNotFoundException JavaDoc e) {
281                 subctx = createSubcontext(n.get(0));
282             }
283             subctx.rebind(suffix, obj);
284         }
285     }
286
287     /**
288      * Unbinds the named object.
289      * @param name the name to unbind; may not be empty
290      * @throws NamingException if a naming exception is encountered
291      * @see javax.naming.NameNotFoundException
292      */

293     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
294         // Just use the string version for now.
295
unbind(name.toString());
296     }
297
298     /**
299      * Unbinds the named object.
300      * @param name the name to unbind; may not be empty
301      * @throws NamingException if a naming exception is encountered
302      * @see javax.naming.NameNotFoundException
303      * @see javax.naming.InvalidNameException
304      */

305     public void unbind(String JavaDoc name) throws NamingException JavaDoc {
306
307         if (TraceCarol.isDebugjndiEncCarol()) {
308             TraceCarol.debugjndiEncCarol(name);
309         }
310
311         Name JavaDoc n = new CompositeName JavaDoc(name);
312         if (n.size() < 1) {
313             TraceCarol.error("CompNamingContext unbind empty name ?");
314             throw new InvalidNameException JavaDoc("CompNamingContext cannot unbind empty name");
315         }
316
317         if (n.size() == 1) {
318             // leaf in the env tree
319
if (bindings.get(name) == null) {
320                 TraceCarol.error("CompNamingContext nothing to unbind");
321                 throw new NameNotFoundException JavaDoc(name);
322             }
323             bindings.remove(name);
324         } else {
325             // sub context in the env tree
326
String JavaDoc suffix = n.getSuffix(1).toString();
327             // should throw exception if sub context not found!
328
Context JavaDoc subctx = lookupCtx(n.get(0));
329             subctx.unbind(suffix);
330         }
331     }
332
333     /**
334      * Binds a new name to the object bound to an old name, and unbinds the old
335      * name.
336      * @param oldName the name of the existing binding; may not be empty
337      * @param newName the name of the new binding; may not be empty
338      * @throws NamingException if a naming exception is encountered
339      */

340     public void rename(Name JavaDoc oldName, Name JavaDoc newName) throws NamingException JavaDoc {
341         // Just use the string version for now.
342
rename(oldName.toString(), newName.toString());
343     }
344
345     /**
346      * Binds a new name to the object bound to an old name, and unbinds the old
347      * name.
348      * @param oldName the name of the existing binding; may not be empty
349      * @param newName the name of the new binding; may not be empty
350      * @throws NamingException if a naming exception is encountered
351      */

352     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException JavaDoc {
353         if (TraceCarol.isDebugjndiEncCarol()) {
354             TraceCarol.debugjndiEncCarol("CompNamingContext rename " + oldName + " in " + newName);
355         }
356         Object JavaDoc obj = lookup(oldName);
357         rebind(newName, obj);
358         unbind(oldName);
359     }
360
361     /**
362      * Enumerates the names bound in the named context, along with the class
363      * names of objects bound to them. The contents of any subcontexts are not
364      * included.
365      * @param name the name of the context to list
366      * @return an enumeration of the names and class names of the bindings in
367      * this context. Each element of the enumeration is of type
368      * NameClassPair.
369      * @throws NamingException if a naming exception is encountered
370      */

371     public NamingEnumeration JavaDoc list(Name JavaDoc name) throws NamingException JavaDoc {
372         // Just use the string version for now.
373
return list(name.toString());
374     }
375
376     /**
377      * Enumerates the names bound in the named context, along with the class
378      * names of objects bound to them.
379      * @param name the name of the context to list
380      * @return an enumeration of the names and class names of the bindings in
381      * this context. Each element of the enumeration is of type
382      * NameClassPair.
383      * @throws NamingException if a naming exception is encountered
384      */

385     public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc {
386         if (TraceCarol.isDebugjndiEncCarol()) {
387             TraceCarol.debugjndiEncCarol(name);
388         }
389
390         if (name.length() == 0) {
391             // List this context
392
return new ListOfNames(bindings);
393         }
394         Object JavaDoc obj = lookup(name);
395         if (obj instanceof Context JavaDoc) {
396             return ((Context JavaDoc) obj).list("");
397         } else {
398             TraceCarol.error("CompNamingContext: can only list a Context");
399             throw new NotContextException JavaDoc(name);
400         }
401     }
402
403     /**
404      * Enumerates the names bound in the named context, along with the objects
405      * bound to them. The contents of any subcontexts are not included. If a
406      * binding is added to or removed from this context, its effect on an
407      * enumeration previously returned is undefined.
408      * @param name the name of the context to list
409      * @return an enumeration of the bindings in this context. Each element of
410      * the enumeration is of type Binding.
411      * @throws NamingException if a naming exception is encountered
412      */

413     public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc {
414         // Just use the string version for now.
415
return listBindings(name.toString());
416     }
417
418     /**
419      * Enumerates the names bound in the named context, along with the objects
420      * bound to them.
421      * @param name the name of the context to list
422      * @return an enumeration of the bindings in this context. Each element of
423      * the enumeration is of type Binding.
424      * @throws NamingException if a naming exception is encountered
425      */

426     public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc {
427         if (TraceCarol.isDebugjndiEncCarol()) {
428             TraceCarol.debugjndiEncCarol(name);
429         }
430
431         if (name.length() == 0) {
432             // List this context
433
return new ListOfBindings(bindings);
434         }
435         Object JavaDoc obj = lookup(name);
436         if (obj instanceof Context JavaDoc) {
437             return ((Context JavaDoc) obj).listBindings("");
438         } else {
439             TraceCarol.error("CompNamingContext: can only list a Context");
440             throw new NotContextException JavaDoc(name);
441         }
442     }
443
444     /**
445      * Destroys the named context and removes it from the namespace. Not
446      * supported yet.
447      * @param name the name of the context to be destroyed; may not be empty
448      * @throws NamingException if a naming exception is encountered
449      */

450     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
451         // Just use the string version for now.
452
destroySubcontext(name.toString());
453     }
454
455     /**
456      * Destroys the named context and removes it from the namespace. Not
457      * supported yet.
458      * @param name the name of the context to be destroyed; may not be empty
459      * @throws NamingException if a naming exception is encountered
460      */

461     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc {
462         TraceCarol.error("CompNamingContext try to destroySubcontext " + name);
463         throw new OperationNotSupportedException JavaDoc("CompNamingContext: destroySubcontext");
464     }
465
466     /**
467      * Creates and binds a new context. Creates a new context with the given
468      * name and binds it in the target context.
469      * @param name the name of the context to create; may not be empty
470      * @return the newly created context
471      * @throws NamingException if a naming exception is encountered
472      * @see javax.naming.directory.InvalidAttributesException
473      * @see javax.naming.NameAlreadyBoundException
474      */

475     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc {
476         // Just use the string version for now.
477
return createSubcontext(name.toString());
478     }
479
480     /**
481      * Creates and binds a new context.
482      * @param name the name of the context to create; may not be empty
483      * @return the newly created context
484      * @throws NamingException if a naming exception is encountered
485      * @see javax.naming.directory.InvalidAttributesException
486      * @see javax.naming.NameAlreadyBoundException
487      */

488     public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc {
489         if (TraceCarol.isDebugjndiEncCarol()) {
490             TraceCarol.debugjndiEncCarol(name);
491         }
492
493         Name JavaDoc n = new CompositeName JavaDoc(name);
494         if (n.size() < 1) {
495             TraceCarol.error("CompNamingContext createSubcontext with empty name ?");
496             throw new InvalidNameException JavaDoc("CompNamingContext cannot create empty Subcontext");
497         }
498
499         Context JavaDoc ctx = null; // returned ctx
500
if (n.size() == 1) {
501             // leaf in the env tree: create ctx and bind it in parent.
502
ctx = new CompNamingContext(compId, myEnv);
503             bindings.put(name, ctx);
504         } else {
505             // as for bind, we must create first all the subcontexts
506
// if they don't exist yet.
507
String JavaDoc suffix = n.getSuffix(1).toString();
508             Context JavaDoc subctx;
509             name = n.get(0);
510             try {
511                 subctx = lookupCtx(name);
512             } catch (NameNotFoundException JavaDoc e) {
513                 subctx = createSubcontext(name);
514             }
515             ctx = subctx.createSubcontext(suffix);
516         }
517         return ctx;
518     }
519
520     /**
521      * Retrieves the named object, following links except for the terminal
522      * atomic component of the name. If the object bound to name is not a link,
523      * returns the object itself.
524      * @param name the name of the object to look up
525      * @return the object bound to name, not following the terminal link (if
526      * any).
527      * @throws NamingException if a naming exception is encountered
528      */

529     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc {
530         // Just use the string version for now.
531
return lookupLink(name.toString());
532     }
533
534     /**
535      * Retrieves the named object, following links except for the terminal
536      * atomic component of the name. If the object bound to name is not a link,
537      * returns the object itself.
538      * @param name the name of the object to look up
539      * @return the object bound to name, not following the terminal link (if
540      * any)
541      * @throws NamingException if a naming exception is encountered
542      */

543     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc {
544         if (TraceCarol.isDebugjndiEncCarol()) {
545             TraceCarol.debugjndiEncCarol(name);
546         }
547
548         // To be done. For now: just return the object
549
TraceCarol.error("CompNamingContext lookupLink not implemented yet!");
550
551         return lookup(name);
552     }
553
554     /**
555      * Retrieves the parser associated with the named context.
556      * @param name the name of the context from which to get the parser
557      * @return a name parser that can parse compound names into their atomic
558      * components
559      * @throws NamingException if a naming exception is encountered
560      */

561     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc {
562         return myParser;
563     }
564
565     /**
566      * Retrieves the parser associated with the named context.
567      * @param name the name of the context from which to get the parser
568      * @return a name parser that can parse compound names into their atomic
569      * components
570      * @throws NamingException if a naming exception is encountered
571      */

572     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc {
573         return myParser;
574     }
575
576     /**
577      * Composes the name of this context with a name relative to this context.
578      * @param name a name relative to this context
579      * @param prefix the name of this context relative to one of its ancestors
580      * @return the composition of prefix and name
581      * @throws NamingException if a naming exception is encountered
582      */

583     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc {
584         TraceCarol.error("CompNamingContext composeName not implemented!");
585
586         throw new OperationNotSupportedException JavaDoc("CompNamingContext composeName");
587     }
588
589     /**
590      * Composes the name of this context with a name relative to this context:
591      * Not supported.
592      * @param name a name relative to this context
593      * @param prefix the name of this context relative to one of its ancestors
594      * @return the composition of prefix and name
595      * @throws NamingException if a naming exception is encountered
596      */

597     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix) throws NamingException JavaDoc {
598         TraceCarol.error("CompNamingContext composeName " + name + " " + prefix);
599
600         throw new OperationNotSupportedException JavaDoc("CompNamingContext composeName");
601     }
602
603     /**
604      * Adds a new environment property to the environment of this context. If
605      * the property already exists, its value is overwritten.
606      * @param propName the name of the environment property to add; may not be
607      * null
608      * @param propVal the value of the property to add; may not be null
609      * @return the previous value of the property, or null if the property was
610      * not in the environment before
611      * @throws NamingException if a naming exception is encountered
612      */

613     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal) throws NamingException JavaDoc {
614
615         TraceCarol.debugjndiEncCarol(propName);
616
617         if (myEnv == null) {
618             myEnv = new Hashtable JavaDoc();
619         }
620         return myEnv.put(propName, propVal);
621     }
622
623     /**
624      * Removes an environment property from the environment of this context.
625      * @param propName the name of the environment property to remove; may not
626      * be null
627      * @return the previous value of the property, or null if the property was
628      * not in the environment
629      * @throws NamingException if a naming exception is encountered
630      */

631     public Object JavaDoc removeFromEnvironment(String JavaDoc propName) throws NamingException JavaDoc {
632
633         TraceCarol.debugjndiEncCarol(propName);
634
635         if (myEnv == null) {
636             return null;
637         }
638         return myEnv.remove(propName);
639     }
640
641     /**
642      * Retrieves the environment in effect for this context.
643      * @return the environment of this context; never null
644      * @throws NamingException if a naming exception is encountered
645      */

646     public Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc {
647         if (TraceCarol.isDebugjndiEncCarol()) {
648             TraceCarol.debugjndiEncCarol("");
649         }
650
651         if (myEnv == null) {
652             myEnv = new Hashtable JavaDoc();
653         }
654         return myEnv;
655     }
656
657     /**
658      * Closes this context.
659      * @throws NamingException if a naming exception is encountered
660      */

661     public void close() throws NamingException JavaDoc {
662         myEnv = null;
663     }
664
665     /**
666      * Retrieves the full name of this context within its own namespace.
667      * @return this context's name in its own namespace; never null
668      */

669     public String JavaDoc getNameInNamespace() {
670         // this is used today for debug only.
671
return compId;
672     }
673
674     // ------------------------------------------------------------------
675
// Private Methods
676
// ------------------------------------------------------------------
677

678     /**
679      * Find if this name is a sub context.
680      * @param name the sub context name
681      * @return the named Context
682      * @throws NamingException When nam?ing fails
683      * @see javax.naming.NameNotFoundException
684      * @see javax.naming.NameAlreadyBoundException
685      */

686     private Context JavaDoc lookupCtx(String JavaDoc name) throws NamingException JavaDoc {
687         Object JavaDoc obj = bindings.get(name);
688         if (obj == null) {
689             throw new NameNotFoundException JavaDoc();
690         }
691         if (obj instanceof CompNamingContext) {
692             return (Context JavaDoc) obj;
693         } else {
694             throw new NameAlreadyBoundException JavaDoc(name);
695         }
696     }
697
698     // ------------------------------------------------------------------
699
// Inner classes for enumerating lists of bindings
700
// ------------------------------------------------------------------
701

702     /**
703      * Implementation of the NamingEnumeration for list operations Each element
704      * is of type NameClassPair.
705      */

706     protected class ListOfNames implements NamingEnumeration JavaDoc {
707
708         /**
709          * list of names
710          */

711         private Enumeration JavaDoc names;
712
713         /**
714          * List of bindings
715          */

716         private Hashtable JavaDoc bindings;
717
718         /**
719          * Constructor. Called by list()
720          * @param bindings list of bindings
721          */

722         ListOfNames(Hashtable JavaDoc bindings) {
723             this.bindings = bindings;
724             this.names = bindings.keys();
725         }
726
727         /**
728          * Determines whether there are any more elements in the enumeration.
729          * @return true if there is more in the enumeration ; false otherwise.
730          * @throws NamingException If a naming exception is encountered while
731          * attempting to determine whether there is another element in
732          * the enumeration.
733          */

734         public boolean hasMore() throws NamingException JavaDoc {
735             return names.hasMoreElements();
736         }
737
738         /**
739          * Retrieves the next element in the enumeration.
740          * @return The possibly null element in the enumeration. null is only
741          * valid for enumerations that can return null (e.g.
742          * Attribute.getAll() returns an enumeration of attribute
743          * values, and an attribute value can be null).
744          * @throws NamingException If a naming exception is encountered while
745          * attempting to retrieve the next element. See NamingException
746          * and its subclasses for the possible naming exceptions.
747          */

748         public Object JavaDoc next() throws NamingException JavaDoc {
749             String JavaDoc name = (String JavaDoc) names.nextElement();
750             String JavaDoc className = bindings.get(name).getClass().getName();
751             return new NameClassPair JavaDoc(name, className);
752         }
753
754         /**
755          * Closes this enumeration.
756          */

757         public void close() {
758         }
759
760         /**
761          * Returns the next element of this enumeration if this enumeration
762          * object has at least one more element to provide.
763          * @return the next element of this enumeration.
764          */

765         public Object JavaDoc nextElement() {
766             try {
767                 return next();
768             } catch (NamingException JavaDoc e) {
769                 throw new NoSuchElementException JavaDoc(e.toString());
770             }
771         }
772
773         /**
774          * Tests if this enumeration contains more elements.
775          * @return <code>true</code> if and only if this enumeration object
776          * contains at least one more element to provide;
777          * <code>false</code> otherwise.
778          */

779         public boolean hasMoreElements() {
780             try {
781                 return hasMore();
782             } catch (NamingException JavaDoc e) {
783                 return false;
784             }
785         }
786
787         /**
788          * @return the bindings.
789          */

790         protected Hashtable JavaDoc getBindings() {
791             return bindings;
792         }
793
794         /**
795          * @return the names.
796          */

797         protected Enumeration JavaDoc getNames() {
798             return names;
799         }
800     }
801
802     /**
803      * Implementation of the NamingEnumeration for listBindings operations
804      */

805     protected class ListOfBindings extends ListOfNames {
806
807         /**
808          * Constructor.
809          * @param bindings list of bindings
810          */

811         ListOfBindings(Hashtable JavaDoc bindings) {
812             super(bindings);
813         }
814
815         /**
816          * It returns a Binding instead of a NameClassPair * Retrieves the next
817          * element in the enumeration.
818          * @return The possibly null element in the enumeration. null is only
819          * valid for enumerations that can return null (e.g.
820          * Attribute.getAll() returns an enumeration of attribute
821          * values, and an attribute value can be null).
822          * @throws NamingException If a naming exception is encountered while
823          * attempting to retrieve the next element. See NamingException
824          * and its subclasses for the possible naming exceptions.
825          */

826         public Object JavaDoc next() throws NamingException JavaDoc {
827             String JavaDoc name = (String JavaDoc) getNames().nextElement();
828             return new Binding JavaDoc(name, getBindings().get(name));
829         }
830     }
831 }
832
Popular Tags