Before a CORBA client can call methods on a servant object, it must
establish a connection to that object. The techniques available for establishing
such connections depend in part on the ORB for which the server is written
and on the design of the application.
The server described in this example puts binding information into the name server and also writes string versions of that information out into files. The client therefore has several choices in the way it looks up the servant objects. It can do any of the following:
- Go directly to the name service and look up the desired servant object.
- Read the name-service binding information from the file created by the server, reconstruct the IOR from this information, use the IOR to reach the name service, and look up the desired servant object.
- Read the servant-object binding information from the file created by the server, reconstruct the IOR, and contact the object directly.
The code that accomplishes these tasks is spread across a variety of methods. The code shown below reflects the logical sequence of events, not explicit code extracts. The getOrb method called in these code fragments is described in Contacting the client-side ORB; this method returns a reference to the client-side ORB.Note: When an enterprise bean declares CORBA naming-context objects, it must explicitly type them as org.omg.CORBA.Object types to distinguish them from java.lang.Object types.
The sample client is designed to test several techniques for establishing connectivity to the servant objects, so it provides the following code to support these techniques:
-
To get a reference to the name service, do one of the following:
-
Get an initial reference to the name server by calling the resolve_initial_references
method on the ORB reference, and then narrow the reference.
org.omg.CORBA.Object nameServiceObject = null;
NamingContext nc = null;
nameServiceObject =
getORB().resolve_initial_references("NameService");
nc = NamingContextHelper.narrow(nameServiceObject);
-
Read the context from the file written by the server, reconstruct the
IOR, and narrow the reference.
org.omg.CORBA.Object nameServiceObject = null;
NamingContext nc = null;
String nameServiceIOR;
nameServiceIOR = readIOR("nameservice.ior", "NameService");
nameServiceObject = getORB().string_to_object(nameServiceIOR);
nc = NamingContextHelper.narrow(nameServiceObject);
-
To get a reference to a servant object, do one of the following:
-
By using the naming context obtained in the previous step, look up the
servant object by name. The resulting object, of the type org.omg.CORBA.Object,
must be narrowed before it can be used. For example, if the client looks up
the Primitive servant object, where the value of the name argument used below
is "primitive", the PrimitiveHelper.narrow method must be used.
org.omg.CORBA.Object servantObject = null;
NamingContext primitive = null;
// Name service...
nc = NamingContextHelper.narrow(nameServiceObject);
NameComponent nameSeq[] = new NameComponent[1];
nameSeq[0] = new NameComponent(name, "");
servantObject = nc.resolve(nameSeq);
// Later...
primitive = PrimitiveHelper.narrow(servantObject);
-
Read the context from the file written by the server, reconstruct the
IOR, and narrow the reference. This does not require a naming-service context.
org.omg.CORBA.Object primitiveObject = null;
NamingContext primitive = null;
String primitiveIOR;
primitiveIOR = readIOR("primitive.ior", "primitive");
primitiveObject = getORB().string_to_object(primitiveIOR);
primitive = PrimitiveHelper.narrow(primitiveObject);