package examples.ejb.basic.statelessSession; import javax.ejb.*; import java.io.Serializable; import java.rmi.RemoteException; import java.util.*; /** * TraderBean is a stateless SessionBean. This bean illustrates: * * * @author Copyright (c) 1998 by WebLogic, Inc. All Rights Reserved. */ public class TraderBean implements SessionBean { static final boolean VERBOSE = true; // ----------------------------------------------------------------- // private variables private transient Hashtable stockPrices; private transient SessionContext ctx; private transient Properties props; // The reason the following attributes are commented out is to test // passivation into a persistent store, because the deployment descriptor // says it should be a stateless session bean. // Removing these attributes (and the corresponding methods for getting // their values -- getTraderName and getBalance), the different signature // for the ejbCreate method, and the Trader interface are the differences // between the examples in the stateful and stateless directories. //public String traderName; //public double tradingBalance; // ----------------------------------------------------------------- // SessionBean implementation /** * This method is required by the EJB Specification, * but is not used by this example. * */ public void ejbActivate() { if (VERBOSE) System.out.println("ejbActivate called"); } /** * This method is required by the EJB Specification, * but is not used by this example. * */ public void ejbRemove() { if (VERBOSE) System.out.println("ejbRemove called"); } /** * This method is required by the EJB Specification, * but is not used by this example. * */ public void ejbPassivate() { if (VERBOSE) System.out.println("ejbPassivate called"); } /** * Sets the session context. * * @param ctx SessionContext Context for session */ public void setSessionContext(SessionContext ctx) { if (VERBOSE) System.out.println("setSessionContext called"); this.ctx = ctx; props = ctx.getEnvironment(); } // Interface exposed to EJBObject (pretty much a clone of the // Trader interface without the RemoteException stuff, and an ejbCreate // without any parameters /** * This method corresponds to the create method in the home interface * "TraderHome.java". * The parameter sets of the two methods are identical. When the client calls * TraderHome.create(), the container allocates an instance of * the EJBean and calls ejbCreate(). * * @exception javax.ejb.CreateException if there is * a communications or systems failure * @see examples.ejb.basic.statelessSession.Trader */ public void ejbCreate () throws CreateException { if (VERBOSE) System.out.println("ejbCreate called"); } /** * Buys shares of a stock for a named customer. * * @param customerName String Customer name * @param stockSymbol String Stock symbol * @param shares int Number of shares to buy * @return TradeResult Trade Result * @exception examples.ejb.basic.statelessSession.ProcessingErrorException * if there is an error while buying the shares */ public TradeResult buy(String customerName, String stockSymbol, int shares) throws ProcessingErrorException { if (VERBOSE && shares >= 0) { System.out.println("buy (" + customerName + ", " + stockSymbol + ", " + shares + ")"); } String str = null; try { int tradeLimit = getTradeLimit(); if (shares > tradeLimit) shares = tradeLimit; else if (shares < -tradeLimit) // limit for selling shares = -tradeLimit; double price = getStockPrice(stockSymbol); if (VERBOSE) { str = "insert into tradingorders " + "(account, stockSymbol, shares, price) VALUES (" + "'" + customerName + "'" + "," + "'" + stockSymbol + "'" + "," + shares + "," + price + ")"; System.out.println ("Executing stmt: " + str); } if (shares < 0) shares = -shares; return new TradeResult(shares, price); } catch (Exception e) { throw new ProcessingErrorException("Trader error: " + e); } } /** * Sells shares of a stock for a named customer. * * @param customerName String Customer name * @param stockSymbol String Stock symbol * @param shares int Number of shares to buy * @return TradeResult Trade Result * @exception examples.ejb.basic.statelessSession.ProcessingErrorException * if there is an error while selling the shares */ public TradeResult sell(String customerName, String stockSymbol, int shares) throws ProcessingErrorException { if (VERBOSE) { System.out.println("sell (" + customerName + ", " + stockSymbol + ", " + shares + ")"); } // convert "sell 100 shares" to "buy -100 shares" return buy (customerName, stockSymbol, -shares); } // The following routines illustrate getting information // from the environment. /** * Returns the trading limit. * * @return int Trading limit */ private int getTradeLimit() { return Integer.parseInt((String)props.get("tradeLimit")); } /** * Returns the stock price for a given stock. * * @param stockSymbol String Stock symbol * @return double Stock price * @exception examples.ejb.basic.statelessSession.ProcessingErrorException * if there is an error while checking the price */ public double getStockPrice(String stockSymbol) throws ProcessingErrorException { if (stockPrices == null) { stockPrices = (Hashtable) props.get("StockPrices"); } String priceStr = (String)stockPrices.get(stockSymbol); if (priceStr == null) throw new ProcessingErrorException ("Stock symbol " + stockSymbol + " does not exist"); return Double.valueOf(priceStr).doubleValue(); } }