Home : Course Map : Chapter 19 :
CORBA Server Example
JavaTech
Course Map
Chapter 19

Introduction
Server Demo
Client Demo
Demo Support
Resources

Exercises

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The CORBA demo uses the following two files to create the server:

The package path diagram for the CORBA server files goes as:

javatech
   |
   |__ cor19
                    |
                    |__ server
                          |
                          |__ impl

The code for the two server classes is shown here.

Cor19Server.java

package javatech.cor19.server.impl;

// import all the IDL-generated classes
import javatech.cor19.server.*;

// import required CORBA packages
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.PortableServer.*;

public class Cor19Server
{
  public static void main (String[] args) {
    try {
      // Create and initialize the ORB
      ORB orb = ORB.init(args, null);

      // Get the root naming context
      org.omg.CORBA.Object ns_ref =
        orb.resolve_initial_references ("NameService");

      // Use NamingContextExt which is part of the Interoperable
      // Naming Service specification.
      NamingContextExt ncRef = NamingContextExtHelper.narrow (ns_ref);

      // Get reference to rootpoa & activate the POAManager
      POA rootpoa =
        POAHelper.narrow (orb.resolve_initial_references ("RootPOA"));
      rootpoa.the_POAManager().activate();

      // Create Cor19Servant
      Servant servant = new Cor19Servant (orb, rootpoa);

      // Get the corresponding obj ref.
      org.omg.CORBA.Object obj_ref =
        rootpoa.servant_to_reference (servant);

      // Narrow to the Cor19Example interface.
      Cor19Example cor19 = Cor19ExampleHelper.narrow (obj_ref);

      // Put into the name server.
      String name = "Cor19";
      NameComponent[] path = ncRef.to_name (name);
      ncRef.rebind (path, cor19);

      // Wait for invocations from clients
      System.out.println (
        "Server launched, ready, and waiting on clients..."
      );
      orb.run();
    } // try
    catch (Exception e){
      System.err.println("ERROR: " + e);
      e.printStackTrace(System.out);
    }
  } // main
} // class Cor19Server

Cor19Servant.java

package javatech.cor19.server.impl;

// import all the IDL-generated classes
import javatech.cor19.*;
import javatech.cor19.server.*;

// import required CORBA packages
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.PortableServer.*;

public class Cor19Servant extends Cor19ExamplePOA
{
  // These are not really needed in this example, but it is good
  // practice to keep a reference to the ORB and POA in the servant.
  private ORB fOrb;
  private POA fPoa;
  // Private variables for the huh and hah attributes from the IDL.
  private int fHuh;
  private float fHah = 3.1416f;

  /** Constructor. Receives a reference to the ORB and the POA. **/
  public Cor19Servant (ORB orb, POA poa) {
    fOrb = orb;
    fPoa = poa;
  } // ctor

  /** Readonly hah accessor method. **/
  public float hah () {
    return fHah;
  } // hah

  /** Getter method for huh. **/
  public int huh () {
    return fHuh;
  } // huh (getter)

  /** Setter method for huh. **/
  public void huh (int huh) {
    fHuh = huh;
  } // huh (setter)

  /** Implementation of method1. **/
  public void method1 (String s) {
    String cor19_con = COR19_CONSTANT.value;
    String server_con = SERVER_CONSTANT.value;
    System.out.println (s + "/" + cor19_con + "/" + server_con);
  } // method1

  /** Returns the sum of the input a and b parameters plus the constant
   * COR19_EXAMPLE_CONSTANT. **/
  public int add (int a, int b) {
    return a + b + Cor19Example.COR19_EXAMPLE_CONSTANT;
  } // add

  /** Implementation of the demo() method. Alters the contents of the
   * CustomData object within the CUstomDataHolder object. **/
  public void demo (CustomDataHolder cdh) throws Cor19UserException {
    CustomData cd = cdh.value;
    float some_float = cd.someFloatValue;
    int some_int = cd.someIntegerValue;
    cd.someFloatValue *= 2;
    cd.someIntegerValue *= 3;
  } // demo
} // class Cor19Servant

 

 

Most recent update: Oct. 13, 2005

  
  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.