KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > naming > cosnaming > NamingContextImpl


1 /*
2  * @(#)NamingContextImpl.java 1.67 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.naming.cosnaming;
9
10 // Imports for Logging
11
import java.util.logging.Logger JavaDoc;
12 import java.util.logging.Level JavaDoc;
13 import com.sun.corba.se.impl.orbutil.LogKeywords;
14
15 // Import general CORBA classes
16
import org.omg.CORBA.Object JavaDoc;
17 import org.omg.CORBA.BAD_PARAM JavaDoc;
18 import org.omg.CORBA.INTERNAL JavaDoc;
19 import org.omg.CORBA.CompletionStatus JavaDoc;
20 import org.omg.PortableServer.POA JavaDoc;
21 import org.omg.PortableServer.Servant JavaDoc;
22
23 // Import org.omg.CosNaming classes
24
import org.omg.CosNaming.BindingType JavaDoc;
25 import org.omg.CosNaming.BindingTypeHolder JavaDoc;
26 import org.omg.CosNaming.BindingListHolder JavaDoc;
27 import org.omg.CosNaming.BindingIteratorHolder JavaDoc;
28 import org.omg.CosNaming.NameComponent JavaDoc;
29 import org.omg.CosNaming.NamingContextHelper JavaDoc;
30 import org.omg.CosNaming.NamingContext JavaDoc;
31 import org.omg.CosNaming.NamingContextPackage.*;
32 import org.omg.CosNaming._NamingContextImplBase JavaDoc;
33 import org.omg.CosNaming.NamingContextExtHelper JavaDoc;
34 import org.omg.CosNaming.NamingContextExt JavaDoc;
35 import org.omg.CosNaming.NamingContextExtPOA JavaDoc;
36 import org.omg.CosNaming.NamingContextExtPackage.*;
37 import org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc;
38
39 import com.sun.corba.se.impl.naming.cosnaming.NamingContextDataStore;
40
41 import com.sun.corba.se.impl.naming.namingutil.INSURLHandler;
42 import com.sun.corba.se.spi.logging.CORBALogDomains;
43 import com.sun.corba.se.impl.logging.NamingSystemException ;
44
45 import com.sun.corba.se.spi.orb.ORB;
46
47 /**
48  * Class NamingContextImpl implements the org.omg.CosNaming::NamingContext
49  * interface, but does not implement the methods associated with
50  * maintaining the "table" of current bindings in a NamingContext.
51  * Instead, this implementation assumes that the derived implementation
52  * implements the NamingContextDataStore interface, which has the necessary
53  * methods. This allows multiple
54  * NamingContext implementations that differ in storage of the bindings,
55  * as well as implementations of interfaces derived from
56  * CosNaming::NamingContext that still reuses the implementation.
57  * <p>
58  * The operations bind(), rebind(), bind_context() and rebind_context()
59  * are all really implemented by doBind(). resolve() is really implemented
60  * by doResolve(), unbind() by doUnbind(). list(), new_context() and
61  * destroy() uses the NamingContextDataStore interface directly. All the
62  * doX() methods are public static.
63  * They synchronize on the NamingContextDataStore object.
64  * <p>
65  * An implementation a NamingContext must extend this class and implement
66  * the NamingContextDataStore interface with the operations:
67  * Bind(), Resolve(),
68  * Unbind(), List(), NewContext() and Destroy(). Calls
69  * to these methods are synchronized; these methods should
70  * therefore not be synchronized.
71  */

72 public abstract class NamingContextImpl
73     extends NamingContextExtPOA JavaDoc
74     implements NamingContextDataStore
75 {
76
77     protected POA JavaDoc nsPOA;
78     private Logger JavaDoc readLogger, updateLogger, lifecycleLogger;
79     private NamingSystemException wrapper ;
80     private static NamingSystemException staticWrapper =
81     NamingSystemException.get( CORBALogDomains.NAMING_UPDATE ) ;
82
83     // The grammer for Parsing and Building Interoperable Stringified Names
84
// are implemented in this class
85
private InterOperableNamingImpl insImpl;
86     /**
87      * Create a naming context servant.
88      * Runs the super constructor.
89      * @param orb an ORB object.
90      * @exception java.lang.Exception a Java exception.
91      */

92     public NamingContextImpl(ORB orb, POA JavaDoc poa) throws java.lang.Exception JavaDoc {
93     super();
94     this.orb = orb;
95     wrapper = NamingSystemException.get( orb,
96         CORBALogDomains.NAMING_UPDATE ) ;
97
98         insImpl = new InterOperableNamingImpl( );
99         this.nsPOA = poa;
100         readLogger = orb.getLogger( CORBALogDomains.NAMING_READ);
101         updateLogger = orb.getLogger( CORBALogDomains.NAMING_UPDATE);
102         lifecycleLogger = orb.getLogger(
103             CORBALogDomains.NAMING_LIFECYCLE);
104     }
105
106     public POA JavaDoc getNSPOA( ) {
107         return nsPOA;
108     }
109   
110     /**
111      * Bind an object under a name in this NamingContext. If the name
112      * contains multiple (n) components, n-1 will be resolved in this
113      * NamingContext and the object bound in resulting NamingContext.
114      * An exception is thrown if a binding with the supplied name already
115      * exists. If the
116      * object to be bound is a NamingContext it will not participate in
117      * a recursive resolve.
118      * @param n a sequence of NameComponents which is the name under which
119      * the object will be bound.
120      * @param obj the object reference to be bound.
121      * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with
122      * multiple components was supplied, but the first component could not be
123      * resolved.
124      * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could
125      * not proceed in resolving the n-1 components of the supplied name.
126      * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The
127      * supplied name is invalid (i.e., has length less than 1).
128      * @exception org.omg.CosNaming.NamingContextPackage.AlreadyBound An object
129      * is already bound under the supplied name.
130      * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
131      * system exceptions.
132      * @see doBind
133      */

134     public void bind(NameComponent JavaDoc[] n, org.omg.CORBA.Object JavaDoc obj)
135     throws org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc,
136            org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc,
137            org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc,
138            org.omg.CosNaming.NamingContextPackage.AlreadyBound JavaDoc
139     {
140     if( obj == null )
141         {
142             updateLogger.warning( LogKeywords.NAMING_BIND +
143                 " unsuccessful because NULL Object cannot be Bound " );
144         throw wrapper.objectIsNull() ;
145         }
146     // doBind implements all four flavors of binding
147
NamingContextDataStore impl = (NamingContextDataStore)this;
148     doBind(impl,n,obj,false,BindingType.nobject);
149         if( updateLogger.isLoggable( Level.FINE ) ) {
150             // isLoggable call to make sure that we save some precious
151
// processor cycles, if there is no need to log.
152
updateLogger.fine( LogKeywords.NAMING_BIND_SUCCESS + " Name = " +
153                 NamingUtils.getDirectoryStructuredName( n ) );
154         }
155     }
156
157   
158     /**
159      * Bind a NamingContext under a name in this NamingContext. If the name
160      * contains multiple (n) components, n-1 will be resolved in this
161      * NamingContext and the object bound in resulting NamingContext.
162      * An exception is thrown if a binding with the supplied name already
163      * exists. The NamingContext will participate in recursive resolving.
164      * @param n a sequence of NameComponents which is the name under which
165      * the object will be bound.
166      * @param obj the NamingContect object reference to be bound.
167      * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with
168      * multiple components was supplied, but the first component could not be
169      * resolved.
170      * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could
171      * not proceed in resolving the n-1 components of the supplied name.
172      * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The
173      * supplied name is invalid (i.e., has length less than 1).
174      * @exception org.omg.CosNaming.NamingContextPackage.AlreadyBound An object
175      * is already bound under the supplied name.
176      * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
177      * system exceptions.
178      * @see doBind
179      */

180     public void bind_context(NameComponent JavaDoc[] n, NamingContext JavaDoc nc)
181     throws org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc,
182            org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc,
183            org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc,
184            org.omg.CosNaming.NamingContextPackage.AlreadyBound JavaDoc
185     {
186         if( nc == null ) {
187             updateLogger.warning( LogKeywords.NAMING_BIND_FAILURE +
188                 " NULL Context cannot be Bound " );
189             throw new BAD_PARAM JavaDoc( "Naming Context should not be null " );
190         }
191     // doBind implements all four flavors of binding
192
NamingContextDataStore impl = (NamingContextDataStore)this;
193     doBind(impl,n,nc,false,BindingType.ncontext);
194         if( updateLogger.isLoggable( Level.FINE ) ) {
195             // isLoggable call to make sure that we save some precious
196
// processor cycles, if there is no need to log.
197
updateLogger.fine( LogKeywords.NAMING_BIND_SUCCESS + " Name = " +
198                 NamingUtils.getDirectoryStructuredName( n ) );
199         }
200     }
201   
202     /**
203      * Bind an object under a name in this NamingContext. If the name
204      * contains multiple (n) components, n-1 will be resolved in this
205      * NamingContext and the object bound in resulting NamingContext.
206      * If a binding under the supplied name already exists it will be
207      * unbound first. If the
208      * object to be bound is a NamingContext it will not participate in
209      * a recursive resolve.
210      * @param n a sequence of NameComponents which is the name under which
211      * the object will be bound.
212      * @param obj the object reference to be bound.
213      * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with
214      * multiple components was supplied, but the first component could not be
215      * resolved.
216      * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not
217      * proceed in resolving the n-1 components of the supplied name.
218      * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The
219      * supplied name is invalid (i.e., has length less than 1).
220      * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
221      * system exceptions.
222      * @see doBind
223      */

224     public void rebind(NameComponent JavaDoc[] n, org.omg.CORBA.Object JavaDoc obj)
225     throws org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc,
226              org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc,
227              org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
228     {
229     if( obj == null )
230         {
231             updateLogger.warning( LogKeywords.NAMING_REBIND_FAILURE +
232                 " NULL Object cannot be Bound " );
233         throw wrapper.objectIsNull() ;
234         }
235     try {
236         // doBind implements all four flavors of binding
237
NamingContextDataStore impl = (NamingContextDataStore)this;
238         doBind(impl,n,obj,true,BindingType.nobject);
239     } catch (org.omg.CosNaming.NamingContextPackage.AlreadyBound JavaDoc ex) {
240             updateLogger.warning( LogKeywords.NAMING_REBIND_FAILURE +
241                 NamingUtils.getDirectoryStructuredName( n ) +
242                 " is already bound to a Naming Context" );
243         // This should not happen
244
throw wrapper.namingCtxRebindAlreadyBound( ex ) ;
245     }
246         if( updateLogger.isLoggable( Level.FINE ) ) {
247             // isLoggable call to make sure that we save some precious
248
// processor cycles, if there is no need to log.
249
updateLogger.fine( LogKeywords.NAMING_REBIND_SUCCESS + " Name = " +
250                 NamingUtils.getDirectoryStructuredName( n ) );
251         }
252     }
253
254     /**
255      * Bind a NamingContext under a name in this NamingContext. If the name
256      * contains multiple (n) components, the first n-1 components will be
257      * resolved in this NamingContext and the object bound in resulting
258      * NamingContext. If a binding under the supplied name already exists it
259      * will be unbound first. The NamingContext will participate in recursive
260      * resolving.
261      * @param n a sequence of NameComponents which is the name under which
262      * the object will be bound.
263      * @param obj the object reference to be bound.
264      * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with
265      * multiple components was supplied, but the first component could not be
266      * resolved.
267      * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not
268      * proceed in resolving the n-1 components of the supplied name.
269      * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The
270      * supplied name is invalid (i.e., has length less than 1).
271      * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
272      * system exceptions.
273      * @see doBind
274      */

275     public void rebind_context(NameComponent JavaDoc[] n, NamingContext JavaDoc nc)
276     throws org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc,
277            org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc,
278            org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
279     {
280     if( nc == null )
281         {
282             updateLogger.warning( LogKeywords.NAMING_REBIND_FAILURE +
283                 " NULL Context cannot be Bound " );
284         throw wrapper.objectIsNull() ;
285         }
286     try {
287         // doBind implements all four flavors of binding
288
NamingContextDataStore impl = (NamingContextDataStore)this;
289         doBind(impl,n,nc,true,BindingType.ncontext);
290     } catch (org.omg.CosNaming.NamingContextPackage.AlreadyBound JavaDoc ex) {
291         // This should not happen
292
updateLogger.warning( LogKeywords.NAMING_REBIND_FAILURE +
293                 NamingUtils.getDirectoryStructuredName( n ) +
294                 " is already bound to a CORBA Object" );
295         throw wrapper.namingCtxRebindctxAlreadyBound( ex ) ;
296     }
297         if( updateLogger.isLoggable( Level.FINE ) ) {
298             // isLoggable call to make sure that we save some precious
299
// processor cycles, if there is no need to log.
300
updateLogger.fine( LogKeywords.NAMING_REBIND_SUCCESS + " Name = " +
301                 NamingUtils.getDirectoryStructuredName( n ) );
302         }
303     }
304
305     /**
306      * Resolve a name in this NamingContext and return the object reference
307      * bound to the name. If the name contains multiple (n) components,
308      * the first component will be resolved in this NamingContext and the
309      * remaining components resolved in the resulting NamingContext, provided
310      * that the NamingContext bound to the first component of the name was
311      * bound with bind_context().
312      * @param n a sequence of NameComponents which is the name to be resolved.
313      * @return the object reference bound under the supplied name.
314      * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with
315      * multiple components was supplied, but the first component could not be
316      * resolved.
317      * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not
318      * proceed in resolving the n-1 components of the supplied name.
319      * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The
320      * supplied name is invalid (i.e., has length less than 1).
321      * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
322      * system exceptions.
323      * @see doResolve
324      */

325     public org.omg.CORBA.Object JavaDoc resolve(NameComponent JavaDoc[] n)
326     throws org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc,
327            org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc,
328            org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
329     {
330     // doResolve actually resolves
331
NamingContextDataStore impl = (NamingContextDataStore)this;
332     org.omg.CORBA.Object JavaDoc obj = doResolve(impl,n);
333         if( obj != null ) {
334             if( readLogger.isLoggable( Level.FINE ) ) {
335                  readLogger.fine( LogKeywords.NAMING_RESOLVE_SUCCESS +
336                  " Name: " + NamingUtils.getDirectoryStructuredName( n ) );
337             }
338         } else {
339              readLogger.warning( LogKeywords.NAMING_RESOLVE_FAILURE +
340                  " Name: " + NamingUtils.getDirectoryStructuredName( n ) );
341         }
342         return obj;
343     }
344             
345
346     /**
347      * Remove a binding from this NamingContext. If the name contains
348      * multiple (n) components, the first n-1 components will be resolved
349      * from this NamingContext and the final component unbound in
350      * the resulting NamingContext.
351      * @param n a sequence of NameComponents which is the name to be unbound.
352      * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with
353      * multiple components was supplied, but the first component could not be
354      * resolved.
355      * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not
356      * proceed in resolving the n-1 components of the supplied name.
357      * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The
358      * supplied name is invalid (i.e., has length less than 1).
359      * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
360      * system exceptions.
361      * @see doUnbind
362      */

363     public void unbind(NameComponent JavaDoc[] n)
364     throws org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc,
365            org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc,
366            org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
367     {
368     // doUnbind actually unbinds
369
NamingContextDataStore impl = (NamingContextDataStore)this;
370     doUnbind(impl,n);
371         if( updateLogger.isLoggable( Level.FINE ) ) {
372             // isLoggable call to make sure that we save some precious
373
// processor cycles, if there is no need to log.
374
updateLogger.fine( LogKeywords.NAMING_UNBIND_SUCCESS +
375                 " Name: " + NamingUtils.getDirectoryStructuredName( n ) );
376         }
377     }
378
379     /**
380      * List the contents of this NamingContest. A sequence of bindings
381      * is returned (a BindingList) containing up to the number of requested
382      * bindings, and a BindingIterator object reference is returned for
383      * iterating over the remaining bindings.
384      * @param how_many The number of requested bindings in the BindingList.
385      * @param bl The BindingList as an out parameter.
386      * @param bi The BindingIterator as an out parameter.
387      * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
388      * system exceptions.
389      * @see BindingListHolder
390      * @see BindingIteratorImpl
391      */

392     public void list(int how_many, BindingListHolder JavaDoc bl,
393         BindingIteratorHolder JavaDoc bi)
394     {
395     // List actually generates the list
396
NamingContextDataStore impl = (NamingContextDataStore)this;
397     synchronized (impl) {
398         impl.List(how_many,bl,bi);
399     }
400         if( readLogger.isLoggable( Level.FINE ) && (bl.value != null )) {
401             // isLoggable call to make sure that we save some precious
402
// processor cycles, if there is no need to log.
403
readLogger.fine ( LogKeywords.NAMING_LIST_SUCCESS +
404                 "list(" + how_many + ") -> bindings[" + bl.value.length +
405             "] + iterator: " + bi.value);
406         }
407     }
408
409     /**
410      * Create a NamingContext object and return its object reference.
411      * @return an object reference for a new NamingContext object implemented
412      * by this Name Server.
413      * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
414      * system exceptions.
415      */

416     public synchronized NamingContext JavaDoc new_context()
417     {
418     // Create actually creates a new naming context
419
lifecycleLogger.fine( "Creating New Naming Context " );
420     NamingContextDataStore impl = (NamingContextDataStore)this;
421     synchronized (impl) {
422             NamingContext JavaDoc nctx = impl.NewContext();
423             if( nctx != null ) {
424                 lifecycleLogger.fine( LogKeywords.LIFECYCLE_CREATE_SUCCESS );
425             } else {
426                 // If naming context is null, then that must be a serious
427
// error.
428
lifecycleLogger.severe ( LogKeywords.LIFECYCLE_CREATE_FAILURE );
429             }
430             return nctx;
431     }
432     }
433
434     /**
435      * Create a new NamingContext, bind it in this Naming Context and return
436      * its object reference. This is equivalent to using new_context() followed
437      * by bind_context() with the supplied name and the object reference for
438      * the newly created NamingContext.
439      * @param n a sequence of NameComponents which is the name to be unbound.
440      * @return an object reference for a new NamingContext object implemented
441      * by this Name Server, bound to the supplied name.
442      * @exception org.omg.CosNaming.NamingContextPackage.AlreadyBound An object
443      * is already bound under the supplied name.
444      * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with
445      * multiple components was supplied, but the first component could not be
446      * resolved.
447      * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not
448      * proceed in resolving the n-1 components of the supplied name.
449      * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The
450      * supplied name is invalid (i.e., has length less than 1).
451      * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
452      * system exceptions.
453      * @see new_context
454      * @see bind_context
455      */

456     public NamingContext JavaDoc bind_new_context(NameComponent JavaDoc[] n)
457     throws org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc,
458            org.omg.CosNaming.NamingContextPackage.AlreadyBound JavaDoc,
459            org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc,
460            org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
461     {
462     NamingContext JavaDoc nc = null;
463     NamingContext JavaDoc rnc = null;
464     try {
465         if (debug)
466         dprint("bind_new_context " + nameToString(n));
467         // The obvious solution:
468
nc = this.new_context();
469         this.bind_context(n,nc);
470         rnc = nc;
471         nc = null;
472     } finally {
473         try {
474         if(nc != null)
475             nc.destroy();
476         } catch (org.omg.CosNaming.NamingContextPackage.NotEmpty JavaDoc e) {
477         }
478     }
479         if( updateLogger.isLoggable( Level.FINE ) ) {
480             // isLoggable call to make sure that we save some precious
481
// processor cycles, if there is no need to log.
482
updateLogger.fine ( LogKeywords.NAMING_BIND +
483                 "New Context Bound To " +
484                 NamingUtils.getDirectoryStructuredName( n ) );
485         }
486     return rnc;
487     }
488
489     /**
490      * Destroy this NamingContext object. If this NamingContext contains
491      * no bindings, the NamingContext is deleted.
492      * @exception org.omg.CosNaming.NamingContextPackage.NotEmpty This
493      * NamingContext is not empty (i.e., contains bindings).
494      * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
495      * system exceptions.
496      */

497     public void destroy()
498     throws org.omg.CosNaming.NamingContextPackage.NotEmpty JavaDoc
499     {
500         lifecycleLogger.fine( "Destroying Naming Context " );
501     NamingContextDataStore impl = (NamingContextDataStore)this;
502     synchronized (impl) {
503         if (impl.IsEmpty() == true) {
504         // The context is empty so it can be destroyed
505
impl.Destroy();
506                 lifecycleLogger.fine ( LogKeywords.LIFECYCLE_DESTROY_SUCCESS );
507             }
508         else {
509         // This context is not empty!
510
// Not a fatal error, warning should do.
511
lifecycleLogger.warning( LogKeywords.LIFECYCLE_DESTROY_FAILURE +
512                     " NamingContext children are not destroyed still.." );
513         throw new NotEmpty();
514             }
515     }
516     }
517
518     /**
519      * Implements all four flavors of binding. It uses Resolve() to
520      * check if a binding already exists (for bind and bind_context), and
521      * unbind() to ensure that a binding does not already exist.
522      * If the length of the name is 1, then Bind() is called with
523      * the name and the object to bind. Otherwise, the first component
524      * of the name is resolved in this NamingContext and the appropriate
525      * form of bind passed to the resulting NamingContext.
526      * This method is static for maximal reuse - even for extended naming
527      * context implementations where the recursive semantics still apply.
528      * @param impl an implementation of NamingContextDataStore
529      * @param n a sequence of NameComponents which is the name under which
530      * the object will be bound.
531      * @param obj the object reference to be bound.
532      * @param rebind Replace an existing binding or not.
533      * @param bt Type of binding (as object or as context).
534      * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with
535      * multiple components was supplied, but the first component could not be
536      * resolved.
537      * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not * proceed
538      * in resolving the first component of the supplied name.
539      * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The
540      * supplied name is invalid (i.e., has length less than 1).
541      * @exception org.omg.CosNaming.NamingContextPackage.AlreadyBound An object
542      * is already bound under the supplied name.
543      * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
544      * system exceptions.
545      * @see resolve
546      * @see unbind
547      * @see bind
548      * @see bind_context
549      * @see rebind
550      * @see rebind_context
551      */

552     public static void doBind(NamingContextDataStore impl,
553                   NameComponent JavaDoc[] n,
554                   org.omg.CORBA.Object JavaDoc obj,
555                   boolean rebind,
556                   org.omg.CosNaming.BindingType JavaDoc bt)
557     throws org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc,
558            org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc,
559            org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc,
560            org.omg.CosNaming.NamingContextPackage.AlreadyBound JavaDoc
561     {
562     // Valid name?
563
if (n.length < 1)
564         throw new InvalidName();
565     
566     // At bottom level?
567
if (n.length == 1) {
568         // The identifier must be set
569
if ( (n[0].id.length() == 0) && (n[0].kind.length() == 0 ) ) {
570         throw new InvalidName();
571             }
572
573         // Ensure synchronization of backend
574
synchronized (impl) {
575         // Yes: bind object in this context under the name
576
BindingTypeHolder JavaDoc bth = new BindingTypeHolder JavaDoc();
577         if (rebind) {
578                     org.omg.CORBA.Object JavaDoc objRef = impl.Resolve( n[0], bth );
579                     if( objRef != null ) {
580                         // Refer Naming Service Doc:00-11-01 section 2.2.3.4
581
// If there is an object already bound with the name
582
// and the binding type is not ncontext a NotFound
583
// Exception with a reason of not a context has to be
584
// raised.
585
// Fix for bug Id: 4384628
586
if ( bth.value.value() == BindingType.nobject.value() ){
587                             if ( bt.value() == BindingType.ncontext.value() ) {
588                                 throw new NotFound JavaDoc(
589                                     NotFoundReason.not_context, n);
590                             }
591                         } else {
592                             // Previously a Context was bound and now trying to
593
// bind Object. It is invalid.
594
if ( bt.value() == BindingType.nobject.value() ) {
595                                 throw new NotFound JavaDoc(
596                                     NotFoundReason.not_object, n);
597                             }
598                         }
599                         impl.Unbind(n[0]);
600                     }
601
602         } else {
603             if (impl.Resolve(n[0],bth) != null)
604             // "Resistence is futile." [Borg pickup line]
605
throw new AlreadyBound();
606         }
607     
608         // Now there are no other bindings under this name
609
impl.Bind(n[0],obj,bt);
610         }
611     } else {
612         // No: bind in a different context
613
NamingContext JavaDoc context = resolveFirstAsContext(impl,n);
614       
615         // Compute tail
616
NameComponent JavaDoc[] tail = new NameComponent JavaDoc[n.length - 1];
617         System.arraycopy(n,1,tail,0,n.length-1);
618
619       // How should we propagate the bind
620
switch (bt.value()) {
621         case BindingType._nobject:
622         {
623             // Bind as object
624
if (rebind)
625             context.rebind(tail,obj);
626             else
627             context.bind(tail,obj);
628         }
629         break;
630         case BindingType._ncontext:
631         {
632             // Narrow to a naming context using Java casts. It must
633
// work.
634
NamingContext JavaDoc objContext = (NamingContext JavaDoc)obj;
635             // Bind as context
636
if (rebind)
637             context.rebind_context(tail,objContext);
638             else
639             context.bind_context(tail,objContext);
640         }
641         break;
642         default:
643         // This should not happen
644
throw staticWrapper.namingCtxBadBindingtype() ;
645         }
646     }
647     }
648
649     /**
650    * Implements resolving names in this NamingContext. The first component
651    * of the supplied name is resolved in this NamingContext by calling
652    * Resolve(). If there are no more components in the name, the
653    * resulting object reference is returned. Otherwise, the resulting object
654    * reference must have been bound as a context and be narrowable to
655    * a NamingContext. If this is the case, the remaining
656    * components of the name is resolved in the resulting NamingContext.
657    * This method is static for maximal reuse - even for extended naming
658    * context implementations where the recursive semantics still apply.
659    * @param impl an implementation of NamingContextDataStore
660    * @param n a sequence of NameComponents which is the name to be resolved.
661    * @return the object reference bound under the supplied name.
662    * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with
663    * multiple components was supplied, but the first component could not be
664    * resolved.
665    * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not
666    * proceed
667    * in resolving the first component of the supplied name.
668    * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The supplied
669    * name is invalid (i.e., has length less than 1).
670    * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA system
671    * exceptions.
672    * @see resolve
673    */

674     public static org.omg.CORBA.Object JavaDoc doResolve(NamingContextDataStore impl,
675                          NameComponent JavaDoc[] n)
676     throws org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc,
677            org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc,
678            org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
679     {
680     org.omg.CORBA.Object JavaDoc obj = null;
681     BindingTypeHolder JavaDoc bth = new BindingTypeHolder JavaDoc();
682     
683             
684     // Length must be greater than 0
685
if (n.length < 1)
686         throw new InvalidName();
687
688     // The identifier must be set
689
if (n.length == 1) {
690         synchronized (impl) {
691         // Resolve first level in this context
692
obj = impl.Resolve(n[0],bth);
693         }
694         if (obj == null) {
695         // Object was not found
696
throw new NotFound JavaDoc(NotFoundReason.missing_node,n);
697         }
698         return obj;
699     } else {
700         // n.length > 1
701
if ( (n[1].id.length() == 0) && (n[1].kind.length() == 0) ) {
702         throw new InvalidName();
703             }
704
705         NamingContext JavaDoc context = resolveFirstAsContext(impl,n);
706       
707         // Compute restOfName = name[1..length]
708
NameComponent JavaDoc[] tail = new NameComponent JavaDoc[n.length -1];
709         System.arraycopy(n,1,tail,0,n.length-1);
710
711             // Resolve rest of name in context
712
try {
713                 // First try to resolve using the local call, this should work
714
// most of the time unless there are federated naming contexts.
715
Servant JavaDoc servant = impl.getNSPOA().reference_to_servant(
716                     context );
717                 return doResolve(((NamingContextDataStore)servant), tail) ;
718             } catch( Exception JavaDoc e ) {
719                 return context.resolve(tail);
720             }
721         }
722     }
723
724     /**
725    * Implements unbinding bound names in this NamingContext. If the
726    * name contains only one component, the name is unbound in this
727    * NamingContext using Unbind(). Otherwise, the first component
728    * of the name is resolved in this NamingContext and
729    * unbind passed to the resulting NamingContext.
730    * This method is static for maximal reuse - even for extended naming
731    * context implementations where the recursive semantics still apply.
732    * @param impl an implementation of NamingContextDataStore
733    * @param n a sequence of NameComponents which is the name to be unbound.
734    * @exception org.omg.CosNaming.NamingContextPackage.NotFound A name with multiple
735    * components was supplied, but the first component could not be
736    * resolved.
737    * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not proceed
738    * in resolving the n-1 components of the supplied name.
739    * @exception org.omg.CosNaming.NamingContextPackage.InvalidName The supplied name
740    * is invalid (i.e., has length less than 1).
741    * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA system exceptions.
742    * @see resolve
743    */

744     public static void doUnbind(NamingContextDataStore impl,
745                 NameComponent JavaDoc[] n)
746     throws org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc,
747            org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc,
748            org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
749     {
750     // Name valid?
751
if (n.length < 1)
752         throw new InvalidName();
753
754     // Unbind here?
755
if (n.length == 1) {
756         // The identifier must be set
757
if ( (n[0].id.length() == 0) && (n[0].kind.length() == 0 ) ) {
758         throw new InvalidName();
759             }
760
761         org.omg.CORBA.Object JavaDoc objRef = null;
762         synchronized (impl) {
763         // Yes: unbind in this context
764
objRef = impl.Unbind(n[0]);
765         }
766       
767         if (objRef == null)
768         // It was not bound
769
throw new NotFound JavaDoc(NotFoundReason.missing_node,n);
770         // Done
771
return;
772     } else {
773         // No: unbind in a different context
774

775       // Resolve first - must be resolveable
776
NamingContext JavaDoc context = resolveFirstAsContext(impl,n);
777       
778         // Compute tail
779
NameComponent JavaDoc[] tail = new NameComponent JavaDoc[n.length - 1];
780         System.arraycopy(n,1,tail,0,n.length-1);
781
782       // Propagate unbind to this context
783
context.unbind(tail);
784     }
785     }
786
787     /**
788    * Implements resolving a NameComponent in this context and
789    * narrowing it to CosNaming::NamingContext. It will throw appropriate
790    * exceptions if not found or not narrowable.
791    * @param impl an implementation of NamingContextDataStore
792    * @param n a NameComponents which is the name to be found.
793    * @exception org.omg.CosNaming.NamingContextPackage.NotFound The
794    * first component could not be resolved.
795    * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed Could not proceed
796    * in resolving the first component of the supplied name.
797    * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA system exceptions.
798    * @see resolve
799    */

800     protected static NamingContext JavaDoc resolveFirstAsContext(NamingContextDataStore impl,
801                              NameComponent JavaDoc[] n)
802     throws org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc {
803     org.omg.CORBA.Object JavaDoc topRef = null;
804     BindingTypeHolder JavaDoc bth = new BindingTypeHolder JavaDoc();
805     NamingContext JavaDoc context = null;
806     
807     synchronized (impl) {
808         // Resolve first - must be resolveable
809
topRef = impl.Resolve(n[0],bth);
810         if (topRef == null) {
811         // It was not bound
812
throw new NotFound JavaDoc(NotFoundReason.missing_node,n);
813         }
814     }
815       
816     // Was it bound as a context?
817
if (bth.value != BindingType.ncontext) {
818         // It was not a context
819
throw new NotFound JavaDoc(NotFoundReason.not_context,n);
820     }
821       
822     // Narrow to a naming context
823
try {
824         context = NamingContextHelper.narrow(topRef);
825     } catch (org.omg.CORBA.BAD_PARAM JavaDoc ex) {
826         // It was not a context
827
throw new NotFound JavaDoc(NotFoundReason.not_context,n);
828     }
829
830     // Hmm. must be ok
831
return context;
832     }
833
834
835    /**
836     * This operation creates a stringified name from the array of Name
837     * components.
838     * @param n Name of the object <p>
839     * @exception org.omg.CosNaming.NamingContextExtPackage.InvalidName
840     * Indicates the name does not identify a binding.<p>
841     *
842     */

843     public String JavaDoc to_string(org.omg.CosNaming.NameComponent JavaDoc[] n)
844      throws org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
845     {
846     // Name valid?
847
if ( (n == null ) || (n.length == 0) )
848     {
849         throw new InvalidName();
850     }
851     NamingContextDataStore impl = (NamingContextDataStore)this;
852
853         String JavaDoc theStringifiedName = insImpl.convertToString( n );
854
855     if( theStringifiedName == null )
856     {
857         throw new InvalidName();
858     }
859     
860     return theStringifiedName;
861     }
862
863
864    /**
865     * This operation converts a Stringified Name into an equivalent array
866     * of Name Components.
867     * @param sn Stringified Name of the object <p>
868     * @exception org.omg.CosNaming.NamingContextExtPackage.InvalidName
869     * Indicates the name does not identify a binding.<p>
870     *
871     */

872     public org.omg.CosNaming.NameComponent JavaDoc[] to_name(String JavaDoc sn)
873      throws org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
874     {
875     // Name valid?
876
if ( (sn == null ) || (sn.length() == 0) )
877     {
878         throw new InvalidName();
879     }
880     NamingContextDataStore impl = (NamingContextDataStore)this;
881     org.omg.CosNaming.NameComponent JavaDoc[] theNameComponents =
882         insImpl.convertToNameComponent( sn );
883     if( ( theNameComponents == null ) || (theNameComponents.length == 0 ) )
884     {
885         throw new InvalidName();
886     }
887         for( int i = 0; i < theNameComponents.length; i++ ) {
888             // If there is a name component whose id and kind null or
889
// zero length string, then an invalid name exception needs to be
890
// raised.
891
if ( ( ( theNameComponents[i].id == null )
892                  ||( theNameComponents[i].id.length() == 0 ) )
893                &&( ( theNameComponents[i].kind == null )
894                  ||( theNameComponents[i].kind.length() == 0 ) ) ) {
895                 throw new InvalidName();
896             }
897         }
898     return theNameComponents;
899     }
900
901    /**
902     * This operation creates a URL based "iiopname://" format name
903     * from the Stringified Name of the object.
904     * @param addr internet based address of the host machine where
905     * Name Service is running <p>
906     * @param sn Stringified Name of the object <p>
907     * @exception org.omg.CosNaming.NamingContextExtPackage.InvalidName
908     * Indicates the name does not identify a binding.<p>
909     * @exception org.omg.CosNaming.NamingContextPackage.InvalidAddress
910     * Indicates the internet based address of the host machine is
911     * incorrect <p>
912     *
913     */

914
915     public String JavaDoc to_url(String JavaDoc addr, String JavaDoc sn)
916         throws org.omg.CosNaming.NamingContextExtPackage.InvalidAddress JavaDoc,
917                org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
918     {
919     // Name valid?
920
if ( (sn == null ) || (sn.length() == 0) )
921     {
922         throw new InvalidName();
923     }
924     if( addr == null )
925     {
926         throw new
927                 org.omg.CosNaming.NamingContextExtPackage.InvalidAddress JavaDoc();
928     }
929     NamingContextDataStore impl = (NamingContextDataStore)this;
930     String JavaDoc urlBasedAddress = null;
931     urlBasedAddress = insImpl.createURLBasedAddress( addr, sn );
932         // Extra check to see that corba name url created is valid as per
933
// INS spec grammer.
934
try {
935             INSURLHandler.getINSURLHandler( ).parseURL( urlBasedAddress );
936         } catch( BAD_PARAM JavaDoc e ) {
937             throw new
938                 org.omg.CosNaming.NamingContextExtPackage.InvalidAddress JavaDoc();
939         }
940     return urlBasedAddress;
941     }
942
943     /**
944      * This operation resolves the Stringified name into the object
945      * reference.
946      * @param sn Stringified Name of the object <p>
947      * @exception org.omg.CosNaming.NamingContextPackage.NotFound
948      * Indicates there is no object reference for the given name. <p>
949      * @exception org.omg.CosNaming.NamingContextPackage.CannotProceed
950      * Indicates that the given compound name is incorrect <p>
951      * @exception org.omg.CosNaming.NamingContextExtPackage.InvalidName
952      * Indicates the name does not identify a binding.<p>
953      * @exception org.omg.CosNaming.NamingContextPackage.AlreadyBound
954      * Indicates the name is already bound.<p>
955      *
956      */

957     public org.omg.CORBA.Object JavaDoc resolve_str(String JavaDoc sn)
958         throws org.omg.CosNaming.NamingContextPackage.NotFound JavaDoc,
959                org.omg.CosNaming.NamingContextPackage.CannotProceed JavaDoc,
960                org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
961     {
962     org.omg.CORBA.Object JavaDoc theObject = null;
963     // Name valid?
964
if ( (sn == null ) || (sn.length() == 0) )
965     {
966         throw new InvalidName();
967     }
968     NamingContextDataStore impl = (NamingContextDataStore)this;
969     org.omg.CosNaming.NameComponent JavaDoc[] theNameComponents =
970         insImpl.convertToNameComponent( sn );
971
972     if( ( theNameComponents == null ) || (theNameComponents.length == 0 ) )
973     {
974         throw new InvalidName();
975     }
976     theObject = resolve( theNameComponents );
977     return theObject;
978     }
979
980
981     transient protected ORB orb;
982   
983     public static String JavaDoc nameToString(NameComponent JavaDoc[] name)
984     {
985     StringBuffer JavaDoc s = new StringBuffer JavaDoc("{");
986     if (name != null || name.length > 0) {
987         for (int i=0;i<name.length;i++) {
988         if (i>0)
989             s.append(",");
990         s.append("[").
991             append(name[i].id).
992             append(",").
993             append(name[i].kind).
994             append("]");
995         }
996     }
997     s.append("}");
998     return s.toString();
999     }
1000
1001    // Debugging aids.
1002
public static final boolean debug = false;
1003
1004    private static void dprint(String JavaDoc msg) {
1005    NamingUtils.dprint("NamingContextImpl(" +
1006               Thread.currentThread().getName() + " at " +
1007               System.currentTimeMillis() +
1008               " ems): " + msg);
1009    }
1010}
1011
Popular Tags