1 25 26 package xdoclet; 27 28 import java.io.BufferedReader; 29 import java.io.FileInputStream; 30 import java.io.InputStreamReader; 31 import java.io.IOException; 32 import java.rmi.RemoteException; 33 import java.util.Collection; 34 import java.util.Iterator; 35 import java.util.Hashtable; 36 import java.util.Properties; 37 import javax.ejb.FinderException; 38 import javax.ejb.RemoveException; 39 import javax.transaction.UserTransaction; 40 import javax.naming.Context; 41 import javax.naming.InitialContext; 42 import javax.rmi.PortableRemoteObject; 43 import xdoclet.CustomerHomeRemote; 44 import xdoclet.CustomerRemote; 45 46 49 50 public class ClientCustomer { 51 52 private static UserTransaction utx = null; 53 54 private static void PrintAllCustomerAndPhones(CustomerHomeRemote h) { 55 Iterator alist; 56 CustomerRemote customer; 57 String phone; 58 try { 59 utx.begin(); alist = h.findAllCustomers().iterator(); 61 while (alist.hasNext()) { 62 customer = (CustomerRemote) alist.next(); 63 System.out.println("Customer name is :" + customer.getLastName() + " " + customer.getFirstName()); 64 Iterator phonelist = customer.getPhoneList().iterator(); 65 while (phonelist.hasNext()) { 66 phone = (String) phonelist.next(); 67 System.out.println("Phone number is: " + phone); 68 } 69 } 70 utx.commit(); 71 } catch (Exception e) { 72 System.err.println("Exception getting all Customer phone list: " + e); 73 System.exit(2); 74 } 75 } 76 77 public static void main(String[] args) { 78 79 String beanName = "CustomerBeanHome"; 80 81 Context initialContext = null; 83 try { 84 initialContext = new InitialContext(); 85 } catch (Exception e) { 86 System.err.println("Cannot get initial context for JNDI: " + e); 87 System.exit(2); 88 } 89 90 System.out.println("Getting a UserTransaction object from JNDI"); 92 try { 93 94 utx = (UserTransaction) initialContext.lookup("javax.transaction.UserTransaction"); 96 97 } catch (Exception e) { 98 System.err.println("Cannot lookup UserTransaction: " + e); 99 System.exit(2); 100 } 101 102 System.out.println("Connecting to the CustomerHome"); 104 CustomerHomeRemote home = null; 105 try { 106 home = (CustomerHomeRemote) PortableRemoteObject.narrow(initialContext.lookup(beanName), 107 CustomerHomeRemote.class); 108 } catch (Exception e) { 109 System.err.println("Cannot lookup " + beanName + ": " + e); 110 System.exit(2); 111 } 112 113 System.out.println("Getting the list of existing Customer in database"); 115 PrintAllCustomerAndPhones(home); 116 117 System.out.println("Creating a new customer in database"); 119 CustomerRemote c1 = null; 120 try { 121 c1 = home.create(new Integer(1), new Name("jerome", "camilleri")); 122 } catch (Exception e) { 123 System.err.println("Cannot create Customer: " + e); 124 System.exit(2); 125 } 126 127 try { 129 c1.addPhoneNumber("0476254717", (byte) 0); 130 c1.addPhoneNumber("0625785511", (byte) 0); 131 } catch (Exception e) { 132 System.out.println("Problem during add phone number"); 133 e.printStackTrace(System.out); 134 } 135 System.out.println("Getting the list of customer with Phone number in database"); 137 PrintAllCustomerAndPhones(home); 138 139 System.out.println("Removing Customer and phone previously created in database"); 141 try { 142 c1.removePhoneNumber((byte) 0); 143 c1.remove(); 144 } catch (Exception e) { 145 System.err.println("exception during remove: " + e); 146 System.exit(2); 147 } 148 149 System.out.println("ClientCustomer terminated"); 151 } 152 153 } 154 155 | Popular Tags |