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