Server code
The server application has to create an instance of the remote object and publish it in a naming service. The Java™ Naming and Directory Interface (JNDI) defines a set of standard interfaces. The interfaces are used to query a naming service, or to bind an object to that service.
Context ctx = new InitialContext(...); // get hold of the initial context
ctx.bind("sample", sampleReference); // bind the reference to the name "sample"
Object obj = ctx.lookup("sample"); // obtain the reference- java.naming.factory.initial
- Defined also as javax.naming.Context.INITIAL_CONTEXT_FACTORY, this property specifies the class name of the initial context factory for the naming service provider. For RMI registry, the class name is com.sun.jndi.rmi.registry.RegistryContextFactory. For the CosNaming Service, the class name is com.sun.jndi.cosnaming.CNCtxFactory.
- java.naming.provider.url
- This property configures the root naming context, the Object Request
Broker (ORB), or both. It is used when the naming service is stored
in a different host, and it can take several URI schemes:
- rmi
- corbaname
- corbaloc
- IOR
- iiop
- iiopname
rmi://[<host>[:<port>]][/<initial_context>] for RMI registry iiop://[<host>[:<port>]][/<cosnaming_name>] for COSNaming
Hashtable env = new Hashtable();
Env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.cosnaming.CNCtxFactory"); and pass
the hash table as an argument to the constructor of InitialContext.For example, with RMI(JRMP), you create an instance of the servant then follow the previous steps to bind this reference in the naming service.
public class Server {
public static void main (String args []) {
try {
ORB orb = ORB.init(args, null);
// Get reference to the root poa & activate the POAManager
POA poa = (POA)orb.resolve_initial_references("RootPOA");
poa.the_POAManager().activate();
// Create a servant and register with the ORB
SampleImpl sample = new SampleImpl();
sample.setORB(orb);
// TIE model ONLY
// create a tie, with servant being the delegate and
// obtain the reference ref for the tie
SamplePOATie tie = new SamplePOATie(sample, poa);
Sample ref = tie._this(orb);
// Inheritance model ONLY
// get object reference from the servant
org.omg.CORBA.Object ref = poa.servant_to_reference(sample);
Sample ref = SampleHelper.narrow(ref);
// bind the object reference ref to the naming service using JNDI
..........(see previous code) .....
orb.run();
}
catch(Exception e) {}
}
}public class Server {
public static void main (String args []) {
try {
ORB orb = ORB.init(args, null);
// Get reference to the root poa & activate the POAManager
POA poa = (POA)orb.resolve_initial_references("RootPOA");
poa.the_POAManager().activate();
// Create servant and its tie
SampleImpl sample = new SampleImpl();
_SampleImpl_Tie tie = (_SampleImpl_Tie)Util.getTie(sample);
// get an usable object reference
org.omg.CORBA.Object ref = poa.servant_to_reference((Servant)tie);
// bind the object reference ref to the naming service using JNDI
..........(see previous code) .....
}
catch(Exception e) {}
}
}To use the previous Portable Object Adapter (POA) server code,
you must use the -iiop -poa options together
to enable rmic to generate the tie. If you do not
use the POA, the RMI(IIOP) server code can be reduced to instantiating
the servant (SampleImpl sample = new SampleImpl()). You then bind the servant to a naming service as is typically
done in the RMI(JRMP) environment. In this case, you need use only
the -iiop option to enable rmic to generate the RMI-IIOP tie. If you omit -iiop, the RMI(JRMP) skeleton is generated.
When you export an RMI-IIOP object on your server, you do not necessarily have to choose between JRMP and IIOP. If you need a single server object to support JRMP and IIOP clients, you can export your RMI-IIOP object to JRMP and to IIOP simultaneously. In RMI-IIOP terminology, this action is called dual export.
public class SampleClient {
public static void main(String [] args) {
try{
Sample sampleref
//Look-up the naming service using JNDI and get the reference
.........
// Invoke method
System.out.println(sampleRef.message());
}
catch(Exception e) {}
}
} public class SampleClient {
public static void main (String [] args) {
try {
ORB orb = ORB.init(args, null);
// Look up the naming service using JNDI
......
// Narrowing the reference to the correct class
Sample sampleRef = SampleHelper.narrow(o);
// Method Invocation
System.out.println(sampleRef.message());
}
catch(Exception e) {}
}
}public class SampleClient {
public static void main (String [] args) {
try{
ORB orb = ORB.init(args, null);
// Retrieving reference from naming service
........
// Narrowing the reference to the correct class
Sample sampleRef = (Sample)PortableRemoteObject.narrow(o, Sample.class);
// Method Invocation
System.out.println(sampleRef.message());
}
catch(Exception e) {}
}
}