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