Make your own free website on Tripod.com

Developing a CORBA Server
Gopalan Suresh Raj

Note
To work with any of these samples, you will need the following:
.........................................JDK 1.2 (Java 2)
.........................................
The idltojava compiler

The Steps involved in developing a CORBA Server are:

  1. Define your Interface as an IDL file
  2. Compile the IDL and generate the stubs and skeletons
  3. Develop the servant class by writing an implementation for your interface
  4. Develop the CORBA Server application
  5. Compile the files, run the CORBA Naming Service provided with JDK 1.2
  6. Run the CORBA Server

1. Define your Interface as an IDL file
Define a module called SimpleStocks with an interface called StockMarket. Place this in a file called StockMarket.idl.

StockMarket.idl
module SimpleStocks {
  interface StockMarket {
    float get_price( in string symbol );
  };
};

2. Compile the IDL file and generate stubs and skeletons
When compiled with the idltojava compiler, the stubs and skeletons are placed in a package named after our module - SimpleStocks

E:\MyProjects\corba\StockCorba>
E:\MyProjects\corba\StockCorba>idltojava -fno-cpp stockmarket.idl

E:\MyProjects\corba\StockCorba>

3. Develop the servant class by writing an implementation for our interface
Notice that we extend _StockMarketImplBase. This class which we use as the parent is generated for us and is placed in the generated SimpleStocks package.

StockMarketImpl.java
/**
 * StockMarketImpl
 */

import org.omg.CORBA.*;    
// All CORBA applications need these classes.
import SimpleStocks.*;     
// The package containing our stubs

public class StockMarketImpl extends _StockMarketImplBase {

  public float get_price( String symbol ) {
    float price = 0;
    for(int i = 0; i < symbol.length(); i++) {
      price += (int) symbol.charAt( i );
    }
    price /= 5;
    return price;
  }

  public StockMarketImpl() { super(); }

}

4. Develop the CORBA server application

StockMarketServer.java
/**
 * StockMarketServer Main
 */
import org.omg.CORBA.*;     // All CORBA applications need these classes.
import org.omg.CosNaming.*;
// Server will use the naming service.
import SimpleStocks.*;     
// The package containing our stubs

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

      // Create the servant and register it with the ORB
      StockMarketImpl stockMarketImpl = new StockMarketImpl();
      orb.connect(stockMarketImpl);

      // Get the root naming context
      org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
      NamingContext ncRef = NamingContextHelper.narrow(objRef);

 
     
// Bind the object reference in naming
      NameComponent nc = new NameComponent("NASDAQ", "");
      NameComponent path[] = {nc};
      ncRef.rebind(path, stockMarketImpl);

 
      System.out.println("The StockMarket Server is up and ready...");

      // Wait forever for current thread to die
      Thread.currentThread().join();
    }
    catch( Exception e ) { e.printStackTrace(); }
  }
 
}

5. Compile the files, run the CORBA Naming Service provided with JDK 1.2
The naming service is called tnameserv and is part of the JDK1.2 download. It is a CORBA Naming Service implementation provided by JavaSoft.

E:\MyProjects\corba\StockCorba>
E:\MyProjects\corba\StockCorba>javac *.java

E:\MyProjects\corba\StockCorba>tnameserv
Initial Naming Context:
IOR:000000000000002849444c3a6f6d672e6f72672f436f734e616d696e672f4e61
6d696e67436f6e746578743a312e3000000000010000000000000030000100000000
0008686f6d655f7063000422000000000018afabcafe0000000272e141a700000008
0000000000000000
TransientNameServer: setting port for initial object references to: 900

6. Run the CORBA Server

E:\MyProjects\corba\StockCorba>
E:\MyProjects\corba\StockCorba>java StockMarketServer
The StockMarket Server is up and ready...

 

About the Author...
Gopalan Suresh Raj is a Software Architect, Developer and an active Author. He is contributing author to a couple of books "Enterprise Java Computing-Applications and Architecture" (Cambridge University Press, June '99) and "The Awesome Power of JavaBeans" (Manning, July'98). His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia site (http://www.execpc.com/~gopalan) or mail him at gopalan@execpc.com.

 

click here to go to the
Developing a CORBA Client Page...
click here to go to
My CORBA HomePage...

Go to the Component Engineering Cornucopia page

This site was developed and is maintained by Gopalan Suresh Raj

This page has been visited times since September 21,1998.

Last Updated : Dec 19, '98

If you have any questions, comments, or problems regarding this site, please write to me I would love to hear from you.


Copyright (c) 1997-99, Gopalan Suresh Raj - All rights reserved. Terms of use.

All products and companies mentioned at this site,are trademarks of their respective owners.