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: *
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);
}
}
}