import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

public class CallPersonBean {
	public static void main(String[] args) {
		try {
			Context initial = new InitialContext();
			Object objRef = initial.lookup("personBean");

			PersonHome home =
				(PersonHome) PortableRemoteObject.narrow(
					objRef,
					PersonHome.class);
			Person p1 = home.create(1911, 1, 1, "Alice", "streetA");
			Person p2 = home.create(1922, 2, 2, "Bob", "streetB");
			System.out.println("Alice's Age: " + p1.getAge());

			System.out.println("Alice's primary key: "+p1.getPrimaryKey());
			p1.remove();

			Person p3 = home.findByPrimaryKey("Bob");
			System.out.println(
				"Bob's modified street (before modification): "
					+ p3.getStreet());
			p3.setStreet("streetC");
			System.out.println(
				"Bob's modified street (after modification): "
					+ p3.getStreet());
			System.out.println("also the other reference: " + p2.getStreet());

			System.out.println("Are both references the same Java object: "+ (p2==p3));
			System.out.println("Are both references the same EJBObject (calls isIdentical): "+p2.isIdentical(p3));

		} catch (NamingException ne) {
			ne.printStackTrace();
		} catch (RemoteException re) {
			re.printStackTrace();
		} catch (CreateException ce) {
			ce.printStackTrace();
		} catch (RemoveException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
		}
	}
}
