1 /*
   2  * Copyright (c) 1997, 2017, 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.activation;
  27 
  28 import java.io.File;
  29 import java.util.Properties;
  30 
  31 import org.omg.CORBA.INITIALIZE;
  32 import org.omg.CORBA.INTERNAL;
  33 import org.omg.CORBA.CompletionStatus;
  34 import org.omg.CosNaming.NamingContext;
  35 import org.omg.PortableServer.POA;
  36 
  37 import com.sun.corba.se.pept.transport.Acceptor;
  38 
  39 import com.sun.corba.se.spi.activation.Repository;
  40 import com.sun.corba.se.spi.activation.RepositoryPackage.ServerDef;
  41 import com.sun.corba.se.spi.activation.Locator;
  42 import com.sun.corba.se.spi.activation.LocatorHelper;
  43 import com.sun.corba.se.spi.activation.Activator;
  44 import com.sun.corba.se.spi.activation.ActivatorHelper;
  45 import com.sun.corba.se.spi.activation.ServerAlreadyRegistered;
  46 import com.sun.corba.se.spi.legacy.connection.LegacyServerSocketEndPointInfo;
  47 import com.sun.corba.se.spi.transport.SocketInfo;
  48 import com.sun.corba.se.spi.orb.ORB;
  49 
  50 import com.sun.corba.se.impl.legacy.connection.SocketFactoryAcceptorImpl;
  51 import com.sun.corba.se.impl.naming.cosnaming.TransientNameService;
  52 import com.sun.corba.se.impl.naming.pcosnaming.NameService;
  53 import com.sun.corba.se.impl.orbutil.ORBConstants;
  54 import com.sun.corba.se.impl.orbutil.CorbaResourceUtil;
  55 import com.sun.corba.se.impl.transport.SocketOrChannelAcceptorImpl;
  56 
  57 /**
  58  *
  59  * @author      Rohit Garg
  60  * @since       JDK1.2
  61  */
  62 public class ORBD
  63 {
  64     private int initSvcPort;
  65 
  66     protected void initializeBootNaming(ORB orb)
  67     {
  68         // create a bootstrap server
  69         initSvcPort = orb.getORBData().getORBInitialPort();
  70 
  71         Acceptor acceptor;
  72         // REVISIT: see ORBConfigurator. use factory in TransportDefault.
  73         if (orb.getORBData().getLegacySocketFactory() == null) {
  74             acceptor =
  75                 new SocketOrChannelAcceptorImpl(
  76                     orb,
  77                     initSvcPort,
  78                     LegacyServerSocketEndPointInfo.BOOT_NAMING,
  79                     SocketInfo.IIOP_CLEAR_TEXT);
  80         } else {
  81             acceptor =
  82                 new SocketFactoryAcceptorImpl(
  83                     orb,
  84                     initSvcPort,
  85                     LegacyServerSocketEndPointInfo.BOOT_NAMING,
  86                     SocketInfo.IIOP_CLEAR_TEXT);
  87         }
  88         orb.getCorbaTransportManager().registerAcceptor(acceptor);
  89     }
  90 
  91     protected ORB createORB(String[] args)
  92     {
  93         Properties props = System.getProperties();
  94 
  95         // For debugging.
  96         //props.put( ORBConstants.DEBUG_PROPERTY, "naming" ) ;
  97         //props.put( ORBConstants.DEBUG_PROPERTY, "transport,giop,naming" ) ;
  98 
  99         props.put( ORBConstants.SERVER_ID_PROPERTY, "1000" ) ;
 100         props.put( ORBConstants.PERSISTENT_SERVER_PORT_PROPERTY,
 101             props.getProperty( ORBConstants.ORBD_PORT_PROPERTY,
 102                 Integer.toString(
 103                     ORBConstants.DEFAULT_ACTIVATION_PORT ) ) ) ;
 104 
 105         // See Bug 4396928 for more information about why we are initializing
 106         // the ORBClass to PIORB (now ORBImpl, but should check the bugid).
 107         props.put("org.omg.CORBA.ORBClass",
 108             "com.sun.corba.se.impl.orb.ORBImpl");
 109 
 110         return (ORB) ORB.init(args, props);
 111     }
 112 
 113     private void run(String[] args)
 114     {
 115         try {
 116             // parse the args and try setting the values for these
 117             // properties
 118             processArgs(args);
 119 
 120             ORB orb = createORB(args);
 121 
 122             if (orb.orbdDebugFlag)
 123                 System.out.println( "ORBD begins initialization." ) ;
 124 
 125             boolean firstRun = createSystemDirs( ORBConstants.DEFAULT_DB_DIR );
 126 
 127             startActivationObjects(orb);
 128 
 129             if (firstRun) // orbd is being run the first time
 130                 installOrbServers(getRepository(), getActivator());
 131 
 132             if (orb.orbdDebugFlag) {
 133                 System.out.println( "ORBD is ready." ) ;
 134                 System.out.println("ORBD serverid: " +
 135                         System.getProperty(ORBConstants.SERVER_ID_PROPERTY));
 136                 System.out.println("activation dbdir: " +
 137                         System.getProperty(ORBConstants.DB_DIR_PROPERTY));
 138                 System.out.println("activation port: " +
 139                         System.getProperty(ORBConstants.ORBD_PORT_PROPERTY));
 140 
 141                 String pollingTime = System.getProperty(
 142                     ORBConstants.SERVER_POLLING_TIME);
 143                 if( pollingTime == null ) {
 144                     pollingTime = Integer.toString(
 145                         ORBConstants.DEFAULT_SERVER_POLLING_TIME );
 146                 }
 147                 System.out.println("activation Server Polling Time: " +
 148                         pollingTime + " milli-seconds ");
 149 
 150                 String startupDelay = System.getProperty(
 151                     ORBConstants.SERVER_STARTUP_DELAY);
 152                 if( startupDelay == null ) {
 153                     startupDelay = Integer.toString(
 154                         ORBConstants.DEFAULT_SERVER_STARTUP_DELAY );
 155                 }
 156                 System.out.println("activation Server Startup Delay: " +
 157                         startupDelay + " milli-seconds " );
 158             }
 159 
 160             // The following two lines start the Persistent NameService
 161             NameServiceStartThread theThread =
 162                 new NameServiceStartThread( orb, dbDir );
 163             theThread.start( );
 164 
 165             orb.run();
 166         } catch( org.omg.CORBA.COMM_FAILURE cex ) {
 167             System.out.println( CorbaResourceUtil.getText("orbd.commfailure"));
 168             System.out.println( cex );
 169             cex.printStackTrace();
 170         } catch( org.omg.CORBA.INTERNAL iex ) {
 171             System.out.println( CorbaResourceUtil.getText(
 172                 "orbd.internalexception"));
 173             System.out.println( iex );
 174             iex.printStackTrace();
 175         } catch (Exception ex) {
 176             System.out.println(CorbaResourceUtil.getText(
 177                 "orbd.usage", "orbd"));
 178             System.out.println( ex );
 179             ex.printStackTrace();
 180         }
 181     }
 182 
 183     private void processArgs(String[] args)
 184     {
 185         Properties props = System.getProperties();
 186         for (int i=0; i < args.length; i++) {
 187             if (args[i].equals("-?") ||
 188                 args[i].equals("-h") ||
 189                 args[i].equals("--help")) {
 190                 System.out.println(CorbaResourceUtil.getText(
 191                         "orbd.usage", "orbd"));
 192                 System.exit(0);
 193             } else if (args[i].equals("-port")) {
 194                 if ((i+1) < args.length) {
 195                     props.put(ORBConstants.ORBD_PORT_PROPERTY, args[++i]);
 196                 } else {
 197                     System.out.println(CorbaResourceUtil.getText(
 198                         "orbd.usage", "orbd"));
 199                 }
 200             } else if (args[i].equals("-defaultdb")) {
 201                 if ((i+1) < args.length) {
 202                     props.put(ORBConstants.DB_DIR_PROPERTY, args[++i]);
 203                 } else {
 204                     System.out.println(CorbaResourceUtil.getText(
 205                         "orbd.usage", "orbd"));
 206                 }
 207             } else if (args[i].equals("-serverid")) {
 208                 if ((i+1) < args.length) {
 209                     props.put(ORBConstants.SERVER_ID_PROPERTY, args[++i]);
 210                 } else {
 211                     System.out.println(CorbaResourceUtil.getText(
 212                         "orbd.usage", "orbd"));
 213                 }
 214             } else if (args[i].equals("-serverPollingTime")) {
 215                 if ((i+1) < args.length) {
 216                     props.put(ORBConstants.SERVER_POLLING_TIME, args[++i]);
 217                 } else {
 218                     System.out.println(CorbaResourceUtil.getText(
 219                         "orbd.usage", "orbd"));
 220                 }
 221             } else if (args[i].equals("-serverStartupDelay")) {
 222                 if ((i+1) < args.length) {
 223                     props.put(ORBConstants.SERVER_STARTUP_DELAY, args[++i]);
 224                 } else {
 225                     System.out.println(CorbaResourceUtil.getText(
 226                         "orbd.usage", "orbd"));
 227                 }
 228             }
 229         }
 230     }
 231 
 232     /**
 233      * Ensure that the Db directory exists. If not, create the Db
 234      * and the log directory and return true. Otherwise return false.
 235      */
 236     protected boolean createSystemDirs(String defaultDbDir)
 237     {
 238         boolean dirCreated = false;
 239         Properties props = System.getProperties();
 240         String fileSep = props.getProperty("file.separator");
 241 
 242         // determine the ORB db directory
 243         dbDir = new File (props.getProperty( ORBConstants.DB_DIR_PROPERTY,
 244             props.getProperty("user.dir") + fileSep + defaultDbDir));
 245 
 246         // create the db and the logs directories
 247         dbDirName = dbDir.getAbsolutePath();
 248         props.put(ORBConstants.DB_DIR_PROPERTY, dbDirName);
 249         if (!dbDir.exists()) {
 250             dbDir.mkdir();
 251             dirCreated = true;
 252         }
 253 
 254         File logDir = new File (dbDir, ORBConstants.SERVER_LOG_DIR ) ;
 255         if (!logDir.exists()) logDir.mkdir();
 256 
 257         return dirCreated;
 258     }
 259 
 260     protected File dbDir;
 261     protected File getDbDir()
 262     {
 263         return dbDir;
 264     }
 265 
 266     private String dbDirName;
 267     protected String getDbDirName()
 268     {
 269         return dbDirName;
 270     }
 271 
 272     protected void startActivationObjects(ORB orb) throws Exception
 273     {
 274         // create Initial Name Service object
 275         initializeBootNaming(orb);
 276 
 277         // create Repository object
 278         repository = new RepositoryImpl(orb, dbDir, orb.orbdDebugFlag );
 279         orb.register_initial_reference( ORBConstants.SERVER_REPOSITORY_NAME, repository );
 280 
 281         // create Locator and Activator objects
 282         ServerManagerImpl serverMgr =
 283             new ServerManagerImpl( orb,
 284                                    orb.getCorbaTransportManager(),
 285                                    repository,
 286                                    getDbDirName(),
 287                                    orb.orbdDebugFlag );
 288 
 289         locator = LocatorHelper.narrow(serverMgr);
 290         orb.register_initial_reference( ORBConstants.SERVER_LOCATOR_NAME, locator );
 291 
 292         activator = ActivatorHelper.narrow(serverMgr);
 293         orb.register_initial_reference( ORBConstants.SERVER_ACTIVATOR_NAME, activator );
 294 
 295         // start Name Service
 296         TransientNameService nameService = new TransientNameService(orb,
 297             ORBConstants.TRANSIENT_NAME_SERVICE_NAME);
 298     }
 299 
 300     protected Locator locator;
 301     protected Locator getLocator()
 302     {
 303         return locator;
 304     }
 305 
 306     protected Activator activator;
 307     protected Activator getActivator()
 308     {
 309         return activator;
 310     }
 311 
 312     protected RepositoryImpl repository;
 313     protected RepositoryImpl getRepository()
 314     {
 315         return repository;
 316     }
 317 
 318     /**
 319      * Go through the list of ORB Servers and initialize and start
 320      * them up.
 321      */
 322     protected void installOrbServers(RepositoryImpl repository,
 323                                      Activator activator)
 324     {
 325         int serverId;
 326         String[] server;
 327         ServerDef serverDef;
 328 
 329         for (int i=0; i < orbServers.length; i++) {
 330             try {
 331                 server = orbServers[i];
 332                 serverDef = new ServerDef(server[1], server[2],
 333                                           server[3], server[4], server[5] );
 334 
 335                 serverId = Integer.valueOf(orbServers[i][0]).intValue();
 336 
 337                 repository.registerServer(serverDef, serverId);
 338 
 339                 activator.activate(serverId);
 340 
 341             } catch (Exception ex) {}
 342         }
 343     }
 344 
 345     public static void main(String[] args) {
 346         ORBD orbd = new ORBD();
 347         orbd.run(args);
 348     }
 349 
 350     /**
 351      * List of servers to be auto registered and started by the ORBd.
 352      *
 353      * Each server entry is of the form {id, name, path, args, vmargs}.
 354      */
 355     private static String[][] orbServers = {
 356         {""}
 357     };
 358 }