KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > ClientCustomer


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ClientCustomer.java,v 1.4 2004/05/12 09:18:14 camillej Exp $
23  * --------------------------------------------------------------------------
24  */

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 /**
47  * Sample for entity beans CMP2 xdoclet
48  */

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(); // faster if made inside a Tx
60
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         // get JNDI initial context
82
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         // We want to start transactions from client: get UserTransaction
91
System.out.println("Getting a UserTransaction object from JNDI");
92         try {
93
94             // Comment the following lines if you want to use a David Client:
95
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         // Connecting to Home thru JNDI
103
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         // List existing customer
114
System.out.println("Getting the list of existing Customer in database");
115         PrintAllCustomerAndPhones(home);
116
117         // Create a first customer
118
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         // Add phone number
128
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         // List existing customer
136
System.out.println("Getting the list of customer with Phone number in database");
137         PrintAllCustomerAndPhones(home);
138
139         // Delete Account2
140
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         // Exit program
150
System.out.println("ClientCustomer terminated");
151     }
152
153 }
154
155
Popular Tags