package examples.jms.messageformat; import javax.ejb.CreateException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import weblogic.ejbgen.*; import weblogic.ejb.GenericSessionBean; /** * TraderBean is a stateful SessionBean. This EJBean illustrates: * * * @author Copyright (c) 1998-2005 by BEA Systems, Inc. All Rights Reserved. */ @EnvEntries({ @EnvEntry(name = "BEAS", type = "java.lang.Double", value = "100.0"), @EnvEntry(name = "MSFT", type = "java.lang.Double", value = "150.0") }) @FileGeneration(remoteClass = Constants.Bool.TRUE, localHome = Constants.Bool.FALSE, remoteHome = Constants.Bool.TRUE, remoteClassName = "Trader", remoteHomeName = "TraderHome", localClass = Constants.Bool.FALSE) @JarSettings(ejbClientJar = "jms_statefulSession_client.jar") @JndiName(remote = "jms-statefulSession-TraderHome") @Session(idleTimeoutSeconds = "600", maxBeansInCache = "1000", transTimeoutSeconds = "600", type = Session.SessionType.STATEFUL, defaultTransaction = Constants.TransactionAttribute.REQUIRED, ejbName = "jmsStatefulSession", enableCallByReference = Constants.Bool.TRUE, allowRemoveDuringTransaction = Constants.Bool.TRUE) public class TraderBean extends GenericSessionBean { static final boolean VERBOSE = false; private Context environment; private double tradingBalance; /** * 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 problem creating the bean * @see examples.jms.messageformat.Trader */ public void ejbCreate() throws CreateException { log("ejbCreate called"); try { InitialContext ic = new InitialContext(); environment = (Context) ic.lookup("java:comp/env"); } catch (NamingException ne) { throw new CreateException("Could not look up context"); } this.tradingBalance = 0.0; } /** * 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.jms.messageformat.ProcessingErrorException * if there is an error while buying the shares */ @RemoteMethod() public TradeResult buy(String customerName, String stockSymbol, int shares) throws ProcessingErrorException { log("buy ("+customerName+", "+stockSymbol+", "+shares+")"); double price = getStockPrice(stockSymbol); tradingBalance -= (shares * price); // subtract purchases from cash account return new TradeResult(shares, price, TradeResult.BUY); } /** * 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.jms.messageformat.ProcessingErrorException * if there is an error while selling the shares */ @RemoteMethod() public TradeResult sell(String customerName, String stockSymbol, int shares) throws ProcessingErrorException { log("sell ("+customerName+", "+stockSymbol+", "+shares+")"); double price = getStockPrice(stockSymbol); tradingBalance += (shares * price); return new TradeResult(shares, price, TradeResult.SELL); } /** * Returns the current balance of a trading session. * * @return double Balance */ @RemoteMethod() public double getBalance() { return tradingBalance; } /** * Returns the stock price for a given stock. * * @param stockSymbol String Stock symbol * @return double Stock price * @exception examples.jms.messageformat.ProcessingErrorException * if there is an error while checking the price */ public double getStockPrice(String stockSymbol) throws ProcessingErrorException { try { return ((Double) environment.lookup(stockSymbol)).doubleValue(); } catch (NamingException ne) { throw new ProcessingErrorException ("Stock symbol "+stockSymbol+ " does not exist"); } catch (NumberFormatException nfe) { throw new ProcessingErrorException("Invalid price for stock "+stockSymbol); } } private void log(String s) { if(VERBOSE) { System.out.println(s); } } }