站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > JDK 5 Documentation v1.2.2, Java 2 SDK 英文文档

Java IDL: Example 4 - JDK 5 Documentation v1.2.2, Java 2 SDK 英文文档

Example 4: Hello World with Implementation Inheritance


Ordinarily, servant classes must inherit from the ImplBase class generated by the idltojava compiler. This is inadequate for servant classes that need to inherit functionality from another Java class. The Java programming language allows a class only one superclass and the generated ImplBase class occupies this position.

Example 4 illustrates how a servant class can inherit (an implementation) from any Java class. In this example, the HelloServant class inherits its entire implementation from another Java class HelloBasic. At runtime, method requests for HelloServant are delegated to another idltojava-generated class.

Example 4 is identical to Example 1 except for implementation inheritance enhancements. This page only discusses the code necessary for these enhancements.

This section contains:

Instructions for compiling and running the example are provided.

Interface Definition (Hello.idl)

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

Compile the IDL interface with the command:

        idltojava -ftie  Hello.idl
This generates two additional files in a HelloApp subdirectory:
_HelloOperations.java
The servant class will implement this interface.
_HelloTie.java
This class acts as the skeleton, receiving invocations from the ORB and delegating them to the servant that actually does the work.

Implementing the Server (HelloServer.java)

// Copyright and License 

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

class HelloBasic 
{
    public String sayHello()
    {
        return "\nHello world !!\n";
    }
}

class HelloServant extends HelloBasic implements _HelloOperations
{
}

 
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 servant = new HelloServant();
	    Hello helloRef = new _HelloTie(servant);
	    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)

// 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);
	}
    }
}

 

Building and Running Hello World

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 (\).


Home

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