1. /*
  2. * @(#)TransientNamingContext.java 1.51 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.internal.CosNaming;
  8. // Import general CORBA classes
  9. import org.omg.CORBA.SystemException;
  10. import org.omg.CORBA.Object;
  11. import org.omg.CORBA.INTERNAL;
  12. import org.omg.CORBA.CompletionStatus;
  13. import org.omg.CORBA.ORB;
  14. import org.omg.PortableServer.POA;
  15. // Import org.omg.CosNaming types
  16. import org.omg.CosNaming.Binding;
  17. import org.omg.CosNaming.BindingType;
  18. import org.omg.CosNaming.BindingTypeHolder;
  19. import org.omg.CosNaming.BindingListHolder;
  20. import org.omg.CosNaming.BindingIteratorHolder;
  21. import org.omg.CosNaming.NameComponent;
  22. import org.omg.CosNaming.NamingContext;
  23. // Get base implementation
  24. import com.sun.corba.se.internal.CosNaming.NamingContextImpl;
  25. import com.sun.corba.se.internal.POA.*;
  26. // Get a hash table
  27. import java.util.Hashtable;
  28. // Get a transient iterator
  29. import com.sun.corba.se.internal.CosNaming.TransientBindingIterator;
  30. import com.sun.corba.se.internal.POA.POAORB;
  31. import com.sun.corba.se.internal.CosNaming.NamingContextDataStore;
  32. import com.sun.corba.se.internal.CosNaming.MinorCodes;
  33. /**
  34. * Class TransientNamingContext implements the methods defined
  35. * by NamingContextDataStore, and extends the NamingContextImpl class to
  36. * provide a servant implementation of CosNaming::NamingContext.
  37. * The TransientNamingContext uses a hash table
  38. * to store the mappings between bindings and object references and the
  39. * hash table is not persistent; thereby the name "transient".
  40. * This class should not be used directly; instead, the class
  41. * TransientNameService should be instantiated.
  42. * <p>
  43. * The keys in the hash table are InternalBindingKey objects, containing
  44. * a single NameComponent and implementing the proper functions, i.e.,
  45. * equals() and hashCode() in an efficient manner. The values in the hash
  46. * table are InternalBindingValues and store a org.omg.CosNaming::Binding and
  47. * the object reference associated with the binding. For iteration,
  48. * TransientBindingIterator objects are created, which are passed a cloned
  49. * copy of the hashtable. Since elements are inserted and deleted and
  50. * never modified, this provides stable iterators at the cost of cloning
  51. * the hash table.
  52. * <p>
  53. * To create and destroy object references, the TransientNamingContext
  54. * uses the orb.connect() and orb.disconnect() methods.
  55. *
  56. * @see NamingContextImpl
  57. * @see NamingContextDataStore
  58. * @see TransientBindingIterator
  59. * @see TransientNameService
  60. */
  61. public class TransientNamingContext extends NamingContextImpl implements NamingContextDataStore
  62. {
  63. /**
  64. * Constructs a new TransientNamingContext object.
  65. * @param orb an orb object.
  66. * @param initial the initial naming context.
  67. * @exception Exception a Java exception thrown of the base class cannot
  68. * initialize.
  69. */
  70. public TransientNamingContext(POAORB orb, org.omg.CORBA.Object initial,
  71. POA nsPOA )
  72. throws java.lang.Exception
  73. {
  74. super(orb, nsPOA );
  75. this.localRoot = initial;
  76. }
  77. /**
  78. * Binds the object to the name component as the specified binding type.
  79. * It creates a InternalBindingKey object and a InternalBindingValue
  80. * object and inserts them in the hash table.
  81. * @param n A single org.omg.CosNaming::NameComponent under which the
  82. * object will be bound.
  83. * @param obj An object reference to be bound under the supplied name.
  84. * @param bt The type of the binding (i.e., as object or as context).
  85. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  86. * system exceptions.
  87. */
  88. public final void Bind(NameComponent n, org.omg.CORBA.Object obj,
  89. BindingType bt)
  90. throws org.omg.CORBA.SystemException
  91. {
  92. // Create a key and a value
  93. InternalBindingKey key = new InternalBindingKey(n);
  94. NameComponent[] name = new NameComponent[1];
  95. name[0] = n;
  96. Binding b = new Binding(name,bt);
  97. InternalBindingValue value = new InternalBindingValue(b,null);
  98. value.theObjectRef = obj;
  99. // insert it
  100. InternalBindingValue oldValue =
  101. (InternalBindingValue)this.theHashtable.put(key,value);
  102. if (oldValue != null) {
  103. throw new org.omg.CORBA.INTERNAL(
  104. MinorCodes.TRANS_NC_BIND_ALREADY_BOUND,
  105. CompletionStatus.COMPLETED_NO);
  106. }
  107. }
  108. /**
  109. * Resolves the supplied name to an object reference and returns
  110. * the type of the resolved binding. It creates a InternalBindingKey
  111. * and uses the key for looking up in the hash table. If nothing
  112. * is found an exception is thrown, otherwise the object reference
  113. * is returned and the binding type set.
  114. * @param n a NameComponent which is the name to be resolved.
  115. * @param bth the BindingType as an out parameter.
  116. * @return the object reference bound under the supplied name, null if not
  117. * found.
  118. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  119. * system exceptions.
  120. */
  121. public final org.omg.CORBA.Object Resolve(NameComponent n,
  122. BindingTypeHolder bth)
  123. throws org.omg.CORBA.SystemException
  124. {
  125. // Is the initial naming context requested?
  126. if ( (n.id.length() == 0)
  127. &&(n.kind.length() == 0 ) )
  128. {
  129. bth.value = BindingType.ncontext;
  130. return localRoot;
  131. }
  132. // Create a key and lookup the value
  133. InternalBindingKey key = new InternalBindingKey(n);
  134. InternalBindingValue value =
  135. (InternalBindingValue) this.theHashtable.get(key);
  136. if (value == null) {
  137. return null;
  138. }
  139. // Copy out binding type and object reference
  140. bth.value = value.theBinding.binding_type;
  141. return value.theObjectRef;
  142. }
  143. /**
  144. * Deletes the binding with the supplied name. It creates a
  145. * InternalBindingKey and uses it to remove the value associated
  146. * with the key. If nothing is found an exception is thrown, otherwise
  147. * the element is removed from the hash table.
  148. * @param n a NameComponent which is the name to unbind
  149. * @return the object reference bound to the name, or null if not found.
  150. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  151. * system exceptions.
  152. */
  153. public final org.omg.CORBA.Object Unbind(NameComponent n)
  154. throws org.omg.CORBA.SystemException
  155. {
  156. // Create a key and remove it from the hashtable
  157. InternalBindingKey key = new InternalBindingKey(n);
  158. InternalBindingValue value =
  159. (InternalBindingValue)this.theHashtable.remove(key);
  160. // Return what was found
  161. if (value == null)
  162. return null;
  163. else
  164. return value.theObjectRef;
  165. }
  166. /**
  167. * List the contents of this NamingContext. It creates a new
  168. * TransientBindingIterator object and passes it a clone of the
  169. * hash table and an orb object. It then uses the
  170. * newly created object to return the required number of bindings.
  171. * @param how_many The number of requested bindings in the BindingList.
  172. * @param bl The BindingList as an out parameter.
  173. * @param bi The BindingIterator as an out parameter.
  174. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  175. * system exceptions.
  176. */
  177. public final void List(int how_many, BindingListHolder bl,
  178. BindingIteratorHolder bi)
  179. throws org.omg.CORBA.SystemException
  180. {
  181. try {
  182. // Create a new binding iterator servant with a copy of this
  183. // hashtable. nsPOA is passed to the object so that it can
  184. // de-activate itself from the Active Object Map when
  185. // Binding Iterator.destroy is called.
  186. TransientBindingIterator bindingIterator =
  187. new TransientBindingIterator(this.orb,
  188. (Hashtable)this.theHashtable.clone(), nsPOA);
  189. // Have it set the binding list
  190. bindingIterator.list(how_many,bl);
  191. byte[] objectId = nsPOA.activate_object( bindingIterator );
  192. org.omg.CORBA.Object obj = nsPOA.id_to_reference( objectId );
  193. // Get the object reference for the binding iterator servant
  194. org.omg.CosNaming.BindingIterator bindingRef =
  195. org.omg.CosNaming.BindingIteratorHelper.narrow( obj );
  196. bi.value = bindingRef;
  197. } catch (org.omg.CORBA.SystemException e) {
  198. NamingUtils.errprint("List(): caught System Exception:");
  199. NamingUtils.printException(e);
  200. throw e;
  201. } catch (Exception e) {
  202. // Convert to a CORBA system exception
  203. NamingUtils.errprint("List(): caught exception:");
  204. NamingUtils.printException(e);
  205. throw new org.omg.CORBA.INTERNAL(MinorCodes.TRANS_NC_LIST_GOT_EXC,
  206. CompletionStatus.COMPLETED_NO);
  207. }
  208. }
  209. /**
  210. * Create a new NamingContext. It creates a new TransientNamingContext
  211. * object, passing it the orb object.
  212. * @return an object reference for a new NamingContext object implemented
  213. * by this Name Server.
  214. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  215. * system exceptions.
  216. */
  217. public final org.omg.CosNaming.NamingContext NewContext()
  218. throws org.omg.CORBA.SystemException
  219. {
  220. try {
  221. // Create a new servant
  222. TransientNamingContext transContext =
  223. new TransientNamingContext(
  224. (POAORB) orb,localRoot, nsPOA);
  225. byte[] objectId = nsPOA.activate_object( transContext );
  226. org.omg.CORBA.Object obj = nsPOA.id_to_reference( objectId );
  227. return org.omg.CosNaming.NamingContextHelper.narrow( obj );
  228. } catch (org.omg.CORBA.SystemException e) {
  229. NamingUtils.errprint("NewContext(): caught System Exception:");
  230. NamingUtils.printException(e);
  231. throw e;
  232. } catch (Exception e) {
  233. // Convert to a CORBA system exception
  234. NamingUtils.errprint("NewContext(): caught Exception:");
  235. NamingUtils.printException(e);
  236. throw new org.omg.CORBA.INTERNAL(MinorCodes.TRANS_NC_NEWCTX_GOT_EXC,
  237. CompletionStatus.COMPLETED_NO);
  238. }
  239. }
  240. /**
  241. * Destroys this NamingContext by disconnecting from the ORB.
  242. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  243. * system exceptions.
  244. */
  245. public final void Destroy()
  246. throws org.omg.CORBA.SystemException
  247. {
  248. // Destroy the object reference by disconnecting from the ORB
  249. try {
  250. byte[] objectId = nsPOA.servant_to_id( this );
  251. if( objectId != null ) {
  252. nsPOA.deactivate_object( objectId );
  253. }
  254. } catch (org.omg.CORBA.SystemException e) {
  255. NamingUtils.errprint("Destroy(): caught System Exception:");
  256. NamingUtils.printException(e);
  257. throw e;
  258. } catch (Exception e) {
  259. // Convert to a CORBA system exception
  260. NamingUtils.errprint("Destroy(): caught exception:");
  261. NamingUtils.printException(e);
  262. throw new org.omg.CORBA.INTERNAL(
  263. MinorCodes.TRANS_NC_DESTROY_GOT_EXC,
  264. CompletionStatus.COMPLETED_NO);
  265. }
  266. }
  267. /**
  268. * Return whether this NamingContext contains any bindings. It forwards
  269. * this request to the hash table.
  270. * @return true if this NamingContext contains no bindings.
  271. */
  272. public final boolean IsEmpty()
  273. {
  274. return this.theHashtable.isEmpty();
  275. }
  276. // A hashtable to store the bindings
  277. private final Hashtable theHashtable = new Hashtable();
  278. /**
  279. * The local root naming context.
  280. */
  281. public org.omg.CORBA.Object localRoot;
  282. }