KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > bank > transaction > explicit > AccountImpl


1 package demo.bank.transaction.explicit;
2
3
4
5 /**
6
7  * Simple Transaction Service example, code taken and adapted
8
9  * from http://www.wiley.com/compbooks/vogel/ejb/code.html
10
11  */

12
13
14
15 import org.omg.CORBA.*;
16
17 import org.omg.CosTransactions.*;
18
19
20
21 public class AccountImpl
22
23     extends AccountPOA
24
25 {
26
27     private float balance;
28
29     private float newBalance;
30
31     private float amount;
32
33     private boolean credit;
34
35     private ORB orb;
36
37     private String JavaDoc name;
38
39     private Lock lock;
40
41
42
43     public AccountImpl( ORB orb, String JavaDoc name, float deposit )
44
45     {
46
47     this.name = name;
48
49     this.orb = orb;
50
51
52
53     balance = deposit;
54
55     lock = new Lock();
56
57     }
58
59
60
61     public float balance()
62
63     {
64
65     return balance;
66
67     }
68
69
70
71     public synchronized void credit( float amount, Control control )
72
73     {
74
75     try
76
77     {
78
79         // lock account
80

81         lock.lock();
82
83
84
85         // memorize current activitity
86

87         this.amount = amount;
88
89         credit = true;
90
91
92
93         System.err.println("Account " + name + "::credit: get coordinator");
94
95         Coordinator coordinator = control.get_coordinator();
96
97
98
99         // register resource
100

101         System.out.println("Account " + name +
102
103                    "::credit: register resource (Account) with ITS");
104
105         RecoveryCoordinator recCoordinator =
106
107         coordinator.register_resource( _this() );
108
109
110
111         newBalance = balance + amount;
112
113         System.out.println(" credit $" + amount );
114
115         System.out.println(" new balance is $" + newBalance );
116
117     }
118
119     catch( Exception JavaDoc ex )
120
121     {
122
123         System.err.println("Account " + name + "::credit: exception: " + ex );
124
125     };
126
127     }
128
129
130
131     public synchronized void debit( float amount, Control control )
132
133     throws InsufficientFunds
134
135     {
136
137     try
138
139     {
140
141
142
143         // lock account
144

145         lock.lock();
146
147
148
149         // memorize current activitity
150

151         this.amount = amount;
152
153         credit = false;
154
155
156
157         // obtain current object
158

159         System.err.println("Accont::debit: resolve transaction current");
160
161     
162
163         System.err.println("Account " + name + "::debit: get coordinator");
164
165         Coordinator coordinator = control.get_coordinator();
166
167
168
169         // register resource
170

171         System.out.println("Account " + name +
172
173                    "::debit: register resource (Account) with ITS");
174
175         RecoveryCoordinator recCoordinator =
176
177         coordinator.register_resource( _this() );
178
179         System.out.println("Account " + name + "::debit: resource registered");
180
181
182
183         if( amount > balance )
184
185         {
186
187         System.out.println("no sufficient funds");
188
189         lock.unlock();
190
191         System.out.println("Resource: " + name + " account unlocked");
192
193         throw new InsufficientFunds();
194
195         }
196
197         newBalance = balance - amount;
198
199         System.out.println(" debit $" + amount );
200
201         System.out.println(" new balance is $" + newBalance );
202
203
204
205     }
206
207     catch( Unavailable u ) {
208
209         System.err.println("Account " + name + "::debit: exception: " + u );
210
211     }
212
213     catch( Inactive i ) {
214
215         System.err.println("Account " + name + "::debit: exception: " + i );
216
217     }
218
219     catch( SystemException se ) {
220
221         System.err.println("Account " + name + "::debit: exception: " + se );
222
223     }
224
225     }
226
227
228
229     // implement methods of the Resource interface
230

231
232
233     public Vote prepare()
234
235     {
236
237     System.out.println("Resource " + name + " : prepare()");
238
239     if( balance == newBalance )
240
241         return Vote.VoteReadOnly;
242
243     return Vote.VoteCommit;
244
245     }
246
247
248
249     public void rollback()
250
251     {
252
253     // remove data from temporary storage
254

255     System.out.println("Resource " + name + " : rollback()");
256
257     System.out.println("Resource " + name +
258
259                " : original balance restored: $" + balance);
260
261     lock.unlock();
262
263     System.out.println("Resource " + name + " account unlocked");
264
265     }
266
267
268
269     public void commit()
270
271     {
272
273     // move data to final storage
274

275     System.out.println("Resource " + name + " : commit()");
276
277     balance = newBalance;
278
279     lock.unlock();
280
281     System.out.println("Resource " + name + " account unlocked");
282
283     }
284
285
286
287     public void commit_one_phase()
288
289     {
290
291     // store data immediately at final destination
292

293     System.out.println("Resource " + name + " : commit_one_phase()");
294
295     if(prepare() == Vote.VoteCommit)
296
297     {
298
299         commit();
300
301     }
302
303     }
304
305
306
307     public void forget()
308
309     {
310
311         System.out.println("Resource " + name + " : forget()");
312
313     }
314
315 }
316
317
Popular Tags