/**
* Sample client code which manipulates a Bank Account Entity Bean.
*/
public class AccountClient {
public static void main(String[] args) throws Exception {
Account account = null;
try {
/*
* Get a reference to the Account Home Object - the
* factory for Account EJB Objects
*/
Context ctx = new InitialContext(System.getProperties());
Object obj = ctx.lookup("AccountHome");
AccountHome home = (AccountHome) javax.rmi.PortableRemoteObject.narrow(obj, AccountHome.class);
System.err.println("Total of all accounts in bank initially = " + home.getTotalBankValue());
/*
* Use the factory to create the Account EJB Object
*/
home.create("123-456-7890", "John Smith");
/*
* Find an account
*/
Iterator i = home.findByOwnerName("John Smith").iterator();
if (i.hasNext()) {
account = (Account)
javax.rmi.PortableRemoteObject.narrow(
i.next(), Account.class);
}
else {
throw new Exception("Could not find account");
}
/*
* Call the balance() method, and print it
*/
System.out.println("Initial Balance = " + account.getBalance());
/*
* Deposit $100 into the account
*/
account.deposit(100);
System.out.println("Total of all accounts in bank now = " + home.getTotalBankValue());
/*
* Retrieve the Primary Key from the EJB Object
*/
AccountPK pk = (AccountPK) account.getPrimaryKey();
/*
* Release our old EJB Object reference. Now call
* find() again, this time querying on Account ID
* (i.e. the Primary Key).
*/
account = null;
account = home.findByPrimaryKey(pk);
/*
* Print out current balance
*/
System.out.println("Found account with ID " + pk + ". Balance = " + account.getBalance());
/*
* Try to withdraw $150
*/
System.out.println("Now trying to withdraw $150, which is more than is currently available. This should generate an exception..");
account.withdraw(150);
public class PerfectTime extends UnicastRemoteObject implements PerfectTimeI
{
public long getPerfectTime() throws RemoteException
{
return System.currentTimeMillis();
}
public PerfectTime() throws RemoteException
{
}
public static void main(String[] args) throws Exception
{
System.setSecurityManager( new RMISecurityManager());
PerfectTime pt=new PerfectTime();
Naming.bind("//localhost:2005/PerfectTime",pt);
System.out.println("Ready to do Time");
}
}