New York
University
Computer
Science Department
Courant
Institute of Mathematical Sciences
Naming
Services - CosNaming
Course Title: Application Servers Course
Number: g22.3033-011
Instructor: Jean-Claude Franchitti Session: 5
Agenda
·
Introduction
·
Coding
client and server with the Name Service - CosNaming
·
Interoperable
ORB example using IOR with CosNaming
·
Portable
Object Adapter (POA) with the Name Service - CosNaming
The
CosNaming module is the module, which implements the CORBA name service. It is
used to associate to each object a human readable string, which is then used to
find the object reference upon need. This is exactly similar to the DNS, which
allows you to associate simple name hierarchies like “www.what…ever.com” to less
useful IP addresses: 137.194.8.56 .
Here,
names like "HOME.servers.Panel" are used to register object
references. Typically, the server registers the name and clients ask the naming
service whether the name was registered or not. If it was registered, clients
get the server object reference from the naming service and send requests to
the server object.
Here is some vocabulary specific to the CORBA naming service: names are bound to objects' references in a given context. Resolving a name is finding the object reference bound to the name within a given context. Contexts are CORBA objects: they may be bound like other objects and are used to create naming graphs. Each naming graph node is a naming context object and leafs are your own objects bindings. An Object name is the compound name made of the node names and the leaf name necessary to describe the path in the naming graph from the root to the object binding. For example, home's naming graph is rather simple. We have: root -> HOME -> servers
The Home Panel compound name is : root -> HOME
-> servers -> Panel
Here is the CosNaming module IDL:
module CosNaming {
typedef string Istring;
struct NameComponent {
Istring id;
Istring kind;
};
typedef sequence<NameComponent> Name;
enum BindingType {
nobject,
ncontext
};
struct Binding {
Name binding_name;
BindingType binding_type;
};
interface NamingContext {
void bind(in Name n, in Object obj)
raises(NotFound,
CannotProceed,
InvalidName,
AlreadyBound);
void rebind(in Name n, in Object obj)
raises(NotFound,
CannotProceed,
InvalidName);
void bind_context(in Name n, in NamingContext nc)
raises(NotFound,
CannotProceed,
InvalidName,
AlreadyBound);
void rebind_context(in Name n, in NamingContext nc)
raises(NotFound,
CannotProceed,
InvalidName);
Object resolve(in Name n)
raises(NotFound,
CannotProceed,
InvalidName);
void unbind(in Name n)
raises(NotFound,
CannotProceed,
InvalidName);
NamingContext new_context();
NamingContext bind_new_context(in Name n)
raises(NotFound,
AlreadyBound,
CannotProceed,
InvalidName);
void destroy()
raises(NotEmpty);
};
Some definitions were purposedly removed from this
huge interface to make things clearer. The interface provides binding
operations allowing the creation of the naming graph: bind, rebind, and unbind allow the creation of name
bindings within a given context (i.e., they allow the creation of a naming
graph leaf), bind_context, rebind_context, new_context and bind_new_context
allow the creation of naming contexts (i.e., they allow the creation of the
naming graph nodes). Lastly, resolve
will help you get the object reference from the naming graph with the compound
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);
}
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) 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.