Netscape Internet Service Broker for Java: Programmer's Guide, Version 1.0

[Contents] [Previous] [Next]

Chapter 6
The Tie Mechanism

This chapter describes how to use the tie mechanism in place of inheritance. The tie mechanism offers an alternative when it is not convenient or possible to have your implementation class inherit from the ISB for Java skeleton class. This chapter includes the following major sections:

Overview

The tie mechanism enables an object to support org.omg.CORBA.Object and inherit from another class. You can use it to work around Java's single-inheritance restriction when it is not convenient or possible to have your implementation class inherit from the ISB for Java skeleton class.

The tie mechanism provides a delegator implementation class that inherits from org.omg.CORBA.Object. The delegator implementation does not provide any semantics of its own. Instead, it delegates every call to the real implementation class, which can be implemented separately and can inherit from any class it wishes.

Inheritance is easier to use than the tie mechanism because implementation objects look and behave just like object references. If a client uses an implementation object  in the same process, method calls involve less overhead because no transport, indirection, or delegation of any kind is required.

The Tie Example Program

The tie example is very similar to the sample application presented in "Getting Started" and assumes you are familiar with that application. The key differences are:

The procedures for developing and running a client application or applet are the same whether you use the tie mechanism or not. This sample application was developed under Windows NT 4.0. It assumes that c:\projects\tieMsg is its root directory, that some files reside there, some in a subdirectory named SendMsg. If you recreate this sample application using a different configuration, change code and commands accordingly.

Generating Tie Files

Following is the IDL file (named SendMsg.idl) for this example.

module SendMsg {
    interface Hello {
        string sayHello();
    };
};
The idl2java compiler generates files to support the tie mechanism by default. The following command compiles SendMsg.idl (the -no_comments flag suppresses comments in generated files, reducing clutter for this example).

c:\projects\tieMsg> java2idl -no_comments SendMsg.idl

Implementing Tie Interfaces

The idl2java compiler generates a file containing a code framework you can use to implement interfaces (in this example, the file is _example_Hello.java). After adding your code, save the file using a different name (this example uses the name HelloImpl.java). idl2java also generates an Operations file for each interface in a module. The Operations file provides the Java definition of the interface. To use the tie mechanism, a class implements the corresponding Operations interface. Here, HelloImpl implements HelloOperations. This class is also free to extend another class, if desired; without the tie mechanism, this class would have to extend the skeleton class SendMsg._sk_Hello.

// HelloImpl.java
/**
  The HelloImpl class implements the HelloOperations interface
  defined in the file HelloOperations.java generated by idl2java.
*/
package SendMsg;
/*
   With the tie mechanism, this class can extend
   a class other than SendMsg._sk_Hello.
   Without the tie mechanism:
   public class HelloImpl extends SendMsg._sk_Hello {
*/
public class HelloImpl implements SendMsg.HelloOperations {
/* This constructor is included when idl2java generates _example_Hello.java,
   but it is not valid in this example.
   public HelloImpl(java.lang.String name) {
     super(name);
  }
*/

  public HelloImpl() {
    super();
  }

  public String sayHello() {
    // Implement the operation.
    System.out.println("Tie Client called sayHello.");
    return "Tie Hello!";
  }
}

Writing a Tie Server Application

The code for a server application is largely the same whether you use the tie mechanism or not. When using the tie mechanism, the key step is to initialize a _tie class with an instance of an implementation class to which it delegates every operation. The following example instantiates the implementation class HelloImpl, then passes that instance to the constructor for the _tie class named _tie_Hello.

// Using Enterprise Server's ORB
// MsgService.java
public class MsgService {
   public static void main (String[] args) {
      try {
         // Initialize the ORB.
         org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init();
         // Initialize the BOA.
         org.omg.CORBA.BOA boa = orb.BOA_init();

         // Activate the object implementation.
         // Without tie mechanism:
         // SendMsg.HelloImpl hi = new SendMsg.HelloImpl("TieExample");
         SendMsg.HelloImpl dlgt = new SendMsg.HelloImpl();
         SendMsg.Hello hi = new SendMsg._tie_Hello(dlgt, "TieExample");

         // Export the newly created object.
         boa.obj_is_ready(hi);
         netscape.WAI.Naming.register("http://myHost/TieExample", hi);

         // Or, if using Communicator's ORB, the call would be:
         // netscape.WAI.Naming.register("http://myHost/NameService/TieExample", hi);

         System.out.println(hi + " is ready.");
         // Wait for incoming requests.
         boa.impl_is_ready();
      }
      catch(org.omg.CORBA.SystemException e) {
         System.err.println(e);
      }
   }
}
The _tie_Hello.java file (shown below) is generated by the idl2java compiler. It implements methods by calling the corresponding method exposed by the member _delegate, an instance of a class that implements the operations defined in HelloOperations. For example, when a client calls the sayHello method, the _tie_Hello class calls the sayHello method provided by _delegate. In the server application (MsgService.java), _delegate is initialized with an instance of HelloImpl, which implements the HelloOperations interface.

package SendMsg;
public class _tie_Hello extends SendMsg._sk_Hello {
  private SendMsg.HelloOperations _delegate;

  public _tie_Hello(SendMsg.HelloOperations delegate,
                    java.lang.String name) {
    super(name);
    this._delegate = delegate;
  }

  public _tie_Hello(SendMsg.HelloOperations delegate) {
    this._delegate = delegate;
  }

  public java.lang.String sayHello() {
    return this._delegate.sayHello();
  }
}


[Contents] [Previous] [Next]

Last Updated: 02/04/98 13:46:59


Copyright © 1997 Netscape Communications Corporation

Any sample code included above is provided for your use on an "AS IS" basis, under the Netscape License Agreement - Terms of Use