KickJava   Java API By Example, From Geeks To Geeks.

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


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

7
8 import org.omg.CORBA.*;
9 import org.omg.CosTransactions.*;
10
11 public class AccountImpl
12     extends AccountPOA
13 {
14     private float balance;
15     private float newBalance;
16     private float amount;
17     private boolean credit;
18     private ORB orb;
19     private String JavaDoc name;
20     private Lock lock;
21
22     private boolean nasty = false;
23     private int nasty_count = 0;
24
25     public AccountImpl( ORB orb, String JavaDoc name, float deposit ){
26         this(orb, name, deposit, false);
27     }
28
29     public AccountImpl( ORB orb, String JavaDoc name, float deposit, boolean nasty )
30     {
31         this.name = name;
32         this.orb = orb;
33         this.nasty = nasty;
34
35         balance = deposit;
36         lock = new Lock();
37     }
38
39     public float balance()
40     {
41         return balance;
42     }
43
44     public synchronized void credit( float amount )
45     {
46         try
47         {
48             if (nasty){
49                 switch (nasty_count++){
50                 case 0 :
51                     break;
52                 case 1 :
53                     System.out.println("Step 1 nastyness: error");
54                     throw new Error JavaDoc("Bank Holidays");
55                 case 2:
56                     System.out.println("Step 2 nastyness: COMM_FALIURE");
57                     throw new org.omg.CORBA.COMM_FAILURE JavaDoc();
58                 }
59             }
60
61             // lock account
62
lock.lock();
63
64             Control control =
65                 org.omg.CosTransactions.CurrentHelper.narrow(
66         orb.resolve_initial_references("TransactionCurrent")).
67         get_control();
68
69             // memorize current activitity
70
this.amount = amount;
71             credit = true;
72
73             System.err.println("Account " + name + "::credit: get coordinator");
74             Coordinator coordinator = control.get_coordinator();
75
76             // register resource
77
System.out.println("Account " + name +
78                                "::credit: register resource (Account) with ITS");
79             RecoveryCoordinator recCoordinator =
80                 coordinator.register_resource( _this() );
81
82             newBalance = balance + amount;
83             System.out.println(" credit $" + amount );
84             System.out.println(" new balance is $" + newBalance );
85         }
86         catch( Exception JavaDoc ex )
87         {
88             System.err.println("Account " + name + "::credit: exception: " + ex );
89         throw new org.omg.CORBA.TRANSACTION_ROLLEDBACK JavaDoc();
90         };
91     }
92
93     public synchronized void debit( float amount )
94         throws InsufficientFunds
95     {
96         try
97         {
98
99             if (nasty){
100                 switch (nasty_count++){
101                 case 0 :
102                     System.out.println("Step 0 nastyness: InsufficientFunds");
103                     throw new InsufficientFunds();
104                 case 1 :
105                     System.out.println("Step 1 nastyness: error");
106                     throw new Error JavaDoc("Bank Holidays");
107                 case 2:
108                     System.out.println("Step 2 nastyness: COMM_FALIURE");
109                     throw new org.omg.CORBA.COMM_FAILURE JavaDoc();
110                 }
111             }
112             // lock account
113
lock.lock();
114
115             Control control =
116                 org.omg.CosTransactions.CurrentHelper.narrow(
117         orb.resolve_initial_references("TransactionCurrent")).
118         get_control();
119
120             // memorize current activitity
121
this.amount = amount;
122             credit = false;
123     
124             System.err.println("Account " + name + "::debit: get coordinator");
125             Coordinator coordinator = control.get_coordinator();
126
127             // register resource
128
System.out.println("Account " + name +
129                                "::debit: register resource (Account) with ITS");
130             RecoveryCoordinator recCoordinator =
131                 coordinator.register_resource( _this() );
132             System.out.println("Account " + name + "::debit: resource registered");
133
134             if( amount > balance )
135             {
136                 System.out.println("no sufficient funds");
137                 lock.unlock();
138                 System.out.println("Resource: " + name + " account unlocked");
139                 throw new InsufficientFunds();
140             }
141             newBalance = balance - amount;
142             System.out.println(" debit $" + amount );
143             System.out.println(" new balance is $" + newBalance );
144
145         }
146         catch( org.omg.CORBA.ORBPackage.InvalidName JavaDoc in ) {
147             System.err.println("Account " + name + "::debit: exception: " +in);
148         throw new org.omg.CORBA.TRANSACTION_ROLLEDBACK JavaDoc();
149         }
150         catch( Unavailable u ) {
151             System.err.println("Account " + name + "::debit: exception: " + u );
152         throw new org.omg.CORBA.TRANSACTION_ROLLEDBACK JavaDoc();
153         }
154         catch( Inactive i ) {
155             System.err.println("Account " + name + "::debit: exception: " + i );
156         throw new org.omg.CORBA.TRANSACTION_ROLLEDBACK JavaDoc();
157         }
158         catch( SystemException se ) {
159             System.err.println("Account " + name + "::debit: exception: " + se );
160         throw new org.omg.CORBA.TRANSACTION_ROLLEDBACK JavaDoc();
161         }
162     }
163
164     // implement methods of the Resource interface
165

166     public Vote prepare()
167     {
168         System.out.println("Resource " + name + " : prepare()");
169         if( balance == newBalance )
170             return Vote.VoteReadOnly;
171         return Vote.VoteCommit;
172     }
173
174     public void rollback()
175     {
176         // remove data from temporary storage
177
System.out.println("Resource " + name + " : rollback()");
178         System.out.println("Resource " + name +
179                            " : original balance restored: $" + balance);
180         lock.unlock();
181         System.out.println("Resource " + name + " account unlocked");
182     }
183
184     public void commit()
185     {
186         // move data to final storage
187
System.out.println("Resource " + name + " : commit()");
188         balance = newBalance;
189         lock.unlock();
190         System.out.println("Resource " + name + " account unlocked");
191     }
192
193     public void commit_one_phase()
194     {
195         // store data immediately at final destination
196
System.out.println("Resource " + name + " : commit_one_phase()");
197         if(prepare() == Vote.VoteCommit)
198         {
199             commit();
200         }
201     }
202
203     public void forget()
204     {
205         System.out.println("Resource " + name + " : forget()");
206     }
207
208 }
209
210
211
Popular Tags