1 /*
   2  * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.corba.se.impl.naming.cosnaming;
  27 
  28 import java.util.Properties;
  29 import java.net.InetAddress;
  30 
  31 import org.omg.CORBA.ORB;
  32 
  33 import org.omg.CosNaming.NamingContext;
  34 
  35 import com.sun.corba.se.spi.logging.CORBALogDomains;
  36 
  37 import com.sun.corba.se.impl.naming.cosnaming.TransientNameService;
  38 
  39 import com.sun.corba.se.impl.orbutil.ORBConstants;
  40 import com.sun.corba.se.impl.orbutil.CorbaResourceUtil;
  41 import com.sun.corba.se.impl.logging.NamingSystemException;
  42 
  43 /**
  44  * Class TransientNameServer is a standalone application which
  45  * implements a transient name service. It uses the TransientNameService
  46  * class for the name service implementation, and the BootstrapServer
  47  * for implementing bootstrapping, i.e., to get the initial NamingContext.
  48  * <p>
  49  * The BootstrapServer uses a Properties object specify the initial service
  50  * object references supported; such as Properties object is created containing
  51  * only a "NameService" entry together with the stringified object reference
  52  * for the initial NamingContext. The BootstrapServer's listening port
  53  * is set by first checking the supplied arguments to the name server
  54  * (-ORBInitialPort), and if not set, defaults to the standard port number.
  55  * The BootstrapServer is created supplying the Properties object, using no
  56  * external File object for storage, and the derived initial port number.
  57  * @see TransientNameService
  58  * @see BootstrapServer
  59  */
  60 public class TransientNameServer
  61 {
  62     static private boolean debug = false ;
  63     static NamingSystemException wrapper = NamingSystemException.get(
  64         CORBALogDomains.NAMING ) ;
  65 
  66     static public void trace( String msg ) {
  67         if (debug)
  68             System.out.println( msg ) ;
  69     }
  70 
  71     static public void initDebug( String[] args ) {
  72         // If debug was compiled to be true for testing purposes,
  73         // don't change it.
  74         if (debug)
  75             return ;
  76 
  77         for (int ctr=0; ctr<args.length; ctr++)
  78             if (args[ctr].equalsIgnoreCase( "-debug" )) {
  79                 debug = true ;
  80             return ;
  81         }
  82         debug = false ;
  83     }
  84 
  85     private static org.omg.CORBA.Object initializeRootNamingContext( ORB orb ) {
  86         org.omg.CORBA.Object rootContext = null;
  87         try {
  88             com.sun.corba.se.spi.orb.ORB coreORB =
  89                 (com.sun.corba.se.spi.orb.ORB)orb ;
  90 
  91             TransientNameService tns = new TransientNameService(coreORB );
  92             return tns.initialNamingContext();
  93         } catch (org.omg.CORBA.SystemException e) {
  94             throw wrapper.transNsCannotCreateInitialNcSys( e ) ;
  95         } catch (Exception e) {
  96             throw wrapper.transNsCannotCreateInitialNc( e ) ;
  97         }
  98     }
  99 
 100     /**
 101      * Main startup routine. It instantiates a TransientNameService
 102      * object and a BootstrapServer object, and then allows invocations to
 103      * happen.
 104      * @param args an array of strings representing the startup arguments.
 105      */
 106     public static void main(String args[]) {
 107         initDebug( args ) ;
 108 
 109         boolean invalidHostOption = false;
 110         boolean orbInitialPort0 = false;
 111 
 112         // Determine the initial bootstrap port to use
 113         int initialPort = 0;
 114         try {
 115             trace( "Transient name server started with args " + args ) ;
 116 
 117             // Create an ORB object
 118             Properties props = System.getProperties() ;
 119 
 120             props.put( ORBConstants.SERVER_ID_PROPERTY, ORBConstants.NAME_SERVICE_SERVER_ID ) ;
 121             props.put( "org.omg.CORBA.ORBClass",
 122                 "com.sun.corba.se.impl.orb.ORBImpl" );
 123 
 124             try {
 125                 // Try environment
 126                 String ips = System.getProperty( ORBConstants.INITIAL_PORT_PROPERTY ) ;
 127                 if (ips != null && ips.length() > 0 ) {
 128                     initialPort = java.lang.Integer.parseInt(ips);
 129                     // -Dorg.omg.CORBA.ORBInitialPort=0 is invalid
 130                     if( initialPort == 0 ) {
 131                         orbInitialPort0 = true;
 132                         throw wrapper.transientNameServerBadPort() ;
 133                     }
 134                 }
 135                 String hostName =
 136                     System.getProperty( ORBConstants.INITIAL_HOST_PROPERTY ) ;
 137                 if( hostName != null ) {
 138                     invalidHostOption = true;
 139                     throw wrapper.transientNameServerBadHost() ;
 140                 }
 141             } catch (java.lang.NumberFormatException e) {
 142                 // do nothing
 143             }
 144 
 145             // Let arguments override
 146             for (int i=0;i<args.length;i++) {
 147                 // Was the initial port specified?
 148                 if (args[i].equals("-ORBInitialPort") &&
 149                     i < args.length-1) {
 150                     initialPort = java.lang.Integer.parseInt(args[i+1]);
 151                     // -ORBInitialPort 0 is invalid
 152                     if( initialPort == 0 ) {
 153                         orbInitialPort0 = true;
 154                         throw wrapper.transientNameServerBadPort() ;
 155                     }
 156                 }
 157                 if (args[i].equals("-ORBInitialHost" ) ) {
 158                     invalidHostOption = true;
 159                     throw wrapper.transientNameServerBadHost() ;
 160                 }
 161             }
 162 
 163             // If initialPort is not set, then we need to set the Default
 164             // Initial Port Property for the ORB
 165             if( initialPort == 0 ) {
 166                 initialPort = ORBConstants.DEFAULT_INITIAL_PORT;
 167                 props.put( ORBConstants.INITIAL_PORT_PROPERTY,
 168                     java.lang.Integer.toString(initialPort) );
 169             }
 170 
 171             // Set -ORBInitialPort = Persistent Server Port so that ORBImpl
 172             // will start Boot Strap.
 173             props.put( ORBConstants.PERSISTENT_SERVER_PORT_PROPERTY,
 174                java.lang.Integer.toString(initialPort) );
 175 
 176             org.omg.CORBA.ORB corb = ORB.init( args, props ) ;
 177             trace( "ORB object returned from init: " + corb ) ;
 178 
 179             org.omg.CORBA.Object ns = initializeRootNamingContext( corb ) ;
 180             ((com.sun.corba.se.org.omg.CORBA.ORB)corb).register_initial_reference(
 181                 "NamingService", ns ) ;
 182 
 183             String stringifiedIOR = null;
 184 
 185             if( ns != null ) {
 186                 stringifiedIOR = corb.object_to_string(ns) ;
 187             } else {
 188                  NamingUtils.errprint(CorbaResourceUtil.getText(
 189                      "tnameserv.exception", initialPort));
 190                  NamingUtils.errprint(CorbaResourceUtil.getText(
 191                      "tnameserv.usage"));
 192                 System.exit( 1 );
 193             }
 194 
 195             trace( "name service created" ) ;
 196 
 197             // This is used for handshaking by the IBM test framework!
 198             // Do not modify, unless another synchronization protocol is
 199             // used to replace this hack!
 200 
 201             System.out.println(CorbaResourceUtil.getText(
 202                 "tnameserv.hs1", stringifiedIOR));
 203             System.out.println(CorbaResourceUtil.getText(
 204                 "tnameserv.hs2", initialPort));
 205             System.out.println(CorbaResourceUtil.getText("tnameserv.hs3"));
 206 
 207             // Serve objects.
 208             java.lang.Object sync = new java.lang.Object();
 209             synchronized (sync) {sync.wait();}
 210         } catch (Exception e) {
 211             if( invalidHostOption ) {
 212                 // Let the User Know that -ORBInitialHost is not valid for
 213                 // tnameserver
 214                 NamingUtils.errprint( CorbaResourceUtil.getText(
 215                     "tnameserv.invalidhostoption" ) );
 216             } else if( orbInitialPort0 ) {
 217                 // Let the User Know that -ORBInitialPort 0 is not valid for
 218                 // tnameserver
 219                 NamingUtils.errprint( CorbaResourceUtil.getText(
 220                     "tnameserv.orbinitialport0" ));
 221             } else {
 222                 NamingUtils.errprint(CorbaResourceUtil.getText(
 223                     "tnameserv.exception", initialPort));
 224                 NamingUtils.errprint(CorbaResourceUtil.getText(
 225                     "tnameserv.usage"));
 226             }
 227 
 228             e.printStackTrace() ;
 229         }
 230     }
 231 
 232     /**
 233      * Private constructor since no object of this type should be instantiated.
 234      */
 235     private TransientNameServer() {}
 236 }