Chapter 2 Tutorial: Java Components and Java Clients


Creating the application

To create and run the sample application:

  1. Start the Jaguar server and Jaguar Manager
  2. Define a package, component, and method
  3. Generate stubs using the Jaguar Manager
  4. Compile the stub code
  5. Write the server-side code
  6. Write the client-side code
  7. Create the HTML page and run the sample

Start the Jaguar server and Jaguar Manager

Steps Start the Jaguar server:

  1. If the Jaguar server is not already running, follow the instructions under "Starting Jaguar" to start the server.


Steps Start Jaguar Manager:

  1. If Jaguar Manager is not already running, start it as described in "Starting Jaguar Manager and Security Manager".


Define a package, component, and method

This section shows you how to use Jaguar Manager to create the package, component, and method for the sample application.

For complete information on creating packages, components, and methods, see the Jaguar CTS Programmer's Guide.

Define a new package

In Jaguar, a package is a unit of deployment for a group of components that perform related tasks. A component must be installed in a package before it can be instantiated by applications. All components created in the Jaguar tutorials are installed in the Tutorial package.

Steps Create the Tutorial package if it does not exist:

  1. Navigate to:

    Jaguar Manager | Servers | Jaguar

  2. In the right panel, click Installed Packages. If the Tutorial package is displayed, skip to the next section, "Define and install a new component".

  3. Select File | Install Package.

    In the Package wizard, select Create and Install a New Package.

    For the package name, enter Tutorial .

  4. Click Create New Package.

    You see the Package Properties window.

  5. Click OK.

    When you click on the Installed Packages expansion sign, it now includes Tutorial.


Define and install a new component

Steps Using Jaguar Manager, define a new component for the package you just created:

  1. Click on the Tutorial package.

  2. Select File | Install Component.

  3. In the Component wizard, select Create and Install a New Component.

    For the component name, enter JavaArithmetic .

  4. Click Finish.

    You see the Component Properties window.

  5. Select the General tab. Fill in the fields as follows:

    Field

    Value

    Description

    Tutorial Java component

    Component Type

    Java - CORBA

    Java Class

    Sample.Intro.JavaArithmetic.JavaArithmeticImpl

  6. Leave the remaining fields at their default settings.

  7. Click OK.


Define the multiply method

Steps After the component is defined, define methods for it that the client will call:

  1. Expand the Tutorial package. Double-click the JavaArithmetic component to show the Roles and Interfaces folders beneath it.

  2. Double-click the Interfaces folder, and highlight the Tutorial::JavaArithmetic interface.

  3. Select File | New Method.

  4. Assign the name multiply to the method.

  5. Click Create New Method.

    You see the Method Properties window.

  6. In the Return field, select double as the method's return type.

  7. Beneath the empty parameter list, click Add to add a parameter. In the New Parameter dialog:

  8. Click OK to close the New Parameter dialog box.

  9. Repeat steps 7 and 8 to add a second parameter named m2 with a Type of double.

  10. Click OK to close the Method Properties dialog box.


Generate stubs using the Jaguar Manager

Once you have created the package, component, and methods, you need to generate the stub files for the component. The client-side applet uses the stubs to invoke the server-side component methods.

Steps Generate the stub files for the component:

  1. Click on the Tutorial package and select the JavaArithmetic component.

  2. Select File | Generate Stub/Skeleton.

  3. Select Generate Stubs, then select Generate Java Stubs. Fill in the Java Stubs fields as follows:

  4. Unselect Generate C++ Stubs.

  5. Select Generate Skeletons.

    Under Skeletons in the Java Code Base field, type the full path to your Jaguar java\classes subdirectory, for example:
    %JAGUAR%\java\classes


  6. Click Generate.


Compile the stub code

Change to the Jaguar html/classes/Tutorial subdirectory and compile the generated files using a JDK 1.1 compiler, for example:

cd %JAGUAR%\html\classes\Tutorial
d:\j11 *.java

Write the server-side code

At this point, Jaguar Manager has created server-side implementation files in the following directory:

%JAGUAR%\java\classes\Sample\Intro\JavaArithmetic

The implementation template file is JavaArithemeticImpl.Java.new and the skeleton is called _sk_Tutorial_JavaArithmetic.java.

Steps Complete the component implementation and compile the classes:

  1. Rename JavaArithemeticImpl.Java.new to JavaArithmeticImpl.Java (that is, delete the .new extension). Open the renamed file in a text editor, then find the definition of the multiply method. Change the definition so that it matches the one below:
        double multiply
    (double m1,
    double m2)
    {
    return m1 * m2;
    }


  2. Save your changes.

  3. Compile the component skeleton and implementation files using a JDK 1.1 compiler--for example:
    cd %JAGUAR%\html\classes\Sample\Intro\JavaArithmetic

    d:\j11 *.java



Write the client-side code

In the html\classes subdirectory of your Jaguar installation, create a new directory called TutorialApps if it does not exist. In this directory, create the Java file below as JAClient.java.

You can find a copy of JAClient.java in the html/docs/tutorial subdirectory of your Jaguar installation. Here is the source for JAClient.java:

//
// This is a sample client that invokes the
// JavaArithmetic component created in the Jaguar
// Java component tutorial.
//
package TutorialApps;

import org.omg.CORBA.*;
import org.omg.CosNaming.*; // CORBA CosNaming interfaces
import org.omg.CosNaming.NamingContextPackage.*;
// CosNaming user exceptions
import SessionManager.*;
import java.awt.*;

import Tutorial.*; // Package for component stub classes

public class JAClient extends java.applet.Applet {

// User interface controls
Button mult_button;
TextField m1_text;
TextField m2_text;
TextField result_text;

// Component's name (relative to the server's
// initial name context), of the form package/component
String _compName = "Tutorial/JavaArithmetic";

// Component stub instance
Tutorial.JavaArithmetic _comp = null;

public void init()
{

// Draw GUI controls
m1_text = new TextField(" ");
m1_text.setText("2.5");
this.add(m1_text);
Label l1 = new Label("*");
this.add(l1);
m2_text = new TextField(" ");
m2_text.setText("3.1");
this.add(m2_text);
Label l2 = new Label("=");
this.add(l2);
result_text = new TextField(" ");
result_text.setEditable(false);
this.add(result_text);
mult_button = new Button("Multiply");
this.add(mult_button);

try {

//
// Initialize the CORBA client-side ORB and
// obtain a stub for the Jaguar component instance.
//
System.out.println("... Creating Jaguar session.");

//
// Initialize the ORB. Note that the org.omg.CORBA.ORBClass
// property must be set in applet parameters.
//
java.util.Properties props = new java.util.Properties();
ORB orb = ORB.init(this, props);

NamingContext nc = null;
org.omg.CORBA.Object objRef = null;
objRef = orb.resolve_initial_references("NameService");
nc = NamingContextHelper.narrow(objRef);

System.out.println("... Creating component instance.");

//
// Create a stub object instance for the
// Tutorial/JavaArithmetic Jaguar component.
//

NameComponent compNc[] =
{ new NameComponent(_compName, "") };
Factory compFactory =
FactoryHelper.narrow ( nc.resolve(compNc) );
_comp = Tutorial.JavaArithmeticHelper.narrow(
compFactory.create("Guest", "GuestPassword") );

System.out.println("... Created component instance.");

} catch (NotFound nfe) {
// This can happen if you have installed
// the tutorial component under a different
// package or component name in Jaguar Manager.
System.out.println("Error: Component " + _compName
+ " not found. Check the package and component "
+ "name in Jaguar Manager.\n");
System.out.println("NotFound exception details:\n");
nfe.printStackTrace();
_comp = null;
} catch (org.omg.CORBA.UserException ue) {
// Check for other CosNaming exceptions
System.out.println("CORBA CosNaming exception:\n"
+ ue.toString());
ue.printStackTrace();
_comp = null;
} catch (org.omg.CORBA.SystemException se) {
System.out.println(
"Received CORBA system exception "
+"while instantiating component:\n"
+ se.toString() );
se.printStackTrace();
_comp = null;
}

} // init()

// Handle button clicks
public boolean action(Event e, java.lang.Object arg) {
if (e.target == mult_button) {
doMultiply();
return true;
}
else return false;
}

// Call the multiply method and update the displayed result
private void doMultiply() {

// Harvest user input
Double d1 = null;
Double d2 = null;
try {
d1 = new Double(m1_text.getText());
d2 = new Double(m2_text.getText());
} catch (NumberFormatException nfe)
{
this.showStatus("ERROR: Bad number format.");
result_text.setText("");
return;
}

// Call the server component method
double m1 = d1.doubleValue();
double m2 = d2.doubleValue();
try {
double result = _comp.multiply(m1, m2);
result_text.setText((new Double(result)).toString());
}
catch (org.omg.CORBA.SystemException se)
{
System.out.println(
"Exception executing multiply method: "
+ se.toString() );
se.printStackTrace();
}

} // doMultiply

}

Compile this application using a JDK 1.1 compiler:

j11 JAClient.java

Create the HTML page and run the sample

In a text editor, create an HTML file that calls the applet. Name the file runjavatut.html and create it in the html/classes/TutorialApps subdirectory in your Jaguar installation.

A copy of this file is provided in your Jaguar installation directory, in the html/docs/tutorial subdirectory. You can copy this file to your html/classes/TutorialApps as an alternative to creating one. Here is the text of runjavatut.html:

<html><body bgcolor="#FFFFFF">
<head><title>This Applet runs the Jaguar tutorial Java component.</head></title>
<hr>

<center>
<applet
codebase="/classes"
code="TutorialApps/JAClient.class"
width=600 height=400>
<h2>This would be a Cool Applet,
but you are not running a Java enabled browser...</h2>
<!-- Specify the Java ORB implementation class -->
<param name = "org.omg.CORBA.ORBClass"
value = "com.sybase.CORBA.ORB">
<!-- Specify the URL for connections to the
Jaguar CosNaming service.
"iiop://:9000/" means to use port 9000 on the
applet's download host, with no initial
name context -->
<param name = "com.sybase.CORBA.NameServiceURL"
value = "iiop://:9000/">
</applet>
</center>

<hr>
</body></html>

No changes are required to this HTML file if your server is configured to use the default IIOP port number, 9000. If you (or your administrator) changed the IIOP port number after installing Jaguar, edit the port number in the HTML file to match.

Run the sample

If you have not restarted the Jaguar server since creating the JavaArithmetic component, do so now before running the sample.

Steps Configuring browsers to run applets on development machines

If you are running the sample client on a machine where EAServer has been installed, you must configure your browser to eliminate conflicts between Jaguar classes that are downloaded with the applet and classes that are loaded from the local classpath. The simplest way to do this is to run the browser from a batch file as described here:

  1. Locate the browser executable, and create a batch file in the same directory with the same base filename, for example netscape.bat or iexplore.bat.

  2. Add these commands to the batch file, where browser.exe is the name of the browser executable:
    @ECHO OFF
    SETLOCAL
    SET CLASSPATH=""
    START browser.exe %*
    ENDLOCAL



  3. Change shortcuts that run the browser so that they launch the batch file instead of the browser executable.


Steps To run the sample:

  1. Start a Netscape 4.x, Internet Explorer 4.x, or HotJava Web browser. If necessary, you can run the applet on a different machine than the Jaguar host, as long as your server uses a real host address and not localhost or 127.0.0.1.

  2. Connect to the following URL, substituting your Jaguar server's host name for host:
    http://host:8080/classes/TutorialApps/
    runjavatut.html


    As installed, Jaguar uses 8080 as the HTTP port number. If your server uses a different HTTP port number, change the port number in the URL to match.

  3. Enter numeric values to be multiplied, then click the Multiply button to invoke the Jaguar method. The return value (product) is displayed.


Debugging

If everything is working, you should be able to download the applet, watch as it is drawn, then invoke the multiply method by clicking the applet's Multiply button. If not, check the browser's Java console for error messages. Also check the Jaguar server log file (srv.log in the Jaguar bin subdirectory).

If the applet connects to the server and instantiates the component successfully, you will see ... created Jaguar session and ... created component instance in the browser's Java console.

 


Copyright © 2000 Sybase, Inc. All rights reserved.