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. package com.sun.corba.se.impl.naming.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. import java.util.logging.Logger;
  24. import java.util.logging.Level;
  25. import java.util.Hashtable;
  26. import com.sun.corba.se.impl.orbutil.LogKeywords;
  27. import com.sun.corba.se.impl.logging.NamingSystemException;
  28. import com.sun.corba.se.spi.logging.CORBALogDomains;
  29. /**
  30. * Class TransientNamingContext implements the methods defined
  31. * by NamingContextDataStore, and extends the NamingContextImpl class to
  32. * provide a servant implementation of CosNaming::NamingContext.
  33. * The TransientNamingContext uses a hash table
  34. * to store the mappings between bindings and object references and the
  35. * hash table is not persistent; thereby the name "transient".
  36. * This class should not be used directly; instead, the class
  37. * TransientNameService should be instantiated.
  38. * <p>
  39. * The keys in the hash table are InternalBindingKey objects, containing
  40. * a single NameComponent and implementing the proper functions, i.e.,
  41. * equals() and hashCode() in an efficient manner. The values in the hash
  42. * table are InternalBindingValues and store a org.omg.CosNaming::Binding and
  43. * the object reference associated with the binding. For iteration,
  44. * TransientBindingIterator objects are created, which are passed a cloned
  45. * copy of the hashtable. Since elements are inserted and deleted and
  46. * never modified, this provides stable iterators at the cost of cloning
  47. * the hash table.
  48. * <p>
  49. * To create and destroy object references, the TransientNamingContext
  50. * uses the orb.connect() and orb.disconnect() methods.
  51. *
  52. * @see NamingContextImpl
  53. * @see NamingContextDataStore
  54. * @see TransientBindingIterator
  55. * @see TransientNameService
  56. */
  57. public class TransientNamingContext extends NamingContextImpl implements NamingContextDataStore
  58. {
  59. private Logger readLogger, updateLogger, lifecycleLogger;
  60. // XXX: the wrapper calls are all preceded by logger updates.
  61. // These can be combined, and then we simply use 3 NamingSystemException wrappers,
  62. // for read, update, and lifecycl.
  63. private NamingSystemException wrapper ;
  64. /**
  65. * Constructs a new TransientNamingContext object.
  66. * @param orb an orb object.
  67. * @param initial the initial naming context.
  68. * @exception Exception a Java exception thrown of the base class cannot
  69. * initialize.
  70. */
  71. public TransientNamingContext(com.sun.corba.se.spi.orb.ORB orb,
  72. org.omg.CORBA.Object initial,
  73. POA nsPOA )
  74. throws java.lang.Exception
  75. {
  76. super(orb, nsPOA );
  77. wrapper = NamingSystemException.get( orb, CORBALogDomains.NAMING ) ;
  78. this.localRoot = initial;
  79. readLogger = orb.getLogger( CORBALogDomains.NAMING_READ);
  80. updateLogger = orb.getLogger( CORBALogDomains.NAMING_UPDATE);
  81. lifecycleLogger = orb.getLogger(
  82. CORBALogDomains.NAMING_LIFECYCLE);
  83. lifecycleLogger.fine( "Root TransientNamingContext LIFECYCLE.CREATED" );
  84. }
  85. /**
  86. * Binds the object to the name component as the specified binding type.
  87. * It creates a InternalBindingKey object and a InternalBindingValue
  88. * object and inserts them in the hash table.
  89. * @param n A single org.omg.CosNaming::NameComponent under which the
  90. * object will be bound.
  91. * @param obj An object reference to be bound under the supplied name.
  92. * @param bt The type of the binding (i.e., as object or as context).
  93. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  94. * system exceptions.
  95. */
  96. public final void Bind(NameComponent n, org.omg.CORBA.Object obj,
  97. BindingType bt)
  98. throws org.omg.CORBA.SystemException
  99. {
  100. // Create a key and a value
  101. InternalBindingKey key = new InternalBindingKey(n);
  102. NameComponent[] name = new NameComponent[1];
  103. name[0] = n;
  104. Binding b = new Binding(name,bt);
  105. InternalBindingValue value = new InternalBindingValue(b,null);
  106. value.theObjectRef = obj;
  107. // insert it
  108. InternalBindingValue oldValue =
  109. (InternalBindingValue)this.theHashtable.put(key,value);
  110. if (oldValue != null) {
  111. updateLogger.warning( LogKeywords.NAMING_BIND + "Name " +
  112. getName( n ) + " Was Already Bound" );
  113. throw wrapper.transNcBindAlreadyBound() ;
  114. }
  115. if( updateLogger.isLoggable( Level.FINE ) ) {
  116. updateLogger.fine( LogKeywords.NAMING_BIND_SUCCESS +
  117. "Name Component: " + n.id + "." + n.kind );
  118. }
  119. }
  120. /**
  121. * Resolves the supplied name to an object reference and returns
  122. * the type of the resolved binding. It creates a InternalBindingKey
  123. * and uses the key for looking up in the hash table. If nothing
  124. * is found an exception is thrown, otherwise the object reference
  125. * is returned and the binding type set.
  126. * @param n a NameComponent which is the name to be resolved.
  127. * @param bth the BindingType as an out parameter.
  128. * @return the object reference bound under the supplied name, null if not
  129. * found.
  130. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  131. * system exceptions.
  132. */
  133. public final org.omg.CORBA.Object Resolve(NameComponent n,
  134. BindingTypeHolder bth)
  135. throws org.omg.CORBA.SystemException
  136. {
  137. // Is the initial naming context requested?
  138. if ( (n.id.length() == 0)
  139. &&(n.kind.length() == 0 ) )
  140. {
  141. bth.value = BindingType.ncontext;
  142. return localRoot;
  143. }
  144. // Create a key and lookup the value
  145. InternalBindingKey key = new InternalBindingKey(n);
  146. InternalBindingValue value =
  147. (InternalBindingValue) this.theHashtable.get(key);
  148. if (value == null) return null;
  149. if( readLogger.isLoggable( Level.FINE ) ) {
  150. readLogger.fine( LogKeywords.NAMING_RESOLVE_SUCCESS
  151. + "Namecomponent :" + getName( n ) );
  152. }
  153. // Copy out binding type and object reference
  154. bth.value = value.theBinding.binding_type;
  155. return value.theObjectRef;
  156. }
  157. /**
  158. * Deletes the binding with the supplied name. It creates a
  159. * InternalBindingKey and uses it to remove the value associated
  160. * with the key. If nothing is found an exception is thrown, otherwise
  161. * the element is removed from the hash table.
  162. * @param n a NameComponent which is the name to unbind
  163. * @return the object reference bound to the name, or null if not found.
  164. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  165. * system exceptions.
  166. */
  167. public final org.omg.CORBA.Object Unbind(NameComponent n)
  168. throws org.omg.CORBA.SystemException
  169. {
  170. // Create a key and remove it from the hashtable
  171. InternalBindingKey key = new InternalBindingKey(n);
  172. InternalBindingValue value =
  173. (InternalBindingValue)this.theHashtable.remove(key);
  174. // Return what was found
  175. if (value == null) {
  176. if( updateLogger.isLoggable( Level.FINE ) ) {
  177. updateLogger.fine( LogKeywords.NAMING_UNBIND_FAILURE +
  178. " There was no binding with the name " + getName( n ) +
  179. " to Unbind " );
  180. }
  181. return null;
  182. } else {
  183. if( updateLogger.isLoggable( Level.FINE ) ) {
  184. updateLogger.fine( LogKeywords.NAMING_UNBIND_SUCCESS +
  185. " NameComponent: " + getName( n ) );
  186. }
  187. return value.theObjectRef;
  188. }
  189. }
  190. /**
  191. * List the contents of this NamingContext. It creates a new
  192. * TransientBindingIterator object and passes it a clone of the
  193. * hash table and an orb object. It then uses the
  194. * newly created object to return the required number of bindings.
  195. * @param how_many The number of requested bindings in the BindingList.
  196. * @param bl The BindingList as an out parameter.
  197. * @param bi The BindingIterator as an out parameter.
  198. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  199. * system exceptions.
  200. */
  201. public final void List(int how_many, BindingListHolder bl,
  202. BindingIteratorHolder bi)
  203. throws org.omg.CORBA.SystemException
  204. {
  205. try {
  206. // Create a new binding iterator servant with a copy of this
  207. // hashtable. nsPOA is passed to the object so that it can
  208. // de-activate itself from the Active Object Map when
  209. // Binding Iterator.destroy is called.
  210. TransientBindingIterator bindingIterator =
  211. new TransientBindingIterator(this.orb,
  212. (Hashtable)this.theHashtable.clone(), nsPOA);
  213. // Have it set the binding list
  214. bindingIterator.list(how_many,bl);
  215. byte[] objectId = nsPOA.activate_object( bindingIterator );
  216. org.omg.CORBA.Object obj = nsPOA.id_to_reference( objectId );
  217. // Get the object reference for the binding iterator servant
  218. org.omg.CosNaming.BindingIterator bindingRef =
  219. org.omg.CosNaming.BindingIteratorHelper.narrow( obj );
  220. bi.value = bindingRef;
  221. } catch (org.omg.CORBA.SystemException e) {
  222. readLogger.warning( LogKeywords.NAMING_LIST_FAILURE + e );
  223. throw e;
  224. } catch (Exception e) {
  225. // Convert to a CORBA system exception
  226. readLogger.severe( LogKeywords.NAMING_LIST_FAILURE + e );
  227. throw wrapper.transNcListGotExc( e ) ;
  228. }
  229. }
  230. /**
  231. * Create a new NamingContext. It creates a new TransientNamingContext
  232. * object, passing it the orb object.
  233. * @return an object reference for a new NamingContext object implemented
  234. * by this Name Server.
  235. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  236. * system exceptions.
  237. */
  238. public final org.omg.CosNaming.NamingContext NewContext()
  239. throws org.omg.CORBA.SystemException
  240. {
  241. try {
  242. // Create a new servant
  243. TransientNamingContext transContext =
  244. new TransientNamingContext(
  245. (com.sun.corba.se.spi.orb.ORB) orb,localRoot, nsPOA);
  246. byte[] objectId = nsPOA.activate_object( transContext );
  247. org.omg.CORBA.Object obj = nsPOA.id_to_reference( objectId );
  248. lifecycleLogger.fine( "TransientNamingContext " +
  249. "LIFECYCLE.CREATE SUCCESSFUL" );
  250. return org.omg.CosNaming.NamingContextHelper.narrow( obj );
  251. } catch (org.omg.CORBA.SystemException e) {
  252. lifecycleLogger.log(
  253. Level.WARNING, LogKeywords.LIFECYCLE_CREATE_FAILURE, e );
  254. throw e;
  255. } catch (Exception e) {
  256. lifecycleLogger.log(
  257. Level.WARNING, LogKeywords.LIFECYCLE_CREATE_FAILURE, e );
  258. throw wrapper.transNcNewctxGotExc( e ) ;
  259. }
  260. }
  261. /**
  262. * Destroys this NamingContext by disconnecting from the ORB.
  263. * @exception org.omg.CORBA.SystemException One of a fixed set of CORBA
  264. * system exceptions.
  265. */
  266. public final void Destroy()
  267. throws org.omg.CORBA.SystemException
  268. {
  269. // Destroy the object reference by disconnecting from the ORB
  270. try {
  271. byte[] objectId = nsPOA.servant_to_id( this );
  272. if( objectId != null ) {
  273. nsPOA.deactivate_object( objectId );
  274. }
  275. if( lifecycleLogger.isLoggable( Level.FINE ) ) {
  276. lifecycleLogger.fine(
  277. LogKeywords.LIFECYCLE_DESTROY_SUCCESS );
  278. }
  279. } catch (org.omg.CORBA.SystemException e) {
  280. lifecycleLogger.log( Level.WARNING,
  281. LogKeywords.LIFECYCLE_DESTROY_FAILURE, e );
  282. throw e;
  283. } catch (Exception e) {
  284. lifecycleLogger.log( Level.WARNING,
  285. LogKeywords.LIFECYCLE_DESTROY_FAILURE, e );
  286. throw wrapper.transNcDestroyGotExc( e ) ;
  287. }
  288. }
  289. /**
  290. * A Utility Method For Logging..
  291. */
  292. private String getName( NameComponent n ) {
  293. return n.id + "." + n.kind;
  294. }
  295. /**
  296. * Return whether this NamingContext contains any bindings. It forwards
  297. * this request to the hash table.
  298. * @return true if this NamingContext contains no bindings.
  299. */
  300. public final boolean IsEmpty()
  301. {
  302. return this.theHashtable.isEmpty();
  303. }
  304. // A hashtable to store the bindings
  305. private final Hashtable theHashtable = new Hashtable();
  306. /**
  307. * The local root naming context.
  308. */
  309. public org.omg.CORBA.Object localRoot;
  310. }