KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)TransientNamingContext.java 1.53 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 // Import general CORBA classes
11
import org.omg.CORBA.SystemException JavaDoc;
12 import org.omg.CORBA.Object JavaDoc;
13 import org.omg.CORBA.INTERNAL JavaDoc;
14 import org.omg.CORBA.CompletionStatus JavaDoc;
15 import org.omg.CORBA.ORB JavaDoc;
16 import org.omg.PortableServer.POA JavaDoc;
17
18 // Import org.omg.CosNaming types
19
import org.omg.CosNaming.Binding JavaDoc;
20 import org.omg.CosNaming.BindingType JavaDoc;
21 import org.omg.CosNaming.BindingTypeHolder JavaDoc;
22 import org.omg.CosNaming.BindingListHolder JavaDoc;
23 import org.omg.CosNaming.BindingIteratorHolder JavaDoc;
24 import org.omg.CosNaming.NameComponent JavaDoc;
25 import org.omg.CosNaming.NamingContext JavaDoc;
26
27 import java.util.logging.Logger JavaDoc;
28 import java.util.logging.Level JavaDoc;
29
30 import java.util.Hashtable JavaDoc;
31 import com.sun.corba.se.impl.orbutil.LogKeywords;
32 import com.sun.corba.se.impl.logging.NamingSystemException;
33 import com.sun.corba.se.spi.logging.CORBALogDomains;
34
35 /**
36  * Class TransientNamingContext implements the methods defined
37  * by NamingContextDataStore, and extends the NamingContextImpl class to
38  * provide a servant implementation of CosNaming::NamingContext.
39  * The TransientNamingContext uses a hash table
40  * to store the mappings between bindings and object references and the
41  * hash table is not persistent; thereby the name "transient".
42  * This class should not be used directly; instead, the class
43  * TransientNameService should be instantiated.
44  * <p>
45  * The keys in the hash table are InternalBindingKey objects, containing
46  * a single NameComponent and implementing the proper functions, i.e.,
47  * equals() and hashCode() in an efficient manner. The values in the hash
48  * table are InternalBindingValues and store a org.omg.CosNaming::Binding and
49  * the object reference associated with the binding. For iteration,
50  * TransientBindingIterator objects are created, which are passed a cloned
51  * copy of the hashtable. Since elements are inserted and deleted and
52  * never modified, this provides stable iterators at the cost of cloning
53  * the hash table.
54  * <p>
55  * To create and destroy object references, the TransientNamingContext
56  * uses the orb.connect() and orb.disconnect() methods.
57  *
58  * @see NamingContextImpl
59  * @see NamingContextDataStore
60  * @see TransientBindingIterator
61  * @see TransientNameService
62  */

63 public class TransientNamingContext extends NamingContextImpl implements NamingContextDataStore
64 {
65     private Logger JavaDoc readLogger, updateLogger, lifecycleLogger;
66
67     // XXX: the wrapper calls are all preceded by logger updates.
68
// These can be combined, and then we simply use 3 NamingSystemException wrappers,
69
// for read, update, and lifecycl.
70
private NamingSystemException wrapper ;
71
72     /**
73      * Constructs a new TransientNamingContext object.
74      * @param orb an orb object.
75      * @param initial the initial naming context.
76      * @exception Exception a Java exception thrown of the base class cannot
77      * initialize.
78      */

79     public TransientNamingContext(com.sun.corba.se.spi.orb.ORB orb,
80     org.omg.CORBA.Object JavaDoc initial,
81         POA JavaDoc nsPOA )
82     throws java.lang.Exception JavaDoc
83     {
84     super(orb, nsPOA );
85     wrapper = NamingSystemException.get( orb, CORBALogDomains.NAMING ) ;
86
87     this.localRoot = initial;
88         readLogger = orb.getLogger( CORBALogDomains.NAMING_READ);
89         updateLogger = orb.getLogger( CORBALogDomains.NAMING_UPDATE);
90         lifecycleLogger = orb.getLogger(
91             CORBALogDomains.NAMING_LIFECYCLE);
92         lifecycleLogger.fine( "Root TransientNamingContext LIFECYCLE.CREATED" );
93     }
94
95     /**
96      * Binds the object to the name component as the specified binding type.
97      * It creates a InternalBindingKey object and a InternalBindingValue
98      * object and inserts them in the hash table.
99      * @param n A single org.omg.CosNaming::NameComponent under which the
100      * object will be bound.
101      * @param obj An object reference to be bound under the supplied name.
102      * @param bt The type of the binding (i.e., as object or as context).
103      * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
104      * system exceptions.
105      */

106     public final void Bind(NameComponent JavaDoc n, org.omg.CORBA.Object JavaDoc obj,
107                BindingType JavaDoc bt)
108     throws org.omg.CORBA.SystemException JavaDoc
109     {
110     // Create a key and a value
111
InternalBindingKey key = new InternalBindingKey(n);
112     NameComponent JavaDoc[] name = new NameComponent JavaDoc[1];
113     name[0] = n;
114     Binding JavaDoc b = new Binding JavaDoc(name,bt);
115     InternalBindingValue value = new InternalBindingValue(b,null);
116     value.theObjectRef = obj;
117     // insert it
118
InternalBindingValue oldValue =
119         (InternalBindingValue)this.theHashtable.put(key,value);
120
121     if (oldValue != null) {
122             updateLogger.warning( LogKeywords.NAMING_BIND + "Name " +
123                 getName( n ) + " Was Already Bound" );
124         throw wrapper.transNcBindAlreadyBound() ;
125     }
126         if( updateLogger.isLoggable( Level.FINE ) ) {
127             updateLogger.fine( LogKeywords.NAMING_BIND_SUCCESS +
128                 "Name Component: " + n.id + "." + n.kind );
129         }
130     }
131
132     /**
133      * Resolves the supplied name to an object reference and returns
134      * the type of the resolved binding. It creates a InternalBindingKey
135      * and uses the key for looking up in the hash table. If nothing
136      * is found an exception is thrown, otherwise the object reference
137      * is returned and the binding type set.
138      * @param n a NameComponent which is the name to be resolved.
139      * @param bth the BindingType as an out parameter.
140      * @return the object reference bound under the supplied name, null if not
141      * found.
142      * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
143      * system exceptions.
144      */

145     public final org.omg.CORBA.Object JavaDoc Resolve(NameComponent JavaDoc n,
146                           BindingTypeHolder JavaDoc bth)
147     throws org.omg.CORBA.SystemException JavaDoc
148     {
149     // Is the initial naming context requested?
150
if ( (n.id.length() == 0)
151            &&(n.kind.length() == 0 ) )
152         {
153         bth.value = BindingType.ncontext;
154         return localRoot;
155     }
156     
157     // Create a key and lookup the value
158
InternalBindingKey key = new InternalBindingKey(n);
159
160     InternalBindingValue value =
161             (InternalBindingValue) this.theHashtable.get(key);
162     if (value == null) return null;
163         if( readLogger.isLoggable( Level.FINE ) ) {
164             readLogger.fine( LogKeywords.NAMING_RESOLVE_SUCCESS
165                 + "Namecomponent :" + getName( n ) );
166         }
167     
168         // Copy out binding type and object reference
169
bth.value = value.theBinding.binding_type;
170     return value.theObjectRef;
171     }
172
173     /**
174      * Deletes the binding with the supplied name. It creates a
175      * InternalBindingKey and uses it to remove the value associated
176      * with the key. If nothing is found an exception is thrown, otherwise
177      * the element is removed from the hash table.
178      * @param n a NameComponent which is the name to unbind
179      * @return the object reference bound to the name, or null if not found.
180      * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
181      * system exceptions.
182      */

183     public final org.omg.CORBA.Object JavaDoc Unbind(NameComponent JavaDoc n)
184     throws org.omg.CORBA.SystemException JavaDoc
185     {
186     // Create a key and remove it from the hashtable
187
InternalBindingKey key = new InternalBindingKey(n);
188     InternalBindingValue value =
189             (InternalBindingValue)this.theHashtable.remove(key);
190
191     // Return what was found
192
if (value == null) {
193             if( updateLogger.isLoggable( Level.FINE ) ) {
194                 updateLogger.fine( LogKeywords.NAMING_UNBIND_FAILURE +
195                     " There was no binding with the name " + getName( n ) +
196                     " to Unbind " );
197             }
198         return null;
199         } else {
200             if( updateLogger.isLoggable( Level.FINE ) ) {
201                 updateLogger.fine( LogKeywords.NAMING_UNBIND_SUCCESS +
202                     " NameComponent: " + getName( n ) );
203             }
204         return value.theObjectRef;
205        }
206     }
207
208     /**
209      * List the contents of this NamingContext. It creates a new
210      * TransientBindingIterator object and passes it a clone of the
211      * hash table and an orb object. It then uses the
212      * newly created object to return the required number of bindings.
213      * @param how_many The number of requested bindings in the BindingList.
214      * @param bl The BindingList as an out parameter.
215      * @param bi The BindingIterator as an out parameter.
216      * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
217      * system exceptions.
218      */

219     public final void List(int how_many, BindingListHolder JavaDoc bl,
220                BindingIteratorHolder JavaDoc bi)
221     throws org.omg.CORBA.SystemException JavaDoc
222     {
223     try {
224         // Create a new binding iterator servant with a copy of this
225
// hashtable. nsPOA is passed to the object so that it can
226
// de-activate itself from the Active Object Map when
227
// Binding Iterator.destroy is called.
228
TransientBindingIterator bindingIterator =
229         new TransientBindingIterator(this.orb,
230             (Hashtable JavaDoc)this.theHashtable.clone(), nsPOA);
231         // Have it set the binding list
232
bindingIterator.list(how_many,bl);
233             
234             byte[] objectId = nsPOA.activate_object( bindingIterator );
235             org.omg.CORBA.Object JavaDoc obj = nsPOA.id_to_reference( objectId );
236       
237         // Get the object reference for the binding iterator servant
238
org.omg.CosNaming.BindingIterator JavaDoc bindingRef =
239                 org.omg.CosNaming.BindingIteratorHelper.narrow( obj );
240       
241         bi.value = bindingRef;
242     } catch (org.omg.CORBA.SystemException JavaDoc e) {
243             readLogger.warning( LogKeywords.NAMING_LIST_FAILURE + e );
244         throw e;
245     } catch (Exception JavaDoc e) {
246         // Convert to a CORBA system exception
247
readLogger.severe( LogKeywords.NAMING_LIST_FAILURE + e );
248         throw wrapper.transNcListGotExc( e ) ;
249     }
250     }
251   
252     /**
253      * Create a new NamingContext. It creates a new TransientNamingContext
254      * object, passing it the orb object.
255      * @return an object reference for a new NamingContext object implemented
256      * by this Name Server.
257      * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
258      * system exceptions.
259      */

260     public final org.omg.CosNaming.NamingContext JavaDoc NewContext()
261     throws org.omg.CORBA.SystemException JavaDoc
262     {
263     try {
264         // Create a new servant
265
TransientNamingContext transContext =
266         new TransientNamingContext(
267                 (com.sun.corba.se.spi.orb.ORB) orb,localRoot, nsPOA);
268
269             byte[] objectId = nsPOA.activate_object( transContext );
270             org.omg.CORBA.Object JavaDoc obj = nsPOA.id_to_reference( objectId );
271             lifecycleLogger.fine( "TransientNamingContext " +
272                 "LIFECYCLE.CREATE SUCCESSFUL" );
273             return org.omg.CosNaming.NamingContextHelper.narrow( obj );
274
275     } catch (org.omg.CORBA.SystemException JavaDoc e) {
276             lifecycleLogger.log(
277                 Level.WARNING, LogKeywords.LIFECYCLE_CREATE_FAILURE, e );
278         throw e;
279     } catch (Exception JavaDoc e) {
280             lifecycleLogger.log(
281                 Level.WARNING, LogKeywords.LIFECYCLE_CREATE_FAILURE, e );
282         throw wrapper.transNcNewctxGotExc( e ) ;
283     }
284     }
285
286     /**
287      * Destroys this NamingContext by disconnecting from the ORB.
288      * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
289      * system exceptions.
290      */

291     public final void Destroy()
292     throws org.omg.CORBA.SystemException JavaDoc
293     {
294     // Destroy the object reference by disconnecting from the ORB
295
try {
296             byte[] objectId = nsPOA.servant_to_id( this );
297             if( objectId != null ) {
298                 nsPOA.deactivate_object( objectId );
299             }
300             if( lifecycleLogger.isLoggable( Level.FINE ) ) {
301                 lifecycleLogger.fine(
302                     LogKeywords.LIFECYCLE_DESTROY_SUCCESS );
303             }
304     } catch (org.omg.CORBA.SystemException JavaDoc e) {
305             lifecycleLogger.log( Level.WARNING,
306                 LogKeywords.LIFECYCLE_DESTROY_FAILURE, e );
307         throw e;
308     } catch (Exception JavaDoc e) {
309             lifecycleLogger.log( Level.WARNING,
310                 LogKeywords.LIFECYCLE_DESTROY_FAILURE, e );
311         throw wrapper.transNcDestroyGotExc( e ) ;
312     }
313     }
314
315     /**
316      * A Utility Method For Logging..
317      */

318     private String JavaDoc getName( NameComponent JavaDoc n ) {
319         return n.id + "." + n.kind;
320     }
321
322     /**
323      * Return whether this NamingContext contains any bindings. It forwards
324      * this request to the hash table.
325      * @return true if this NamingContext contains no bindings.
326      */

327     public final boolean IsEmpty()
328     {
329     return this.theHashtable.isEmpty();
330     }
331
332     // A hashtable to store the bindings
333
private final Hashtable JavaDoc theHashtable = new Hashtable JavaDoc();
334
335     /**
336      * The local root naming context.
337      */

338     public org.omg.CORBA.Object JavaDoc localRoot;
339 }
340
341
Popular Tags