!--a11y-->
Developing RMI-IIOP Applications with Java
Remote Clients 
To develop your RMI-IIOP application, you must follow the same steps as when you develop an RMI-P4 application. However, there are three major differences that you should observe when developing applications with RMI-IIOP:
· You must use a different initial context factory. The two options are:
¡ com.sap.engine.services.jndi.CosNamingInitialContextFactoryImpl - the SAP J2EE Engine specific implementation.
¡ com.sun.jndi.cosnaming.CNCtxFactory – the Sun Microsystems implementation.
· You must use the javax.rmi.PortableRemoteObject.narrow() method in your client code to narrow the looked up object.
· You must generate RMI-IIOP specific stubs and ties using the SAP RMIC compiler. For more information on this, see Generating Stubs and Ties.
These are actually the only steps that must follow so that your existing RMI-P4 applications will work with IIOP.
We will describe the steps in this process based on the Account example from Developing RMI-P4 Applications.
...
1. Define the remote interface and implement it as described in Defining a Remote Interface and Implementing the Remote Interface.
2. Generate the stubs and the ties against the implementation class of the remote interface using the SAP J2EE Engine RMIC tool.
3. Implement the client as described in Implementing a Client. To make the BankClient work with IIOP, you must change the code that specifies the implementation of the initial context factory to be used.

The original code of the RMI-P4 BankClient contains:
// Specify the type of the initial context factory. p.put(Context.INITIAL_CONTEXT_FACTORY, "com.sap.engine.services.jndi.InitialContextFactoryImpl"); |
The code of the RMI-IIOP BankClient example should contain:
// Specify the type of the initial context factory. p.put(Context.INITIAL_CONTEXT_FACTORY, "com.sap.engine.services.jndi.CosNamingInitialContextFactoryImpl"); |
or
// Specify the type of the initial context factory. p.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory"); |
Here is the full code of the RMI-IIOP BankClient client of the Account interface. It also illustrates how you can narrow the object that you have looked up from the initial context factory.

package exapmles.rmi_p4;
import javax.naming.Context; import javax.naming.InitialContext; import java.util.Properties;
public class BankClient {
public static void main(String[] args) { try { if (args.length < 2) { System.out.println(" Use: BankClient <hostName> <port>"); return; } Properties p = new Properties(); // Specify the type of the initial context factory. p.put(Context.INITIAL_CONTEXT_FACTORY, "com.sap.engine.services.jndi.CosNamingInitialContextFactoryImpl"); // Specify the provider URL. p.put(Context.PROVIDER_URL, args[0] + ":" + args[1]); // Specify the security principal (user). p.put(Context.SECURITY_PRINCIPAL, args[2]); // Specify the security credentials (password). p.put(Context.SECURITY_CREDENTIALS, args[3]);
// Connect to the server by the initial context. Context initialContext = new InitialContext(p); // Lookup the remote object using the narrow() method Account account = (Account) PortableRemoteObject.narrow(initialContext.lookup("Bank"), Account.class); // Invoke methods remotely. account.deposit(100); System.out.println("Balance:" + account.getBalance()); System.out.println("Try to draw..."); account.draw(50); System.out.println("Balance:" + account.getBalance()); } catch (Exception ex) { ex.printStackTrace(); } } } |
