package examples.ejb.basic.beanManaged; import javax.naming.*; import javax.ejb.*; import java.rmi.RemoteException; import java.util.*; /** * This class demonstrates calling an entity EJBean, * followed by two exercises: *

* Part A: Deposit and attempt to withdraw more than the current * account balance. An application-specific exception should be thrown. *

* Part B: Create some new accounts, with different initial balances. * Find all the accounts with a balance greater than a specific value. * When finished, the new accounts are removed. *

* This class also illustrates how to search the JNDI tree * for an appropriate container. * * @author Copyright (c) 1998 by WebLogic, Inc. All Rights Reserved. * @author Copyright (c) 1998-1999 by BEA WebXpress. All Rights Reserved. */ public class Client { static String url = "t3://localhost:7001"; static String user = null; static String password = null; static String accountId = "10020"; /** * Runs this example from the command line. Example: *

* java examples.ejb.basic.beanManaged.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 beanManaged.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; AccountPK accountKey = new AccountPK(); accountKey.accountId = accountId; Vector v = new Vector(); try { // Contact the AccountBean container (the "AccountHome") through JNDI. Context ctx = getInitialContext(); AccountHome home = (AccountHome) ctx.lookup("beanManaged.AccountHome"); // Find the Account or create it. Account ac = null; try { System.out.println("Looking up account " + accountId + "...\n"); ac = (Account) home.findByPrimaryKey(accountKey); } catch (Exception ee) { System.out.println("Did not find " + accountId); } if (ac == null) { System.out.println("Account " + accountId + " being created; opening balance is $" + balance); ac = home.create(accountId, balance); } else { System.out.println("Account " + accountId + " found; balance is $" + ac.balance()); } // Part A: Deposit and attempt to withdraw more than the current // account balance. An application-specific exception should be thrown. System.out.println("\nPart A: Depositing $" + amount ); balance = ac.deposit(amount); System.out.println("Current balance is $" + balance); amount = balance + 10; try { System.out.println("\nWithdrawing amount greater than current balance. Expecting an exception...\n"); balance = ac.withdraw(amount); System.out.println("Error: expected an exception."); } catch (ProcessingErrorException pe) { System.out.println("Received expected Processing Error:\n" + pe); } // Part B: Create some new accounts, with different initial balances. // Find all the accounts with a balance greater than a specific value. // When finished, the new accounts are removed. int numAccounts = 5; System.out.println("\nPart B: Creating " + numAccounts + " new accounts..."); long now = System.currentTimeMillis(); for (int i = 0; i < numAccounts; i++) { String id = "" + now + i; // unique account id balance = i*100; // initial balance v.addElement(home.create(id, balance)); System.out.println("Created account: " + id + "; balance is $" + balance); } if (v.size() == numAccounts) { System.out.println("\n" + numAccounts + " accounts successfully created"); } else { System.out.println("\nError: Only " + v.size() + " accounts were created successfully"); } double balanceGreaterThan = 700; System.out.println("\nQuerying for accounts with a balance greater than " + balanceGreaterThan + "..."); Enumeration e = home.findBigAccounts(balanceGreaterThan); if (e != null) { while (e.hasMoreElements()) { Account bigAccount= (Account) e.nextElement(); System.out.println("Account " + bigAccount.getPrimaryKey() + "; balance is $" + bigAccount.balance()); Thread.sleep(1000); } } System.out.println("\nRemoving accounts just created..."); for (int i = 0; i < numAccounts; i++) { String id = "" + now + i; ((Account)(v.elementAt(i))).remove(); System.out.println("Removed account: " +id); } // Catch any exceptions } catch (ProcessingErrorException pe) { System.out.println("Unexpected Processing Error: " + pe); } catch (Exception e) { System.out.println(":::::::::::::: Unexpected Error :::::::::::::::::"); e.printStackTrace(); } finally { System.out.println("\nEnd beanManaged.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); } }