New York
University
Computer
Science Department
Courant
Institute of Mathematical Sciences
Coding
Examples for VisiBroker, OrbixWeb, and Java
Course Title: Application Servers Course
Number: g22.3033-011
Instructor: Jean-Claude Franchitti Session: 4
·
Problem
statement and IDL file
·
UML
Object Diagrams showing the structure of VisiBroker classes for ImplBase
·
UML
Object Diagrams showing the structure of VisiBroker classes for TIE
·
Coding
the client with the Static Invocation Interface - SII
·
Coding
the client with the Dynamic Invocation Interface - DII
·
Coding
the server with the BOA - ImplBase approach
·
Coding
the server with the BOA - TIE approach
·
Coding
client and server with the Interoperable Object Reference - IOR
·
Coding
client and server with the Name Service - CosNaming
·
Interoperable
ORB example using IOR with CosNaming
·
Portable
Object Adapter (POA) with the CORBA 2.3 IDL/Java Language Mapping
·
Portable
Object Adapter (POA) with the Interoperable Object Reference - IOR
·
Portable
Object Adapter (POA) with the Name Service - CosNaming
Downloads:
You will need the JavaSoft
JDK and Inprise VisiBroker for
Java
The code examples were tested on the following
software versions:
·
JDK
1.3, 1.1.8, 1.1.7, 1.1.6, 1.1.5
·
JavaIDL
JDK 1.2 beta 4
·
VisiBroker
for Java 4.1, 4.0, 3.3, 3.2
·
OrbixWeb
3.0 patch 4
·
Win98,
Win95
Getting Started:
Win95 system
environment settings for JavaSoft JDK 1.1.7 for directory C:\jdk1.1.7:
REM
SET PATH=C:\jdk1.1.7\bin;%PATH%
SET CLASSPATH=C:\jdk1.1.7;C:\jdk1.1.7\lib\classes.zip;%CLASSPATH%
REM
Win95 system
environment settings for Inprise VisiBroker for Java 3.3 (including Naming and Event
Service). This assumes that the installation directory is C:\Inprise\vbroker:
REM VisiBroker for Java
3.3
REM
SET VBROKER_ADM=C:\Inprise\vbroker\adm
SET PATH=C:\Inprise\vbroker\bin;%PATH%
SET CLASSPATH=C:\Inprise\vbroker\lib\vbjcosnm.jar;%CLASSPATH%
SET CLASSPATH=C:\Inprise\vbroker\lib\vbjcosev.jar;%CLASSPATH%
SET OSAGENT_ADDR=ip-address
REM
Win95 system
environment settings for Visigenic VisiBroker for Java 3.2 (including Naming
and Event Service). This assumes that the installation directory is
C:\Visigenic\vbroker:
REM VisiBroker for Java
3.2
REM
SET VBROKER_ADM=C:\Visigenic\vbroker\adm
SET PATH=C:\Visigenic\vbroker\bin;%PATH%
SET CLASSPATH=C:\Visigenic\vbroker\lib\vbjcosnm.jar;%CLASSPATH%
SET CLASSPATH=C:\Visigenic\vbroker\lib\vbjcosev.jar;%CLASSPATH%
SET OSAGENT_ADDR=ip-address
REM
Make sure also
that the OSAgent is running on the network and that your system can access it.
Problem Statement:
The
application that we will build involves an AircraftCarrier class and an Aircraft
class. The client application will send a message to an AircraftCarrier
instance to launch an Aircraft instance. Once the Aircraft has been launched,
it can be queried for it's code number and also ordered to change altitude.
The IDL file:
// Ship.idl
module Ship {
interface Aircraft {
string codeNumber();
void attitude(inout long altiude, in string direction);
};
interface
AircraftCarrier {
Aircraft launch(in string name);
};
};
UML Object
Diagrams showing the structure of VisiBroker classes for ImplBase
UML Object Diagrams showing the structure of
VisiBroker classes for TIE
Coding the
client with the Static Invocation Interface - SII
Client.java
public class
Client {
public static void main(String[] args) {
if(args.length != 2) {
System.out.println("Usage: vbj Client <carrier-name>
<aircraft-name>\n");
return;
}
String carrierName = args[0];
String aircraftName = args[1];
org.omg.CORBA.ORB
orb = null;
try {
orb = org.omg.CORBA.ORB.init(args, null);
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("ORB init failure " + se);
System.exit(1);
}
org.omg.CORBA.BindOptions
bindOptions = new org.omg.CORBA.BindOptions();
bindOptions.defer_bind = false;
bindOptions.enable_rebind = true;
// Third
parameter in bind can be IP Address like 127.0.0.1
// or Hostname
Ship.AircraftCarrier carrier = null;
try {
carrier = Ship.AircraftCarrierHelper.bind(orb, carrierName,
"127.0.0.1", bindOptions);
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("ORB bind failure " + se);
System.exit(1);
}
Ship.Aircraft
aircraft = null;
try {
aircraft = carrier.launch(aircraftName);
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("Carrier launch failure " + se);
System.exit(1);
}
String
designation = aircraft.codeNumber();
System.out.println("Aircraft
" + designation + " is airborne");
org.omg.CORBA.IntHolder
alt = new org.omg.CORBA.IntHolder(10000);
try {
aircraft.attitude(alt, "headup");
System.out.println(designation + " going up to " + alt.value + "
Ft.");
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("Aircraft instruction failure " + se);
System.exit(1);
}
try {
aircraft.attitude(alt, "headdown");
System.out.println(designation + " going down to " + alt.value +
" Ft.");
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("Aircraft instruction failure " + se);
System.exit(1);
}
}
}
Building the client:
Create
a directory and place Ship.idl and Client.java into it.
prompt> idl2java Ship.idl
prompt> vbjc Client.java
Coding the
client with the Dynamic Invocation Interface - DII
Client.java
public class
Client {
public static void main(String[] args) {
if(args.length != 2) {
System.out.println("Usage: vbj Client <carrier-name>
<aircraft-name>\n");
return;
}
String carrierName = args[0];
String aircraftName = args[1];
org.omg.CORBA.Object carrier = null;
org.omg.CORBA.Object aircraft = null;
org.omg.CORBA.ORB
orb = null;
try {
orb = org.omg.CORBA.ORB.init(args, null);
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("ORB init failure " + se);
System.exit(1);
}
{ // scope
try {
carrier = orb.bind("IDL:Ship/AircraftCarrier:1.0", carrierName, null,
null);
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("ORB init failure " + se);
System.exit(1);
}
org.omg.CORBA.Request request = carrier._request("launch");
request.add_in_arg().insert_string(aircraftName);
request.set_return_type(orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_objref));
request.invoke();
aircraft = request.result().value().extract_Object();
}
{ // scope
org.omg.CORBA.Request request = aircraft._request("codeNumber");
request.set_return_type(orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_string));
request.invoke();
String designation = request.result().value().extract_string();
System.out.println ("Aircraft " + designation + " is coming your
way");
}
{ // scope
org.omg.CORBA.Request request = aircraft._request("attitude");
int altitude
= 10000;
org.omg.CORBA.Any ioAltitude = request.add_inout_arg();
ioAltitude.insert_long(altitude);
String
direction = "headup";
request.add_in_arg().insert_string(direction);
request.invoke();
altitude =
ioAltitude.extract_long();
System.out.println
("Aircraft is heading up to " + altitude + " Feet.");
}
}
}
Building the client:
Create a
directory and place Ship.idl and Client.java into it.
prompt> idl2java
Ship.idl
prompt> vbjc Client.java
Coding the
server with the BOA - ImplBase approach
Server.java
public class Server {
public static void main(String[] args) {
org.omg.CORBA.ORB orb = null;
try {
orb = org.omg.CORBA.ORB.init(args, null);
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("ORB init failure " + se);
System.exit(1);
}
org.omg.CORBA.BOA
boa = null;
try {
boa = orb.BOA_init();
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("BOA init failure " + se);
System.exit(1);
}
Ship.AircraftCarrier
carrier = new AircraftCarrierImpl("Nimitz");
try {
boa.obj_is_ready(carrier);
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("Object Ready failure " + se);
System.exit(1);
}
System.out.println(carrier
+ " ready for launch !!!");
try {
boa.impl_is_ready();
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("Impl Ready failure " + se);
System.exit(1);
}
}
}
AircraftCarrierImpl.java
public class
AircraftCarrierImpl extends Ship._AircraftCarrierImplBase {
public AircraftCarrierImpl(String name) {
super(name);
}
public
AircraftCarrierImpl() {
super();
}
public
Ship.Aircraft launch(String name) {
Ship.Aircraft aircraft = new AircraftImpl(name);
_boa().obj_is_ready(aircraft);
System.out.println(aircraft
+ " launched on Catapult 2");
return aircraft;
}
}
AircraftImpl.java
public class AircraftImpl
extends Ship._AircraftImplBase {
private String _codeNumber;
public
AircraftImpl(String codeNumber) {
super(codeNumber);
_codeNumber = codeNumber;
}
public String
codeNumber() {
return _codeNumber;
}
public void
attitude(org.omg.CORBA.IntHolder altiude, String direction) {
if (direction.compareTo("headup") == 0)
altiude.value += 1000;
else if (direction.compareTo("headdown") == 0)
altiude.value -= 1000;
// otherwise no change in altitude
}
}
Building the server:
Create
a directory and place Ship.idl, Server.java, AircraftCarrierImpl.java, and
AircraftImpl.java into it.
prompt> idl2java
Ship.idl
prompt> vbjc Server.java
Running the program:
You
should have previously built the client.
Now
start the Smart Agent and server in one window, client in another window
prompt> osagent
prompt> vbj Server
prompt> vbj Client Nimitz "Ghost 202"
Coding the
server with the BOA - TIE approach
Server.java
public class Server {
public static void main(String[] args) {
org.omg.CORBA.ORB orb = null;
try {
orb = org.omg.CORBA.ORB.init(args, null);
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("ORB init failure " + se);
System.exit(1);
}
org.omg.CORBA.BOA
boa = null;
try {
boa = orb.BOA_init();
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("BOA init failure " + se);
System.exit(1);
}
// TIE uses
these two new lines
AircraftCarrierImpl
new_carrier = new AircraftCarrierImpl(boa);
Ship.AircraftCarrier carrier = new Ship._tie_AircraftCarrier(new_carrier,
"Nimitz");
try {
boa.obj_is_ready(carrier);
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("Object Ready failure " + se);
System.exit(1);
}
System.out.println(carrier
+ " ready for launch !!!");
try {
boa.impl_is_ready();
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("Impl Ready failure " + se);
System.exit(1);
}
}
}
AircraftCarrierImpl.java
public class AircraftCarrierImpl implements Ship.AircraftCarrierOperations {
// data
member and constructor for TIE
private
org.omg.CORBA.BOA _boa;
public AircraftCarrierImpl(org.omg.CORBA.BOA boa) {
_boa = boa;
}
public
Ship.Aircraft launch(String name) {
AircraftImpl new_aircraft = new AircraftImpl(name);
Ship.Aircraft aircraft = new Ship._tie_Aircraft(new_aircraft);
_boa.obj_is_ready(aircraft);
System.out.println(aircraft
+ " on Catapult 2");
return
aircraft;
}
}
AircraftImpl.java
public class
AircraftImpl implements Ship.AircraftOperations {
private
String _codeNumber;
public
AircraftImpl(String codeNumber) {
_codeNumber = codeNumber;
}
public String
codeNumber() {
return _codeNumber;
}
public void
attitude(org.omg.CORBA.IntHolder altiude, String direction) {
if (direction.compareTo("headup") == 0) {
altiude.value += 1000;
System.out.println("Going UP !!! " + altiude.value);
}
else if (direction.compareTo("headdown") == 0) {
altiude.value -= 1000;
System.out.println("Going DOWN !!! " + altiude.value);
}
else System.out.println("No CHANGE !!! " + altiude.value);
// otherwise no change in altitude
}
}
Building the server:
Create
a directory and place Ship.idl, Server.java, AircraftCarrierImpl.java, and
AircraftImpl.java into it.
prompt> idl2java
Ship.idl
prompt> vbjc Server.java
Running the program:
You should
have previously built the client.
Now start the Smart Agent and server in one window, client in another window
prompt> osagent
prompt> vbj Server
prompt> vbj Client Nimitz "Ghost 202"
Coding client
and server with the Interoperable Object Reference - IOR
Downloads:
You
will need OrbixWeb to test the client
Getting Started:
Win95
system environment settings for OrbixWeb:
REM
SET PATH=C:\Iona\OrbixWeb3.0\bin;%path%
REM
Server.java
import
java.io.*;
public class Server {
public static void main(String[] args) {
org.omg.CORBA.ORB orb = null;
try {
orb = org.omg.CORBA.ORB.init(args, null);
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("ORB init failure " + se);
System.exit(1);
}
org.omg.CORBA.BOA
boa = null;
try {
boa = orb.BOA_init();
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("BOA init failure " + se);
System.exit(1);
}
// TIE uses
these two new lines
AircraftCarrierImpl
new_carrier = new AircraftCarrierImpl(boa);
Ship.AircraftCarrier carrier = new Ship._tie_AircraftCarrier(new_carrier,
"Nimitz");
try {
boa.obj_is_ready(carrier);
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("Object Ready failure " + se);
System.exit(1);
}
System.out.println(carrier
+ " ready for launch !!!");
// Write the
Aircraft Carrier's object reference to an IOR file so Orbix Client can access
try
{
FileWriter output = new FileWriter("ns.ior");
output.write(orb.object_to_string(carrier));
output.close();
System.out.println("Wrote AircraftCarrier's IOR to file: ns.ior");
}
catch(java.io.IOException e)
{
System.out.println("Exception: " + e);
System.exit(1);
}
// End IOR
try {
boa.impl_is_ready();
}
catch (org.omg.CORBA.SystemException se) {
System.err.println("Impl Ready failure " + se);
System.exit(1);
}
}
}
Client.java
import
java.io.*;
public class
Client
{
public static void main(String[] args)
{
org.omg.CORBA.ORB orb = null;
try {
orb = org.omg.CORBA.ORB.init(args, null);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("ORB init failure " + se);
System.exit(1);
}
org.omg.CORBA.Object
objRef = null;
// Read IOR
from file
try
{
LineNumberReader input = new LineNumberReader(new
FileReader("ns.ior"));
objRef = orb.string_to_object(input.readLine());
}
catch(java.io.IOException e)
{
System.out.println("Exception: " + e);
System.exit(1);
}
// End IOR
Ship.AircraftCarrier
carrier = null;
try {
carrier = Ship.AircraftCarrierHelper.narrow(objRef);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("AircraftCarrier narrow failure " + se);
System.exit(1);
}
// Standard
program continues
String flight
= args.length > 0 ? args[0]: "Ghost Rider 101";
Ship.Aircraft
aircraft = null;
try {
aircraft = carrier.launch(flight);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Carrier launch failure " + se);
System.exit(1);
}
String
designation = aircraft.codeNumber();
System.out.println
("Aircraft " + designation + " is coming your way");
}
}
Building the server with VisiBroker:
Create a
directory and place Ship.idl, Server.java, AircraftCarrierImpl.java, and
AircraftImpl.java into it.
prompt> idl2java
Ship.idl
prompt> vbjc Server.java
Building the client with OrbixWeb:
prompt> idl Ship.idl
prompt> owjavac Client.java java_output\Ship\*.java
Running the program:
Now
start the Smart Agent and server in one window, client in another window
prompt> osagent
prompt> vbj Server
prompt> owjava Client
Coding client
and server with the Name Service - CosNaming
Server.java
import java.io.*;
import org.omg.CosNaming.*;
public class Server
{
public static void main(String[] args)
{
org.omg.CORBA.ORB orb = null;
try {
orb = org.omg.CORBA.ORB.init(args, null);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("ORB init failure " + se);
System.exit(1);
}
org.omg.CORBA.BOA
boa = null;
try {
boa = orb.BOA_init();
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("BOA init failure " + se);
System.exit(1);
}
// TIE uses
these two new lines
AircraftCarrierImpl
new_carrier = new AircraftCarrierImpl(boa);
Ship.AircraftCarrier carrier = new Ship._tie_AircraftCarrier(new_carrier,
"Nimitz");
try {
boa.obj_is_ready(carrier);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Object Ready failure " + se);
System.exit(1);
}
org.omg.CORBA.Object initRef = null;
try {
initRef = orb.resolve_initial_references("NameService");
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Resolve init failure " + se);
System.exit(1);
}
catch (org.omg.CORBA.UserException ue)
{
System.err.println("Resolve init failure " + ue);
System.exit(1);
}
NamingContext
initContext = null;
try {
initContext = NamingContextHelper.narrow(initRef);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Context narrow failure " + se);
System.exit(1);
}
NameComponent[]
name = new NameComponent[1];
NamingContext
objContext = null;
try {
name[0] = new NameComponent("objects", "");
objContext = initContext.bind_new_context(name);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Bind init failure " + se);
System.exit(1);
}
catch (org.omg.CORBA.UserException ue)
{
System.err.println("Bind init failure " + ue);
System.exit(1);
}
NamingContext milContext = null;
try {
name[0] = new NameComponent("military", "");
milContext = objContext.bind_new_context(name);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Bind obj failure " + se);
System.exit(1);
}
catch (org.omg.CORBA.UserException ue)
{
System.err.println("Bind obj failure " + ue);
System.exit(1);
}
try {
name[0] = new NameComponent("navy", "");
milContext.rebind(name, carrier);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Bind mil failure " + se);
System.exit(1);
}
catch (org.omg.CORBA.UserException ue)
{
System.err.println("Bind mil failure " + ue);
System.exit(1);
}
System.out.println(carrier
+ " ready for launch !!!");
try {
boa.impl_is_ready();
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Impl Ready failure " + se);
System.exit(1);
}
}
}
Client.java
import
org.omg.CosNaming.*;
public class Client
{
public static void main(String[] args)
{
org.omg.CORBA.ORB orb = null;
try {
orb = org.omg.CORBA.ORB.init(args, null);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("ORB init failure " + se);
System.exit(1);
}
org.omg.CORBA.Object
initRef = null;
try {
initRef = orb.resolve_initial_references("NameService");
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Resolve init failure " + se);
System.exit(1);
}
catch (org.omg.CORBA.UserException ue)
{
System.err.println("Resolve init failure " + ue);
System.exit(1);
}
NamingContext initContext = null;
try {
initContext = NamingContextHelper.narrow(initRef);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Context narrow failure " + se);
System.exit(1);
}
NameComponent[]
name = new NameComponent[3];
name[0] = new NameComponent("objects", "");
name[1] = new NameComponent("military", "");
name[2] = new NameComponent("navy", "");
org.omg.CORBA.Object
objRef = null;
try {
objRef = initContext.resolve(name);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Resolve name failure " + se);
System.exit(1);
}
catch (org.omg.CORBA.UserException ue)
{
System.err.println("Resolve name failure " + ue);
System.exit(1);
}
Ship.AircraftCarrier
carrier = null;
try {
carrier = Ship.AircraftCarrierHelper.narrow(objRef);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("AircraftCarrier narrow failure " + se);
System.exit(1);
}
// Standard
program continues
String flight = args.length > 0 ? args[0]: "Ghost Rider 101";
Ship.Aircraft
aircraft = null;
try {
aircraft = carrier.launch(flight);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Carrier launch failure " + se);
System.exit(1);
}
String
designation = aircraft.codeNumber();
System.out.println("Aircraft
" + designation + " is airborne");
org.omg.CORBA.IntHolder
alt = new org.omg.CORBA.IntHolder(10000);
try {
aircraft.attitude(alt, "headup");
System.out.println(designation + " going up to " + alt.value + "
Ft.");
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Carrier launch failure " + se);
System.exit(1);
}
try {
aircraft.attitude(alt, "headdown");
System.out.println(designation + " going down to " + alt.value +
" Ft.");
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Carrier launch failure " + se);
System.exit(1);
}
}
}
Building the server with VisiBroker:
Create a
directory and place Ship.idl, Server.java, AircraftCarrierImpl.java, and
AircraftImpl.java into it.
prompt> idl2java Ship.idl
prompt> vbjc Server.java
Building the client with VisiBroker:
Place
Client.java into the same directory as above or another directory together with
Ship.idl
prompt> idl2java
Ship.idl
prompt> vbjc Client.java
Running the program:
Three
windows will be required
Smart
Agent and NameService in one window, server in one window, client in another
prompt> osagent
prompt> vbj -DJDKrenameBug -DORBservices=CosNaming
com.visigenic.vbroker.services.CosNaming.ExtFactory objects navy.log
prompt>
vbj -DORBservices=CosNaming -DSVCnameroot=objects Server
prompt>
vbj -DORBservices=CosNaming -DSVCnameroot=objects Client "SunDowner
102"
Note:
It
is usually easier to create three BAT files to contain the commands above.
Interoperable
ORB Example using IOR with CosNaming
Downloads:
You
will need JavaIDL to test the client
Getting Started:
Win95
system environment settings for JavaIDL:
REM
SET PATH=C:\jdk1.2beta4\bin;%path%
REM
Introduction:
This
example extends the IDL from the previous pages, and also demonstrates Threading.
Ship.idl
// Ship.idl
#pragma
prefix "jcf"
module Ship {
interface
Aircraft {
string codeNumber();
void run();
readonly attribute long fuelCapacity;
};
interface
AircraftCarrier {
Aircraft launch(in string name);
};
};
Server.java
import
java.io.*;
import org.omg.CosNaming.*;
public class Server
{
public static void main(String[] args)
{
org.omg.CORBA.ORB orb = null;
try {
orb = org.omg.CORBA.ORB.init(args, null);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("ORB init failure " + se);
System.exit(1);
}
org.omg.CORBA.BOA
boa = null;
try {
boa = orb.BOA_init();
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("BOA init failure " + se);
System.exit(1);
}
// TIE uses
these two new lines
AircraftCarrierImpl
new_carrier = new AircraftCarrierImpl(boa);
Ship.AircraftCarrier carrier = new Ship._tie_AircraftCarrier(new_carrier,
"Nimitz");
try {
boa.obj_is_ready(carrier);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Object Ready failure " + se);
System.exit(1);
}
org.omg.CORBA.Object initRef = null;
try {
initRef = orb.resolve_initial_references("NameService");
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Resolve init failure " + se);
System.exit(1);
}
catch (org.omg.CORBA.UserException ue)
{
System.err.println("Resolve init failure " + ue);
System.exit(1);
}
NamingContext
initContext = null;
try {
initContext = NamingContextHelper.narrow(initRef);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Context narrow failure " + se);
System.exit(1);
}
// Write
object reference to an IOR file so Orbix Client can access
try
{
FileWriter output = new FileWriter("ns.ior");
output.write(orb.object_to_string(initRef));
output.close();
System.out.println("Wrote IOR to file: ns.ior");
}
catch(java.io.IOException e)
{
System.out.println("Exception: " + e);
System.exit(1);
}
NameComponent[]
name = new NameComponent[1];
NamingContext objContext = null;
try {
name[0] = new NameComponent("objects" , "");
objContext = initContext.bind_new_context(name);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Bind init failure " + se);
System.exit(1);
}
catch (org.omg.CORBA.UserException ue)
{
System.err.println("Bind init failure " + ue);
System.exit(1);
}
NamingContext
milContext = null;
try {
name[0] = new NameComponent("military" , "");
milContext = objContext.bind_new_context(name);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Bind obj failure " + se);
System.exit(1);
}
catch (org.omg.CORBA.UserException ue)
{
System.err.println("Bind obj failure " + ue);
System.exit(1);
}
try {
name[0] = new NameComponent("navy" , "");
milContext.rebind(name, carrier);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Bind mil failure " + se);
System.exit(1);
}
catch (org.omg.CORBA.UserException ue)
{
System.err.println("Bind mil failure " + ue);
System.exit(1);
}
System.out.println(carrier
+ " ready for launch !!!");
try {
boa.impl_is_ready();
}
catch
(org.omg.CORBA.SystemException se)
{
System.err.println("Impl Ready failure " + se);
System.exit(1);
}
}
}
Client.java
import
java.io.*;
import org.omg.CosNaming.*;
public class
Client
{
public static void main(String[] args)
{
org.omg.CORBA.ORB orb = null;
try {
orb = org.omg.CORBA.ORB.init(args, null);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("ORB init failure " + se);
System.exit(1);
}
org.omg.CORBA.Object
initRef = null;
// Read IOR
from file
try
{
LineNumberReader input = new LineNumberReader(new
FileReader("ns.ior"));
initRef = orb.string_to_object(input.readLine());
}
catch(java.io.IOException e)
{
System.out.println("Exception: " + e);
System.exit(1);
}
// End IOR
NamingContext
initContext = null;
try {
initContext = NamingContextHelper.narrow(initRef);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Context narrow failure " + se);
System.exit(1);
}
NameComponent[]
name = new NameComponent[3];
name[0] = new NameComponent("objects", "");
name[1] = new NameComponent("military", "");
name[2] = new NameComponent("navy", "");
org.omg.CORBA.Object
objRef = null;
try {
objRef = initContext.resolve(name);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Resolve name failure " + se);
System.exit(1);
}
catch (org.omg.CORBA.UserException ue)
{
System.err.println("Resolve name failure " + ue);
System.exit(1);
}
Ship.AircraftCarrier
carrier = null;
try {
carrier = Ship.AircraftCarrierHelper.narrow(objRef);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("AircraftCarrier narrow failure " + se);
System.exit(1);
}
// Standard
program continues
String flight
= args.length > 0 ? args[0]: "Ghost Rider 101";
Ship.Aircraft aircraft = null;
try {
aircraft = carrier.launch(flight);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Carrier launch failure " + se);
System.exit(1);
}
String designation
= null;
try {
designation = aircraft.codeNumber();
System.out.println("Aircraft " + designation + " is
airborne");
}
catch
(org.omg.CORBA.SystemException se)
{
System.err.println("Get name failure " + se);
System.exit(1);
}
try {
int fuel = aircraft.fuelCapacity();
System.out.println ("Aircraft " + designation + " has " +
fuel + " Pounds");
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Get fuel failure " + se);
System.exit(1);
}
}
}
AircraftCarrierImpl.java
import
java.util.*;
public class
AircraftCarrierImpl implements Ship.AircraftCarrierOperations
{
// data
member and constructor for TIE
private
org.omg.CORBA.BOA _boa;
private Dictionary _aircrafts = new Hashtable();
public
AircraftCarrierImpl(org.omg.CORBA.BOA boa)
{
_boa = boa;
}
public
synchronized Ship.Aircraft launch(String name)
{
Ship.Aircraft aircraft = (Ship.Aircraft) _aircrafts.get(name);
if (aircraft
== null)
{
AircraftImpl new_aircraft = new AircraftImpl(name);
aircraft = new Ship._tie_Aircraft(new_aircraft);
// TIE uses
_boa. instead of _boa().
_boa.obj_is_ready(aircraft);
System.out.println(aircraft + " on Catapult
2");
_aircrafts.put(name, aircraft);
}
return aircraft;
}
}
AircraftImpl.java
public class AircraftImpl implements
Ship.AircraftOperations, Runnable
{
private String _codeNumber;
private int _fuelCapacity;
private static Thread engineThread;
public void
run()
{
System.out.println(_codeNumber + " fuel status = " + _fuelCapacity +
" pounds");
while (_fuelCapacity > 0)
{
try
{
Thread.sleep(10000);
}
catch (InterruptedException e)
{
return;
}
_fuelCapacity = _fuelCapacity - 500;
System.out.println(_codeNumber + " fuel status = " + _fuelCapacity +
" pounds");
}
}
public AircraftImpl(String codeNumber)
{
_codeNumber = codeNumber;
_fuelCapacity = 10000;
this.engineThread
= new Thread(this);
this.engineThread.start();
}
public String
codeNumber()
{
return _codeNumber;
}
public int fuelCapacity()
{
return _fuelCapacity;
}
}
Building the server with VisiBroker:
Create
a directory and place Ship.idl, Server.java, AircraftCarrierImpl.java, and
AircraftImpl.java into it.
prompt> idl2java
Ship.idl
prompt> vbjc Server.java
Building the client with OrbixWeb:
Place
Client.java into the same directory as above
prompt> idl Ship.idl
prompt> owjavac Client.java java_output\Ship\*.java
Building the client with JavaIDL:
prompt> idltojava
Ship.idl
prompt> javac Client.java
Running the program:
Three
windows will be required
Smart
Agent and NameService in one window, server in one window, client in another
prompt> osagent
prompt> vbj -DJDKrenameBug -DORBservices=CosNaming
com.visigenic.vbroker.services.CosNaming.ExtFactory objects navy.log
prompt>
vbj -DORBservices=CosNaming -DSVCnameroot=objects Server
prompt>
owjava Client
or
prompt>
java Client
Note:
It
is usually easier to create two BAT files to contain the commands above.
Portable
Object Adapter (POA)
Introduction:
The
Portable Object Adapter Specification was introduced in CORBA 2.2 (released in
Feb 1998). Only recently has the ORB Portability IDL/Java Language Mapping been
made available. The following pages have been revised and are now based on the
CORBA 2.3 IDL/Java Language Mapping (which differs from previous versions).
Glossary:
The
POA Specification introduces some new vocabulary so please familiarize yourself
with the following:
Servant
Object
instances of a particular class implemented in a programming language like Java
or C++.
Activation
Starting
an existing CORBA object.
Deactivation
Shutting
down of an active CORBA object.
Object ID
Identifier
used to name an object within the scope of its OA.
Incarnation
Association
of a servant body with a virtual CORBA object so that it may service requests.
Etherealization
Destruction
of the association between the CORBA object and its servant.
Active Object Map
Table
maintained by an OA that maps its active CORBA objects and their servants via
Object IDs.
Changes in CORBA Inheritance approach:
The
CORBA 2.3 IDL/Java Language Mapping also specifies changes in files generated
as well as the inheritance structure. For the inheritance approach, the mapping
specifies the creation of a <interface>POA class.
Changes in CORBA Delegation
approach:
The
CORBA 2.3 IDL/Java Language Mapping specifies the creation of a
<interface>POATie class.
Portable Object Adapter (POA) with the Interoperable
Object Reference (IOR)
Downloads:
The
following examples were tested with Javasoft
JDK 1.3 and Inprise VisiBroker
for Java 4.1 on a Win98 machine.
Getting Started:
Win98
system environment settings for JavaSoft JDK1.3 for directory C:\jdk1.3:
REM
SET PATH=C:\jdk1.3\bin;%PATH%
SET CLASSPATH=C:\progra~1\javasoft\jre\1.3\lib\rt.jar;%CLASSPATH%
REM
Win98 system
environment settings for Inprise VisiBroker for Java 4.1 (including Naming and
Event Service). This assumes that the installation directory is
C:\Inprise\vbroker:
REM VisiBroker for Java
4.1
REM
SET VBROKER_ADM=C:\Inprise\vbroker\adm
SET PATH=C:\Inprise\vbroker\bin;%PATH%
REM
Introduction:
This example
uses a very basic IDL file.
Ship.idl
// Ship.idl
module Ship {
interface
Aircraft {
string codeNumber();
};
interface
AircraftCarrier {
Aircraft launch(in string name);
};
};
Server.java
import
java.io.*;
// import org.omg.CosNaming.*;
public class
Server
{
public static void main(String[] args)
{
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
org.omg.CORBA.Object objPOA = null;
try {
objPOA = orb.resolve_initial_references("RootPOA");
}
catch (org.omg.CORBA.ORBPackage.InvalidName ex) {
}
org.omg.PortableServer.POA
rootPOA = null;
rootPOA = (org.omg.PortableServer.POA) objPOA;
org.omg.PortableServer.POA
myPOA = null;
try {
myPOA = rootPOA.create_POA("personalPOA", rootPOA.the_POAManager(),
new org.omg.CORBA.Policy[] {
rootPOA.create_id_assignment_policy(org.omg.PortableServer.IdAssignmentPolicyValue.USER_ID)
});
}
catch (java.lang.Exception ex)
{
System.err.println("Create POA Exception " + ex);
System.exit(1);
}
org.omg.PortableServer.Servant
carrier = null;
try {
carrier = new AircraftCarrierImpl(myPOA);
myPOA.activate_object_with_id("Nimitz".getBytes(), carrier);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Activate failure " + se);
System.exit(1);
}
catch (org.omg.CORBA.UserException ue)
{
System.err.println("Activate failure " + ue);
System.exit(1);
}
// Write
object reference to an IOR file
org.omg.CORBA.Object
initRef = null;
try {
initRef = myPOA.servant_to_reference(carrier);
FileWriter
output = new FileWriter("ns.ior");
output.write(orb.object_to_string(initRef));
output.close();
System.out.println("Wrote IOR to file: ns.ior");
myPOA.the_POAManager().activate();
System.out.println(carrier + " ready for launch !!!");
orb.run();
}
catch (java.lang.Exception exb)
{
System.err.println("Exception Last deep in here " + exb);
System.exit(1);
}
}
}
Client.java
import
java.io.*;
import org.omg.CosNaming.*;
public class
Client
{
public static void main(String[] args)
{
org.omg.CORBA.ORB orb = null;
try {
orb = org.omg.CORBA.ORB.init(args, null);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("ORB init failure " + se);
System.exit(1);
}
// Read IOR
from file
org.omg.CORBA.Object
initRef = null;
try
{
LineNumberReader input = new LineNumberReader(new
FileReader("ns.ior"));
initRef = orb.string_to_object(input.readLine());
}
catch(java.io.IOException e)
{
System.out.println("Exception: " + e);
System.exit(1);
}
Ship.AircraftCarrier
carrier = null;
try {
carrier = Ship.AircraftCarrierHelper.narrow(initRef);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("AircraftCarrier narrow failure " + se);
System.exit(1);
}
// Standard
program continues
String flight
= args.length > 0 ? args[0]: "Ghost Rider 101";
Ship.Aircraft
aircraft = null;
try {
aircraft = carrier.launch(flight);
System.out.println("Aircraft has been launched" );
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Carrier launch failure " + se);
System.exit(1);
}
String designation = null;
try {
designation = aircraft.codeNumber();
System.out.println("Aircraft " + designation + " is
airborne");
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Get name failure " + se);
System.exit(1);
}
}
}
AircraftCarrierImpl.java
public class AircraftCarrierImpl extends
Ship.AircraftCarrierPOA
{
private
org.omg.PortableServer.POA _rootPOA;
public
AircraftCarrierImpl(org.omg.PortableServer.POA rootPOA)
{
_rootPOA = rootPOA;
}
public
Ship.Aircraft launch(String name)
{
org.omg.PortableServer.Servant
aircraft = new AircraftImpl(name);
try {
_rootPOA.activate_object_with_id("name".getBytes(), aircraft);
}
catch (java.lang.Exception ex)
{
System.err.println("Exception 2 " + ex);
System.exit(1);
}
System.out.println(name
+ " on Catapult 2");
Ship.Aircraft
_aircraft = null;
try {
_aircraft =
Ship.AircraftHelper.narrow(_rootPOA.create_reference_with_id("name".getBytes(),
aircraft._all_interfaces(null, null)[0]));
}
catch (java.lang.Exception ex)
{
System.err.println("Exception 3 " + ex);
System.exit(1);
}
return _aircraft;
}
}
AircraftImpl.java
public class
AircraftImpl extends Ship.AircraftPOA
{
private String _codeNumber;
public
AircraftImpl()
{}
public
AircraftImpl(String codeNumber)
{
_codeNumber = codeNumber;
}
public String
codeNumber()
{
return _codeNumber;
}
}
Building the server with VisiBroker:
Create
a directory and place Ship.idl, Server.java, AircraftCarrierImpl.java, and
AircraftImpl.java into it.
prompt> idl2java
Ship.idl
prompt> vbjc Server.java
Building the client with VisiBroker:
Place
Client.java into the same directory as above
prompt> idl2java
Ship.idl
prompt> vbjc Client.java
Running the program:
Now
start the Smart Agent and server in one window, client in another window
prompt> osagent
prompt> vbj Server
prompt> vbj Client "Jolly 700"
Portable
Object Adapter (POA) with the Name Service - CosNaming
Server.java
import
org.omg.CosNaming.*;
public class
Server
{
public static void main(String[] args)
{
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
org.omg.CORBA.Object objPOA = null;
try {
objPOA = orb.resolve_initial_references("RootPOA");
}
catch (org.omg.CORBA.ORBPackage.InvalidName ex) {
}
org.omg.PortableServer.POA
rootPOA = null;
rootPOA = (org.omg.PortableServer.POA) objPOA;
org.omg.PortableServer.POA
myPOA = null;
try {
myPOA = rootPOA.create_POA("personalPOA", rootPOA.the_POAManager(),
new org.omg.CORBA.Policy[] {
rootPOA.create_id_assignment_policy(
org.omg.PortableServer.IdAssignmentPolicyValue.USER_ID)
});
}
catch (java.lang.Exception ex)
{
System.err.println("Exception 1 deep in here " + ex);
System.exit(1);
}
org.omg.PortableServer.Servant
carrier = null;
try {
carrier = new AircraftCarrierImpl(myPOA);
myPOA.activate_object_with_id("Nimitz".getBytes(), carrier);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Activate failure " + se);
System.exit(1);
}
catch (org.omg.CORBA.UserException ue)
{
System.err.println("Activate failure " + ue);
System.exit(1);
}
catch (java.lang.Exception ex)
{
System.err.println("Exception here " + ex);
System.exit(1);
}
// Name
Service
org.omg.CORBA.Object
nsRef = null;
try {
nsRef = orb.resolve_initial_references("NameService");
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Resolve init failure " + se);
System.exit(1);
}
catch (org.omg.CORBA.UserException ue)
{
System.err.println("Resolve init failure " + ue);
System.exit(1);
}
NamingContextExt
initContext = null;
try {
initContext = NamingContextExtHelper.narrow(nsRef);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Context narrow failure " + se);
System.exit(1);
}
// Write
object reference to NS
org.omg.CORBA.Object
initRef = null;
try {
initRef = myPOA.servant_to_reference(carrier);
myPOA.the_POAManager().activate();
System.out.println(carrier + " ready for launch !!!");
initContext.rebind(initContext.to_name("Nimitz"),
initRef);
orb.run();
}
catch (java.lang.Exception exb)
{
System.err.println("Exception " + exb);
System.exit(1);
}
}
}
Client.java
import org.omg.CosNaming.*;
public class Client
{
public static void main(String[] args)
{
org.omg.CORBA.ORB orb = null;
try {
orb = org.omg.CORBA.ORB.init(args, null);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("ORB init failure " + se);
System.exit(1);
}
// Use Name
Service
org.omg.CORBA.Object
nsRef = null;
try {
nsRef = orb.resolve_initial_references("NameService");
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Resolve init failure " + se);
System.exit(1);
}
catch (org.omg.CORBA.UserException ue)
{
System.err.println("Resolve init failure " + ue);
System.exit(1);
}
NamingContextExt initContext = null;
try {
initContext = NamingContextExtHelper.narrow(nsRef);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Context narrow failure " + se);
System.exit(1);
}
org.omg.CORBA.Object
initRef = null;
try
{
initRef =
((NamingContext)initContext).resolve(initContext.to_name("Nimitz"));
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Context narrow failure " + se);
System.exit(1);
}
catch (java.lang.Exception exb)
{
System.err.println("Exception here " + exb);
System.exit(1);
}
Ship.AircraftCarrier
carrier = null;
try {
carrier = Ship.AircraftCarrierHelper.narrow(initRef);
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("AircraftCarrier narrow failure " + se);
System.exit(1);
}
// Standard
program continues
String flight
= args.length > 0 ? args[0]: "Ghost Rider 101";
Ship.Aircraft
aircraft = null;
try {
aircraft = carrier.launch(flight);
System.out.println("Aircraft has been launched" );
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Carrier launch failure " + se);
System.exit(1);
}
String designation = null;
try {
designation = aircraft.codeNumber();
System.out.println("Aircraft " + designation + " is
airborne");
}
catch (org.omg.CORBA.SystemException se)
{
System.err.println("Get name failure " + se);
System.exit(1);
}
}
}
Building the server with VisiBroker:
Create
a directory and place Ship.idl, AircraftCarrierImpl.java, and AircraftImpl.java
from the previous page into it. Also place this Server.java in the same
directory.
prompt> idl2java
Ship.idl
prompt> vbjc Server.java
Building the client with VisiBroker:
prompt> idl2java
Ship.idl
prompt> vbjc Client.java
Running the program:
Three windows
will be required.
Start the Smart Agent and Name Service in one window, server in one window,
client in another window
prompt> osagent
prompt> start nameserv objects
prompt> vbj -DSVCnameroot=objects Server
prompt> vbj -DSVCnameroot=objects Client "Sundowner 406"
Note:
It is usually
easier to create three BAT files to contain the commands above.