FileDocCategorySizeDatePackage
IIOPAccountClient.javaAPI DocExample1712Wed Apr 05 11:25:42 BST 2000None

IIOPAccountClient.java

/*
 * This example is from the book "Java Enterprise in a Nutshell".
 * Copyright (c) 1999 by O'Reilly & Associates.  
 * You may distribute this source code for non-commercial purposes only.
 * You may study, modify, and use this example for any purpose, as long as
 * this notice is retained.  Note that this example is provided "as is",
 * WITHOUT WARRANTY of any kind either expressed or implied.
 */

import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.rmi.RMISecurityManager;
import java.util.Hashtable;

// Client that accesses a specific remote account, makes a deposit
// and reports the current balance.

public class IIOPAccountClient {
  public static void main(String argv[]) {
    try {
      // Set the RMI security manager, in case we need to load remote classes
//       System.setSecurityManager(new RMISecurityManager());
      // Lookup account object
      Hashtable props = new Hashtable();
      props.put("java.naming.factory.initial",
                "com.sun.jndi.cosnaming.CNCtxFactory");
      props.put("java.naming.provider.url", "iiop://localhost:900");
      Context ctx = new InitialContext(props);
      Account jimAcct =
        (Account)PortableRemoteObject.narrow(ctx.lookup("JimF"),
                                             Account.class);
      // Make deposit
      jimAcct.deposit(12000);
      // Report results and balance.
      System.out.println("Deposited 12,000 into account owned by " +
                         jimAcct.getName());
      System.out.println("Balance now totals: " + jimAcct.getBalance());
    }
    catch (Exception e) {
      System.out.println("Error while looking up account:");
      e.printStackTrace();
    }
  }
}