package examples.ejb.basic.statefulSession; import javax.ejb.*; import java.io.Serializable; import java.rmi.RemoteException; import java.util.*; /** * TraderBean is a stateful SessionBean. This EJBean illustrates: *
TraderHome.create()
, the container allocates an instance of
* the EJBean and calls ejbCreate()
.
*
* @param traderName String Trader Name
* @exception javax.ejb.CreateException
* if there is a problem creating the bean
* @see examples.ejb.basic.statefulSession.Trader
*/
public void ejbCreate(String traderName) throws CreateException {
if (VERBOSE)
System.out.println("ejbCreate (" + traderName + ")");
this.traderName = traderName;
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.ejb.basic.statefulSession.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) // limit for buying
shares = tradeLimit;
else if (shares < -tradeLimit) // limit for selling
shares = -tradeLimit;
double price = getStockPrice(stockSymbol);
str = "insert into tradingorders " +
"(traderName, account, stockSymbol, shares, price) VALUES (" +
"'" + traderName + "','"
+ customerName + "','"
+ stockSymbol + "',"
+ shares + "," +
+ price + ")";
System.out.println("Executing transaction: " + str);
tradingBalance = tradingBalance - (shares * price); // subtract purchases from cash account
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.statefulSession.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 name of this Trader.
*
* @return String traderName
*/
public String getTraderName() {
return this.traderName;
}
/**
* Returns the current balance of a trading session.
*
* @return double Balance
*/
public double getBalance() {
return this.tradingBalance;
}
/**
* 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.statefulSession.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();
}
}