Example 1: Hello World


This page contains:

Instructions for compiling and running the example are provided, and source code is also included.

Interface Definition (Hello.idl)

The following example OMG IDL describes a CORBA object whose single sayHello() operation returns a string.

module HelloApp
{
    interface Hello
    {
        string sayHello();
    };
};

Compile the IDL interface with the command:

        idltojava  Hello.idl
This generates five files in a HelloApp subdirectory:
_HelloImplBase.java
This abstract class is the server skeleton, providing basic CORBA functionality for the server. It implements the Hello.java interface. The server class HelloServant extends _HelloImplBase.
_HelloStub.java
This class is the client stub, providing CORBA functionality for the client. It implements the Hello.java interface.
Hello.java
This interface contains the Java version of our IDL interface. It contains the single method sayHello(). The Hello.java interface extends org.omg.CORBA.Object, providing standard CORBA object functionality as well.
HelloHelper.java
This final class provides auxiliary functionality, notably the narrow() method required to cast CORBA object references to their proper types.
HelloHolder.java
This final class holds a public instance member of type Hello. It provides operations for out and inout arguments, which CORBA has but which do not map easily to Java's semantics.
To complete the application, you simply provide the server and client implementations in the files HelloServer.java and HelloClient.java.

Implementing the Server (HelloServer.java)

The example server consists of two classes, the servant and the server. The servant, HelloServant, is the implementation of the Hello IDL interface; each Hello instance is implemented by a HelloServant instance. The servant is a subclass of _HelloImplBase, which is generated by the idltojava compiler from the example IDL. The servant contains one method for each IDL operation, in this example, just the sayHello() method. Servant methods are just like ordinary Java methods; the extra code to deal with the ORB, with marshaling arguments and results, and so on, is provided by the server and the stubs.

The server class has the server's main() method, which:

// Copyright and License 

import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
 
class HelloServant extends _HelloImplBase 
{
    public String sayHello()
    {
	return "\nHello world !!\n";
    }
}

 
public class HelloServer {
 
    public static void main(String args[])
    {
	try{
	    // create and initialize the ORB
	    ORB orb = ORB.init(args, null);
 
	    // create servant and register it with the ORB
	    HelloServant helloRef = new HelloServant();
	    orb.connect(helloRef);
 
	    // 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("Hello", "");
	    NameComponent path[] = {nc};
	    ncRef.rebind(path, helloRef);
 
	    // wait for invocations from clients
            java.lang.Object sync = new java.lang.Object();
            synchronized (sync) {
                sync.wait();
            }
 
	} catch (Exception e) {
	    System.err.println("ERROR: " + e);
	    e.printStackTrace(System.out);
	}
    }
}
 

Implementing the Client (HelloClient.java)

The example application client that follows:

// Copyright and License 
 
import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
 
public class HelloClient 
{
    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 objRef = orb.resolve_initial_references("NameService");
            NamingContext ncRef = NamingContextHelper.narrow(objRef);

            // resolve the Object Reference in Naming
            NameComponent nc = new NameComponent("Hello", "");
            NameComponent path[] = {nc};
            Hello helloRef = HelloHelper.narrow(ncRef.resolve(path));
 
	    // call the Hello server object and print results
	    String hello = helloRef.sayHello();
	    System.out.println(hello);
 
	} catch (Exception e) {
	    System.out.println("ERROR : " + e) ;
	    e.printStackTrace(System.out);
	}
    }
}

 

Implementing the Applet Client (HelloApplet.java)

The example applet client that follows:

// Copyright and License 

import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.awt.Graphics;
 
public class HelloApplet extends java.applet.Applet 
{
    public void init() {
	try {
	    // create and initialize the ORB
            ORB orb = ORB.init(this, null);

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

            // resolve the Object Reference in Naming
            NameComponent nc = new NameComponent("Hello", "");
            NameComponent path[] = {nc};
            Hello helloRef = HelloHelper.narrow(ncRef.resolve(path));

            // call the Hello server object and print results
            message = helloRef.sayHello();
 
	} catch (Exception e) {
	    System.out.println("HelloApplet exception: " + e.getMessage());
	    e.printStackTrace(System.out);
        }
    }
 

    public void paint(Graphics g) 
    {
	g.drawString(message, 25, 50);
    }
 
    String message = "";
}
 

Building and Running Hello World

The source code for the Hello World example is located in the examples/hello directory. The following instructions assume you can use port 1050 for the Java IDL name server. Substitute a different port if necessary. Note that for ports below 1024, you need root access on UNIX machines, and administrator privileges on Windows95 and NT. Note also that the instructions use a slash (/) for path names. Windows95 and NT users should subtitute a backslash (\).

NOTE: You must have downloaded the idltojava compiler before you can build and run Hello World. It is available free from the JDC (Java Developer Connection) web page. If you are not already a member, you must register with the JDC to access this page, but there is no cost. The download page is


    http://developer.java.sun.com/developer/earlyAccess/jdk12/idltojava.html

You will get a file with the extension (".bin"). Simply run this file (type the complete filename at the command line) to unbundle its contents . You cannot use the idltojava compiler until after you have unbundled it.


Home

Copyright © 1995-98 Sun Microsystems, Inc. All Rights Reserved.