KickJava   Java API By Example, From Geeks To Geeks.

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


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: RegisteredStatics.java
32
//
33
// Description: Static Resource management.
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.util.*;
54
55 import org.omg.CORBA.*;
56 import org.omg.CosTransactions.*;
57
58 /**
59  * The RegisteredStatics class provides operations that manage the set of
60  * StaticResource objects and distributes association operations to those
61  * registered objects.
62  *
63  * @version 0.01
64  *
65  * @author Simon Holdsworth, IBM Corporation
66  *
67  * @see
68  */

69
70 //---------------------------------------------------------------------------
71
// CHANGE HISTORY
72
//
73
// Version By Change Description
74
// 0.01 SAJH Initial implementation.
75
//---------------------------------------------------------------------------
76

77 class RegisteredStatics {
78
79     private Vector registered = new Vector();
80
81     /**
82      * Default RegisteredStatics constructor.
83      *
84      * @param
85      *
86      * @return
87      *
88      * @see
89      */

90     RegisteredStatics() {}
91
92     /**
93      * Cleans up the state of the RegisteredStatics object.
94      *
95      * @param
96      *
97      * @return
98      *
99      * @see
100      */

101     public void finalize() {
102
103         if (registered != null) {
104             registered.removeAllElements();
105         }
106         registered = null;
107     }
108
109     /**
110      * Informs all registered objects an association has started.
111      * <p>
112      * The Control object which represents the transaction is given.
113      * <p>
114      * A flag is passed indicating whether this association
115      * is as a result of a Current.begin operation.
116      *
117      * @param control The transaction whose association has started.
118      * @param begin Indicates if this is a begin rather than a resume.
119      *
120      * @return
121      *
122      * @see
123      */

124     void distributeStart(ControlImpl control, boolean begin) {
125
126         // Determine the Coordinator for the transaction.
127

128         org.omg.CosTransactions.Coordinator coord = null;
129
130         try {
131             coord = control.get_coordinator();
132         } catch (Unavailable exc) {}
133
134         // Browse through the set, telling each that association is starting.
135

136         if (coord != null) {
137
138             for (int i = 0; i < registered.size(); i++) {
139
140                 StaticResource resource =
141                     (StaticResource) registered.elementAt(i);
142
143                 try {
144                     resource.startAssociation(coord, begin);
145                 } catch (INVALID_TRANSACTION exc) {
146                     // Catch INVALID_TRANSACTION exception, and allow it to
147
// percolate. We need to inform all previously called
148
// StaticResources that the association has ended
149
// immediately.
150

151                     for (int j = i - 1; j >= 0; j--) {
152                         ((StaticResource) registered.elementAt(j)).
153                             endAssociation(coord, begin);
154                     }
155
156                     throw (INVALID_TRANSACTION)exc.fillInStackTrace();
157                 } catch (Throwable JavaDoc exc) {
158                     // discard any other exception
159
}
160             }
161         }
162     }
163
164     /**
165      * Informs all registered StaticResource objects that a thread association
166      * has ended.
167      * <p>
168      * The Control object representing the transaction is given.
169      * <p>
170      * A flag is passed indicating whether this association
171      * is as a result of the transaction completing.
172      *
173      * @param control The transaction whose association has ended.
174      * @param complete Indicates that this is a commit/rollback rather than a
175      * suspend.
176      *
177      * @return
178      *
179      * @see
180      */

181     void distributeEnd(ControlImpl control, boolean complete) {
182
183         // Determine the Coordinator for the transaction.
184

185         org.omg.CosTransactions.Coordinator coord = null;
186
187         try {
188             coord = control.get_coordinator();
189         } catch (Unavailable exc) {}
190
191         // Browse through the set, telling each that the association is ending.
192

193         if (coord != null) {
194             for (int i = 0; i < registered.size(); i++) {
195                 StaticResource resource =
196                     (StaticResource)registered.elementAt(i);
197                 try {
198                     resource.endAssociation(coord, complete);
199                 } catch (Throwable JavaDoc e) {
200                     // Discard any exception.
201
}
202             }
203         }
204     }
205
206     /**
207      * Adds the given StaticResource object to the set of those informed of
208      * thread association changes.
209      * <p>
210      * If there is a current thread association, then the added StaticResource
211      * is called immediately so that it is aware of the association.
212      *
213      * @param obj The StaticResource to be added.
214      *
215      * @return
216      *
217      * @see
218      */

219
220     void addStatic(StaticResource obj) {
221
222         registered.addElement(obj);
223
224         // Determine whether there is a current association.
225

226         try {
227             org.omg.CosTransactions.Coordinator coord =
228                 CurrentTransaction.getCurrentCoordinator();
229
230             // Tell the StaticResource that the association has started.
231
// Pretend that it is a begin association, as the
232
// StaticResource has not seen the transaction before.
233

234             if (coord != null) {
235                 obj.startAssociation(coord, true);
236             }
237         } catch(Throwable JavaDoc exc) {
238             // Discard any exception.
239
}
240     }
241
242     /**
243      * Removes the given StaticResource object from the set of those
244      * informed of thread association changes.
245      *
246      * @param obj The StaticResource to be removed.
247      *
248      * @return Indicates success of the operation.
249      *
250      * @see
251      */

252     boolean removeStatic(StaticResource obj) {
253
254         boolean result = registered.removeElement(obj);
255         return result;
256     }
257 }
258
Popular Tags