FileDocCategorySizeDatePackage
ServerNamingInit.javaAPI DocExample1798Wed Apr 05 11:25:42 BST 2000None

ServerNamingInit.java

/*
 * This example is from the book "Java Enterprise in a Nutshell".
 * Copyright (c) 1999 by O'Reilly & Associates.  
 * You may distribute this source code for non-commercial purposes only.
 * You may study, modify, and use this example for any purpose, as long as
 * this notice is retained.  Note that this example is provided "as is",
 * WITHOUT WARRANTY of any kind either expressed or implied.
 */

//
// CORBA example 8: Registering an object with the Naming Service
//

import org.omg.CORBA.*;
import org.omg.CosNaming.*;

/**
 * ServerNamingInit: Register a local CORBA ThisOrThatServer directly with
 *                   a COS Naming Service.
 *
 * Example 4-8, Java Enterprise in a Nutshell, 1st ed.
 * Author: Jim Farley
 */

public class ServerNamingInit {
  public static void main(String[] argv) {
    try {
      // Obtain ORB reference
      ORB myORB = ORB.init(argv, null); 

      // Make a ThisOrThatServer object to register with the ORB
      ThisOrThatServer impl = new ThisOrThatServerImpl();

      // Get the root name context
      org.omg.CORBA.Object objRef = 
        myORB.resolve_initial_references("NameService");
      NamingContext nc = NamingContextHelper.narrow(objRef);

      // Register the local object with the Name Service
      NameComponent ncomp = new NameComponent("ThisOrThatServer", "");
      NameComponent[] name = {ncomp};
      nc.rebind(name, impl);

      // Go into a wait state, waiting for clients to connect
      System.out.println("Waiting for clients...");
      java.lang.Object dummy = new String("I wait...");
      synchronized (dummy) {
        dummy.wait();
      }
    }
    catch (Exception e) {
      System.out.println("An error occurred while initializing server object:");
      e.printStackTrace();
    }
  }
}