Chapter 2 Creating CORBA Java Components and Clients
To create and run the sample application:
Starting EAServer
Starting EAServer Manager
This section shows you how to use EAServer Manager to create the package, component, and method for the sample application.
For complete information on creating packages, components, and methods, see Chapter 5, "Defining Component Interfaces," in the EAServer Programmer's Guide.
In EAServer, a package is a unit of deployment for a group of components that perform related tasks. Before a component can be instantiated by clients, it must be installed in a package, and that package must be installed in the server. The steps below create the package and component within the predefined "Jaguar" server to satisfy these requirements.
Creating the Tutorial package if it does not exist
Tutorial
.
Defining the new component
JavaArithmetic
.
| Field | Value |
|---|---|
| Description | Tutorial Java component |
| Component Type | Java - CORBA |
| Java Class | Sample.Intro.JavaArithmetic.JavaArithmeticImpl |
The component interface will have one method, multiply.
Defining the component interface
m1
.
in
.
double
.
m2
with
a Type of double.
Once you have created the package, component, and methods, you generate the stub and skeleton files for the component. The client-side application uses the stubs to invoke the server-side component methods. The skeleton acts as an interface between EAServer and your component methods.
Generating the stub and skeleton files for the component
%JAGUAR%\html\classes
|
For Windows |
$JAGUAR/html/classes
|
For UNIX |
%JAGUAR%\java\classes
|
For Windows |
$JAGUAR/java/classes
|
For UNIX |
At this point, EAServer Manager has created server-side implementation files in the following directory under your EAServer installation:
java/classes/Sample/Intro/JavaArithmetic
The implementation template file is JavaArithemeticImpl.Java.new and the skeleton is called _sk_Tutorial_JavaArithmetic.java.
Completing the component implementation
double multiply
(double m1,
double m2)
{
return m1 * m2;
}
cd %JAGUAR%\java\classes\Sample\Intro\JavaArithmetic %JAGUAR%\bin\jc.bat *.java
cd $JAGUAR/java/classes/Sample/Intro/JavaArithmetic $JAGUAR/bin/jc *.java
In the html\classes subdirectory of your EAServer installation, create a new directory called TutorialApps if it does not exist. In this directory, create the Java file below as JAConsole.java.
This is a simple command-line application that:
You can find a copy of JAConsole.java in the html/docs/tutorial/java-corba subdirectory of your EAServer installation. Here is the source for JAConsole.java:
//
// This is a sample command-line Java application that
// invokes the JavaArithmetic component created in the EAServer
// Java component tutorial.
//
// Usage:
// arith iiop://<host>:<port>
//
// Where:
//
// <host> is the host name or IP address of the server machine.
//
// <iiop-port> is the server's IIOP port (9000 in the
// default configuration).
//
//
package TutorialApps;
import org.omg.CORBA.*;
import SessionManager.*;
import Tutorial.*; // Package for EAServer stub classes
public class JAConsole {
static public void main(String options[]) {
String _usage = "Usage: JAConsole iiop://<host>:<port>\n";
String _ior = null;
try {
if (options.length >= 1)
{
_ior = options[0];
}
else
{
System.out.println(_usage);
return;
}
//
// Initialize the CORBA client-side ORB and
// obtain a stub for the EAServer component instance.
//
System.out.println("... Creating session.");
//
// Initialize the ORB.
//
java.util.Properties props = new java.util.Properties();
props.put("org.omg.CORBA.ORBClass", "com.sybase.CORBA.ORB");
ORB orb = ORB.init(options, props);
//
// Create an instance of the EAServer SessionManager::Manager
// CORBA IDL object.
//
Manager manager = ManagerHelper.narrow(orb.string_to_object(_ior));
//
// Create an authenticated session with user "Guest" and password
// "GuestPassword".
//
Session session = manager.createSession("Guest", "GuestPassword");
System.out.println("... Creating component instance.");
//
// Create a stub object instance for the
// Tutorial/JavaArithmetic EAServer component.
//
Tutorial.JavaArithmetic comp =
Tutorial.JavaArithmeticHelper.narrow(
session.create("Tutorial/JavaArithmetic"));
if (comp == null)
{
System.out.print("ERROR: Null component instance. ");
System.out.print(
"Check Jaguar Manager and verify that the component ");
System.out.print(
"Tutorial/JavaArithmetic exists and that it implements the ");
System.out.println(
"Tutorial::JavaArithmetic IDL interface.");
return;
}
System.out.println("... Created component instance.");
//
// Invoke the multiply method.
//
System.out.println("... Multiplying:\n");
double m1 = 3.1;
double m2 = 2.5;
double result = comp.multiply(m1, m2);
System.out.println(" " + m1 + "*" + m2 + "=" + result);
// Explicitly catch exceptions that can occur due to user error,
// and print a generic error message for any other CORBA system
// exception.
} catch ( org.omg.CORBA.COMM_FAILURE cfe)
{
// The server is not running, or the specified URL is
// wrong.
System.out.println(
"Error: could not connect to server at " + _ior + "\n"
+ "Make sure the specified address is correct and the "
+ "server is running.\n\n" + _usage );
} catch ( org.omg.CORBA.OBJECT_NOT_EXIST cone )
{
// Requested object (component) does not exist.
System.out.println(
"Error: CORBA OBJECT_NOT_EXIST exception. Check the "
+ "server log file for more information. Also verify "
+ "that the Tutorial/JavaArithmetic "
+ "component has been created properly in "
+ "Jaguar Manager. \n");
} catch (org.omg.CORBA.NO_PERMISSION npe) {
// Login failed, or the component requires an authorization role
// that this user is not a member of.
System.out.println(
"Error: CORBA NO_PERMISSION exception. Check whether "
+ "login authentication is enabled for your server and "
+ "whether the component has restricted access.\n");
npe.printStackTrace();
} catch (org.omg.CORBA.SystemException se)
{
// Generic CORBA exception
System.out.println(
"Received CORBA system exception: "
+ se.toString() );
se.printStackTrace();
}
return;
} // main()
}
Compile the application source using a JDK 1.3 or later compiler, for example:
%JAGUAR%\bin\jc JAConsole.java
If you have not refreshed or restarted the server since creating the JavaArithmetic component, refresh the server before running the client program.
Create a batch file or UNIX shell script to run the client application, then run it. The batch file or shell script configures the CLASSPATH environment variable, then runs the application using the JDK 1.3 java program included with your EAServer installation.
If necessary, you can run the client on a different machine than the server host, as long as your server uses a real host address and not localhost or 127.0.0.1.
Creating the Windows batch file
call %JAGUAR%\bin\setenv.bat set CLASSPATH=%JAGUAR%\java\lib\easj2ee.jar; set CLASSPATH=%CLASSPATH%;%JAGUAR%\java\lib\easclient.jar set CLASSPATH=%CLASSPATH%;%JAGUAR%\html\classes set JAVA_HOME=%JAGUAR_JDK13% %JAVA_HOME%\jre\bin\java TutorialApps.JAConsole %*
Creating the UNIX shell script
#!/bin/sh . $JAGUAR/bin/setenv.sh CLASSPATH=$JAGUAR/java/lib/easj2ee.jar CLASSPATH=$CLASSPATH:$JAGUAR/java/lib/easclient.jar CLASSPATH=$CLASSPATH:$JAGUAR/html/classes export CLASSPATH JAVA_HOME=$JAGUAR_JDK13 $JAVA_HOME/jre/bin/java TutorialApps.JAConsole $*
chmod 777 runja
Running the client application
runja iiop://host:iiop-port
runja iiop://myhost:9000
| Copyright (C) 2004. Sybase Inc. All rights reserved. |
| |