KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jts > CosTransactions > SuperiorInfo


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 //----------------------------------------------------------------------------
30
//
31
// Module: SuperiorInfo.java
32
//
33
// Description: Global transaction information.
34
//
35
// Product: com.sun.jts.CosTransactions
36
//
37
// Author: Simon Holdsworth
38
//
39
// Date: March, 1997
40
//
41
// Copyright (c): 1995-1997 IBM Corp.
42
//
43
// The source code for this program is not published or otherwise divested
44
// of its trade secrets, irrespective of what has been deposited with the
45
// U.S. Copyright Office.
46
//
47
// This software contains confidential and proprietary information of
48
// IBM Corp.
49
//----------------------------------------------------------------------------
50

51 package com.sun.jts.CosTransactions;
52
53 import java.io.*;
54
55 import org.omg.CORBA.*;
56 import org.omg.CosTransactions.*;
57 import com.sun.corba.ee.spi.presentation.rmi.StubAdapter;
58
59 /**
60  * The SuperiorInfo interface provides operations that record the local
61  * transaction ID, global transaction ID for the superior (client) and the
62  * superior Coordinator.
63  * As an instance of this class may be accessed from multiple threads within
64  * a process, serialisation for thread-safety is necessary in the
65  * implementation. The information recorded in an instance of this class
66  * needs to be reconstructible in the case of a system failure.
67  *
68  * @version 0.01
69  *
70  * @author Simon Holdsworth, IBM Corporation
71  *
72  * @see
73 */

74
75 //----------------------------------------------------------------------------
76
// CHANGE HISTORY
77
//
78
// Version By Change Description
79
// 0.01 SAJH Initial implementation.
80
//----------------------------------------------------------------------------
81

82 class SuperiorInfo {
83     /**
84      * The local identifier for the transaction.
85      */

86     Long JavaDoc localTID = null;
87
88     /**
89      * The global identifier for the transaction.
90      */

91     GlobalTID globalTID = null;
92
93     /**
94      * The reference of the superior Coordinator.
95      */

96     Coordinator superior = null;
97
98     /**
99      * The reference of the RecoveryCoordinator returned on the
100      * register_resource call to the superior Coordinator,
101      * if any. Note that this member may be directly read,
102      * but must be set using the setRecovery method in this class.
103      */

104     RecoveryCoordinator recovery = null;
105
106     /**
107      * The reference of the Resource registered on the
108      * register_resource call to the superior Coordinator, if any.
109      * Note that this member may be directly
110      * read, but must be set using the setResource method in this class.
111      */

112     SubtransactionAwareResource resource = null;
113
114     private CoordinatorLog logRecord = null;
115     private java.lang.Object JavaDoc logSection = null;
116     private int resyncRetries = 0;
117
118     private final static String JavaDoc LOG_SECTION_NAME = "SI"/*#Frozen*/;
119
120     /**
121      * Default SuperiorInfo constructor.
122      *
123      * @param
124      *
125      * @return
126      *
127      * @see
128      */

129     SuperiorInfo() { }
130
131     /**
132      * Defines the local transaction ID, global ID and superior Coordinator
133      * reference.
134      * <p>
135      * The CoordinatorLog is used as the basis for recovering the
136      * SuperiorInfo at restart. The SuperiorInfo section in the CoordinatorLog
137      * is created, with the global identifier added to the section.
138      *
139      * @param localTID The local identifier for the transaction.
140      * @paran globalTID The global identifier for the transaction.
141      * @param superior The superior Coordinator reference (may be null).
142      * @param log The CoordinatorLog object (may be null).
143      *
144      * @return
145      *
146      * @see
147      */

148     SuperiorInfo(Long JavaDoc localTID, GlobalTID globalTID,
149                  Coordinator superior, CoordinatorLog log) {
150
151         this.localTID = localTID;
152         this.globalTID = globalTID;
153         this.superior = superior;
154         recovery = null;
155         resource = null;
156         logRecord = log;
157         resyncRetries = 0;
158
159         // Set up CoordinatorLog section for this class
160

161         if (log != null) {
162
163             // Create a section in the CoordinatorLog for SuperiorInfo
164

165             logSection = log.createSection(LOG_SECTION_NAME);
166
167             // Add the Global Id to the SuperiorInfo section of the log
168

169             log.addData(logSection,globalTID.toBytes());
170         }
171     }
172
173     /**
174      * Cleans up the objects state.
175      *
176      * @param
177      *
178      * @return
179      *
180      * @see
181      */

182     public void finalize() {
183
184         // Destroy the global transaction identifier.
185

186         //$ if( globalTID != null ) globalTID.finalize();
187

188         // Release superior Coordinator references.
189

190         if (superior != null &&
191                 !(superior instanceof org.omg.CORBA.LocalObject JavaDoc)) {
192             superior._release();
193         }
194
195         //$#ifndef OTS_DEFERRED_REGISTRATION
196
//$For deferred registration, the CurrentTransaction
197
//$will release the proxy, so there is no need to do it here.
198
//$For non-deferred registration, the RecoveryCoordinator
199
//$must be destroyed by the subordinate as no-one else will.
200

201         if (recovery != null &&
202                 !(recovery instanceof org.omg.CORBA.LocalObject JavaDoc)) {
203             recovery._release();
204         }
205
206         //$#endif /* OTS_DEFERRED_REGISTRATION */
207

208         localTID = null;
209         globalTID = null;
210         superior = null;
211         recovery = null;
212         resource = null;
213     }
214
215     /**
216      * Directs the SuperiorInfo to recover its state after a failure, based on
217      * the given CoordinatorLog object.
218      * <p>
219      * The SuperiorInfo then adds the given Coordinator to the
220      * RecoveryManager mappings. If the SuperiorInfo has already been defined
221      * or reconstructed, the operation does nothing.
222      * If the state cannot be reconstructed, the
223      * Coordinator is not added.
224      * <p>
225      * The global identifier, local ID, RecoveryCoordinator
226      * and CoordinatorResource object references are restored.
227      *
228      * @param log The CoordinatorLog object for the transaction.
229      * @param coord The Coordinator object recreated after recovery.
230      *
231      * @return
232      *
233      * @see
234      */

235     void reconstruct(CoordinatorLog log, CoordinatorImpl coord) {
236
237         superior = null;
238
239         // Recover our state from the CoordinatorLog object.
240
// Get the section id in the CoordinatorLog for SuperiorInfo
241

242         logSection = log.createSection(LOG_SECTION_NAME);
243         byte[][] logData = log.getData(logSection);
244
245         // Construct the global identifier.
246

247         globalTID = new GlobalTID(logData[0]);
248
249         // Get the local transaction identifier from the CoordinatorLog
250
// attribute rather than from the log record itself.
251

252         localTID = log.localTID;
253         logRecord = log;
254         resyncRetries = 0;
255
256         // Add the Coordinator to the RecoveryManager map. This is
257
// to allow the recovery of the CoordinatorResource reference,
258
// which results in a call to getCoordinator.
259

260         RecoveryManager.addCoordinator(globalTID, localTID, coord, 0);
261
262         // Get all objects that were logged. If this SuperiorInfo represents
263
// a root TopCoordinator object, then there will be no objects logged.
264

265         java.lang.Object JavaDoc[] logObjects = log.getObjects(logSection);
266
267         try {
268             if (logObjects.length > 1) {
269                 if (((org.omg.CORBA.Object JavaDoc) logObjects[0]).
270                         _is_a(RecoveryCoordinatorHelper.id())) {
271                     // TN - used to be com.sun.CORBA.iiop.CORBAObjectImpl
272
java.lang.Object JavaDoc rcimpl = logObjects[0];
273                         
274                     String JavaDoc[] ids = StubAdapter.getTypeIds(rcimpl);
275                     /*
276                     for (int i = 0; i < ids.length; i++)
277                         if( trc != null )
278                             trc.exit(998).data(i).data(ids[i]).write();
279                     */

280
281                     // TN - used to be com.sun.CORBA.iiop.CORBAObjectImpl
282
java.lang.Object JavaDoc crimpl = logObjects[1];
283                         
284                     ids = StubAdapter.getTypeIds(crimpl);
285                     /*
286                     for( int i = 0; i < ids.length; i++ )
287                         if( trc != null )
288                             trc.exit(998).data(i).data(ids[i]).write();
289                     */

290                     recovery = RecoveryCoordinatorHelper.
291                                 narrow((org.omg.CORBA.Object JavaDoc) logObjects[0]);
292
293                     resource = SubtransactionAwareResourceHelper.
294                                 narrow((org.omg.CORBA.Object JavaDoc) logObjects[1]);
295                 } else {
296                     recovery = RecoveryCoordinatorHelper.
297                                 narrow((org.omg.CORBA.Object JavaDoc) logObjects[1]);
298                     resource = SubtransactionAwareResourceHelper.
299                                 narrow((org.omg.CORBA.Object JavaDoc) logObjects[0]);
300                 }
301             } else {
302                 recovery = null;
303                 resource = null;
304             }
305         } catch (Throwable JavaDoc exc) {}
306     }
307
308     // same as reconstruct method: added for delegated recovery support
309
void delegated_reconstruct(CoordinatorLog log, CoordinatorImpl coord, String JavaDoc logPath) {
310
311         superior = null;
312
313         // Recover our state from the CoordinatorLog object.
314
// Get the section id in the CoordinatorLog for SuperiorInfo
315

316         logSection = log.createSection(LOG_SECTION_NAME);
317         byte[][] logData = log.getData(logSection);
318
319         // Construct the global identifier.
320

321         globalTID = new GlobalTID(logData[0]);
322
323         // Get the local transaction identifier from the CoordinatorLog
324
// attribute rather than from the log record itself.
325

326         localTID = log.localTID;
327         logRecord = log;
328         resyncRetries = 0;
329
330         // Add the Coordinator to the RecoveryManager map. This is
331
// to allow the recovery of the CoordinatorResource reference,
332
// which results in a call to getCoordinator.
333

334         DelegatedRecoveryManager.addCoordinator(globalTID, localTID, coord, 0, logPath);
335
336         // Get all objects that were logged. If this SuperiorInfo represents
337
// a root TopCoordinator object, then there will be no objects logged.
338

339         java.lang.Object JavaDoc[] logObjects = log.getObjects(logSection);
340
341         try {
342             if (logObjects.length > 1) {
343                 if (((org.omg.CORBA.Object JavaDoc) logObjects[0]).
344                         _is_a(RecoveryCoordinatorHelper.id())) {
345                     // TN - used to be com.sun.CORBA.iiop.CORBAObjectImpl
346
java.lang.Object JavaDoc rcimpl = logObjects[0];
347                         
348                     String JavaDoc[] ids = StubAdapter.getTypeIds(rcimpl);
349                     /*
350                     for (int i = 0; i < ids.length; i++)
351                         if( trc != null )
352                             trc.exit(998).data(i).data(ids[i]).write();
353                     */

354
355                     // TN - used to be com.sun.CORBA.iiop.CORBAObjectImpl
356
java.lang.Object JavaDoc crimpl = logObjects[1];
357                         
358                     ids = StubAdapter.getTypeIds(crimpl);
359                     /*
360                     for( int i = 0; i < ids.length; i++ )
361                         if( trc != null )
362                             trc.exit(998).data(i).data(ids[i]).write();
363                     */

364                     recovery = RecoveryCoordinatorHelper.
365                                 narrow((org.omg.CORBA.Object JavaDoc) logObjects[0]);
366
367                     resource = SubtransactionAwareResourceHelper.
368                                 narrow((org.omg.CORBA.Object JavaDoc) logObjects[1]);
369                 } else {
370                     recovery = RecoveryCoordinatorHelper.
371                                 narrow((org.omg.CORBA.Object JavaDoc) logObjects[1]);
372                     resource = SubtransactionAwareResourceHelper.
373                                 narrow((org.omg.CORBA.Object JavaDoc) logObjects[0]);
374                 }
375             } else {
376                 recovery = null;
377                 resource = null;
378             }
379         } catch (Throwable JavaDoc exc) {}
380     }
381
382
383     /**
384      * Records the RecoveryCoordinator for the transaction.
385      *
386      * @param rec The RecoveryCoordinator from the superior.
387      *
388      * @return
389      *
390      * @see
391      */

392     void setRecovery(RecoveryCoordinator rec) {
393
394         if (recovery == null) {
395             recovery = rec;
396
397             // Add the RecoveryCoordinator to the CoordinatorLog object
398

399             if (logRecord != null) {
400                 logRecord.addObject(logSection, rec);
401             }
402         }
403     }
404
405     /**
406      * Records the CoordinatorResource for the transaction.
407      *
408      * @param res The CoordinatorResource registered with the superior.
409      *
410      * @return
411      *
412      * @see
413      */

414     void setResource(SubtransactionAwareResource res) {
415
416         if (resource == null) {
417             resource = res;
418
419             // Add the CoordinatorResource to the CoordinatorLog object
420

421             if (logRecord != null) {
422                 logRecord.addObject(logSection, res);
423             }
424         }
425     }
426
427     /**
428      * Returns the number of retries so far, and increments the count.
429      *
430      * @param
431      *
432      * @return The number of retries so far.
433      *
434      * @see
435      */

436     int resyncRetries() {
437
438         int result = resyncRetries++;
439
440         return result;
441     }
442 }
443
Popular Tags