KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > eb > ClientAccount


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 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: ClientAccount.java,v 1.10 2005/06/10 12:44:51 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package eb;
27
28 import java.util.Enumeration;
29
30 import javax.naming.Context;
31 import javax.naming.InitialContext;
32 import javax.rmi.PortableRemoteObject;
33 import javax.transaction.UserTransaction;
34
35 /**
36  * Sample for entity beans. Usage: implicit persistance: java eb.ClientAccount
37  * AccountImplHome explicit persistance: java eb.ClientAccount AccountExplHome
38  * @author JOnAS team
39  */

40 public class ClientAccount {
41
42     /**
43      * UserTransaction object
44      */

45     private static UserTransaction utx = null;
46
47     private static void accountList(AccountHome h) {
48         Enumeration alist;
49         Account acc;
50         try {
51             utx.begin(); // faster if made inside a Tx
52
alist = h.findAllAccounts();
53             while (alist.hasMoreElements()) {
54                 acc = (Account) PortableRemoteObject.narrow(alist.nextElement(), Account.class);
55                 System.out.println(acc.getNumber() + " " + acc.getCustomer() + " " + acc.getBalance());
56             }
57             utx.commit();
58         } catch (Exception e) {
59             System.err.println("Exception getting account list: " + e);
60             System.exit(2);
61         }
62     }
63
64     public static void main(String[] args) {
65
66         // 1st arg. is the bean home (AccountImplHome or AccountExplHome)
67
String beanName = args[0];
68
69         // get JNDI initial context
70
Context initialContext = null;
71         try {
72             initialContext = new InitialContext();
73         } catch (Exception e) {
74             System.err.println("Cannot get initial context for JNDI: " + e);
75             System.exit(2);
76         }
77
78         // We want to start transactions from client: get UserTransaction
79
System.out.println("Getting a UserTransaction object from JNDI");
80         try {
81
82             // Comment the following lines if you want to use a David Client:
83
utx = (UserTransaction) initialContext.lookup("javax.transaction.UserTransaction");
84
85         } catch (Exception e) {
86             System.err.println("Cannot lookup UserTransaction: " + e);
87             System.exit(2);
88         }
89
90         // Connecting to Home thru JNDI
91
System.out.println("Connecting to the AccountHome");
92         AccountHome home = null;
93         try {
94             home = (AccountHome) PortableRemoteObject.narrow(initialContext.lookup(beanName), AccountHome.class);
95         } catch (Exception e) {
96             System.err.println("Cannot lookup " + beanName + ": " + e);
97             System.exit(2);
98         }
99
100         // List existing Accounts
101
System.out.println("Getting the list of existing accounts in database");
102         accountList(home);
103
104         // Create a first Account
105
System.out.println("Creating a new Account in database");
106         Account a1 = null;
107         try {
108             a1 = home.create(109, "John Smith", 0);
109         } catch (Exception e) {
110             System.err.println("Cannot create Account: " + e);
111             System.exit(2);
112         }
113
114         // Find a second Account
115
System.out.println("Finding an Account by its number in database");
116         Account a2 = null;
117         try {
118             a2 = home.findByNumber(102);
119         } catch (Exception e) {
120             System.err.println("Cannot find Account: " + e);
121             System.exit(2);
122         }
123
124         // First transaction (committed):
125
// transfert 100 from a2 to a1
126
System.out.println("Starting a first transaction, that will be committed");
127         try {
128             double value = 100.00;
129             utx.begin();
130             a1.setBalance(value);
131             a2.setBalance(-value);
132             utx.commit();
133         } catch (Exception e) {
134             System.err.println("exception during 1st Tx: " + e);
135             System.exit(2);
136         }
137
138         // Start another transaction (rolled back):
139
// transfert 20 from a1 to a2
140
System.out.println("Starting a second transaction, that will be rolled back");
141         try {
142             double value = 20.00;
143             utx.begin();
144             a1.setBalance(-value);
145             a2.setBalance(value);
146             utx.rollback();
147         } catch (Exception e) {
148             System.err.println("exception during 2nd Tx: " + e);
149             System.exit(2);
150         }
151
152         // List existing Accounts
153
System.out.println("Getting the new list of accounts in database");
154         accountList(home);
155
156         // Delete account
157
System.out.println("Removing Account previously created in database");
158         try {
159             a1.remove();
160         } catch (Exception e) {
161             System.err.println("exception during remove: " + e);
162             System.exit(2);
163         }
164
165         // Exit program
166
System.out.println("ClientAccount terminated");
167     }
168
169 }
170
171
Popular Tags