Chapter 2 Tutorial: Java Components and Java Clients
To create and run the sample application:
Start the Jaguar server:
Start Jaguar Manager:
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.
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.
Create the Tutorial package
if it does not exist:
Tutorial
. Using Jaguar Manager, define a new component for
the package you just created:
JavaArithmetic
.Field |
Value |
---|---|
Description |
Tutorial Java component |
Component Type |
Java - CORBA |
Java Class |
Sample.Intro.JavaArithmetic.JavaArithmeticImpl |
After the component is defined, define methods
for it that the client will call:
m1
.
in
.
double
.
m2
with a Type of double.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.
Generate the stub files for the component:
%JAGUAR%\html\classes
%JAGUAR%\java\classes
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
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.
Complete the component implementation and compile
the classes:
double multiply
(double m1,
double m2)
{
return m1 * m2;
}
cd %JAGUAR%\html\classes\Sample\Intro\JavaArithmetic
d:\j11 *.java
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
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.
If you have not restarted the Jaguar server since creating the JavaArithmetic component, do so now before running the sample.
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:
@ECHO OFF
SETLOCAL
SET CLASSPATH=""
START browser.exe %*
ENDLOCAL
To run the sample:
http://host:8080/classes/TutorialApps/
runjavatut.html
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. |
![]() |