KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > concurrency > TransactionCoordinator


1 package org.jacorb.concurrency;
2
3
4
5 /*
6
7  * JacORB concurrency control service - a free CCS for JacORB
8
9  *
10
11  * Copyright (C) 1999-2004 LogicLand group, Viacheslav Tararin.
12
13  *
14
15  * This library is free software; you can redistribute it and/or
16
17  * modify it under the terms of the GNU Library General Public
18
19  * License as published by the Free Software Foundation; either
20
21  * version 2 of the License, or (at your option) any later version.
22
23  *
24
25  * This library is distributed in the hope that it will be useful,
26
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30
31  * Library General Public License for more details.
32
33  *
34
35  * You should have received a copy of the GNU Library General Public
36
37  * License along with this library; if not, write to the Free
38
39  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
40
41  */

42
43
44
45 import org.omg.CosConcurrencyControl.*;
46
47 import org.omg.PortableServer.POA JavaDoc;
48
49 import org.omg.PortableServer.POAPackage.*;
50
51 import org.omg.CosTransactions.*;
52
53 import java.util.*;
54
55
56 class TransactionCoordinator extends ResourcePOA /* implements Runnable */ {
57
58     static final int ACTIVE = 0;
59
60     static final int COMMITED = 1;
61
62     static final int PREPARED = 2;
63
64     static final int ROLLEDBACK = 3;
65
66
67
68     private Coordinator current;
69
70     private POA JavaDoc poa;
71
72     private Hashtable locksets = new Hashtable();
73
74     private int state;
75
76     private LockSetFactoryImpl factory;
77
78     TransactionCoordinator( LockSetFactoryImpl factory, Coordinator current, POA JavaDoc poa ) {
79
80         this.current = current;
81
82         this.poa = poa;
83
84         this.factory = factory;
85
86         Status status = current.get_status();
87
88         if( status.equals( Status.StatusActive ) ){
89
90             state = ACTIVE;
91
92         } else if( status.equals( Status.StatusPrepared ) ||
93
94                    status.equals( Status.StatusPreparing ) ) {
95
96             state = PREPARED;
97
98         } else if ( status.equals( Status.StatusCommitted ) ||
99
100                    status.equals( Status.StatusUnknown ) ||
101
102                    status.equals( Status.StatusNoTransaction ) ||
103
104                    status.equals( Status.StatusCommitting ) ) {
105
106             state = COMMITED;
107
108         } else if (status.equals( Status.StatusRollingBack ) ||
109
110                    status.equals( Status.StatusMarkedRollback) ||
111
112                    status.equals( Status.StatusRolledBack) ) {
113
114             state = ROLLEDBACK;
115
116         }
117
118     };
119
120     Coordinator get_coordinator() {
121
122         return current;
123
124     };
125
126     synchronized Status get_state(){
127
128         switch( state ){
129
130             case ACTIVE:
131
132                return Status.StatusActive;
133
134             case COMMITED:
135
136                return Status.StatusCommitted;
137
138             case PREPARED:
139
140                return Status.StatusPrepared;
141
142             case ROLLEDBACK:
143
144                return Status.StatusRolledBack;
145
146         }
147
148         return Status.StatusNoTransaction;
149
150     };
151
152     public synchronized Vote prepare() throws HeuristicMixed,HeuristicHazard {
153
154         if( state == ACTIVE ){
155
156             state = PREPARED;
157
158             return Vote.VoteCommit;
159
160         }
161
162         return Vote.VoteRollback;
163
164     };
165
166     public synchronized void rollback() throws HeuristicCommit,HeuristicMixed,HeuristicHazard {
167
168         if( state == ACTIVE ) {
169
170             state = ROLLEDBACK;
171
172             run();
173
174         }
175
176     };
177
178     public synchronized void commit() throws NotPrepared,HeuristicRollback,HeuristicMixed,HeuristicHazard{
179
180         if( state == PREPARED ) {
181
182             state = COMMITED;
183
184             run();
185
186         } else {
187
188             throw new NotPrepared();
189
190         }
191
192     };
193
194     public synchronized void commit_one_phase() throws HeuristicHazard {
195
196         if( state == ACTIVE ){
197
198             state = COMMITED;
199
200             run();
201
202         }
203
204     };
205
206     public synchronized void forget(){
207
208         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
209
210     };
211
212     synchronized LockCoordinator get_lock_coordinator( TransactionalLockSetImpl ls ){
213
214          LockCoordinatorImpl lc = (LockCoordinatorImpl)locksets.get( ls );
215
216          if( lc == null ){
217
218              lc = new LockCoordinatorImpl( this, ls );
219
220              locksets.put( ls, lc );
221
222          }
223
224          try {
225
226              return LockCoordinatorHelper.narrow(poa.servant_to_reference(lc));
227
228          } catch ( Exception JavaDoc e ){
229
230              e.printStackTrace( System.out );
231
232              throw new org.omg.CORBA.INTERNAL JavaDoc();
233
234          }
235
236     };
237
238
239
240     synchronized void set_lock_coordinator( TransactionalLockSetImpl ls ){
241
242         check_state();
243
244         LockCoordinatorImpl lc = (LockCoordinatorImpl)locksets.get( ls );
245
246         if( lc == null ){
247
248             lc = new LockCoordinatorImpl( this, ls );
249
250             locksets.put( ls, lc );
251
252         }
253
254     };
255
256     synchronized void remove_coordinator( TransactionalLockSetImpl ls ){
257
258         LockCoordinatorImpl lc = (LockCoordinatorImpl)locksets.get( ls );
259
260         if( lc != null ){
261
262             try {
263
264                 byte [] ObjId = poa.servant_to_id( lc );
265
266                 poa.deactivate_object( ObjId );
267
268             } catch ( Exception JavaDoc e ) {
269
270             }
271
272             locksets.remove( ls );
273
274         }
275
276     };
277
278     public void run() {
279
280         Enumeration enumeration = locksets.elements();
281
282         while( enumeration.hasMoreElements() ){
283
284             LockCoordinatorImpl lc = (LockCoordinatorImpl)enumeration.nextElement();
285
286             lc.drop_locks();
287
288             try {
289
290                 byte [] ObjId = poa.servant_to_id( lc );
291
292                 poa.deactivate_object( ObjId );
293
294             } catch ( ServantNotActive e ) {
295
296             } catch ( Exception JavaDoc e ){
297
298                 e.printStackTrace( System.out );
299
300                 throw new org.omg.CORBA.INTERNAL JavaDoc();
301
302             }
303
304         };
305
306         factory.remove_me( this );
307
308     };
309
310     private void check_state(){
311
312         if( state == PREPARED || state == COMMITED ) {
313
314             throw new org.omg.CORBA.INVALID_TRANSACTION JavaDoc();
315
316         } else if( state == ROLLEDBACK ) {
317
318             throw new org.omg.CORBA.TRANSACTION_ROLLEDBACK JavaDoc();
319
320         }
321
322     };
323
324 };
325
326
327
328
329
330
331
332
333
334
335
336
337
338
Popular Tags