/*
 * Created on Nov 10, 2003
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package address.ejb;

import java.rmi.RemoteException;
import java.util.Hashtable;

import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * @author SDNakhla
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class AddressBookClient {

	private address.ejb.PersonHome getPersonHome() throws NamingException {
		return (address.ejb.PersonHome) getContext().lookup(
			address.ejb.PersonHome.JNDI_NAME);
	}

	private address.ejb.AddressHome getAddressHome() throws NamingException {
		return (address.ejb.AddressHome) getContext().lookup(
			address.ejb.AddressHome.JNDI_NAME);
	}

	private InitialContext getContext() throws NamingException {
		Hashtable props = new Hashtable();

		props.put(
			InitialContext.INITIAL_CONTEXT_FACTORY,
			"org.jnp.interfaces.NamingContextFactory");
		props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");

		// This establishes the security for authorization/authentication
		// props.put(InitialContext.SECURITY_PRINCIPAL,"username");
		// props.put(InitialContext.SECURITY_CREDENTIALS,"password");

		InitialContext initialContext = new InitialContext(props);
		return initialContext;
	}
	public void testBean() {
	
		System.out.println("AddressBookClient::testBean()");
		try {
			System.out.println("\tCreating Address...");
			address.ejb.Address address = getAddressHome().create();
			System.out.println("\tAddress created.");
			
			System.out.println("\tSetting Address values...");
			address.setStreet("123 Main St.");
			address.setCity("Anytown");
			address.setState("US");
			address.setZipCode("12345");
			System.out.println("\tAddress values set.");
			
			System.out.println("\tCreating Person...");
			
			address.ejb.Person person = getPersonHome().create(address);

			/** MY EXCEPTION OCCURS IN THE LINE ABOVE */
			
			System.out.println("\tPerson created.");
			
			System.out.println("\tSetting Person values...");
			person.setFirstName("Joseph");
			person.setMiddleName("J");
			person.setLastName("Schmoe");
			System.out.println("\tPerson values set.");
			
			//--------------------------------------
			//This is the place you make your calls.
			//System.out.println(myBean.callYourMethod());

		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (CreateException e) {
			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		AddressBookClient test = new AddressBookClient();
		test.testBean();

	}
}
