KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jts > jtsxa > OTSResourceImpl


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 package com.sun.jts.jtsxa;
30
31 import org.omg.CosTransactions.*;
32 import org.omg.CORBA.*;
33 import org.omg.PortableServer.*;
34
35 import com.sun.jts.CosTransactions.*;
36
37 import com.sun.jts.jta.*;
38 import javax.transaction.xa.*;
39 //import com.sun.jts.etsjdbc.odbc.NativeXAResourceImpl;
40
import com.sun.jts.jta.TransactionState;
41
42 import java.util.logging.Logger JavaDoc;
43 import java.util.logging.Level JavaDoc;
44 import com.sun.logging.LogDomains;
45 import com.sun.jts.utils.LogFormatter;
46
47  /* An implementation of org.omg.CosTransactions.Resource to support
48  * X/Open XA compliant resource managers.
49  */

50
51 public class OTSResourceImpl extends OTSResourcePOA implements OTSResource {
52
53     private static POA poa = null;
54     private OTSResource thisRef = null;
55     private XAResource xaRes = null;
56     private Xid xid = null;
57     private TransactionState tranState = null;
58
59
60     /*
61         Logger to log transaction messages
62     */

63     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER);
64     /**
65      * Construct an XAResource object.
66      *
67      * @param xid the global transaction identifier
68      * @param xaRes the XAServer object associated with the resource manager
69      */

70
71     public OTSResourceImpl(Xid xid, XAResource xaRes,
72             TransactionState tranState) {
73         this.xaRes= xaRes; // Stash away the XAServer object
74
this.xid= xid; // Stash away the Transaction identifier
75
this.tranState = tranState;
76     }
77
78     /**
79      * Commit a transaction.
80      *
81      * @exception NotPrepared the resource manager has not been called
82      * for prepare.
83      * @exception HeuristicRollback a heuristic decision has been made,
84      * and the resource rolledback.
85      * @exception HeuristicHazard a heuristic decision has been made,
86      * but the disposition of
87      * the resource is unknown.
88      * @exception HeuristicMixed a heuristic decision has been made, some
89      * updates have been commited
90      * and others rolled back.
91      * @exception SystemException an unindentified error has been reported
92      * by the resource manager.
93      */

94
95     public void commit() throws NotPrepared, HeuristicRollback,
96             HeuristicHazard, HeuristicMixed, SystemException {
97
98         //ensureInitialized();
99

100         try {
101             xaRes.commit(xid, false);
102         } catch (Exception JavaDoc ex) {
103             destroy();
104             if (!(ex instanceof XAException)) {
105                 INTERNAL internal = new INTERNAL(0,CompletionStatus.COMPLETED_MAYBE);
106                 internal.initCause(ex);
107                 _logger.log(Level.WARNING, "jts.unexpected_error_occurred_twopc_commit", ex);
108                 throw internal;
109             }
110
111             XAException e = (XAException) ex;
112             if (_logger.isLoggable(Level.FINE))
113                 _logger.log(Level.FINE, "An XAException occurred in twopc commit", e);
114             if (e.errorCode == XAException.XA_HEURRB)
115                 throw new HeuristicRollback(ex.getMessage());
116             if (e.errorCode == XAException.XA_HEURHAZ)
117                 throw new HeuristicHazard(ex.getMessage());
118             if (e.errorCode == XAException.XA_HEURMIX)
119                 throw new HeuristicMixed(ex.getMessage());
120             if (e.errorCode == XAException.XA_RBPROTO)
121                 throw new NotPrepared(ex.getMessage());
122             if (e.errorCode == XAException.XA_HEURCOM)
123                 return;
124             if ((e.errorCode == XAException.XA_RETRY) ||
125         (e.errorCode == XAException.XA_RBTRANSIENT) ||
126         (e.errorCode == XAException.XA_RBCOMMFAIL))
127         throw new TRANSIENT();
128             if (e.errorCode >= XAException.XA_RBBASE &&
129                 e.errorCode <= XAException.XA_RBEND) {
130                 throw new HeuristicRollback(ex.getMessage());
131         }
132             throw new INTERNAL(0,CompletionStatus.COMPLETED_MAYBE);
133         }
134
135         destroy();
136         return;
137     }
138
139     /**
140      * Commit a transaction, using one-phase optimization.
141      *
142      * @exception HeuristicHazard a heuristic decision has been made,
143      * but the disposition of
144      * the resource is unknown.
145      * @exception SystemException an unindentified error has been reported
146      * by the resource manager.
147      */

148     public void commit_one_phase() throws HeuristicHazard, SystemException {
149
150         //ensureInitialized();
151

152         try {
153             xaRes.commit(xid, true);
154         } catch (Exception JavaDoc ex) {
155             destroy();
156             if (!(ex instanceof XAException)) {
157                 INTERNAL internal = new INTERNAL(0,CompletionStatus.COMPLETED_MAYBE);
158                 internal.initCause(ex);
159                 _logger.log(Level.WARNING, "jts.unexpected_error_occurred_twopc_commit", ex);
160                 throw internal;
161             }
162             XAException e = (XAException) ex;
163             if (_logger.isLoggable(Level.FINE))
164                 _logger.log(Level.FINE, "An XAException occurred in c_o_p", e);
165             if (e.errorCode == XAException.XA_HEURRB)
166                 throw new HeuristicHazard(ex.getMessage());
167             if (e.errorCode == XAException.XA_HEURHAZ)
168                 throw new HeuristicHazard(ex.getMessage());
169             if (e.errorCode == XAException.XA_HEURMIX)
170                 throw new HeuristicHazard(ex.getMessage());
171             //IASRI START 4722883
172
/**
173             if (e.errorCode >= XAException.XA_RBBASE &&
174                 e.errorCode <= XAException.XA_RBEND)
175                 return;
176             **/

177             if (e.errorCode == XAException.XA_HEURCOM)
178                 return;
179             if ((e.errorCode == XAException.XA_RETRY) ||
180                     (e.errorCode == XAException.XA_RBTRANSIENT) ||
181                     (e.errorCode == XAException.XA_RBCOMMFAIL))
182                 throw new TRANSIENT();
183             if (e.errorCode >= XAException.XA_RBBASE &&
184                 e.errorCode <= XAException.XA_RBEND) {
185                 HeuristicHazard hazex = new HeuristicHazard();
186                 ((Throwable JavaDoc)hazex).initCause((Throwable JavaDoc)ex);
187                 throw hazex;
188             }
189             //IASRI END 4722883
190
INTERNAL internal = new INTERNAL(0,CompletionStatus.COMPLETED_MAYBE);
191             internal.initCause(ex);
192             throw internal;
193         }
194
195         destroy();
196         return;
197     }
198
199     /**
200      * The resource manager can forget all knowledge of the transaction.
201      *
202      */

203     public void forget() {
204
205         //ensureInitialized();
206

207         // Perform the XA operation.
208
try {
209             xaRes.forget(xid);
210         } catch (XAException e) {
211             if (_logger.isLoggable(Level.FINE))
212                 _logger.log(Level.FINE,"An XAException occurred in forget", e);
213             // currently do nothing..
214
}
215
216         destroy();
217         return;
218     }
219
220     /**
221      * Prepare a transaction.
222      *
223      * <p>This is the first phase of the two-phase commit protocol.
224      *
225      * @exception HeuristicHazard a heuristic decision has been made,
226      * but the disposition of
227      * the resource is unknown.
228      * @exception HeuristicMixed a heuristic decision has been made,
229      * some updates have been commited
230      * and others rolled back.
231      */

232     public Vote prepare() throws HeuristicHazard, HeuristicMixed {
233
234         //ensureInitialized();
235

236         int rc = XAException.XAER_PROTO;
237
238         // Perform the XA operation.
239

240         try {
241             rc = xaRes.prepare(xid); // xa_prepare()
242
} catch (XAException e) {
243             if (_logger.isLoggable(Level.FINE))
244                 _logger.log(Level.FINE,"An XAException occurred in prepare", e);
245             // currently do nothing..
246
}
247
248         // Convert to Vote
249
if (rc == XAResource.XA_OK) {
250             return Vote.VoteCommit;
251         }
252
253         if (rc == XAResource.XA_RDONLY) {
254             destroy();
255             return Vote.VoteReadOnly;
256         }
257
258         if (rc == Configuration.LAO_PREPARE_OK) {
259             destroy();
260             return null;
261         }
262
263
264         destroy();
265         return Vote.VoteRollback; // Any other return code is rollback
266
}
267
268     /**
269      * Rollback a transaction.
270      *
271      * @exception HeuristicCommit a heuristic decision has been made,
272      * and the resource committed.
273      * @exception HeuristicHazard a heuristic decision has been made,
274      * but the disposition of
275      * the resource is unknown.
276      * @exception HeuristicMixed a heuristic decision has been made,
277      * some updates have been commited
278      * and others rolled back.
279      * @exception SystemException an unindentified error has been reported
280      * by the resource manager
281      */

282     public void rollback() throws HeuristicCommit, HeuristicMixed,
283             HeuristicHazard, SystemException {
284
285         //ensureInitialized();
286

287         try {
288             if (tranState == null) {
289                 // this block will be entered during recovery processing.
290
// there is no tranState object available during recovery.
291
xaRes.rollback(xid);
292             } else {
293                 // need to worry about asynchronous rollback
294
tranState.rollback(xaRes);
295             }
296         } catch (Exception JavaDoc ex) {
297             destroy();
298             if (!(ex instanceof XAException)) {
299                 INTERNAL internal = new INTERNAL(0,CompletionStatus.COMPLETED_MAYBE);
300                 internal.initCause(ex);
301                 _logger.log(Level.WARNING, "jts.unexpected_error_occurred_twopc_rollback", ex);
302                 throw internal;
303             }
304
305             XAException e = (XAException) ex;
306             if (_logger.isLoggable(Level.FINE))
307                 _logger.log(Level.FINE, "An XAException occurred in rollback", e);
308             if (e.errorCode == XAException.XA_HEURCOM)
309                 throw new HeuristicCommit(ex.getMessage());
310             if (e.errorCode == XAException.XA_HEURHAZ)
311                 throw new HeuristicHazard(ex.getMessage());
312             if (e.errorCode == XAException.XA_HEURMIX)
313                 throw new HeuristicMixed(ex.getMessage());
314             if (e.errorCode == XAException.XA_HEURCOM)
315                 return;
316             if ((e.errorCode == XAException.XA_RETRY) ||
317         (e.errorCode == XAException.XA_RBTRANSIENT) ||
318         (e.errorCode == XAException.XA_RBCOMMFAIL))
319         throw new TRANSIENT();
320             INTERNAL internal = new INTERNAL(0,CompletionStatus.COMPLETED_MAYBE);
321             internal.initCause(ex);
322             throw internal;
323         }
324
325         destroy();
326         return;
327     }
328
329     /**
330      * Return the global transaction identifier.
331      *
332      * @return the global transaction identifier as defined
333      * by org.omg.CosTransactions.otid_t
334      *
335      */

336     public otid_t getGlobalTID() {
337
338         byte[] gtrid = xid.getGlobalTransactionId();
339         byte[] otidData = new byte[gtrid.length];
340         System.arraycopy(gtrid,0,otidData,0,gtrid.length);
341         otid_t otid = new otid_t(xid.getFormatId(),0,otidData);
342
343         return otid;
344     }
345
346     /**
347      * Returns the CORBA Object which represents this object.
348      *
349      * @return The CORBA object.
350      *
351      */

352     public OTSResource getCORBAObjReference() {
353
354         if (thisRef == null) {
355             if (poa == null) {
356                 poa = Configuration.getPOA("transient"/*#Frozen*/);
357             }
358
359             try {
360                 poa.activate_object(this);
361                 thisRef = OTSResourceHelper.
362                             narrow(poa.servant_to_reference(this));
363                 //thisRef = (com.sun.jts.jtsxa.OTSResource)this;
364
} catch (Exception JavaDoc exc) {
365                 _logger.log(Level.SEVERE,"jts.create_xaresource_object_error");
366                 String JavaDoc msg = LogFormatter.getLocalizedMessage(_logger,
367                             "jts.create_xaresource_object_error");
368                 throw new org.omg.CORBA.INTERNAL JavaDoc(msg);
369             }
370         }
371
372         return thisRef;
373     }
374
375     /**
376      * Destroy the OTSResourceImpl object.
377      *
378      * @param servant the object to be destroyed.
379      *
380      */

381     private void destroy() {
382
383         if (poa != null && thisRef != null) {
384             try {
385                 poa.deactivate_object(poa.reference_to_id(thisRef));
386                 thisRef = null;
387             } catch (Exception JavaDoc exc) {
388                 _logger.log(Level.WARNING,"jts.object_destroy_error","OTSResource");
389             }
390         }
391         // finalize();
392
}
393
394     /**
395      * Ensure that the Native XA interface is initialized.
396      *
397      * Invoke the nativeXA initializer to ensure that this resource has had
398      * any necessary initialization performed on this thread.
399      * This will be necessary in the server environment where we
400      * have not been invoked via a transactional flow, and
401      * we may very well be executing on a new thread. The resource manager may
402      * have specific requirements, but it should not matter if initialization
403      * is performed twice.
404      *
405      * We also call down to the native interface informing that the transaction
406      * is about to complete. This permits any database specific action to be
407      * performed.prior to the actual xa operations marking completion.
408      *
409      */

410     private void ensureInitialized() {
411         /* COMMENT(Ram J) - we do not support native xa drivers.
412         if (this.xaRes instanceof NativeXAResourceImpl) {
413             NativeXAResourceImpl xaResImpl = (NativeXAResourceImpl) xaRes;
414
415             xaResImpl.nativeXA.initialize(xaResImpl,
416                 xaResImpl.xaswitch,
417                 xaResImpl.open,
418                 xaResImpl.close,
419                 xaResImpl.getRMID(),
420                 false);
421
422             xaResImpl.nativeXA.aboutToComplete(xaResImpl, (XID) xid);
423         }
424         */

425     }
426
427     /*
428      * These methods are there to satisy the compiler. At some point
429      * when we move towards a tie based model, the org.omg.Corba.Object
430      * interface method implementation below shall be discarded.
431      */

432
433     private static org.omg.CORBA.NO_IMPLEMENT JavaDoc no_implement =
434         new org.omg.CORBA.NO_IMPLEMENT JavaDoc("This is a locally constrained object.");
435
436
437     public org.omg.CORBA.Object JavaDoc _duplicate() {
438         throw no_implement;
439     }
440
441     public void _release() {
442         throw no_implement;
443     }
444
445     public boolean _is_a(String JavaDoc repository_id) {
446         throw no_implement;
447     }
448
449     public boolean _is_equivalent(org.omg.CORBA.Object JavaDoc that) {
450         throw no_implement;
451     }
452
453     public boolean _non_existent() {
454         throw no_implement;
455     }
456
457     public int _hash(int maximum) {
458         throw no_implement;
459     }
460
461     public Request _request(String JavaDoc operation) {
462         throw no_implement;
463     }
464
465     public Request _create_request(Context ctx,
466                    String JavaDoc operation,
467                    NVList arg_list,
468                    NamedValue result) {
469         throw no_implement;
470     }
471
472     public Request _create_request(Context ctx,
473                    String JavaDoc operation,
474                    NVList arg_list,
475                    NamedValue result,
476                    ExceptionList exceptions,
477                    ContextList contexts) {
478         throw no_implement;
479     }
480
481     public org.omg.CORBA.Object JavaDoc _get_interface_def() {
482         throw no_implement;
483     }
484
485     public org.omg.CORBA.Policy JavaDoc _get_policy(int policy_type) {
486         throw no_implement;
487     }
488
489     public org.omg.CORBA.DomainManager JavaDoc[] _get_domain_managers() {
490         throw no_implement;
491     }
492
493     public org.omg.CORBA.Object JavaDoc _set_policy_override(
494             org.omg.CORBA.Policy JavaDoc[] policies,
495             org.omg.CORBA.SetOverrideType JavaDoc set_add) {
496         throw no_implement;
497     }
498 }
499
Popular Tags