Lab 9 - Introduction to CORBA

Lab 9 - Introduction to CORBA

Lab 7 - Introduction to CORBA

HelloWorld

We will start with a "HelloWorld" application and the CORBA implementation that is built into the Sun JDK.

Take a copy of the sample CORBA application from the Lab 7directory

  • Note what files you have copied and inspect them:
  • Hello.idl is the CORBA IDL description of a very simple application with a single remote method sayHello()
  • HelloServer.java implementes the server:
  • The import line imports the HelloApp package which contains the IDL compiler generated files - the stubs that implement all the fancy CORBA stuff.
  • The sayHello() method is contained in an inner class HelloServant which extends the IDL compiler generated class _HelloImplBase
  • The main() method of the HelloServer class does the following:
  • Create an ORB
  • Create a new instance of HelloServant
  • Connect the servant to the ORB
  • Find the CORBA name server - this consists of two steps:
  • Getting a generic remote object reference to the nameserver
  • Using narrow() to ( in effect ) cast this to a NamingContext reference.
  • Register the servant with the name server using rebind and the name "Hello" ( just like RMI )
  • Wait for remote calls
  • HelloClient.java is a Java application client for this server
  • The same import line as the server
  • Create an ORB
  • Find the CORBA name server as in HelloServerelloServerH
  • Ask the name server for a reference to the named object ("Hello" )
  • Use narrow() to convert this into a reference to the remote object
  • Invoke on the remote object
  • With the directory you have just copied as your current directory run the IDL compiler using:
    IDLTOJAVA -fno-cpp Hello.idl

type

idlj –fserver –fclient Hello.idl

IDLTOJAVA does not work.

“The tool that generates Java bindings froma given IDL file has been renamed from idltojava to idlj. The idltojava compiler was a separate download in JDK 1.2, the idlj compiler is available as part of the J2SE v.1.3 download and can be found in the /bin directory.”

- the -f switch is to prevent the C pre-processor from being run.

  • The stubs & skeletons generated are placed in a directory using the name given in the module directive in the IDL.
  • Compile these files and the sample server/client – do not worry about the “deprecation” message.
  • Start a nameserver using:
    start tnameserv
  • Launch your server with:
    java HelloServer
  • In a separate window launch your client with
    java HelloClient.

CORBA Calculator

You will need to:

Create a package directory and distribution menu item

Call the package/distribution corba/CORBA

This distribution will only work in the Sun JVM

Dealing with the IDL

Write a CORBA IDL file for the plain calculator server. Put this file in the package directory so that all the CORBA files stay together. This is a fairly obvious adaptation of the Hello.idl file that provides:

  • Name the file calculator.idl
  • A module name - I used calcserver
  • An interface name - I used plaincalc
  • The definitions of the methods - for example:
    double add( in double a, in double b );

Compile this file using IDLTOJAVA. In terms of packages, and where things get stored, it would be nice if the IDL generated package ( which will be called calcserver ) were stored in a directory within your corba package ( this will mean that the stubs should be written as being in the corba.calcserverpackage. To achieve this you need to do the following:

  • Change to the main Calc directory
  • Use the following command line:
    idltojava -fno-cpp -p corba corba\calculator.idl ( or whatever you named your IDL file )

(once again, use idlj instead of idltojava) look at

idlj -fserver -fclient -td corba corba\calculator.idl

When this has executed check for a calcserver within the corba directory and inspect one of the files to ensure that it is in the appropriate package.

Making a CORBA Impl
  • Copy a PlainCalculatorImpl from another package and start making changes:
  • Change the package (corba)
  • Import the CORBA classes -as in HelloClient.java
  • Import the IDL generated classes - this should now be corba.calcserver.* (calcserver.*)

Make sure that you are really clear about the following aspects of HelloClient as you transfer things into PlainCalculatorImpl:

  • Where the connection is being made to the server. This needs to be placed in the newConnection() method. In HelloClient this is of type Hello – the name of the interface in the IDL file. For the calculator this will be plaincalc
  • You need to fake things a little with the orb.init() call. The init() method we are calling expects a String [] as its first argument - this is in order to pass special extra settings to the ORB. When this is called from main(), as it is in HelloClient & HelloServer this parameter is conveniently the same as the args parameter to main(). When we are in an Impl we have to create an empty String array using:
    new String[0]
    for the first parameter to init()
  • The reference to HelloHelper becomes plaincalcHelper.
  • In the line where the client sets up the search for the server (new NameComponent) use a name for the server that helps you identify it ( I used “plaincalc” )
  • How the reference to the server is stored so that it can be used each time we invoke
  • How the remote methods are invoked
Setting up the CORBA server

Copy HelloServer into your corba directory and modify it appropriately:

  • Change the package (calcserver)
  • Import the IDL generated classes
  • helloServant becomes plaincalcServant and you need to make similar changes wherever "hello" is mentioned. This is the name of the interface that the server is implementing.
  • In the line where the server is registering with the name service (new NameComponent)use the same name that the client is looking for.
  • Add implementations of the plain calc methods to the plaincalcServant class.
  • Try it out!

A Remote Exception

The object of this exercise is to cause the text associated with an exception at the server to be relayed to the user via the CORBA remote exception handling architecture. This is what you need to think about:

You must modify your IDL file as shown in the lecture adding an exception declaration and associating this exception with the divide() method with a raises statement:

  • The exception is declared within the body of the interface declaration:
    exception divideByZero

{

string message;
};

  • The raises statement is inserted at the end of the line declaring the method:
    raises( divideByZero );

Recompile with IDLTOJAVA and note that yet another package has been generated within your calcserver package (called plaincalcPackage). This extra package contains the stub code for the remote exception class and this must be imported into CalcServer and PlainCalcImpl

Modify your server code so that divide()throws a new one of these exceptions when the second parameter is zero. Include an appropriate message in the constructor.

(you can explicitly set the message variable: e.message = “Division by zero”) where e is the new exception you have created.

The calc.CalculatorGui class already checks for an Exception when it calls divide() and brings up an error box that displays e.getMessage()}. This is the accepted way of extracting the text message from an Exception. The problem is that the message you put in at the server cannot be retrieved by this method - if you look at the Java code for the remote exception class you will see that it does not extend Exception nor does it implement getMessage(). In order for the message to be displayed you need to do something like this in your Impl:

  • Catch the remote exception that you have declared using something like:

catch(divideByZero re)

  • Throw a new Exception with the message of the remote exception passed to the constructor:
    throw( new Exception( re.detailMessage ));

Do this instead:

throw( new Exception( re.message ));
where re is the remote exception that you have caught and message is the member of that class that contains the message.