package examples.ejb.extensions.isModifiedBeanManaged; import javax.naming.*; import javax.ejb.*; import java.rmi.RemoteException; import java.util.*; import examples.ejb.basic.containerManaged.AccountHome; import examples.ejb.basic.containerManaged.Account; /** * This class demonstrates calling two different entity EJBeans, * and performing the same exercise twice: *

* Part A: *

*

* Part B: *

* * @author
Copyright (c) 1999 by BEA WebXpress, Inc. All Rights Reserved. */ public class Client { static String url = "t3://localhost:7001"; static String user = null; static String password = null; static String accountId = "10020"; static String ACCOUNT_TYPE = "Checking"; /** * Runs this example from the command line. Example: *

* java examples.ejb.extensions.isModifiedBeanManaged.Client * "t3://localhost:7001" scott tiger 10020 *

* The parameters are optional, but if any are supplied, * they are interpreted in this order: *

* @param url URL such as "t3://localhost:7001" of Server * @param user User name, default null * @param password User password, default null * @param accountID String Account ID to test, default "10020" */ public static void main(String[] args) { System.out.println("\nBeginning isModifiedBeanManaged.Client...\n"); // Parse the argument list if ((args == null) || (args.length == 0)) {} else for (int i = 0; i < args.length; i++) { switch(i) { case 0: url = args[i]; break; case 1: user = args[i]; break; case 2: password = args[i]; break; case 3: accountId = args[i]; break; default: } } double amount = 100; double balance = 3000; try { Context ctx = getInitialContext(); // Using the bean with isModified set, find a bean, do a deposit // and attempt a withdrawl for more than the balance. System.out.println("\nPart A: using isModified...\n"); AccountPK accountKeyIsMod = new AccountPK(); accountKeyIsMod.accountId = accountId; // Contact the isModified AccountBean container (the "AccountHome") through JNDI. examples.ejb.extensions.isModifiedBeanManaged.AccountHome homeIsMod = (examples.ejb.extensions.isModifiedBeanManaged.AccountHome) ctx.lookup("isModifiedBeanManaged.AccountHome"); // Find the Account or create it. examples.ejb.extensions.isModifiedBeanManaged.Account acIsMod = null; try { System.out.println("Looking up account " + accountId + "...\n"); acIsMod = (examples.ejb.extensions.isModifiedBeanManaged.Account) homeIsMod.findByPrimaryKey(accountKeyIsMod); } catch (Exception ee) { System.out.println("Did not find " + accountId); } if (acIsMod == null) { System.out.println("Account " + accountId + " being created; opening balance is $" + balance + "\n"); acIsMod = homeIsMod.create(accountId, balance); } else { System.out.println("Account " + accountId + " found; balance is $" + acIsMod.balance() + "\n"); } // Deposit and attempt to withdraw more than the current // account balance. An application-specific exception should be thrown. System.out.println("Depositing $" + amount ); acIsMod.deposit(amount); System.out.println("Current balance is $" + acIsMod.balance() + "\n"); System.out.println("Withdrawing $" + amount ); acIsMod.withdraw(amount); System.out.println("Current balance is $" + acIsMod.balance() + "\n"); // Now, let's do the same thing, but using the containerManaged example // that doesn't use isModified. System.out.println("\nPart B: not using isModified...\n"); examples.ejb.basic.containerManaged.AccountPK accountKeyJDBC = new examples.ejb.basic.containerManaged.AccountPK(); accountKeyJDBC.accountId = accountId; System.out.println("Contacting the home..."); // Contact the isModified AccountBean container (the "AccountHome") through JNDI. examples.ejb.basic.containerManaged.AccountHome homeJDBC = (examples.ejb.basic.containerManaged.AccountHome) ctx.lookup("containerManaged.AccountHome"); System.out.println("Finding the account..."); // Find the Account or create it. examples.ejb.basic.containerManaged.Account acJDBC = null; try { System.out.println("Looking up account " + accountId + "...\n"); acJDBC = (examples.ejb.basic.containerManaged.Account) homeJDBC.findByPrimaryKey(accountKeyJDBC); } catch (Exception ee) { System.out.println("Did not find " + accountId); } if (acJDBC == null) { System.out.println("Account " + accountId + " being created; opening balance is $" + balance + "\n"); acJDBC = homeJDBC.create(accountId, balance, ACCOUNT_TYPE); } else { System.out.println("Account " + accountId + " found; balance is $" + acJDBC.balance() + "\n"); } // Deposit and attempt to withdraw more than the current // account balance. An application-specific exception should be thrown. System.out.println("Depositing $" + amount ); acJDBC.deposit(amount); System.out.println("Current balance is $" + acJDBC.balance() + "\n"); System.out.println("Withdrawing $" + amount ); acJDBC.withdraw(amount); System.out.println("Current balance is $" + acJDBC.balance() + "\n"); // Catch any exceptions } catch (Exception e) { System.out.println(":::::::::::::: Unexpected Error :::::::::::::::::"); e.printStackTrace(); } finally { System.out.println("\nEnd isModifiedBeanManaged.Client..."); } } /** * Gets an initial context for the current user, password and url. * * @return Context * @exception java.lang.Exception if there is * an error in getting the Context */ static public Context getInitialContext() throws Exception { Hashtable h = new Hashtable(); h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); h.put(Context.PROVIDER_URL, url); if (user != null) { System.out.println ("user: " + user); h.put(Context.SECURITY_PRINCIPAL, user); if (password == null) password = ""; h.put(Context.SECURITY_CREDENTIALS, password); } return new InitialContext(h); } }