Previous | Next | Trail Map | Programming with Java IDL | The Hello Client Server Example

Developing the Hello World Server

This lesson introduces the basics of writing a CORBA transient server. The steps in this lesson are:

Here's a completed version of HelloServer.java.

Performing Basic Setup

The structure of a CORBA server program is the same as most Java applications: You import required library packages, declare the server class, define a main method, and remember to handle any exceptions.

Importing Required Packages
Start your text editor and save a new file titled HelloServer.java.

Import the packages required for the client class:

// The package containing our stubs.
import HelloApp.*;

// HelloServer will use the naming service.
import org.omg.CosNaming.*;

// The package containing special exceptions thrown by the name service.
import org.omg.CosNaming.NamingContextPackage.*;

// All CORBA applications need these classes.
import org.omg.CORBA.*;

Declaring the Server Class
Declare the server class:

public class HelloServer 
{
    // Add the main method here in the next step.
}

Defining the main Method
Declare a standard main method:

public static void main(String args[])
{
    // Add the try-catch block here in the next step.
}

Handling CORBA System Exceptions
Because all CORBA programs can throw CORBA system exceptions at runtime, you will place all of the main functionality within a try-catch block. CORBA programs throw runtime exceptions whenever trouble occurs during any of the processes (marshaling, unmarshaling, upcall) involved in invocation. The exception handler simply prints the exception and its stack trace to standard output so you can see what kind of thing has gone wrong.

Inside main, set up a try-catch block:

try {
    // Add the rest of the HelloServer code here.
} catch(Exception e) {
    System.err.println("ERROR: " + e);
    e.printStackTrace(System.out);
}

Creating an ORB Object

Just like a client, a CORBA server also needs a local ORB object. Every server instantiates an ORB and registers its servant objects so that the ORB can find the server when it receives an invocation for it.

If you closed HelloServer.java, open it now.

Inside the try-catch block, declare and initialize an ORB variable:

ORB orb = ORB.init(args, null);

The call to the ORB's init() method passes in the server's command line arguments, allowing you to set certain properties at runtime.

Managing the Servant Object

A server is a process that instantiates one or more servant objects. The servant implements the interface generated by idltojava and actually performs the work of the operations on that interface. Our HelloServer needs a HelloServant.

Instantiating the Servant Object

Inside the try-catch block, just below the call to init(), instantiate the servant object:

HelloServant helloRef = new HelloServant();

This servant class isn't defined yet; you will do that in a later step.

Next, connect the servant to the ORB, so that the ORB can recognize invocations on it and pass them along to the correct servant:

orb.connect(helloRef);

Defining the Servant Class
At the end of HelloServer.java, outside the HelloServer class, define the class for the servant object.

  1. Declare the servant class:
    class HelloServant extends _HelloImplBase
    {
        // Add the sayHello() method here in the next step.
    }
    

    The servant is a subclass of _HelloImplBase so that it inherits the general CORBA functionality generated for it by the compiler.

  2. Declare the required sayHello method:
    public String sayHello()
    {
        // Add the method implementation here in the next step.
    }
    
  3. Write the sayHello implementation:
    return "\nHello world!!\n";
    

Working with COS Naming

The HelloServer works with the naming service to make the servant object's operations available to clients. The server needs an object reference to the name service, so that it can register itself and ensure that invocations on the Hello interface are routed to its servant object.

Obtaining the Initial Naming Context
In the try-catch block, below instantiation of the servant, call orb.resolve_initial_references to get an object reference to the name server:

org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");

The string NameService is defined for all CORBA ORBs. When you pass in that string, the ORB returns a naming context object that is an object reference for the name service.

Narrowing the Object Reference
As with all CORBA object references, objRef is a generic CORBA object. To use it as a NamingContext object, you must narrow it to its proper type. Add the call to narrow just below the previous statement:

NamingContext ncRef = NamingContextHelper.narrow(objRef);

Here you see the use of an idltojava-generated helper class, similar in function to HelloHelper. The ncRef object is now an org.omg.CosNaming.NamingContext and you can use it to access the naming service and register the server. You will do that in the next step.

Registering the Servant with the Name Server

  1. Just below the call to narrow, create a new NameComponent member:
    NameComponent nc = new NameComponent("Hello", "");
    

    This statement sets the id field of the new NameComponent, nc to "Hello" and the kind component to the empty string.

    Because the path to the Hello has a single element, create the single-element array that NamingContext.resolve requires for its work:

    NameComponent path[] = {nc};
    
  2. Finally, pass path and the servant object to the naming service, binding the servant object to the "Hello" id:
    ncRef.rebind(path, helloRef);
    

    Now, when the client calls resolve("Hello") on the initial naming context, the naming service returns an object reference to the Hello servant.

Waiting for Invocation

The server is ready; it simply needs to wait around for a client to request its service. To achieve that, enter the following code at the end of (but within) the try-catch block:

java.lang.Object sync = new java.lang.Object();
synchronized(sync) {
    sync.wait();
}

This form of Object.wait requires HelloServer to remain alive (though quiescent) until an invocation comes from the ORB. Because of its placement in main, after an invocation completes and sayHello returns, the server will wait again.

Compiling and Running the Hello World Server

To run HelloServer, you need some client files that you may not have created. These files are provided for you in [Path_to_JDK]\docs\guide\idl\tutorial\server. Copy them as needed to build your project directory.

UNIX users note that you should substitute slashes (/) for the backslashes (\) in all paths in this document.

Server Setup

  1. Create a new project directory, called Server.

  2. Copy the files HelloServer.java and HelloClient.class from the directory [Path_to_JDK]\docs\guide\idl\tutorial\server to your Server directory.

  3. Copy the directory [Path_to_JDK]\docs\guide\idl\tutorial\server\HelloApp and its entire contents to the Server directory.

Your project directory should look like this:

Server
 |-HelloServer.java
 |-HelloClient.class
 |-HelloApp
    |-_HelloImplBase.class
    |-_HelloStub.class
    |-Hello.class
    |-HelloHelper.class
    |-HelloHolder.class

Compiling the Server

  1. Change directory to the Server directory you created.

  2. Run the Java compiler on HelloServer.java:
    javac HelloServer.java
    
  3. Correct any errors in your file and recompile if necessary. (You can copy the file from the [Path_to_JDK]\docs\guide\idl\tutorial\server directory if you have trouble finding your typographical errors).

  4. You should see HelloServer.class and HelloServant.class in the Server directory.

Running the Hello World Server
To be certain that you are running your own server program, check to see if you have left a server running from a previous lesson and stop it if necessary.

  1. Start the Java IDL name server:
    tnameserv -ORBInitialPort 1050 &
    
  2. Start the Hello server:
    java HelloServer -ORBInitialPort 1050 &
    
  3. Run the Hello application client in another window:
    java HelloClient -ORBInitialPort 1050
    

    The string prints to the command line:

    Hello world!!
    

Remember to stop both server processes before continuing to the next lesson.

For More Information

[PENDING]


Previous | Next | Trail Map | Programming with Java IDL | The Hello Client Server Example