Show TOC

Procedure documentationBinding the Implementation to the Naming System Locate this document in the navigation structure

 

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 the 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:

Syntax Syntax

  1. try  {
      javax.naming.Context ctx = new InitialContext();
      ctx.rebind(“Bank”, new AccountImpl());
    } catch (NamingException ne) {
      ne.printStackTrace();
    }
    
End of the code.
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 AccountImpl class to the AS Java Naming system.

Syntax Syntax

  1. 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);
    
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
End of the code.