1. /*
  2. * @(#)TransientNameServer.java 1.38 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 java.util.Properties;
  9. import java.net.InetAddress;
  10. import org.omg.CORBA.ORB;
  11. import org.omg.CosNaming.NamingContext;
  12. import com.sun.corba.se.spi.logging.CORBALogDomains;
  13. import com.sun.corba.se.impl.naming.cosnaming.TransientNameService;
  14. import com.sun.corba.se.impl.orbutil.ORBConstants;
  15. import com.sun.corba.se.impl.orbutil.CorbaResourceUtil;
  16. import com.sun.corba.se.impl.logging.NamingSystemException;
  17. /**
  18. * Class TransientNameServer is a standalone application which
  19. * implements a transient name service. It uses the TransientNameService
  20. * class for the name service implementation, and the BootstrapServer
  21. * for implementing bootstrapping, i.e., to get the initial NamingContext.
  22. * <p>
  23. * The BootstrapServer uses a Properties object specify the initial service
  24. * object references supported; such as Properties object is created containing
  25. * only a "NameService" entry together with the stringified object reference
  26. * for the initial NamingContext. The BootstrapServer's listening port
  27. * is set by first checking the supplied arguments to the name server
  28. * (-ORBInitialPort), and if not set, defaults to the standard port number.
  29. * The BootstrapServer is created supplying the Properties object, using no
  30. * external File object for storage, and the derived initial port number.
  31. * @see TransientNameService
  32. * @see BootstrapServer
  33. */
  34. public class TransientNameServer
  35. {
  36. static private boolean debug = false ;
  37. static NamingSystemException wrapper = NamingSystemException.get(
  38. CORBALogDomains.NAMING ) ;
  39. static public void trace( String msg ) {
  40. if (debug)
  41. System.out.println( msg ) ;
  42. }
  43. static public void initDebug( String[] args ) {
  44. // If debug was compiled to be true for testing purposes,
  45. // don't change it.
  46. if (debug)
  47. return ;
  48. for (int ctr=0; ctr<args.length; ctr++)
  49. if (args[ctr].equalsIgnoreCase( "-debug" )) {
  50. debug = true ;
  51. return ;
  52. }
  53. debug = false ;
  54. }
  55. private static org.omg.CORBA.Object initializeRootNamingContext( ORB orb ) {
  56. org.omg.CORBA.Object rootContext = null;
  57. try {
  58. com.sun.corba.se.spi.orb.ORB coreORB =
  59. (com.sun.corba.se.spi.orb.ORB)orb ;
  60. TransientNameService tns = new TransientNameService(coreORB );
  61. return tns.initialNamingContext();
  62. } catch (org.omg.CORBA.SystemException e) {
  63. throw wrapper.transNsCannotCreateInitialNcSys( e ) ;
  64. } catch (Exception e) {
  65. throw wrapper.transNsCannotCreateInitialNc( e ) ;
  66. }
  67. }
  68. /**
  69. * Main startup routine. It instantiates a TransientNameService
  70. * object and a BootstrapServer object, and then allows invocations to
  71. * happen.
  72. * @param args an array of strings representing the startup arguments.
  73. */
  74. public static void main(String args[]) {
  75. initDebug( args ) ;
  76. boolean invalidHostOption = false;
  77. boolean orbInitialPort0 = false;
  78. // Determine the initial bootstrap port to use
  79. int initialPort = 0;
  80. try {
  81. trace( "Transient name server started with args " + args ) ;
  82. // Create an ORB object
  83. Properties props = System.getProperties() ;
  84. props.put( ORBConstants.SERVER_ID_PROPERTY, ORBConstants.NAME_SERVICE_SERVER_ID ) ;
  85. props.put( "org.omg.CORBA.ORBClass",
  86. "com.sun.corba.se.impl.orb.ORBImpl" );
  87. try {
  88. // Try environment
  89. String ips = System.getProperty( ORBConstants.INITIAL_PORT_PROPERTY ) ;
  90. if (ips != null && ips.length() > 0 ) {
  91. initialPort = java.lang.Integer.parseInt(ips);
  92. // -Dorg.omg.CORBA.ORBInitialPort=0 is invalid
  93. if( initialPort == 0 ) {
  94. orbInitialPort0 = true;
  95. throw wrapper.transientNameServerBadPort() ;
  96. }
  97. }
  98. String hostName =
  99. System.getProperty( ORBConstants.INITIAL_HOST_PROPERTY ) ;
  100. if( hostName != null ) {
  101. invalidHostOption = true;
  102. throw wrapper.transientNameServerBadHost() ;
  103. }
  104. } catch (java.lang.NumberFormatException e) {
  105. // do nothing
  106. }
  107. // Let arguments override
  108. for (int i=0;i<args.length;i++) {
  109. // Was the initial port specified?
  110. if (args[i].equals("-ORBInitialPort") &&
  111. i < args.length-1) {
  112. initialPort = java.lang.Integer.parseInt(args[i+1]);
  113. // -ORBInitialPort 0 is invalid
  114. if( initialPort == 0 ) {
  115. orbInitialPort0 = true;
  116. throw wrapper.transientNameServerBadPort() ;
  117. }
  118. }
  119. if (args[i].equals("-ORBInitialHost" ) ) {
  120. invalidHostOption = true;
  121. throw wrapper.transientNameServerBadHost() ;
  122. }
  123. }
  124. // If initialPort is not set, then we need to set the Default
  125. // Initial Port Property for the ORB
  126. if( initialPort == 0 ) {
  127. initialPort = ORBConstants.DEFAULT_INITIAL_PORT;
  128. props.put( ORBConstants.INITIAL_PORT_PROPERTY,
  129. java.lang.Integer.toString(initialPort) );
  130. }
  131. // Set -ORBInitialPort = Persistent Server Port so that ORBImpl
  132. // will start Boot Strap.
  133. props.put( ORBConstants.PERSISTENT_SERVER_PORT_PROPERTY,
  134. java.lang.Integer.toString(initialPort) );
  135. org.omg.CORBA.ORB corb = ORB.init( args, props ) ;
  136. trace( "ORB object returned from init: " + corb ) ;
  137. org.omg.CORBA.Object ns = initializeRootNamingContext( corb ) ;
  138. ((com.sun.corba.se.org.omg.CORBA.ORB)corb).register_initial_reference(
  139. "NamingService", ns ) ;
  140. String stringifiedIOR = null;
  141. if( ns != null ) {
  142. stringifiedIOR = corb.object_to_string(ns) ;
  143. } else {
  144. NamingUtils.errprint(CorbaResourceUtil.getText(
  145. "tnameserv.exception", initialPort));
  146. NamingUtils.errprint(CorbaResourceUtil.getText(
  147. "tnameserv.usage"));
  148. System.exit( 1 );
  149. }
  150. trace( "name service created" ) ;
  151. // This is used for handshaking by the IBM test framework!
  152. // Do not modify, unless another synchronization protocol is
  153. // used to replace this hack!
  154. System.out.println(CorbaResourceUtil.getText(
  155. "tnameserv.hs1", stringifiedIOR));
  156. System.out.println(CorbaResourceUtil.getText(
  157. "tnameserv.hs2", initialPort));
  158. System.out.println(CorbaResourceUtil.getText("tnameserv.hs3"));
  159. // Serve objects.
  160. java.lang.Object sync = new java.lang.Object();
  161. synchronized (sync) {sync.wait();}
  162. } catch (Exception e) {
  163. if( invalidHostOption ) {
  164. // Let the User Know that -ORBInitialHost is not valid for
  165. // tnameserver
  166. NamingUtils.errprint( CorbaResourceUtil.getText(
  167. "tnameserv.invalidhostoption" ) );
  168. } else if( orbInitialPort0 ) {
  169. // Let the User Know that -ORBInitialPort 0 is not valid for
  170. // tnameserver
  171. NamingUtils.errprint( CorbaResourceUtil.getText(
  172. "tnameserv.orbinitialport0" ));
  173. } else {
  174. NamingUtils.errprint(CorbaResourceUtil.getText(
  175. "tnameserv.exception", initialPort));
  176. NamingUtils.errprint(CorbaResourceUtil.getText(
  177. "tnameserv.usage"));
  178. }
  179. e.printStackTrace() ;
  180. }
  181. }
  182. /**
  183. * Private constructor since no object of this type should be instantiated.
  184. */
  185. private TransientNameServer() {}
  186. }