package examples.ejb.basic.statefulSession; import javax.ejb.*; import javax.naming.*; import java.rmi.RemoteException; import java.util.*; /** * This class illustrates using a stateful SessionBean: * * * @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; /** * Runs this example from the command line. Example: *

* java examples.ejb.basic.statefulSession.Client "t3://localhost:7001" scott tiger *

* 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 */ public static void main(String[] args) { System.out.println("\nBeginning statefulSession.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; default: } } String customerName = "Erin"; try { // Create a trader object, who'll later help us execute trades Context ctx = getInitialContext(); TraderHome brokerage = (TraderHome) ctx.lookup("statefulSession.TraderHome"); // Give this trader a name Trader trader = brokerage.create("Terry"); System.out.println("Creating trader " + trader.getTraderName() + "\n"); String stockName; int numberOfShares; for (int i = 1 ; i <= 5; i++) { System.out.println("Start of Transaction " + i + " for " + customerName); // Buying stockName = "WEBL"; numberOfShares = 100 * i; System.out.println("Buying " + numberOfShares + " of " + stockName); TradeResult tr = trader.buy(customerName, stockName, numberOfShares); System.out.println("...Bought " + tr.numberTraded + " at $" + tr.priceSoldAt); // Selling stockName = "INTL"; numberOfShares = 100 * (i+1); System.out.println("Selling " + numberOfShares + " of " + stockName); tr = trader.sell(customerName, stockName, numberOfShares); System.out.println("...Sold " + tr.numberTraded + " at $" + tr.priceSoldAt); // Get change in Cash Account from EJBean System.out.println("Change in Cash Account: $" + trader.getBalance()); System.out.println("End of Transaction " + i + "\n"); } System.out.println("Change in Cash Account: $" + trader.getBalance() + "\n"); System.out.println("Removing trader " + trader.getTraderName()); trader.remove(); } catch (ProcessingErrorException pe) { System.out.println("Processing Error: " + pe); } catch (Exception e) { System.out.println(":::::::::::::: Error :::::::::::::::::"); e.printStackTrace(); } System.out.println("\nEnd statefulSession.Client...\n"); } /** * Parses an argument list for a url, user or password. * * @param args[] String array of arguments */ static void parseArgs(String args[]) { if ((args == null) || (args.length == 0)) return; for (int i = 0; i < args.length; i++) { if (args[i].equals("-url")) url = args[++i]; else if (args[i].equals("-user")) user = args[++i]; else if (args[i].equals("-password")) password = args[++i]; } } /** * Gets an initial context. * * @return Context * @exception java.lang.Exception if there is * an error in getting a 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); } }