Exploring CORBA applications in JBuilder

Distributed application development is a feature of JBuilder Enterprise.

This chapter discusses creating a distributed application using JBuilder, VisiBroker, and the Common Object Request Broker Architecture (CORBA).

What is CORBA?

The Common Object Request Broker Architecture (CORBA) allows distributed applications to interoperate (application to application communication), regardless of what language they are written in or where these applications reside.

The CORBA specification was adopted by the Object Management Group to address the complexity and high cost of developing distributed object applications. CORBA uses an object-oriented approach for creating software components that can be reused and shared between applications. Each object encapsulates the details of its inner workings and presents a well defined interface, which reduces application complexity. The cost of developing applications is reduced, because once an object is implemented and tested, it can be used over and over again.

The Object Request Broker (ORB) (shown in Figure 1) connects a client application with the objects it wants to use. The client program does not need to know whether the object implementation it is in communication with resides on the same computer or is located on a remote computer somewhere on the network. The client program only needs to know the object's name and understand how to use the object's interface. The ORB takes care of the details of locating the object, routing the request, and returning the result.

Figure 1    Client program acting on an object

CORBA client program

Note: The ORB itself is not a separate process. It is a collection of libraries and network resources that integrates within end-user applications, and allows your client applications to locate and use objects.

What is VisiBroker?

VisiBroker for Java provides a complete CORBA 2.3 ORB runtime and supporting development environment for building, deploying, and managing distributed Java applications that are open, flexible, and inter-operable. Objects built with VisiBroker for Java are easily accessed by Web-based applications that communicate using OMG's Internet Inter-ORB Protocol (IIOP) standard for communication between distributed objects through the Internet or through local intranets. VisiBroker has a native implementation of IIOP to ensure high-performance and inter-operability.

Figure 2    VisiBroker architecture

[Picture]

How JBuilder and VisiBroker work together

JBuilder provides a set of wizards and other tools to make development of CORBA applications simple, well, as simple as a CORBA application can get anyway. The chapter "CORBA Tools" explains the various CORBA tools and wizards and how they can be used to create CORBA and other distributed applications.

This chapter includes a tutorial to help you become acquainted with some of the JBuilder and VisiBroker for Java development tools, and explores developing a distributed, object-based application using JBuilder and VisiBroker for Java. A sample application, "Tutorial: creating a CORBA application in JBuilder", is used to illustrate each step of the process.

When you develop distributed applications with JBuilder and VisiBroker, you must first identify the objects required by the application. You will then usually follow these steps:

  1. Write a specification for each object using the IDL wizard to generate the Interface Definition Language (IDL) file.

    IDL is the language that an implementer uses to specify the operations that an object will provide and how they should be invoked. In this example, we define, in IDL, the Account interface with a balance() method and the AccountManager interface with an open() method.

  2. Use the IDL compiler to generate the client stub code and server Portable Object Adapter (POA) servant code. For more about the POA, see "What is the POA."

    Using the idl2java compiler, we'll produce client-side stubs (which provide the interface to the Account and the AccountManager objects' methods) and server-side classes (which provides classes for the implementation of the remote objects).

  3. Write the client program code.

    To complete the implementation of the client program, initialize the ORB, bind to the Account and the AccountManager objects, invoke the methods on these object, and print out the balance.

  4. Write the server object code.

    To complete the implementation of the server object code, we must derive from the AccountPOA and AccountManagerPOA classes, provide implementations of the interface's method, and implement the server's main routine.

  5. Compile the client and server code.

    To create the client program, compile the client program code with the client stub. To create the Account server, compile the server object code with the server servants.

  6. Start the server.

  7. Run the client program.

Figure 3    Developing the sample bank application

[Picture]

For more advanced information on using JBuilder and VisiBroker to create distributed applications, see the VisiBroker for Java Programmer's Guide, and the VisiBroker for Java Reference Guide, available from the doc directory of your VisiBroker for Java installation.

For more information about Common Object Request Broker Architecture (CORBA), some useful links are:


Tutorial: creating a CORBA application in JBuilder

In this tutorial, you will explore a sample client application that can query on the balance in a bank account. This tutorial provides the steps to create the basic files needed to run and deploy a simple, CORBA-based distributed application.

From this example, you will learn how to


Set up the project

This tutorial assumes that you are familiar with the JBuilder development environment. If you are new to JBuilder, refer to "The JBuilder environment" in Building Applications with JBuilder for an introduction.

  1. Set up JBuilder and VisiBroker to "see" one another, as described in "Setting up JBuilder for CORBA applications", if you have not already done so.
  2. Select File|New Project from the JBuilder menu to create a new project in JBuilder.
  3. Change the Project Name to BankTutorial. Click Next.
  4. Modify the Project Path from .../jbproject/BankTutorial/BankTutorial.jpx, to .../jbproject/Bank/BankTutorial.jpx.
  5. Click the Add button in Required Libraries if VisiBroker is not included, and select VisiBroker. Click OK to close the select library dialog. The dialog should look like this:

    Project wizard for CORBA tutorial

    Project wizard for CORBA tutorial

  6. Optionally, click the Next button to fill in the Title, Author, Company, and Description fields.
  7. Click Finish.
  8. Check to ensure that VisiBroker is set as the default IDL compiler. To do this,
    1. Select Project|Project Properties.
    2. Select the Build tab.
    3. Ensure that VisiBroker is selected in the IDL Compiler field.
    4. Click OK to close the properties dialog.

The BankTutorial project appears in the project pane. It currently includes one file, BankTutorial.html, which displays the information entered in the Project wizard. Next, we will define the object interfaces by writing the account interface in IDL.


Define the interfaces for the CORBA objects in IDL

The first step to creating a CORBA application is to specify all of your objects and their interfaces using the OMG's Interface Definition Language (IDL). IDL has a syntax similar to C++ and can be used to define modules, interfaces, data structures, and more. The IDL can be mapped to a variety of programming languages. The IDL mapping for Java is summarized in the VisiBroker for Java Reference Guide.

This topic shows how to create the BankTutorial.idl file for this example. The Account interface provides a single method for obtaining the current balance. The AccountManager interface creates an account for the user if one does not already exist.

To create the IDL file, and define the object interfaces,

  1. Select File|New, then select Sample IDL from the Enterprise tab of the Object Gallery.
  2. Type BankTutorial.idl in the File Name field. Click OK. A sample IDL file is created and added to the project.
  3. Remove the sample code, and add the following IDL code to this file by typing it directly into the Source pane.

    module Bank {
        interface Account {
            float balance();
        };
        interface AccountManager {
            Account open(in string name);
        };
    };
    
  4. Select File|Save All. The IDL file must be saved before it will compile.

Next, you use the VisiBroker IDL compiler, named idl2java, to generate stub routines and servant code from the IDL specification. The stub routines are used by your client program to invoke operations on an object. You use the servant code, along with code you write, to create a server that implements the object. The code for the client and object, once completed, is used as input to your Java compiler to produce a client Java applet or application and an object server.


Generating client stubs and server servants

The interface specification you create in IDL is used by the idl2java compiler to generate Java classes for the client program, and skeleton code for the object implementation. The Java classes are used by the client program for all method invocations. You use the skeleton code, along with code you write, to create the server that implements the objects.

The code for the client program and server object, once completed, is used as input to your Java compiler to produce the client and server executables classes.

To generate the client stubs and server servants,

  1. Right-click on the BankTutorial.idl file in the project pane.
  2. Select Make to invoke the idl2java compiler.

If the file had required special handling, you can set IDL properties by right-clicking the IDL file in the project pane, and selecting Properties. This dialog enables you to specify options for compiling remote interfaces defined in IDL.

Generated files

Because Java allows only one public interface or class per file, compiling the IDL file will generate several .java files. These files are stored in a generated sub-directory called Generated Source, which is the module name specified in the IDL and is the package to which the generated files belong. You can view the generated files by clicking the expand glyph beside BankTutorial.idl.

The following files are generated:

For more information about the Helper, Holder, and Operations classes, see the "Generated Classes" chapter in the VisiBroker for Java Reference Manual.


Implement the client

Many of the classes used in implementing the bank client are contained in the Bank package generated by the idl2java compiler as shown in the previous example. This next set of steps creates a Java client to further illustrate this example.

To create the client,

  1. Create a new application that will be used to create the user interface. To do this,
    1. Select File|New, and then select Application from the New tab of the Object Gallery to start the Application wizard.
    2. Accept all defaults.
  2. Create an interface.
    1. Select Wizards|Use CORBA Interface. Accept all defaults.
    2. Click Next.
    3. Select BankTutorial.idl as the IDL file. The other fields are automatically generated.
    4. Select Bank.AccountManager from the Interface list.
    5. Click Next.
    6. Accept the default field name of myAccountManager. Click Finish.
  3. Repeat the steps for the Account Interface. In Step 2, select Bank.Account as the Interface.
  4. Select File|Save All.
  5. Right-click the file Frame1.java in the Project pane. Select Make.

Files named AccountClientImpl1.java and AccountManagerClientImpl1.java display in the Project pane.

Bind to the AccountManager object

The next step is to connect the factory interface (AccountManager) to the Object Request Broker (ORB). To do this,

  1. Select the Frame file in the content pane (double-click it in the project pane to open it in the content pane if it is not visible there). Select the Design tab.
  2. Select the CORBA Express tab of the component palette.
  3. Select an OrbConnect object.
  4. Click in the Structure pane to add an OrbConnect object to the Frame.
  5. Select the object named myAccountManager in the Structure pane.
  6. Select orbConnect1 from the ORBConnect property in the Inspector.
  7. Select the OrbConnect object in the structure pane.
  8. Set its initialize property to true using the Inspector.

Binding the wrapper class at runtime

To bind the wrapper class at runtime,

  1. Select the contentPane object from the Structure pane.
  2. Change the layout property to XYLayout.
  3. Select a JButton control from the Swing tab of the Component palette. Click in the Design pane to add a JButton to the application. This button will be used to create a new account and bind it to the AccountManager reference.
  4. Select the text property of the JButton in the Inspector. Enter Create account in the text field.
  5. Select a JTextField control from the Swing tab. Click in the Design pane to add a JTextField control to the application. The field will be used to enter the name of the new account to be created.
  6. Place a JLabel to the left of the JButton (jLabel1 by default). Enter "Enter name" in its text field using the Inspector.
  7. Select a JLabel control from the Swing tab (jLabel2 by default). Click in the Design pane to add a JLabel control to the application. This label will display the balance in the account.
  8. Select the JButton control. Select the Events tab of the Inspector. Select the actionPerformed event, then double-click its value box to create the event. The focus moves to the Source pane.
  9. Enter the following code in the jButton1_actionPerformed event:

    myAccount.setCorbaInterface(myAccountManager.open(jTextField1.getText()));
    jLabel1.setText("The account balance is " + myAccount.balance());
    
  10. Select File|Save All.

When you've completed these steps, the Designer will look like this:

UI Designer for CORBA tutorial

The generated client class does the following:

  1. Initializes the ORB, using the ORB specified in the OrbConnect properties.

  2. Binds to an AccountManager object.

    Before your client program can invoke the balance() method, it must first use the bind() method to establish a connection to the server that implements the AccountManager object. The implementation of the bind() method is generated automatically by the idl2java compiler. The bind() method requests the ORB to locate and establish a connection to the server. If the server is successfully located and a connection is established, a proxy object is created to represent the server's AccountManagerPOA object. An object reference to the AccountManager object is returned to your client program.

    Next your client class needs to call the open() method on the AccountManager object to get an object reference to the Account object for the specified customer name.

  3. Obtains the balance of the Account using the object reference returned by bind().

    Once your client program has established a connection with an Account object, the balance() method can be used to obtain the balance. The balance() method on the client side is actually a stub generated by the idl2java compiler that gathers all the data required for the request and sends it to the server object.

  4. Displays the balance.

Providing an implementation for the CORBA interface

The application now contains a user interface that will create Account objects, each with a zero balance. To make the example more interesting, we could add a database of customers, account numbers, and balances that could be looked up and used to debit and credit the account with withdrawals and deposits. However, in this simple example, we will simply add an implementation to generate a balance for a given name based on the number of characters in the name.

To implement the CORBA interface,

  1. Expand the banktutorial.Bank.server object in the project pane to expose all files.
  2. Double-click the file AccountImpl.java to open it in the content pane.
  3. Find the balance() method (you can use Ctrl+F). Replace return 0 with
    return _name.length()*100;
    
  4. Select File|Save All.

Implement the server

Just as with the client, many of the files used in implementing the bank server are contained in the BankTutorial package generated when the IDL file is compiled. However, you need to create the server implementation.

From the compiled IDL file, JBuilder can help create multi-tier CORBA applications. Your distributed application can be a Java CORBA server with a Java CORBA client or an HTML client.

The client class implements the client application that obtains the current balance of a bank account. The server class implements the server application that is a default implementation servant.

To create the server,

  1. Select File|New, then select CORBA Server Application from the Enterprise tab. The CORBA Server Application wizard displays.
  2. Select BankTutorial.idl in the IDL File field.
  3. Select Generate Visible Application With Monitor to create a server monitor. Click OK.

The package banktutorial.Bank.server and the file BankServerApp.java are generated. The file BankAppGenFileList.html, is also added to the project, and contains the list of generated files.

The server program, named BankServerApp.java, does the following:

Refer to the VisiBroker for Java Programmer's Guide for a description of the generated files.

What is the POA?

The Portable Object Adaptor, or POA, intercepts a client request and identifies the object that satisfies the client request. The object is then invoked and the response is returned to the client. The POA is a replacement for the Basic Object Adaptor (BOA) and was developed to offer portability on the server side.

The POA is designed to:

For more information on POA, go to the OMG Web site at http://cgi.omg.org/corba/beginners.html.

For information on migrating CORBA applications from BOA to POA, see the Object Management Group Web site at http://cgi.omg.org.


Compile the application

To make the project, select

Any errors will be noted in the message view. Correct any syntax errors before continuing to the next section. For more information on debugging distributed applications, see "Debugging distributed applications."


Run the Java client application

To run the sample java client application,

  1. Make sure the Smart Agent is running.
  2. Start the server implementation.
  3. Start the client application.
  4. Test and deploy the application.
This steps are discussed in more detail in the following sections.

Start the Smart Agent

The Smart Agent, or OsAgent, is the object location service. The client finds the server by using OsAgent and the server advertises services by using OsAgent - it's how they find each other. Ordinarily, you will want the Smart Agent to be running on at least one host in your local network. The Smart Agent is described in detail in "The Smart Agent," in the VisiBroker for Java Programmer's Guide.

To start the Smart Agent,

If you're running Windows NT and you want to start the Smart Agent as an NT Service, you need to register the ORB Services as NT Services during installation. See the VisiBroker for Java Installation Guide for instructions on installing this product. If the Services were registered, you then are able to start the Smart Agent as an NT Service through the Services Control Panel, you do not have to start it from the menu.

Alternatively, you could run the Smart Agent from the command line by running osagent.exe from the VisiBroker bin directory.

Start the server

To start the server implementation,

  1. Right click on the file BankServerApp.java in the project pane.
  2. Choose Run.

The Bank server window displays. Running BankServerApp.java invokes the virtual machine and offers other special features. For command-line options and more information, see "Programmer Tools" in the VisiBroker for Java Reference Manual.

Run the client

To run the Java client,

  1. Right-click on the Application1.java file in the project pane.
  2. Select Run.

The running application appears, and looks like this:

Running CORBA client application

The user interface for the application appears in a window. Enter a name (for example, Paul) in the text field, click the button, and the generated balance is displayed in the label, as shown below:

Running CORBA client with data

The account balance is computed based on the number of characters in the name * 100.

The server monitor prints out a message every time it creates a new Account. You will see the following output on the server side:

Deploy the application

VisiBroker is also used in the deployment phase. This phase occurs when a developer has created client programs or server applications that have been tested and are ready for production. At this point a system administrator is ready to deploy the client programs on end-users' desktops or server applications on server-class machines.

For deployment, the VisiBroker ORB supports client programs on the front end. You must install the ORB on each machine that runs the client program. Clients (that make use of the ORB) on the same host share the ORB. The VisiBroker ORB also supports server applications on the middle tier. You must install the full ORB on each machine that runs the server application. Server applications or objects (that make use of the ORB) on the same server machine share the ORB. Clients may be GUI front ends, applets, or client programs. Server implementations contain the business logic on the middle tier.

JBuilder includes a single DEVELOPMENT LICENSE of VisiBroker for Java "Developer Version". This license must be used in conjunction with JBuilder. Additional developers will require their own VisiBroker development licenses. Deployed programs require a separate VisiBroker deployment license for each server machine.


Other sample applications

Some sample CORBA applications using are provided with VisiBroker, in the examples directory of your VisiBroker installation. Each sample attempts to incorporate functionality described in the VisiBroker for Java Programmer's Guide.