package examples.ejb.basic.containerManaged; 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: *

*

* Part B: *

*

* 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"; static String ACCOUNT_TYPE = "Checking"; /** * Runs this example from the command line. Example: *

* java examples.ejb.basic.containerManaged.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 containerManaged.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("containerManaged.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 + "\n"); ac = home.create(accountId, balance, ACCOUNT_TYPE); } else { System.out.println("Account " + accountId + " found; balance is $" + ac.balance() + "; type: " + ac.type() + "\n"); } // Part A: Deposit and attempt to withdraw more than the current // account balance. An application-specific exception should be thrown. System.out.println("Part A: Depositing $" + amount ); balance = ac.deposit(amount); System.out.println("Current balance is $" + balance + "\n"); amount = balance + 10; try { System.out.println("Withdrawing 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 + "\n"); } // Part B: Create some new accounts, with different initial balances. // Creates an account with a 'null' type. // Find all the accounts with a balance greater than a specific value // and in both ascending and descending order. // Find a single account with a balance equal to a specific value. // Find any accounts with a 'null' type. int numAccounts = 5; Enumeration enum; System.out.println("Part B: Creating " + numAccounts + " new accounts...\n"); 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, ACCOUNT_TYPE)); System.out.println("Created account: " + id + "; balance is $" + balance + "; type: " + ACCOUNT_TYPE); } if (v.size() == numAccounts) { System.out.println("\n" + numAccounts + " accounts successfully created"); } else { System.out.println("\nError: Only " + v.size() + " accounts were created successfully"); } // Creates an account with a 'null' type. System.out.println("\nCreating an account with a null balance..."); v.addElement(home.create("" + now + numAccounts, 0, null)); // Find all the accounts with a balance greater than a specific value // and in both ascending and descending order. double balanceGreaterThan = 50; System.out.println("\nQuerying for accounts with a balance greater than " + balanceGreaterThan + "...ordered by Account ID\n"); enum = (Enumeration) home.findOrderedBigAccounts(balanceGreaterThan); printEnumeratedBeans(enum, "There's no accounts with a balance greater than $" + balanceGreaterThan + "\n"); System.out.println("\nQuerying for accounts with a balance greater than " + balanceGreaterThan + "...in descending order\n"); enum = (Enumeration) home.findDescOrderedBigAccounts(balanceGreaterThan); printEnumeratedBeans(enum, "There's no accounts with a balance greater than $" + balanceGreaterThan + "\n"); // Find a single account with a balance equal to a specific value. double balanceEqualTo = 200; System.out.println("\nQuerying for an account with a balance of $" + balanceEqualTo + "..."); Account equalAccount = home.findAccount(balanceEqualTo); if (equalAccount != null) { printAccount(equalAccount); } else { System.out.println("There's no account with a balance equal to $" + balanceEqualTo + "\n"); } // Find any accounts with a 'null' type. System.out.println("\nQuerying for an account with a type 'null'\n"); enum = (Enumeration) home.findNullAccounts(); printEnumeratedBeans(enum, "There's no account with a type 'null'\n"); // When finished, the new accounts are removed. System.out.println("\nRemoving all 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 containerManaged.Client..."); } } static private void printEnumeratedBeans(Enumeration e, String message) throws Exception { if ((e != null)&&(e.hasMoreElements())) { while (e.hasMoreElements()) { printAccount((Account) e.nextElement()); Thread.sleep(1000); } } else { System.out.println(message); } } static private void printAccount(Account account) throws RemoteException{ System.out.println("Account " + account.getPrimaryKey() + "; balance is $" + account.balance() + "; type: " + account.type()); } /** * 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); } }