1 /*
   2  * Copyright (c) 1997, 2004, 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("-port")) {
 188                 if ((i+1) < args.length) {
 189                     props.put(ORBConstants.ORBD_PORT_PROPERTY, args[++i]);
 190                 } else {
 191                     System.out.println(CorbaResourceUtil.getText(
 192                         "orbd.usage", "orbd"));
 193                 }
 194             } else if (args[i].equals("-defaultdb")) {
 195                 if ((i+1) < args.length) {
 196                     props.put(ORBConstants.DB_DIR_PROPERTY, args[++i]);
 197                 } else {
 198                     System.out.println(CorbaResourceUtil.getText(
 199                         "orbd.usage", "orbd"));
 200                 }
 201             } else if (args[i].equals("-serverid")) {
 202                 if ((i+1) < args.length) {
 203                     props.put(ORBConstants.SERVER_ID_PROPERTY, args[++i]);
 204                 } else {
 205                     System.out.println(CorbaResourceUtil.getText(
 206                         "orbd.usage", "orbd"));
 207                 }
 208             } else if (args[i].equals("-serverPollingTime")) {
 209                 if ((i+1) < args.length) {
 210                     props.put(ORBConstants.SERVER_POLLING_TIME, args[++i]);
 211                 } else {
 212                     System.out.println(CorbaResourceUtil.getText(
 213                         "orbd.usage", "orbd"));
 214                 }
 215             } else if (args[i].equals("-serverStartupDelay")) {
 216                 if ((i+1) < args.length) {
 217                     props.put(ORBConstants.SERVER_STARTUP_DELAY, args[++i]);
 218                 } else {
 219                     System.out.println(CorbaResourceUtil.getText(
 220                         "orbd.usage", "orbd"));
 221                 }
 222             }
 223         }
 224     }
 225 
 226     /**
 227      * Ensure that the Db directory exists. If not, create the Db
 228      * and the log directory and return true. Otherwise return false.
 229      */
 230     protected boolean createSystemDirs(String defaultDbDir)
 231     {
 232         boolean dirCreated = false;
 233         Properties props = System.getProperties();
 234         String fileSep = props.getProperty("file.separator");
 235 
 236         // determine the ORB db directory
 237         dbDir = new File (props.getProperty( ORBConstants.DB_DIR_PROPERTY,
 238             props.getProperty("user.dir") + fileSep + defaultDbDir));
 239 
 240         // create the db and the logs directories
 241         dbDirName = dbDir.getAbsolutePath();
 242         props.put(ORBConstants.DB_DIR_PROPERTY, dbDirName);
 243         if (!dbDir.exists()) {
 244             dbDir.mkdir();
 245             dirCreated = true;
 246         }
 247 
 248         File logDir = new File (dbDir, ORBConstants.SERVER_LOG_DIR ) ;
 249         if (!logDir.exists()) logDir.mkdir();
 250 
 251         return dirCreated;
 252     }
 253 
 254     protected File dbDir;
 255     protected File getDbDir()
 256     {
 257         return dbDir;
 258     }
 259 
 260     private String dbDirName;
 261     protected String getDbDirName()
 262     {
 263         return dbDirName;
 264     }
 265 
 266     protected void startActivationObjects(ORB orb) throws Exception
 267     {
 268         // create Initial Name Service object
 269         initializeBootNaming(orb);
 270 
 271         // create Repository object
 272         repository = new RepositoryImpl(orb, dbDir, orb.orbdDebugFlag );
 273         orb.register_initial_reference( ORBConstants.SERVER_REPOSITORY_NAME, repository );
 274 
 275         // create Locator and Activator objects
 276         ServerManagerImpl serverMgr =
 277             new ServerManagerImpl( orb,
 278                                    orb.getCorbaTransportManager(),
 279                                    repository,
 280                                    getDbDirName(),
 281                                    orb.orbdDebugFlag );
 282 
 283         locator = LocatorHelper.narrow(serverMgr);
 284         orb.register_initial_reference( ORBConstants.SERVER_LOCATOR_NAME, locator );
 285 
 286         activator = ActivatorHelper.narrow(serverMgr);
 287         orb.register_initial_reference( ORBConstants.SERVER_ACTIVATOR_NAME, activator );
 288 
 289         // start Name Service
 290         TransientNameService nameService = new TransientNameService(orb,
 291             ORBConstants.TRANSIENT_NAME_SERVICE_NAME);
 292     }
 293 
 294     protected Locator locator;
 295     protected Locator getLocator()
 296     {
 297         return locator;
 298     }
 299 
 300     protected Activator activator;
 301     protected Activator getActivator()
 302     {
 303         return activator;
 304     }
 305 
 306     protected RepositoryImpl repository;
 307     protected RepositoryImpl getRepository()
 308     {
 309         return repository;
 310     }
 311 
 312     /**
 313      * Go through the list of ORB Servers and initialize and start
 314      * them up.
 315      */
 316     protected void installOrbServers(RepositoryImpl repository,
 317                                      Activator activator)
 318     {
 319         int serverId;
 320         String[] server;
 321         ServerDef serverDef;
 322 
 323         for (int i=0; i < orbServers.length; i++) {
 324             try {
 325                 server = orbServers[i];
 326                 serverDef = new ServerDef(server[1], server[2],
 327                                           server[3], server[4], server[5] );
 328 
 329                 serverId = Integer.valueOf(orbServers[i][0]).intValue();
 330 
 331                 repository.registerServer(serverDef, serverId);
 332 
 333                 activator.activate(serverId);
 334 
 335             } catch (Exception ex) {}
 336         }
 337     }
 338 
 339     public static void main(String[] args) {
 340         ORBD orbd = new ORBD();
 341         orbd.run(args);
 342     }
 343 
 344     /**
 345      * List of servers to be auto registered and started by the ORBd.
 346      *
 347      * Each server entry is of the form {id, name, path, args, vmargs}.
 348      */
 349     private static String[][] orbServers = {
 350         {""}
 351     };
 352 }