package examples.ejb.basic.containerManaged; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javax.naming.*; import javax.ejb.*; import java.rmi.RemoteException; import java.util.*; import weblogic.html.*; /** * This servlet is a program similar to that in * examples.ejb.basic.containerManaged.Client. *
* The servlet must be registered in the weblogic.properties
file
* of the WebLogic Server:
*
* weblogic.httpd.register.containerManaged=examples.ejb.basic.containerManaged.Servlet
* weblogic.allow.execute.weblogic.servlet.containerManaged=servletUser
* weblogic.password.servletUser=servletUserPassword
*
* Call this servlet using an URL such as *
* http://localhost:7001/containerManaged?user=servletUser&password=servletUserPassword
*
* where you've substituted appropriate values for servletUser and servletUserPassword
* in both the weblogic.properties
file and the URL.
*
* @author Adapted by WebLogic, Inc.; adaptions copyright (c) 1998 by WebLogic, Inc. All Rights Reserved.
* @author
Adapted by BEA WebXpress; adaptions copyright (c) 1998-1999 by BEA WebXpress. All Rights Reserved.
*/
public class Servlet extends HttpServlet {
static String accountId = "10020";
static String ACCOUNT_TYPE = "Checking";
/**
* Builds an HTML page, finds the containerManaged home,
* creates beans and calls methods on the bean's remote interfaces.
*
* @param req HttpServletRequest
* @param res HttpServletResponse
* @throws java.io.IOException
*/
public void service(HttpServletRequest req, HttpServletResponse res)
throws IOException
{
res.setContentType("text/html");
OutputStream out = res.getOutputStream();
String title = "EJBean Container-managed JDBC Persistence Servlet";
ServletPage sp = new ServletPage(title);
sp.getBody()
.addElement(new HeadingElement(title, 1));
printElement("", sp);
// Get parameters off URL; example:
// "http://localhost:7001/beanManaged?user=foobar&password=FooBarNone"
String user = req.getParameter("user");
String password = req.getParameter("password");
if(user!=null && password!=null){
printElement("Using user " + user
+ " and password " + password + "", sp);
printElement("", sp);
} else {
printElement("No user and password credentials", sp);
printElement("", sp);
}
double amount = 100;
double balance = 3000;
AccountPK accountKey = new AccountPK();
accountKey.accountId = accountId;
Vector v = new Vector();
try {
// Contact the AccountBean container (the "AccountHome") through JNDI.
Context ctx = getInitialContext();
AccountHome home = (AccountHome) ctx.lookup("containerManaged.AccountHome");
// Find the Account or create it.
Account ac = null;
try {
printElement("Looking up account " + accountId + "...", sp);
ac = (Account) home.findByPrimaryKey(accountKey);
}
catch (Exception ee) {
printElement("Did not find " + accountId, sp);
}
if (ac == null) {
printElement("Account " + accountId +
" being created; opening balance is $" + balance, sp);
ac = home.create(accountId, balance, ACCOUNT_TYPE);
}
else {
printElement("Account " + accountId +
" found; balance is $" + ac.balance() +
"; type: " + ac.type(), sp);
}
printElement("", sp);
// Part A: Deposit and attempt to withdraw more than the current
// account balance. An application-specific exception should be thrown.
printElement("Part A: Depositing $" + amount, sp);
printElement("", sp);
balance = ac.deposit(amount);
printElement("Current balance is $" + balance, sp);
printElement("", sp);
amount = balance + 10;
try {
printElement("Withdrawing amount greater than current balance."
+ " Expecting an exception...", sp);
balance = ac.withdraw(amount);
printElement("Error: expected an exception.", sp);
}
catch (ProcessingErrorException pe) {
printElement("Received expected Processing Error:
" + pe, sp);
}
printElement("", sp);
// Part B: Create some new accounts, with different initial balances.
// Creates an account with a 'null' type.
// Find all the accounts with a balance greater than a specific value
// and in both ascending and descending order.
// Find a single account with a balance equal to a specific value.
// Find any accounts with a 'null' type.
// When finished, the new accounts are removed.
int numAccounts = 5;
Enumeration enum;
long now = System.currentTimeMillis();
printElement("Part B: Creating " + numAccounts + " new accounts...", sp);
printElement("", sp);
for (int i = 0; i < numAccounts; i++) {
String id = "" + now + i; // unique account id
balance = i*100; // initial balance
v.addElement(home.create(id, balance, ACCOUNT_TYPE));
printElement("Created account: " + id +
"; balance is $" + balance +
"; type: " + ACCOUNT_TYPE, sp);
}
if (v.size() == numAccounts) {
printElement("" + numAccounts + " accounts successfully created", sp);
}
else {
printElement("Error: Only " + v.size() +
" accounts were created successfully", sp);
}
printElement("", sp);
printElement("Creating an account with a null balance...", sp);
printElement("", sp);
v.addElement(home.create("" + now + numAccounts, 0, null));
double balanceGreaterThan = 50;
printElement("Querying for accounts with a balance greater than " +
balanceGreaterThan + "...ordered by Account ID", sp);
printElement("", sp);
enum = (Enumeration) home.findOrderedBigAccounts(balanceGreaterThan);
printEnumeratedBeans(enum, "There's no accounts with a balance greater than $" +
balanceGreaterThan, sp);
printElement("", sp);
printElement("Querying for accounts with a balance greater than " +
balanceGreaterThan + "...in descending order", sp);
printElement("", sp);
enum = (Enumeration) home.findDescOrderedBigAccounts(balanceGreaterThan);
printEnumeratedBeans(enum, "There's no accounts with a balance greater than $" +
balanceGreaterThan, sp);
printElement("", sp);
double balanceEqualTo = 200;
printElement("Querying for an account with a balance of $" +
balanceEqualTo + "...", sp);
printElement("", sp);
Account equalAccount = home.findAccount(balanceEqualTo);
if (equalAccount != null) {
printAccount(equalAccount, sp);
}
else {
printElement("There's no account with a balance equal to $" + balanceEqualTo, sp);
}
printElement("", sp);
printElement("Querying for an account with a type 'null'", sp);
enum = (Enumeration) home.findNullAccounts();
printEnumeratedBeans(enum, "There's no account with a type 'null'", sp);
printElement("", sp);
printElement("Removing all accounts just created...", sp);
for (int i = 0; i <= numAccounts; i++) {
String id = "" + now + i;
((Account)(v.elementAt(i))).remove();
printElement("Removed account: " +id, sp);
}
// Catch exceptions
}
catch (ProcessingErrorException pe) {
printElement("Unexpected Processing Error: " + pe, sp);
}
catch (Exception e) {
System.out.println(":::::::::::::: Unexpected Error :::::::::::::::::");
e.printStackTrace();
}
finally {
printElement("", sp);
printElement(new Date(System.currentTimeMillis()).toString(), sp);
printElement("End containerManaged.Servlet...", sp);
}
sp.output(out);
out.flush();
}
/**
* Basic servlet information.
*/
public String getServletInfo() {
return "EJBean Servlet";
}
/**
* Prints to the HTML stream an enumeration of beans.
*/
static private void printEnumeratedBeans(
Enumeration e, String message, ServletPage sp)
throws Exception {
if ((e != null) && (e.hasMoreElements())) {
while (e.hasMoreElements()) {
printAccount((Account) e.nextElement(), sp);
Thread.sleep(1000);
}
}
else {
System.out.println(message);
}
}
/**
* Prints to the HTML stream information on a bean.
*/
static private void printAccount(Account account, ServletPage sp)
throws RemoteException, IOException {
printElement("Account " + account.getPrimaryKey() +
"; balance is $" + account.balance() +
"; type: " + account.type(), sp);
}
/**
* Prints message to browser.
*/
static private void printElement(String message, ServletPage sp)
throws IOException {
sp.getBody()
.addElement(new BreakElement())
.addElement(new StringElement(message));
}
/**
* Gets an initial context for the current user, password and url.
*
* @return Context
* @exception java.lang.Exception if there is
* an error in getting the Context
*/
static public Context getInitialContext() throws Exception {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
return new InitialContext(p);
}
}