1. /*
  2. * @(#)NameService.java 1.8 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. /*
  8. * @(#)NameService.java 1.3 00/04/06
  9. *
  10. * Copyright 1993-1997 Sun Microsystems, Inc. 901 San Antonio Road,
  11. * Palo Alto, California, 94303, U.S.A. All Rights Reserved.
  12. *
  13. * This software is the confidential and proprietary information of Sun
  14. * Microsystems, Inc. ("Confidential Information"). You shall not
  15. * disclose such Confidential Information and shall use it only in
  16. * accordance with the terms of the license agreement you entered into
  17. * with Sun.
  18. *
  19. * CopyrightVersion 1.2
  20. *
  21. */
  22. package com.sun.corba.se.internal.PCosNaming;
  23. import java.io.File;
  24. import java.util.Properties;
  25. import org.omg.CORBA.Policy;
  26. import org.omg.PortableServer.POA;
  27. import org.omg.PortableServer.LifespanPolicyValue;
  28. import org.omg.PortableServer.RequestProcessingPolicyValue;
  29. import org.omg.PortableServer.IdAssignmentPolicyValue;
  30. import org.omg.PortableServer.ServantRetentionPolicyValue;
  31. import org.omg.CosNaming.NamingContext;
  32. import org.omg.CosNaming.NamingContextHelper;
  33. import org.omg.PortableServer.*;
  34. import com.sun.corba.se.internal.POA.POAORB;
  35. /**
  36. * @version 1.3, 00/04/06
  37. * @author Hemanth Puttaswamy
  38. * @since JDK1.2
  39. */
  40. public class NameService
  41. {
  42. private NamingContext rootContext = null;
  43. private POA nsPOA = null;
  44. private ServantManagerImpl contextMgr;
  45. private POAORB theorb;
  46. /**
  47. * Create NameService which starts the Root Naming Context in Persistent CosNaming
  48. * @param orb an ORB object.
  49. * @param logDir a File
  50. * @exception java.lang.Exception a Java exception.
  51. */
  52. public NameService(POAORB orb, File logDir)
  53. throws Exception
  54. {
  55. theorb = orb;
  56. // This is required for creating Persistent Servants under this ORB
  57. // Right now the Persistent NameService and ORBD are launched together
  58. // Find out a better way of doing this, Since ORBD is an important
  59. // process which should not be killed because of some external process
  60. orb.setPersistentServerId( (int) 1000 );
  61. // get and activate the root naming POA
  62. POA rootPOA = (POA)orb.resolve_initial_references("RootPOA");
  63. rootPOA.the_POAManager().activate();
  64. // create a new POA for persistent Naming Contexts
  65. // With Non-Retain policy, So that every time Servant Manager
  66. // will be contacted when the reference is made for the context
  67. // The id assignment is made by the NameServer, The Naming Context
  68. // id's will be in the format NC<Index>
  69. int i=0;
  70. Policy[] poaPolicy = new Policy[4];
  71. poaPolicy[i++] = rootPOA.create_lifespan_policy(
  72. LifespanPolicyValue.PERSISTENT);
  73. poaPolicy[i++] = rootPOA.create_request_processing_policy(
  74. RequestProcessingPolicyValue.USE_SERVANT_MANAGER);
  75. poaPolicy[i++] = rootPOA.create_id_assignment_policy(
  76. IdAssignmentPolicyValue.USER_ID);
  77. poaPolicy[i++] = rootPOA.create_servant_retention_policy(
  78. ServantRetentionPolicyValue.NON_RETAIN);
  79. nsPOA = rootPOA.create_POA("NameService", null, poaPolicy);
  80. nsPOA.the_POAManager().activate( );
  81. // create and set the servant manager
  82. contextMgr = new
  83. ServantManagerImpl(orb, logDir, this );
  84. // The RootObject key will be NC0
  85. String rootKey = contextMgr.getRootObjectKey( );
  86. // initialize the root Naming Context
  87. NamingContextImpl nc =
  88. new NamingContextImpl( orb, rootKey, this, contextMgr );
  89. nc = contextMgr.addContext( rootKey, nc );
  90. nc.setServantManagerImpl( contextMgr );
  91. nc.setORB( orb );
  92. nc.setRootNameService( this );
  93. nsPOA.set_servant_manager(contextMgr);
  94. rootContext = NamingContextHelper.narrow(
  95. nsPOA.create_reference_with_id( rootKey.getBytes( ),
  96. NamingContextHelper.id( ) ) );
  97. }
  98. /**
  99. * This method returns the Root Naming Context
  100. */
  101. public NamingContext initialNamingContext()
  102. {
  103. return rootContext;
  104. }
  105. /**
  106. * This method returns nsPOA which is the only POA that we use for
  107. * Persistent Naming Contexts.
  108. */
  109. POA getNSPOA( ) {
  110. return nsPOA;
  111. }
  112. /**
  113. * This method creates a NewContext, This will internally invoked from
  114. * NamingContextImpl. It is not a public API. NewContext is in this class
  115. * because a Persiten reference has to be created with Persistent NameService
  116. * POA.
  117. */
  118. public NamingContext NewContext( ) throws org.omg.CORBA.SystemException
  119. {
  120. try
  121. {
  122. // Get the new Naming Context Key from
  123. // the ServantManager
  124. String newKey =
  125. contextMgr.getNewObjectKey( );
  126. // Create the new Naming context and create the Persistent
  127. // reference
  128. NamingContextImpl theContext =
  129. new NamingContextImpl( theorb, newKey,
  130. this, contextMgr );
  131. NamingContextImpl tempContext = contextMgr.addContext( newKey,
  132. theContext );
  133. if( tempContext != null )
  134. {
  135. theContext = tempContext;
  136. }
  137. // If the context is read from the File, The following three entries
  138. // will be null. So a fresh setup may be required.
  139. theContext.setServantManagerImpl( contextMgr );
  140. theContext.setORB( theorb );
  141. theContext.setRootNameService( this );
  142. NamingContext theNewContext =
  143. NamingContextHelper.narrow(
  144. nsPOA.create_reference_with_id( newKey.getBytes( ),
  145. NamingContextHelper.id( )) );
  146. return theNewContext;
  147. }
  148. catch( org.omg.CORBA.SystemException e )
  149. {
  150. throw e;
  151. }
  152. catch( java.lang.Exception e )
  153. {
  154. //throw e;
  155. }
  156. return null;
  157. }
  158. /**
  159. * getObjectReferenceFromKey returns the Object reference from the objectkey using POA.create_reference_with_id method
  160. * @param Object Key as String
  161. * @returns reference an CORBA.Object.
  162. */
  163. org.omg.CORBA.Object getObjectReferenceFromKey( String key )
  164. {
  165. org.omg.CORBA.Object theObject = null;
  166. try
  167. {
  168. theObject = nsPOA.create_reference_with_id( key.getBytes( ), NamingContextHelper.id( ) );
  169. }
  170. catch (Exception e )
  171. {
  172. theObject = null;
  173. }
  174. return theObject;
  175. }
  176. /**
  177. * getObjectKey gets the Object Key from the reference using POA.reference_to_id method
  178. * @param reference an CORBA.Object.
  179. * @returns Object Key as String
  180. */
  181. String getObjectKey( org.omg.CORBA.Object reference )
  182. {
  183. byte theId[];
  184. try
  185. {
  186. theId = nsPOA.reference_to_id( reference );
  187. }
  188. catch( org.omg.PortableServer.POAPackage.WrongAdapter e )
  189. {
  190. return null;
  191. }
  192. catch( org.omg.PortableServer.POAPackage.WrongPolicy e )
  193. {
  194. return null;
  195. }
  196. catch( Exception e )
  197. {
  198. return null;
  199. }
  200. String theKey = new String( theId );
  201. return theKey;
  202. }
  203. }