KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jts > jta > SynchronizationImpl


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 package com.sun.jts.jta;
29
30 import java.util.*;
31 import javax.transaction.Synchronization JavaDoc;
32
33 import org.omg.CORBA.*;
34 import org.omg.CosTransactions.*;
35 import org.omg.PortableServer.POA JavaDoc;
36 import com.sun.jts.CosTransactions.Configuration;
37 import java.util.logging.Logger JavaDoc;
38 import java.util.logging.Level JavaDoc;
39 import com.sun.logging.LogDomains;
40
41 /**
42  * An implementation of org.omg.CosTransactions.Synchronization
43  * this object is activated at creation time and de-activated
44  * when after_completion is called
45  *
46  * @author Tony Ng
47  */

48 public class SynchronizationImpl extends SynchronizationPOA
49         implements org.omg.CosTransactions.Synchronization {
50
51     private Vector syncs;
52     private Vector interposedSyncs;
53     private POA JavaDoc poa;
54     private org.omg.CosTransactions.Synchronization corbaRef = null;
55     private TransactionState state = null;
56     /*
57         Logger to log transaction messages
58     */

59     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER);
60
61     public SynchronizationImpl() {
62         syncs = new Vector();
63         interposedSyncs = new Vector();
64         poa = Configuration.getPOA("transient"/*#Frozen*/);
65     }
66
67     public SynchronizationImpl(TransactionState state) {
68         this();
69         this.state = state;
70     }
71
72     public void addSynchronization(Synchronization JavaDoc sync,
73                                    boolean interposed) {
74         if (!interposed)
75             syncs.addElement(sync);
76         else
77             interposedSyncs.addElement(sync);
78     }
79
80     public void before_completion() {
81         // Regular syncs first then the interposed syncs
82
Enumeration e = syncs.elements();
83         while (e.hasMoreElements()) {
84             Synchronization JavaDoc sync = (Synchronization JavaDoc) e.nextElement();
85             try {
86                 sync.beforeCompletion();
87             } catch (RuntimeException JavaDoc rex) {
88                 try {
89                     state.setRollbackOnly();
90                 } catch (Exception JavaDoc ex1) {
91             _logger.log(Level.WARNING,
92                 "jts.unexpected_error_occurred_in_after_completion",ex1);
93                 }
94         _logger.log(Level.WARNING,
95             "jts.unexpected_error_occurred_in_after_completion",rex);
96             } catch (Exception JavaDoc ex) {
97                 _logger.log(Level.WARNING,
98                         "jts.unexpected_error_occurred_in_after_completion",ex);
99             }
100         }
101         Enumeration e1 = interposedSyncs.elements();
102         while (e1.hasMoreElements()) {
103             Synchronization JavaDoc sync = (Synchronization JavaDoc) e1.nextElement();
104             try {
105                 sync.beforeCompletion();
106             } catch (RuntimeException JavaDoc rex) {
107                 try {
108                     state.setRollbackOnly();
109                 } catch (Exception JavaDoc ex1) {
110             _logger.log(Level.WARNING,
111                 "jts.unexpected_error_occurred_in_after_completion",ex1);
112                 }
113         _logger.log(Level.WARNING,
114             "jts.unexpected_error_occurred_in_after_completion",rex);
115             } catch (Exception JavaDoc ex) {
116                 _logger.log(Level.WARNING,
117                         "jts.unexpected_error_occurred_in_after_completion",ex);
118             }
119         }
120         state.beforeCompletion();
121     }
122
123     public void after_completion(Status status) {
124         try {
125             int result = TransactionManagerImpl.mapStatus(status);
126             // Interposed Syncs First and then the regular syncs
127
Enumeration e1 = interposedSyncs.elements();
128             while (e1.hasMoreElements()) {
129                 Synchronization JavaDoc sync = (Synchronization JavaDoc) e1.nextElement();
130                 try {
131                     sync.afterCompletion(result);
132                 } catch (Exception JavaDoc ex) {
133                     _logger.log(Level.WARNING,
134                             "jts.unexpected_error_occurred_in_after_completion",ex);
135                 }
136             }
137             Enumeration e = syncs.elements();
138             while (e.hasMoreElements()) {
139                 Synchronization JavaDoc sync = (Synchronization JavaDoc) e.nextElement();
140                 try {
141                     sync.afterCompletion(result);
142                 } catch (Exception JavaDoc ex) {
143                     _logger.log(Level.WARNING,
144                             "jts.unexpected_error_occurred_in_after_completion",ex);
145                 }
146             }
147         } finally {
148             try {
149                 // deactivate object
150
if (corbaRef != null) {
151                     if (poa == null) {
152                         poa = Configuration.getPOA("transient"/*#Frozen*/);
153                     }
154                     poa.deactivate_object(poa.reference_to_id(corbaRef));
155                 }
156             } catch (Exception JavaDoc ex) {
157                 _logger.log(Level.WARNING,
158                         "jts.unexpected_error_occurred_in_after_completion",ex);
159             }
160         }
161     }
162
163     public org.omg.CosTransactions.Synchronization getCORBAReference() {
164         if (poa == null) {
165             poa = Configuration.getPOA("transient"/*#Frozen*/);
166         }
167
168         if (corbaRef == null) {
169             try {
170                 poa.activate_object(this);
171                 corbaRef = SynchronizationHelper.narrow(
172                             poa.servant_to_reference(this));
173                 //corbaRef = (org.omg.CosTransactions.Synchronization) this;
174
} catch (Exception JavaDoc ex) {
175                 _logger.log(Level.SEVERE,
176                         "jts.unexpected_error_in_getcorbareference",ex);
177             }
178         }
179
180         return corbaRef;
181     }
182
183     /*
184      * These methods are there to satisy the compiler. At some point
185      * when we move towards a tie based model, the org.omg.Corba.Object
186      * interface method implementation below shall be discarded.
187      */

188
189     private static org.omg.CORBA.NO_IMPLEMENT JavaDoc no_implement =
190         new org.omg.CORBA.NO_IMPLEMENT JavaDoc("This is a locally constrained object.");
191
192
193     public org.omg.CORBA.Object JavaDoc _duplicate() {
194         throw no_implement;
195     }
196
197     public void _release() {
198         throw no_implement;
199     }
200
201     public boolean _is_a(String JavaDoc repository_id) {
202         throw no_implement;
203     }
204
205     public boolean _is_equivalent(org.omg.CORBA.Object JavaDoc that) {
206         throw no_implement;
207     }
208
209     public boolean _non_existent() {
210         throw no_implement;
211     }
212
213     public int _hash(int maximum) {
214         throw no_implement;
215     }
216
217     public Request _request(String JavaDoc operation) {
218         throw no_implement;
219     }
220
221     public Request _create_request(Context ctx,
222                    String JavaDoc operation,
223                    NVList arg_list,
224                    NamedValue result) {
225         throw no_implement;
226     }
227
228     public Request _create_request(Context ctx,
229                    String JavaDoc operation,
230                    NVList arg_list,
231                    NamedValue result,
232                    ExceptionList exceptions,
233                    ContextList contexts) {
234         throw no_implement;
235     }
236
237     public org.omg.CORBA.Object JavaDoc _get_interface_def() {
238         throw no_implement;
239     }
240
241     public org.omg.CORBA.Policy JavaDoc _get_policy(int policy_type) {
242         throw no_implement;
243     }
244
245     public org.omg.CORBA.DomainManager JavaDoc[] _get_domain_managers() {
246         throw no_implement;
247     }
248
249     public org.omg.CORBA.Object JavaDoc _set_policy_override(
250             org.omg.CORBA.Policy JavaDoc[] policies,
251             org.omg.CORBA.SetOverrideType JavaDoc set_add) {
252         throw no_implement;
253     }
254 }
255
Popular Tags