The implementation of the remote object can be bound to the naming system by a component that runs on the AS Java (either a service, or an application), or bind it from a client residing in a different JVM.
To do this for the AccountImpl class, you must add the following try clause after the implementation of its methods:
try { javax.naming.Context ctx = new InitialContext(); ctx.rebind(“Bank”, new AccountImpl()); } catch (NamingException ne) { ne.printStackTrace(); } |
...
1. Instantiate the remote object’s implementation class.
2. Bind it to the AS Java’s Naming System from the client JVM.
The BankServer class below is a remote client that binds the AccountImpl class to the AS Java’s Naming System.
package exapmles.rmi_p4;
import javax.naming.Context; import javax.naming.InitialContext; import java.util.Properties;
public class BankServer { public static void main(String[] args) { try { if (args.length < 4) { System.out.println("Use: BankClient <hostName> <port> <user> <pass>"); return; } AccountImpl obj = new AccountImpl(); // Create the InitialContext properties. Properties p = new Properties(); // Specify the type of the InitialContext factory. p.put(Context.INITIAL_CONTEXT_FACTORY, "com.sap.engine.services.jndi.InitialContextFactoryImpl"); // Specify the provider URL. p.put(Context.PROVIDER_URL, “p4://” 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 using the InitialContext. Context initialContext = new InitialContext(p); initialContext.rebind("Bank", obj);
} catch (Exception e) { e.printStackTrace(); } } } |