KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > iiop > TransactionServiceImpl


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9
10 package org.jboss.tm.iiop;
11
12 import java.util.HashMap JavaDoc;
13
14 import javax.naming.Context JavaDoc;
15 import javax.naming.InitialContext JavaDoc;
16 import javax.naming.NamingException JavaDoc;
17 import javax.transaction.HeuristicMixedException JavaDoc;
18 import javax.transaction.HeuristicRollbackException JavaDoc;
19 import javax.transaction.InvalidTransactionException JavaDoc;
20 import javax.transaction.NotSupportedException JavaDoc;
21 import javax.transaction.RollbackException JavaDoc;
22 import javax.transaction.SystemException JavaDoc;
23 import javax.transaction.Transaction JavaDoc;
24 import javax.transaction.TransactionManager JavaDoc;
25 import javax.transaction.xa.Xid JavaDoc;
26
27 import org.omg.CORBA.BAD_INV_ORDER JavaDoc;
28 import org.omg.CORBA.BAD_OPERATION JavaDoc;
29 import org.omg.CORBA.BAD_PARAM JavaDoc;
30 import org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc;
31 import org.omg.CORBA.ORB JavaDoc;
32 import org.omg.CORBA.ORBPackage.InvalidName JavaDoc;
33 import org.omg.CORBA.INVALID_TRANSACTION JavaDoc;
34 import org.omg.CORBA.NO_IMPLEMENT JavaDoc;
35 import org.omg.CORBA.NO_PERMISSION JavaDoc;
36 import org.omg.CORBA.TRANSACTION_ROLLEDBACK JavaDoc;
37 import org.omg.CORBA.UNKNOWN JavaDoc;
38 import org.omg.CosTransactions.Control;
39 import org.omg.CosTransactions.ControlHelper;
40 import org.omg.CosTransactions.Coordinator;
41 import org.omg.CosTransactions.CoordinatorHelper;
42 import org.omg.CosTransactions.HeuristicHazard;
43 import org.omg.CosTransactions.HeuristicMixed;
44 import org.omg.CosTransactions.Inactive;
45 import org.omg.CosTransactions.NotSubtransaction;
46 import org.omg.CosTransactions.PropagationContext;
47 import org.omg.CosTransactions.RecoveryCoordinator;
48 import org.omg.CosTransactions.Resource;
49 import org.omg.CosTransactions.Status;
50 import org.omg.CosTransactions.SubtransactionAwareResource;
51 import org.omg.CosTransactions.SubtransactionsUnavailable;
52 import org.omg.CosTransactions.Synchronization;
53 import org.omg.CosTransactions.SynchronizationUnavailable;
54 import org.omg.CosTransactions.Terminator;
55 import org.omg.CosTransactions.TerminatorHelper;
56 import org.omg.CosTransactions.TransactionFactoryHelper;
57 import org.omg.CosTransactions.TransIdentity;
58 import org.omg.CosTransactions.Unavailable;
59 import org.omg.CosTransactions.otid_t;
60 import org.omg.PortableServer.POA JavaDoc;
61 import org.omg.PortableServer.CurrentPackage.NoContext JavaDoc;
62
63 import org.jboss.logging.Logger;
64 import org.jboss.tm.LocalId;
65 import org.jboss.tm.TransactionImpl;
66 import org.jboss.tm.TransactionPropagationContextFactory;
67 import org.jboss.tm.TransactionPropagationContextImporter;
68 import org.jboss.tm.TxManager;
69 import org.jboss.tm.XidImpl;
70
71 /**
72  * CORBA servant for the JBoss TransactionService.
73  *
74  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
75  * @version $Revision: 1.2 $
76  */

77 public class TransactionServiceImpl
78       extends TransactionServicePOA
79 {
80    // Constants ----------------------------------------------------
81

82    private static final Logger log =
83       Logger.getLogger(TransactionServiceImpl.class);
84
85    // The first byte of our CORBA oids tells the type of the object:
86

87    /** Value in position 0 of the oid within a Control reference. */
88    private static final byte CONTROL = 0x43;
89
90    /** Value in position 0 of the oid within a Coordinator reference. */
91    private static final byte COORDINATOR = 0x63;
92
93    /** Value in position 0 of the oid within a TransactionFactory reference. */
94    private static final byte TX_FACTORY = 0x46;
95
96    /** Value in position 0 of the oid within a Terminator reference. */
97    private static final byte TERMINATOR = 0x74;
98
99    // String arrays returned by _all_interfaces:
100

101    /** Interface ids of Control and its superinterfaces. */
102    private static final String JavaDoc[] controlInterfaceIds = {
103       ControlHelper.id()
104    };
105
106    /** Interface ids of CoordinatorExt and its superinterfaces. */
107    private static final String JavaDoc[] coordinatorInterfaceIds = {
108       CoordinatorExtHelper.id(),
109       CoordinatorHelper.id()
110    };
111
112    /** Interface ids of TransactionFactoryExt and its superinterfaces. */
113    private static final String JavaDoc[] txFactoryInterfaceIds = {
114       TransactionFactoryExtHelper.id(),
115       TransactionFactoryHelper.id()
116    };
117
118    /** Interface ids of Terminator and its superinterfaces. */
119    private static final String JavaDoc[] terminatorInterfaceIds = {
120       TerminatorHelper.id()
121    };
122
123    // Static -------------------------------------------------------
124

125    private static TransactionManager JavaDoc tm;
126    private static TransactionPropagationContextImporter tpcImporter;
127
128    // Attributes ----------------------------------------------------
129

130    private ORB JavaDoc orb;
131    private POA JavaDoc poa;
132    private org.omg.PortableServer.Current JavaDoc poaCurrent;
133
134    // Package -------------------------------------------------------
135

136    static byte[] theFactoryId() { return new byte[] { TX_FACTORY }; }
137
138    // Constructor ---------------------------------------------------
139

140    TransactionServiceImpl(ORB JavaDoc orb, POA JavaDoc poa)
141    {
142       this.orb = orb;
143       this.poa = poa;
144
145       Context JavaDoc ic;
146
147       try
148       {
149          org.omg.CORBA.Object JavaDoc obj =
150             orb.resolve_initial_references("POACurrent");
151          poaCurrent = org.omg.PortableServer.CurrentHelper.narrow(obj);
152       }
153       catch (InvalidName JavaDoc e) // thrown by resolve_initial_references
154
{
155          log.warn("Call to resolve_initial_references failed: ", e);
156          throw new RuntimeException JavaDoc(
157                            "Call to resolve_initial_references failed: " + e);
158       }
159       catch (BAD_PARAM JavaDoc e) // thrown by narrow
160
{
161          log.warn("Call to narrow failed: ", e);
162          throw new RuntimeException JavaDoc("Call to narrow failed: " + e);
163       }
164    }
165
166    // org.omg.PortableServer.Servant override -----------------------
167
public String JavaDoc[] _all_interfaces(POA JavaDoc poa, byte[] oid)
168    {
169       if (oid[0] == TX_FACTORY)
170          return txFactoryInterfaceIds;
171       else if (oid[0] == CONTROL)
172          return controlInterfaceIds;
173       else if (oid[0] == COORDINATOR)
174          return coordinatorInterfaceIds;
175       else if (oid[0] == TERMINATOR)
176          return terminatorInterfaceIds;
177       else
178          throw new BAD_PARAM JavaDoc("Unknown CORBA object id");
179    }
180
181    // TransactionFactoryExt operation -------------------------------
182

183    public TransactionDesc create_transaction(int timeout)
184    {
185       checkInvocationTarget(TX_FACTORY);
186       try
187       {
188          TransactionManager JavaDoc tm = getTransactionManager();
189
190          // Set timeout value
191
if (timeout != 0)
192             tm.setTransactionTimeout(timeout);
193
194          // Start tx
195
tm.begin();
196
197          // Suspend thread association
198
// and get the xid and the local id the transaction
199
TransactionImpl tx = (TransactionImpl)tm.suspend();
200          XidImpl xid = tx.getXid();
201          long localId = xid.getLocalIdValue();
202
203          // Set up oid byte array to create CORBA reference to the Coordinator
204
byte[] oid = new byte[9];
205          oid[0] = COORDINATOR;
206          LocalId.toByteArray(localId, oid, 1);
207
208          // Create CORBA reference to the Coordinator
209
Coordinator coord = CoordinatorHelper.narrow(
210                poa.create_reference_with_id(oid, CoordinatorHelper.id()));
211
212          // Reuse the oid array to create CORBA reference to the Terminnator
213
oid[0] = TERMINATOR;
214          Terminator JavaDoc term = TerminatorHelper.narrow(
215                poa.create_reference_with_id(oid, TerminatorHelper.id()));
216
217          // Create and initialize PropagationContext object
218
PropagationContext pc = new PropagationContext();
219          pc.current = createTransIdentity(xid, coord, term);
220          pc.timeout = timeout;
221          pc.parents = new TransIdentity[0];
222          pc.implementation_specific_data = orb.create_any();
223
224          // Create and initialize TransactionDesc object
225
TransactionDesc td = new TransactionDesc();
226          td.control = ControlHelper.narrow(
227                      poa.create_reference_with_id(oid, ControlHelper.id()));
228          td.propagationContext = pc;
229
230          // Return TransactionDesc
231
return td;
232       }
233       catch (SystemException JavaDoc e)
234       {
235          log.trace("Unexpected exception: ", e);
236          throw new UNKNOWN JavaDoc(e.toString());
237       }
238       catch (NotSupportedException JavaDoc e)
239       {
240          log.trace("Unexpected exception: ", e);
241          throw new BAD_INV_ORDER JavaDoc(e.toString());
242       }
243    }
244
245    // CosTransactions::TransactionFactory operations ----------------
246

247    public Control create(int timeout)
248    {
249       checkInvocationTarget(TX_FACTORY);
250       try
251       {
252          TransactionManager JavaDoc tm = getTransactionManager();
253
254          // Set timeout value
255
if (timeout != 0)
256             tm.setTransactionTimeout(timeout);
257
258          // Start tx
259
tm.begin();
260
261          // Suspend thread association
262
// and get the xid and the local id the transaction
263
TransactionImpl tx = (TransactionImpl)tm.suspend();
264          XidImpl xid = tx.getXid();
265          long localId = xid.getLocalIdValue();
266
267          // Set up oid byte array to create CORBA reference to the Control
268
byte[] oid = new byte[9];
269          oid[0] = CONTROL;
270          LocalId.toByteArray(localId, oid, 1);
271
272          // Return CORBA reference to the control
273
return ControlHelper.narrow(
274                      poa.create_reference_with_id(oid, ControlHelper.id()));
275       }
276       catch (SystemException JavaDoc e)
277       {
278          log.trace("Unexpected exception: ", e);
279          throw new UNKNOWN JavaDoc(e.toString());
280       }
281       catch (NotSupportedException JavaDoc e)
282       {
283          log.trace("Unexpected exception: ", e);
284          throw new BAD_INV_ORDER JavaDoc(e.toString());
285       }
286    }
287
288    public Control recreate(PropagationContext pc)
289    {
290       checkInvocationTarget(TX_FACTORY);
291       log.trace("Operation recreate is not implemented");
292       throw new NO_IMPLEMENT JavaDoc("recreate not implemented");
293    }
294
295    // CosTransactions::Control operations ---------------------------
296

297    public Terminator JavaDoc get_terminator()
298       throws Unavailable
299    {
300       byte[] oid = getTargetId();
301       if (oid[0] != CONTROL)
302          throw new BAD_OPERATION JavaDoc();
303       oid[0] = TERMINATOR;
304       return TerminatorHelper.narrow(
305                   poa.create_reference_with_id(oid, TerminatorHelper.id()));
306    }
307
308    public Coordinator get_coordinator()
309       throws Unavailable
310    {
311       byte[] oid = getTargetId();
312       if (oid[0] != CONTROL)
313          throw new BAD_OPERATION JavaDoc();
314       oid[0] = COORDINATOR;
315       return CoordinatorHelper.narrow(
316                   poa.create_reference_with_id(oid, CoordinatorHelper.id()));
317    }
318
319    // CosTransactions::Terminator operations ------------------------
320

321    public void commit(boolean reportHeuristics)
322       throws HeuristicHazard, HeuristicMixed
323    {
324       byte[] oid = getTargetId();
325       if (oid[0] != TERMINATOR)
326          throw new BAD_OPERATION JavaDoc();
327
328       long localIdValue = LocalId.fromByteArray(oid, 1);
329       LocalId localId = new LocalId(localIdValue);
330       Transaction JavaDoc tx = getTransaction(localId);
331
332       if (tx == null)
333       {
334          log.trace("RuntimeException in commit: transaction not found");
335          throw new OBJECT_NOT_EXIST JavaDoc("No transaction.");
336       }
337
338       TransactionManager JavaDoc tm = getTransactionManager();
339       try
340       {
341          tm.resume(tx);
342          tm.commit();
343       }
344       catch (RollbackException JavaDoc e)
345       {
346          log.trace("Exception: ", e);
347          throw new TRANSACTION_ROLLEDBACK JavaDoc(e.toString());
348       }
349       catch (HeuristicMixedException JavaDoc e)
350       {
351          log.trace("Exception: ", e);
352          if (reportHeuristics)
353             throw new HeuristicMixed(e.toString());
354       }
355       catch (HeuristicRollbackException JavaDoc e)
356       {
357          log.trace("Exception: ", e);
358          if (reportHeuristics)
359             throw new HeuristicHazard(e.toString());
360       }
361       catch (InvalidTransactionException JavaDoc e)
362       {
363          log.trace("Unexpected exception: ", e);
364          throw new INVALID_TRANSACTION JavaDoc(e.toString());
365       }
366       catch (IllegalStateException JavaDoc e)
367       {
368          log.trace("Unexpected exception: ", e);
369          throw new BAD_INV_ORDER JavaDoc(e.toString());
370       }
371       catch (SystemException JavaDoc e)
372       {
373          log.trace("Unexpected exception: ", e);
374          throw new UNKNOWN JavaDoc(e.toString());
375       }
376       catch (SecurityException JavaDoc e)
377       {
378          log.trace("Unexpected exception: ", e);
379          throw new NO_PERMISSION JavaDoc(e.toString());
380       }
381    }
382
383    public void rollback()
384    {
385       byte[] oid = getTargetId();
386       if (oid[0] != TERMINATOR)
387          throw new BAD_OPERATION JavaDoc();
388
389       long localIdValue = LocalId.fromByteArray(oid, 1);
390       LocalId localId = new LocalId(localIdValue);
391       Transaction JavaDoc tx = getTransaction(localId);
392
393       if (tx == null)
394       {
395          log.trace("RuntimeException in rollback: transaction not found");
396          throw new OBJECT_NOT_EXIST JavaDoc("No transaction.");
397       }
398
399       TransactionManager JavaDoc tm = getTransactionManager();
400       try
401       {
402          tm.resume(tx);
403          tm.rollback();
404       }
405       catch (InvalidTransactionException JavaDoc e)
406       {
407          log.trace("Unexpected exception: ", e);
408          throw new INVALID_TRANSACTION JavaDoc(e.toString());
409       }
410       catch (IllegalStateException JavaDoc e)
411       {
412          log.trace("Unexpected exception: ", e);
413          throw new BAD_INV_ORDER JavaDoc(e.toString());
414       }
415       catch (SystemException JavaDoc e)
416       {
417          log.trace("Unexpected exception: ", e);
418          throw new UNKNOWN JavaDoc(e.toString());
419       }
420       catch (SecurityException JavaDoc e)
421       {
422          log.trace("Unexpected exception: ", e);
423          throw new NO_PERMISSION JavaDoc(e.toString());
424       }
425    }
426
427    // CoordinatorExt operation --------------------------------------
428

429    public TransactionId get_transaction_id()
430    {
431       byte[] oid = getTargetId();
432       if (oid[0] != COORDINATOR)
433          throw new BAD_OPERATION JavaDoc();
434
435       long localIdValue = LocalId.fromByteArray(oid, 1);
436       LocalId localId = new LocalId(localIdValue);
437       TransactionImpl tx = (TransactionImpl)getTransaction(localId);
438
439       if (tx == null)
440       {
441          log.trace("RuntimeException in get_transaction_id: transaction not found");
442          throw new OBJECT_NOT_EXIST JavaDoc("No transaction.");
443       }
444       
445       Xid JavaDoc xid = tx.getXid();
446       return new TransactionId(xid.getFormatId(),
447                                xid.getGlobalTransactionId());
448    }
449
450    // CosTransactions::Coordinator operations -----------------------
451

452    public Status get_status()
453    {
454       byte[] oid = getTargetId();
455       if (oid[0] != COORDINATOR)
456          throw new BAD_OPERATION JavaDoc();
457
458       long localIdValue = LocalId.fromByteArray(oid, 1);
459       LocalId localId = new LocalId(localIdValue);
460       Transaction JavaDoc tx = getTransaction(localId);
461
462       if (tx == null)
463       {
464          log.trace("RuntimeException in get_status: transaction not found");
465          // Not sure if here I should return StatusNoTransaction
466
// instead of throwing OBJECT_NOT_EXIST... (Francisco)
467
throw new OBJECT_NOT_EXIST JavaDoc("No transaction.");
468          // return org.omg.CosTransactions.Status.StatusNoTransaction; // ?
469
}
470       
471       int status;
472
473       try
474       {
475          status = tx.getStatus();
476       }
477       catch (SystemException JavaDoc e)
478       {
479          log.trace("Unexpected exception: ", e);
480          throw new UNKNOWN JavaDoc(e.toString());
481       }
482       return javaxToCosTransactions(status);
483    }
484
485    public Status get_parent_status()
486    {
487       // The target coordinator is associated with a top-level
488
// transaction, because we do not support nested transactions.
489
// Therefore get_parent_status() is equivalent to get_status().
490
return get_status();
491    }
492
493    public Status get_top_level_status()
494    {
495       // The target coordinator is associated with a top-level
496
// transaction, because we do not support nested transactions.
497
// Therefore get_top_level_status() is equivalent to get_status().
498
return get_status();
499    }
500
501    public boolean is_same_transaction(Coordinator other)
502    {
503       byte[] oid = getTargetId();
504       if (oid[0] != COORDINATOR)
505          throw new BAD_OPERATION JavaDoc();
506
507       long localIdValue = LocalId.fromByteArray(oid, 1);
508       LocalId localId = new LocalId(localIdValue);
509       TransactionImpl tx = (TransactionImpl)getTransaction(localId);
510
511       if (tx == null)
512       {
513          log.trace("RuntimeException in is_same_transaction: transaction not found");
514          throw new OBJECT_NOT_EXIST JavaDoc("No transaction.");
515       }
516       
517       Xid JavaDoc xid = tx.getXid();
518
519       try
520       {
521          CoordinatorExt otherExt = CoordinatorExtHelper.narrow(other);
522          TransactionId otherId = otherExt.get_transaction_id();
523          return compare(xid,
524                         otherId.formatId,
525                         otherId.globalId,
526                         otherId.globalId.length);
527       }
528       catch (BAD_PARAM JavaDoc e)
529       {
530           // narrow failed: foreign transaction case
531
try
532          {
533             otid_t otherOtid = other.get_txcontext().current.otid;
534             return compare(xid,
535                            otherOtid.formatID,
536                            otherOtid.tid,
537                            otherOtid.tid.length - otherOtid.bqual_length);
538          }
539          catch (Unavailable u)
540          {
541             log.trace("Foreign Coordinator do not support get_txcontext(): ",
542                       e);
543             throw new BAD_PARAM JavaDoc(e.toString());
544          }
545       }
546    }
547
548    public boolean is_ancestor_transaction(Coordinator other)
549    {
550       // We do not support nested transactions. In this case
551
// is_ancestor_transaction() is equivalent to is_same_transaction().
552
return is_same_transaction(other);
553    }
554
555    public boolean is_descendant_transaction(Coordinator other)
556    {
557       // We do not support nested transactions. In this case
558
// is_descendant_transaction() is equivalent to is_same_transaction().
559
return is_same_transaction(other);
560    }
561
562    public boolean is_related_transaction(Coordinator other)
563    {
564       // We do not support nested transactions. In this case
565
// is_descendant_transaction() is equivalent to is_same_transaction().
566
return is_same_transaction(other);
567    }
568
569    public boolean is_top_level_transaction()
570    {
571       checkInvocationTarget(COORDINATOR);
572       return true; // because we do not support nested transactions.
573
}
574
575    public int hash_transaction()
576    {
577       byte[] oid = getTargetId();
578       if (oid[0] != COORDINATOR)
579          throw new BAD_OPERATION JavaDoc();
580
581       long localIdValue = LocalId.fromByteArray(oid, 1);
582       LocalId localId = new LocalId(localIdValue);
583       TransactionImpl tx = (TransactionImpl)getTransaction(localId);
584       if (tx == null)
585       {
586          log.trace("RuntimeException in is_same_transaction: transaction not found");
587          throw new OBJECT_NOT_EXIST JavaDoc("No transaction.");
588       }
589       return tx.getXid().hashCode();
590    }
591
592    public int hash_top_level_tran()
593    {
594       // We do not support nested transactions. In this case
595
// hash_top_level_transaction() is equivalent to hash_transaction().
596
return hash_transaction();
597    }
598
599    public RecoveryCoordinator register_resource(Resource r)
600       throws Inactive
601    {
602       checkInvocationTarget(COORDINATOR);
603       throw new NO_IMPLEMENT JavaDoc("Two-phase commit is not supported."); // TODO!
604
}
605
606    public void register_synchronization(final Synchronization sync)
607       throws SynchronizationUnavailable, Inactive
608    {
609       byte[] oid = getTargetId();
610       if (oid[0] != COORDINATOR)
611          throw new BAD_OPERATION JavaDoc();
612
613       long localIdValue = LocalId.fromByteArray(oid, 1);
614       LocalId localId = new LocalId(localIdValue);
615       Transaction JavaDoc tx = getTransaction(localId);
616
617       if (tx == null)
618       {
619          log.trace("RuntimeException in register_synchronization: transaction not found");
620          throw new OBJECT_NOT_EXIST JavaDoc("No transaction.");
621       }
622       
623       try
624       {
625          tx.registerSynchronization(
626             new javax.transaction.Synchronization JavaDoc()
627             {
628                public void beforeCompletion()
629                {
630                   sync.before_completion();
631                }
632                public void afterCompletion(int status)
633                {
634                   sync.after_completion(javaxToCosTransactions(status));
635                }
636             });
637       }
638       catch (RollbackException JavaDoc e)
639       {
640          log.trace("Exception: ", e);
641          throw new TRANSACTION_ROLLEDBACK JavaDoc(e.toString());
642       }
643       catch (IllegalStateException JavaDoc e)
644       {
645          log.trace("Unexpected exception: ", e);
646          throw new BAD_INV_ORDER JavaDoc(e.toString());
647       }
648       catch (SystemException JavaDoc e)
649       {
650          log.trace("Unexpected exception: ", e);
651          throw new UNKNOWN JavaDoc(e.toString());
652       }
653    }
654
655    public void register_subtran_aware(SubtransactionAwareResource r)
656       throws NotSubtransaction, Inactive
657    {
658       checkInvocationTarget(COORDINATOR);
659       throw new NotSubtransaction("Nested transactions are not supported");
660    }
661
662    public void rollback_only()
663       throws Inactive
664    {
665       byte[] oid = getTargetId();
666       if (oid[0] != COORDINATOR)
667          throw new BAD_OPERATION JavaDoc();
668
669       long localIdValue = LocalId.fromByteArray(oid, 1);
670       LocalId localId = new LocalId(localIdValue);
671       Transaction JavaDoc tx = getTransaction(localId);
672
673       if (tx == null)
674       {
675          log.trace("RuntimeException in rollback_only: transaction not found");
676          throw new OBJECT_NOT_EXIST JavaDoc("No transaction.");
677       }
678
679       try
680       {
681          tx.setRollbackOnly();
682       }
683       catch (IllegalStateException JavaDoc e)
684       {
685          log.trace("Unexpected exception: ", e);
686          throw new BAD_INV_ORDER JavaDoc(e.toString());
687       }
688       catch (SystemException JavaDoc e)
689       {
690          log.trace("Unexpected exception: ", e);
691          throw new UNKNOWN JavaDoc(e.toString());
692       }
693    }
694
695    public String JavaDoc get_transaction_name()
696    {
697       byte[] oid = getTargetId();
698       if (oid[0] != COORDINATOR)
699          throw new BAD_OPERATION JavaDoc();
700
701       long localIdValue = LocalId.fromByteArray(oid, 1);
702       LocalId localId = new LocalId(localIdValue);
703       Transaction JavaDoc tx = getTransaction(localId);
704
705       if (tx == null)
706       {
707          log.trace("RuntimeException in get_transaction_name: transaction not found");
708          throw new OBJECT_NOT_EXIST JavaDoc("No transaction.");
709       }
710       return tx.toString();
711    }
712
713    public Control create_subtransaction()
714       throws SubtransactionsUnavailable, Inactive
715    {
716       checkInvocationTarget(COORDINATOR);
717       throw new SubtransactionsUnavailable(
718                               "Nested transactions are not supported");
719    }
720
721    public PropagationContext get_txcontext()
722       throws Unavailable
723    {
724       byte[] oid = getTargetId();
725       if (oid[0] != COORDINATOR)
726          throw new BAD_OPERATION JavaDoc();
727       
728       long localIdValue = LocalId.fromByteArray(oid, 1);
729       LocalId localId = new LocalId(localIdValue);
730       TransactionImpl tx = (TransactionImpl)getTransaction(localId);
731       if (tx == null)
732       {
733          log.trace("RuntimeException in get_txcontext: transaction not found");
734          throw new OBJECT_NOT_EXIST JavaDoc("No transaction.");
735       }
736       Xid JavaDoc xid = tx.getXid();
737
738       // Create CORBA reference to the Coordinator (the target of this call)
739
Coordinator coord = CoordinatorHelper.narrow(
740             poa.create_reference_with_id(oid, CoordinatorHelper.id()));
741
742       // Create and initialize PropagationContext object
743
PropagationContext pc = new PropagationContext();
744       pc.current = createTransIdentity(xid, coord, null);
745       pc.timeout = divideAndRoundUp(tx.getTimeLeftBeforeTimeout(), 1000);
746       pc.parents = new TransIdentity[0];
747       pc.implementation_specific_data = orb.create_any();
748       
749       // Return PropagationContext
750
return pc;
751   }
752
753    // Protected -----------------------------------------------------
754

755    /**
756     * Get a reference to the transaction manager.
757     */

758    protected static TransactionManager JavaDoc getTransactionManager()
759    {
760       if (tm == null)
761       {
762          try
763          {
764             Context JavaDoc ctx = new InitialContext JavaDoc();
765             tm = (TransactionManager JavaDoc)ctx.lookup("java:/TransactionManager");
766          }
767          catch (NamingException JavaDoc ex)
768          {
769             log.error("java:/TransactionManager lookup failed", ex);
770          }
771       }
772       return tm;
773    }
774
775    /**
776     * Get a reference to the transaction importer.
777     */

778    protected static TransactionPropagationContextImporter getTPCImporter()
779    {
780       if (tpcImporter == null)
781       {
782          try
783          {
784             Context JavaDoc ctx = new InitialContext JavaDoc();
785             tpcImporter = (TransactionPropagationContextImporter)ctx.lookup(
786                                  "java:/TransactionPropagationContextImporter");
787          }
788          catch (NamingException JavaDoc ex)
789          {
790             log.error(
791                   "java:/TransactionPropagationContextImporter lookup failed",
792                   ex);
793          }
794       }
795       return tpcImporter;
796    }
797
798    // Private -------------------------------------------------------
799

800    private static Transaction JavaDoc getTransaction(LocalId localId)
801    {
802       return getTPCImporter().importTransactionPropagationContext(localId);
803    }
804
805    private byte[] getTargetId()
806    {
807       byte[] id = null;
808       try
809       {
810          id = poaCurrent.get_object_id();
811       }
812       catch (NoContext JavaDoc e)
813       {
814          log.trace("Unexpected exception: " + e);
815          throw new RuntimeException JavaDoc("Unexpected exception: " + e);
816       }
817       return id;
818    }
819
820    private void checkInvocationTarget(int targetType)
821    {
822       if (getTargetId()[0] != targetType)
823          throw new BAD_OPERATION JavaDoc();
824    }
825
826    /**
827     * Create and initialize TransIdentity object.
828     */

829    private static TransIdentity createTransIdentity(Xid JavaDoc xid,
830                                                     Coordinator coord,
831                                                     Terminator JavaDoc term)
832    {
833       // Concatenate global transaction id and branch qualifier
834
byte gtrid[] = xid.getGlobalTransactionId();
835       byte bqual[] = xid.getBranchQualifier();
836       byte[] trid = new byte[gtrid.length + bqual.length];
837       System.arraycopy(gtrid, 0, trid, 0, gtrid.length);
838       System.arraycopy(bqual, 0, trid, gtrid.length, bqual.length);
839
840       // Create TransIdentity instance and set its fields
841
TransIdentity ti = new TransIdentity();
842       ti.coord = coord;
843       ti.term = term;
844       ti.otid = new otid_t(xid.getFormatId(), bqual.length, trid);
845       return ti;
846    }
847
848    private static int divideAndRoundUp(long m, long n)
849    {
850       long retval = m / n;
851
852       if ((m % n) != 0)
853          retval = retval + 1;
854       return (int)retval ;
855    }
856
857    private static Status javaxToCosTransactions(int status)
858    {
859       switch (status)
860       {
861       case javax.transaction.Status.STATUS_ACTIVE:
862          return org.omg.CosTransactions.Status.StatusActive;
863       case javax.transaction.Status.STATUS_COMMITTED:
864          return org.omg.CosTransactions.Status.StatusCommitted;
865       case javax.transaction.Status.STATUS_COMMITTING:
866          return org.omg.CosTransactions.Status.StatusCommitting;
867       case javax.transaction.Status.STATUS_MARKED_ROLLBACK:
868          return org.omg.CosTransactions.Status.StatusMarkedRollback;
869       case javax.transaction.Status.STATUS_NO_TRANSACTION:
870          return org.omg.CosTransactions.Status.StatusNoTransaction;
871       case javax.transaction.Status.STATUS_PREPARED:
872          return org.omg.CosTransactions.Status.StatusPrepared;
873       case javax.transaction.Status.STATUS_PREPARING:
874          return org.omg.CosTransactions.Status.StatusPreparing;
875       case javax.transaction.Status.STATUS_ROLLEDBACK:
876          return org.omg.CosTransactions.Status.StatusRolledBack;
877       case javax.transaction.Status.STATUS_ROLLING_BACK:
878          return org.omg.CosTransactions.Status.StatusRollingBack;
879       case javax.transaction.Status.STATUS_UNKNOWN:
880          return org.omg.CosTransactions.Status.StatusUnknown;
881       default:
882          log.trace("Invalid transaction status.");
883          return org.omg.CosTransactions.Status.StatusUnknown;
884       }
885    }
886
887     private static boolean compare(Xid JavaDoc xid,
888                                    int otherFormatId,
889                                    byte[] otherGlobalId,
890                                    int otherLength)
891     {
892         if (xid.getFormatId() != otherFormatId)
893             return false;
894
895         byte[] globalId = xid.getGlobalTransactionId();
896         int len = globalId.length;
897
898         if (len != otherLength)
899             return false;
900
901         for (int i = 0; i < len; ++i)
902             if (globalId[i] != otherGlobalId[i])
903                 return false;
904
905         return true;
906     }
907
908 }
909
Popular Tags