package examples.ejb.basic.containerManaged; import java.rmi.RemoteException; import java.util.Enumeration; import java.util.Hashtable; import java.util.Properties; import java.util.Vector; import javax.ejb.CreateException; import javax.ejb.DuplicateKeyException; import javax.ejb.FinderException; import javax.ejb.ObjectNotFoundException; import javax.ejb.RemoveException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.rmi.PortableRemoteObject; /** * This class demonstrates calling an entity EJBean, * followed by two exercises: *
* Part A: Create an Account and Deposit some money into it. Next * attempt to withdraw more than the current account balance. An * application-specific exception should be thrown. Finally, remove * the account. *
* Part B: Create some new accounts, with different initial balances. * Find all the accounts with a balance greater than a specific value. * Find the account with a balance of zero. Attempt to find accounts * with a null balance. Finally, remove all newly created accounts. *
* This class also illustrates how to lookup an EJB home in the JNDI tree. * * @author Copyright (c) 1998 by WebLogic, Inc. All Rights Reserved. * @author Copyright (c) 1998-2000 by BEA Systems, Inc. All Rights Reserved. */ public class Client { private String url; private String user; private String password; private String accountId; private AccountHome home; public Client(String url, String user, String password, String accountId) throws NamingException { this.url = url; this.user = user; this.password = password; this.accountId = accountId; home = lookupHome(); } /** * Runs this example from the command line. Example: *
* java examples.ejb.basic.containerManaged.Client "t3://localhost:7001" scott tiger 10020 *
* The parameters are optional, but if any are supplied, * they are interpreted in this order: *
* @param url URL such as "t3://localhost:7001" of Server
* @param user User name, default null
* @param password User password, default null
* @param accountID String Account ID to test, default "10020"
*/
public static void main(String[] args) {
System.out.println("\nBeginning containerManaged.Client...\n");
String url = "t3://localhost:7001";
String user = null;
String password = null;
String accountId = "10020";
// Parse the argument list
switch(args.length) {
case 4:
accountId = args[3];
// fall through
case 3:
password = args[2];
// fall through
case 2:
user = args[1];
// fall through
case 1:
url = args[0];
break;
}
Client client = null;
try {
client = new Client(url, user, password, accountId);
} catch (NamingException ne) {
log("Unable to look up the beans home: " + ne.getMessage());
System.exit(1);
}
try {
client.runExamplePartA();
client.runExamplePartB();
} catch (Exception e) {
log("There was an exception while creating and using the Accounts.");
log("This indicates that there was a problem communicating with the server: "+e);
}
System.out.println("\nEnd containerManaged.Client...\n");
}
/**
* Executes Part A of this example.
*
* Part A: Deposit and attempt to withdraw more than the current
* account balance. An application-specific exception should be thrown.
*/
public void runExamplePartA() {
log("Starting Part A of the example...\n");
double initialBalance = 3000;
String accountType = "Savings";
Account ac = null;
try {
ac = createAccount(accountId, initialBalance, accountType);
} catch(DuplicateKeyException dke) {
// This won't prevent us from continuing the example
log("An account with id " + accountId + " already exists!");
} catch(Exception e) {
log("Error creating account: " + e.getMessage());
System.exit(1);
}
int amount = 2000;
double balance = 0;
log("\nPart A: Depositing $" + amount );
try {
balance = ac.deposit(amount);
log("Current balance is $" + balance);
} catch (Exception e) {
log("Error during deposit: "+e.getMessage());
}
try {
log("Attempting to withdraw an amount greater than current balance. Expecting an exception...\n");
balance = ac.withdraw(balance+1);
log("Error: expected an exception.");
} catch (ProcessingErrorException pe) {
log("Received expected Processing Error:\n" + pe);
} catch(Exception e) {
log("Error during withdraw: "+e.getMessage());
}
log("Removing account...\n");
try {
ac.remove();
} catch(Exception e) {
log("Error removing account: "+e.getMessage());
}
log("End Part A of the example...\n");
}
/**
* Executes Part B of this example.
*
* Part B: Create 20 new accounts, with different initial balances.
* Find all the accounts with a balance greater than a specific value.
* When finished, remove the new accounts.
*/
public void runExamplePartB()
throws CreateException, RemoteException, FinderException,
RemoveException
{
log("Starting Part B of the example...\n");
int numBeans = 20;
Account [] accounts = new Account [numBeans];
// Create 20 accounts with varying balances and some null accountTypes.
for (int i=0; i