Show TOC

Binding the Implementation to the Naming SystemLocate this document in the navigation structure

Use

To make the server part of your application available to the client, you have to bind the implementation of the remote object to the Naming system. The implementation can be bound to the Naming system from a component that runs on AS Java (either a service, or an application), or bound from a client residing in a different JVM.

Procedure

Binding the Implementation from an AS Java Component

To do this for the AccountImpl class, you must add the following try clause after the implementation of its methods and then call this init() method from an application containing AccountImpl class:

                public void init() {
                try  {
                  javax.naming.Context ctx = new InitialContext();
                  ctx.rebind("Bank", new AccountImpl());
                } catch (NamingException ne) {
                  ne.printStackTrace();
                        }
        }

            

Binding the Implementation from a Remote Client

  1. Instantiate the remote object's implementation class.

  2. Bind it to the AS Java Naming system from the client JVM.

The BankServer class in the example below is a remote client that binds the Account Impl class to the AS Java Naming system. Thus the remote object life cycle is on the client's JVM. Only a reference to this object is bound to AS Java.

               package examples.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);

                System.out.println("Bank is operational");
                Thread.sleep(60000);
                initialContext.unbind("Bank");
                System.out.println("Bank is stopped");
 
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}