KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > remoting > server > DTMServant


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.tm.remoting.server;
23
24 import java.lang.reflect.Proxy JavaDoc;
25 import java.rmi.AccessException JavaDoc;
26 import java.rmi.NoSuchObjectException JavaDoc;
27 import java.rmi.RemoteException JavaDoc;
28 import java.rmi.UnexpectedException JavaDoc;
29
30 import javax.transaction.HeuristicCommitException JavaDoc;
31 import javax.transaction.HeuristicMixedException JavaDoc;
32 import javax.transaction.HeuristicRollbackException JavaDoc;
33 import javax.transaction.NotSupportedException JavaDoc;
34 import javax.transaction.RollbackException JavaDoc;
35 import javax.transaction.SystemException JavaDoc;
36 import javax.transaction.Transaction JavaDoc;
37 import javax.transaction.TransactionManager JavaDoc;
38 import javax.transaction.TransactionRolledbackException JavaDoc;
39 import javax.transaction.xa.XAResource JavaDoc;
40
41 import org.jboss.logging.Logger;
42 import org.jboss.tm.CoordinatorFactory;
43 import org.jboss.tm.GlobalId;
44 import org.jboss.tm.LocalId;
45 import org.jboss.tm.ResourceFactory;
46 import org.jboss.tm.StringRemoteRefConverter;
47 import org.jboss.tm.TransactionImpl;
48 import org.jboss.tm.TMUtil;
49 import org.jboss.tm.TxManager;
50 import org.jboss.tm.XidImpl;
51 import org.jboss.tm.remoting.RemoteProxy;
52 import org.jboss.tm.remoting.Invocation.ICoordinator;
53 import org.jboss.tm.remoting.Invocation.IRecoveryCoordinator;
54 import org.jboss.tm.remoting.Invocation.IResource;
55 import org.jboss.tm.remoting.Invocation.ISynchronization;
56 import org.jboss.tm.remoting.Invocation.ITerminator;
57 import org.jboss.tm.remoting.Invocation.ITransactionFactory;
58 import org.jboss.tm.remoting.interfaces.Coordinator;
59 import org.jboss.tm.remoting.interfaces.HeuristicHazardException;
60 import org.jboss.tm.remoting.interfaces.RecoveryCoordinator;
61 import org.jboss.tm.remoting.interfaces.Resource;
62 import org.jboss.tm.remoting.interfaces.Status;
63 import org.jboss.tm.remoting.interfaces.Synchronization;
64 import org.jboss.tm.remoting.interfaces.Terminator;
65 import org.jboss.tm.remoting.interfaces.TxPropagationContext;
66 import org.jboss.tm.remoting.interfaces.TransactionInactiveException;
67 import org.jboss.tm.remoting.interfaces.TransactionNotPreparedException;
68 import org.jboss.tm.remoting.interfaces.Vote;
69
70 /**
71  * Remoting servant for the following Distributed Transaction Manager
72  * interfaces: <code>TransactionFactory</code>, <code>Coordinator</code>,
73  * <code>Terminator</code>, <code>RecoveryCoordinator</code>,
74  * <code>Synchronization</code>.
75  *
76  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
77  * @version $Revision: 37459 $
78  */

79 public class DTMServant implements ITransactionFactory,
80                                    ICoordinator,
81                                    ITerminator,
82                                    IResource,
83                                    IRecoveryCoordinator,
84                                    ISynchronization,
85                                    CoordinatorFactory,
86                                    ResourceFactory,
87                                    StringRemoteRefConverter
88 {
89
90    // Constants ----------------------------------------------------
91

92    private static final Logger log =
93       Logger.getLogger(DTMServant.class);
94
95    // Private fields ------------------------------------------------
96

97    private DistributedTransactionManager dtm;
98
99    // Constructor ---------------------------------------------------
100

101    public DTMServant(DistributedTransactionManager dtm)
102    {
103       this.dtm = dtm;
104    }
105    
106    // ITransactionFactory methods -----------------------------------
107

108    public TxPropagationContext create(long targetId, int timeout)
109          throws RemoteException JavaDoc
110    {
111       log.trace("TransactionFactory.create");
112       try
113       {
114          TransactionManager JavaDoc tm = TMUtil.getTransactionManager();
115
116          // Set timeout value
117
if (timeout != 0)
118             tm.setTransactionTimeout(timeout);
119
120          // Start tx
121
tm.begin();
122
123          // Suspend thread association
124
// and get the xid and the local id of the transaction
125
TransactionImpl tx = (TransactionImpl)tm.suspend();
126          XidImpl xid = tx.getXid();
127          long localId = xid.getLocalIdValue();
128
129          // Get the TxPropagationContext of the transaction
130
TxPropagationContext tpc = tx.getPropagationContext();
131          
132          // Set the terminator in the TPC and return the TPC
133
tpc.terminator =
134             (Terminator JavaDoc) RemoteProxy.create(Terminator JavaDoc.class,
135                   localId,
136                   dtm.getLocators());
137          
138          return tpc;
139       }
140       catch (SystemException JavaDoc e)
141       {
142          if (log.isTraceEnabled())
143             log.trace("Unexpected exception: ", e);
144          throw new UnexpectedException JavaDoc("Unexpected " + e, e);
145       }
146       catch (NotSupportedException JavaDoc e)
147       {
148          if (log.isTraceEnabled())
149             log.trace("Unexpected exception: ", e);
150          throw new UnexpectedException JavaDoc("Unexpected " + e, e);
151       }
152    }
153
154    public TxPropagationContext recreate(long targetId,
155                                         TxPropagationContext tpc)
156          throws RemoteException JavaDoc
157    {
158       log.trace("TransactionFactory.recreate");
159       
160       if (tpc == null)
161          throw new IllegalArgumentException JavaDoc(
162                "recreate: TxPropagationContext parameter cannot be null");
163       
164       TxManager tm = (TxManager) TMUtil.getTransactionManager();
165       TransactionImpl tx = tm.importExternalTransaction(tpc.formatId,
166                                                         tpc.globalId,
167                                                         tpc.coordinator,
168                                                         tpc.timeout * 1000);
169             
170       // Create remote proxy to the local Coordinator
171
Coordinator subCoordinator =
172          (Coordinator) RemoteProxy.create(Coordinator.class,
173                                           tx.getLocalIdValue(),
174                                           dtm.getLocators());
175
176       // Return TPC with the interposed subCoordinator and a null Terminator
177
return new TxPropagationContext(tpc.formatId,
178                                       tpc.globalId,
179                                       tpc.timeout,
180                                       subCoordinator,
181                                       null);
182    }
183
184    // ICoordinator methods ------------------------------------------
185

186    public Status getStatus(long targetId)
187          throws RemoteException JavaDoc
188    {
189       if (log.isTraceEnabled())
190          log.trace("Coordinator.getStatus, targetId=" +
191                    Long.toHexString(targetId));
192       
193       LocalId localId = new LocalId(targetId);
194       Transaction JavaDoc tx = TMUtil.getTransaction(localId);
195
196       if (tx == null)
197       {
198          log.trace("RemoteException in getStatus: transaction not found");
199          // Not sure if here it would be better to return StatusNoTransaction
200
// instead of throwing NoSuchObjectException... (Francisco)
201
throw new NoSuchObjectException JavaDoc("No transaction.");
202          // return Status.NO_TRANSACTION; // ?
203
}
204       
205       int status;
206
207       try
208       {
209          status = tx.getStatus();
210       }
211       catch (SystemException JavaDoc e)
212       {
213          if (log.isTraceEnabled())
214             log.trace("Unexpected exception: ", e);
215          throw new UnexpectedException JavaDoc("Unexpected " + e, e);
216       }
217       return javaxToJBoss(status);
218    }
219
220    public boolean isSameTransaction(long targetId, Coordinator c)
221          throws RemoteException JavaDoc
222    {
223       if (log.isTraceEnabled())
224          log.trace("Coordinator.isSameTransaction, targetId=" +
225                    Long.toHexString(targetId));
226       return getTransactionId(targetId).equals(c.getTransactionId());
227    }
228
229    public int hashTransaction(long targetId)
230          throws RemoteException JavaDoc
231    {
232       if (log.isTraceEnabled())
233          log.trace("Coordinator.hashTransaction, targetId=" +
234                    Long.toHexString(targetId));
235       return getTransactionId(targetId).hashCode();
236    }
237
238    public RecoveryCoordinator registerResource(long targetId, Resource JavaDoc r)
239          throws RemoteException JavaDoc,
240                 TransactionInactiveException
241    {
242       if (log.isTraceEnabled())
243          log.trace("Coordinator.registerResource, targetId=" +
244                    Long.toHexString(targetId));
245       
246       LocalId localId = new LocalId(targetId);
247       TransactionImpl tx = (TransactionImpl)TMUtil.getTransaction(localId);
248
249       if (tx == null)
250       {
251          log.trace("RemoteException in registerResource: " +
252                    "transaction not found");
253          throw new NoSuchObjectException JavaDoc("No transaction.");
254       }
255    
256       try
257       {
258          tx.enlistRemoteResource(r);
259       }
260       catch (RollbackException JavaDoc e)
261       {
262          if (log.isTraceEnabled())
263             log.trace("Exception: ", e);
264          TransactionRolledbackException JavaDoc ex =
265             new TransactionRolledbackException JavaDoc(e.toString());
266          ex.detail = e;
267          throw ex;
268       }
269       catch (IllegalStateException JavaDoc e)
270       {
271          if (log.isTraceEnabled())
272             log.trace("Exception: ", e);
273          throw new TransactionInactiveException(e);
274       }
275
276       // Create remote proxies to the RecoveryCoordinator
277
RecoveryCoordinator rc =
278          (RecoveryCoordinator) RemoteProxy.create(RecoveryCoordinator.class,
279                                                   targetId,
280                                                   dtm.getLocators());
281       return rc;
282    }
283
284    public void registerSynchronization(long targetId,
285                                        final Synchronization sync)
286       throws RemoteException JavaDoc,
287              TransactionInactiveException
288    {
289       if (log.isTraceEnabled())
290          log.trace("Coordinator.registerSynchronization, targetId=" +
291                    Long.toHexString(targetId));
292       
293       LocalId localId = new LocalId(targetId);
294       Transaction JavaDoc tx = TMUtil.getTransaction(localId);
295       
296       if (tx == null)
297       {
298          log.trace("RemoteException in registerSynchronization: " +
299                    "transaction not found");
300          throw new NoSuchObjectException JavaDoc("No transaction.");
301       }
302       
303       try
304       {
305          tx.registerSynchronization(
306             new javax.transaction.Synchronization JavaDoc()
307             {
308                public void beforeCompletion()
309                {
310                   try
311                   {
312                      sync.beforeCompletion();
313                   }
314                   catch (RemoteException JavaDoc e)
315                   {
316                      if (log.isTraceEnabled())
317                         log.trace("RemoteException in beforeCompletion: " + e);
318                   }
319                }
320                public void afterCompletion(int status)
321                {
322                   try
323                   {
324                      sync.afterCompletion(javaxToJBoss(status));
325                   }
326                   catch (RemoteException JavaDoc e)
327                   {
328                      if (log.isTraceEnabled())
329                         log.trace("RemoteException in afterCompletion: " + e);
330                   }
331                }
332             });
333       }
334       catch (RollbackException JavaDoc e)
335       {
336          if (log.isTraceEnabled())
337             log.trace("Exception: ", e);
338          TransactionRolledbackException JavaDoc ex =
339             new TransactionRolledbackException JavaDoc(e.toString());
340          ex.detail = e;
341          throw ex;
342       }
343       catch (IllegalStateException JavaDoc e)
344       {
345          if (log.isTraceEnabled())
346             log.trace("Unexpected exception: ", e);
347          throw new TransactionInactiveException(e);
348       }
349       catch (SystemException JavaDoc e)
350       {
351          if (log.isTraceEnabled())
352             log.trace("Unexpected exception: ", e);
353          throw new UnexpectedException JavaDoc("Unexpected " + e, e);
354       }
355    }
356
357    public void rollbackOnly(long targetId)
358          throws RemoteException JavaDoc,
359                 TransactionInactiveException
360    {
361       if (log.isTraceEnabled())
362          log.trace("Coordinator.rollbackOnly, targetId=" +
363                    Long.toHexString(targetId));
364  
365       LocalId localId = new LocalId(targetId);
366       Transaction JavaDoc tx = TMUtil.getTransaction(localId);
367
368       if (tx == null)
369       {
370          log.trace("RemoteException in rollbackOnly: transaction not found");
371          throw new NoSuchObjectException JavaDoc("No transaction.");
372       }
373       
374       try
375       {
376          tx.setRollbackOnly();
377       }
378       catch (IllegalStateException JavaDoc e)
379       {
380          if (log.isTraceEnabled())
381             log.trace("Unexpected exception: ", e);
382          throw new TransactionInactiveException(e);
383       }
384       catch (SystemException JavaDoc e)
385       {
386          if (log.isTraceEnabled())
387             log.trace("Unexpected exception: ", e);
388          throw new UnexpectedException JavaDoc("Unexpected " + e, e);
389       }
390    }
391
392    public TxPropagationContext getTransactionContext(long targetId)
393          throws RemoteException JavaDoc,
394                 TransactionInactiveException
395    {
396       if (log.isTraceEnabled())
397          log.trace("Coordinator.getTransactionContext, targetId=" +
398                    Long.toHexString(targetId));
399       
400       LocalId localId = new LocalId(targetId);
401       TransactionImpl tx = (TransactionImpl)TMUtil.getTransaction(localId);
402
403       if (tx == null)
404       {
405          log.trace("RemoteException in getTransactionContext: " +
406                    "transaction not found");
407          throw new NoSuchObjectException JavaDoc("No transaction.");
408       }
409       
410       TxPropagationContext txPropagationContext = tx.getPropagationContext();
411       if (txPropagationContext != null)
412          return tx.getPropagationContext();
413       else
414          throw new TransactionInactiveException();
415    }
416
417    public GlobalId getTransactionId(long targetId)
418          throws RemoteException JavaDoc
419    {
420       if (log.isTraceEnabled())
421          log.trace("Coordinator.getTransactionId, targetId="
422                    + Long.toHexString(targetId));
423       
424       LocalId localId = new LocalId(targetId);
425       TransactionImpl tx = (TransactionImpl)TMUtil.getTransaction(localId);
426
427       if (tx == null)
428       {
429          log.trace("RemoteException in getTransactionId: " +
430                    "transaction not found");
431          throw new NoSuchObjectException JavaDoc("No transaction.");
432       }
433       
434       return tx.getGlobalId();
435    }
436
437    // ITerminator methods -------------------------------------------
438

439    public void commit(long targetId, boolean reportHeuristics)
440          throws RemoteException JavaDoc,
441                 HeuristicMixedException JavaDoc,
442                 HeuristicHazardException
443    {
444       if (log.isTraceEnabled())
445          log.trace("Terminator.commit, targetId=" +
446                    Long.toHexString(targetId));
447       
448       LocalId localId = new LocalId(targetId);
449       Transaction JavaDoc tx = TMUtil.getTransaction(localId);
450       
451       if (tx == null)
452       {
453          log.trace("RemoteException in commit: transaction not found");
454          throw new NoSuchObjectException JavaDoc("No transaction.");
455       }
456       
457       try
458       {
459          tx.commit();
460       }
461       catch (RollbackException JavaDoc e)
462       {
463          if (log.isTraceEnabled())
464             log.trace("Exception: ", e);
465          TransactionRolledbackException JavaDoc ex =
466             new TransactionRolledbackException JavaDoc(e.toString());
467          ex.detail = e;
468          throw ex;
469       }
470       catch (HeuristicMixedException JavaDoc e)
471       {
472          if (log.isTraceEnabled())
473             log.trace("Exception: ", e);
474          if (reportHeuristics)
475          {
476             HeuristicMixedException JavaDoc ex = new HeuristicMixedException JavaDoc();
477             ex.initCause(e);
478             throw ex;
479          }
480       }
481       catch (HeuristicRollbackException JavaDoc e)
482       {
483          if (log.isTraceEnabled())
484             log.trace("Exception: ", e);
485          TransactionRolledbackException JavaDoc ex =
486             new TransactionRolledbackException JavaDoc(e.toString());
487          ex.detail = e;
488          throw ex;
489       }
490       catch (SecurityException JavaDoc e)
491       {
492          if (log.isTraceEnabled())
493             log.trace("Unexpected exception: ", e);
494          throw new AccessException JavaDoc(e.toString(), e);
495       }
496       catch (IllegalStateException JavaDoc e)
497       {
498          if (log.isTraceEnabled())
499             log.trace("Unexpected exception: ", e);
500          throw new RemoteException JavaDoc(e.toString(), e);
501       }
502       catch (SystemException JavaDoc e)
503       {
504          if (log.isTraceEnabled())
505             log.trace("Unexpected exception: ", e);
506          throw new UnexpectedException JavaDoc(e.toString(), e);
507       }
508    }
509
510    public void rollback(long targetId)
511          throws RemoteException JavaDoc
512    {
513       if (log.isTraceEnabled())
514          log.trace("Terminator.rollback, targetId=" +
515                    Long.toHexString(targetId));
516       
517       LocalId localId = new LocalId(targetId);
518       Transaction JavaDoc tx = TMUtil.getTransaction(localId);
519       
520       if (tx == null)
521       {
522          log.trace("RemoteException in rollback: transaction not found");
523          throw new NoSuchObjectException JavaDoc("No transaction.");
524       }
525       
526       try
527       {
528          tx.rollback();
529       }
530       catch (IllegalStateException JavaDoc e)
531       {
532          if (log.isTraceEnabled())
533             log.trace("Unexpected exception: ", e);
534          throw new RemoteException JavaDoc(e.toString(), e);
535       }
536       catch (SecurityException JavaDoc e)
537       {
538          if (log.isTraceEnabled())
539             log.trace("Unexpected exception: ", e);
540          throw new AccessException JavaDoc(e.toString(), e);
541       }
542       catch (SystemException JavaDoc e)
543       {
544          if (log.isTraceEnabled())
545             log.trace("Unexpected exception: ", e);
546          throw new UnexpectedException JavaDoc(e.toString(), e);
547       }
548    }
549
550    // IResource methods ------------------------------------------
551

552    public Vote prepare(long targetId)
553          throws RemoteException JavaDoc,
554                 HeuristicMixedException JavaDoc,
555                 HeuristicHazardException
556    {
557       if (log.isTraceEnabled())
558          log.trace("Resource.prepare, targetId=" + Long.toHexString(targetId));
559       
560       LocalId localId = new LocalId(targetId);
561       TransactionImpl tx = (TransactionImpl)TMUtil.getTransaction(localId);
562       
563       if (tx == null)
564       {
565          log.trace("RemoteException in prepare: transaction not found");
566          throw new NoSuchObjectException JavaDoc("No transaction.");
567       }
568       
569       TransactionManager JavaDoc tm = TMUtil.getTransactionManager();
570       try
571       {
572          tm.resume(tx);
573          int vote = tx.prepare(null);
574          
575          if (vote == XAResource.XA_OK)
576             return Vote.COMMIT;
577          else // (vote == XAResource.XA_RDONLY)
578
return Vote.READONLY;
579       }
580       catch (RollbackException JavaDoc e)
581       {
582          return Vote.ROLLBACK;
583       }
584       catch (javax.transaction.HeuristicMixedException JavaDoc e)
585       {
586          if (log.isTraceEnabled())
587             log.trace("Exception: ", e);
588          HeuristicMixedException JavaDoc ex = new HeuristicMixedException JavaDoc();
589          ex.initCause(e);
590          throw ex;
591       }
592       catch (javax.transaction.HeuristicRollbackException JavaDoc e)
593       {
594          if (log.isTraceEnabled())
595             log.trace("Exception: ", e);
596          HeuristicHazardException ex = new HeuristicHazardException();
597          ex.initCause(e);
598          throw ex;
599       }
600       catch (Exception JavaDoc e)
601       {
602          if (log.isTraceEnabled())
603             log.trace("Unexpected exception: ", e);
604          throw new UnexpectedException JavaDoc(e.toString(), e);
605       }
606       finally
607       {
608          try
609          {
610             tm.suspend();
611          }
612          catch (SystemException JavaDoc e)
613          {
614             if (log.isTraceEnabled())
615                log.trace("Unexpected exception: ", e);
616             throw new UnexpectedException JavaDoc(e.toString(), e);
617          }
618       }
619    }
620
621    public void rollbackResource(long targetId)
622          throws RemoteException JavaDoc,
623                 HeuristicCommitException JavaDoc,
624                 HeuristicMixedException JavaDoc,
625                 HeuristicHazardException
626    {
627       if (log.isTraceEnabled())
628          log.trace("Resource.rollback, targetId=" +
629                    Long.toHexString(targetId));
630       
631       LocalId localId = new LocalId(targetId);
632       TransactionImpl tx = (TransactionImpl)TMUtil.getTransaction(localId);
633       
634       if (tx == null)
635       {
636          log.trace("RemoteException in rollback: transaction not found");
637          throw new NoSuchObjectException JavaDoc("No transaction.");
638       }
639       
640       try
641       {
642          tx.rollbackBranch();
643       }
644       catch (IllegalStateException JavaDoc e)
645       {
646          if (log.isTraceEnabled())
647             log.trace("Unexpected exception: ", e);
648          throw new RemoteException JavaDoc(e.toString(), e);
649       }
650    }
651
652    public void commit(long targetId)
653          throws RemoteException JavaDoc,
654                 TransactionNotPreparedException,
655                 HeuristicRollbackException JavaDoc,
656                 HeuristicMixedException JavaDoc,
657                 HeuristicHazardException
658    {
659       if (log.isTraceEnabled())
660          log.trace("Resource.commit, targetId=" + Long.toHexString(targetId));
661       
662       LocalId localId = new LocalId(targetId);
663       TransactionImpl tx = (TransactionImpl)TMUtil.getTransaction(localId);
664       
665       if (tx == null)
666       {
667          log.trace("RemoteException in commit: transaction not found");
668          throw new NoSuchObjectException JavaDoc("No transaction.");
669       }
670       
671       try
672       {
673          tx.commit(false);
674       }
675       catch (IllegalStateException JavaDoc e)
676       {
677          if (log.isTraceEnabled())
678             log.trace("Exception: ", e);
679          TransactionNotPreparedException ex =
680             new TransactionNotPreparedException();
681          ex.initCause(e);
682          throw ex;
683       }
684       catch (RollbackException JavaDoc e)
685       {
686          if (log.isTraceEnabled())
687             log.trace("Exception: ", e);
688          TransactionRolledbackException JavaDoc ex =
689             new TransactionRolledbackException JavaDoc(e.toString());
690          ex.detail = e;
691          throw ex;
692       }
693       catch (javax.transaction.HeuristicMixedException JavaDoc e)
694       {
695          if (log.isTraceEnabled())
696             log.trace("Exception: ", e);
697          HeuristicMixedException JavaDoc ex = new HeuristicMixedException JavaDoc();
698          ex.initCause(e);
699          throw ex;
700       }
701       catch (javax.transaction.HeuristicRollbackException JavaDoc e)
702       {
703          if (log.isTraceEnabled())
704             log.trace("Exception: ", e);
705          HeuristicRollbackException JavaDoc ex = new HeuristicRollbackException JavaDoc();
706          ex.initCause(e);
707          throw ex;
708       }
709       catch (SystemException JavaDoc e)
710       {
711          if (log.isTraceEnabled())
712             log.trace("Unexpected exception: ", e);
713          throw new UnexpectedException JavaDoc(e.toString(), e);
714       }
715    }
716
717    public void commitOnePhase(long targetId)
718          throws RemoteException JavaDoc,
719                 HeuristicHazardException
720    {
721       if (log.isTraceEnabled())
722          log.trace("Resource.commitOnePhase, targetId=" +
723                    Long.toHexString(targetId));
724
725       LocalId localId = new LocalId(targetId);
726       TransactionImpl tx = (TransactionImpl)TMUtil.getTransaction(localId);
727       
728       if (tx == null)
729       {
730          log.trace("RemoteException in commit: transaction not found");
731          throw new NoSuchObjectException JavaDoc("No transaction.");
732       }
733       
734       try
735       {
736          tx.commit(true);
737       }
738       catch (RollbackException JavaDoc e)
739       {
740          if (log.isTraceEnabled())
741             log.trace("Exception: ", e);
742          TransactionRolledbackException JavaDoc ex =
743             new TransactionRolledbackException JavaDoc(e.toString());
744          ex.detail = e;
745          throw ex;
746       }
747       catch (javax.transaction.HeuristicMixedException JavaDoc e)
748       {
749          if (log.isTraceEnabled())
750             log.trace("Exception: ", e);
751          throw new UnexpectedException JavaDoc(e.toString(), e);
752       }
753       catch (javax.transaction.HeuristicRollbackException JavaDoc e)
754       {
755          if (log.isTraceEnabled())
756             log.trace("Exception: ", e);
757          throw new UnexpectedException JavaDoc(e.toString(), e);
758       }
759       catch (SystemException JavaDoc e)
760       {
761          if (log.isTraceEnabled())
762             log.trace("Unexpected exception: ", e);
763          throw new UnexpectedException JavaDoc(e.toString(), e);
764       }
765   }
766
767    public void forget(long targetId)
768       throws RemoteException JavaDoc
769    {
770       if (log.isTraceEnabled())
771          log.trace("Resource.forget, targetId=" + Long.toHexString(targetId));
772       
773       LocalId localId = new LocalId(targetId);
774       TransactionImpl tx = (TransactionImpl)TMUtil.getTransaction(localId);
775       
776       if (tx == null)
777       {
778          log.trace("RemoteException in forget: transaction not found");
779          throw new NoSuchObjectException JavaDoc("No transaction.");
780       }
781
782       tx.forget();
783    }
784
785    // IRecoveryCoordinator method -----------------------------------
786

787    public Status replayCompletion(long targetId, final Resource JavaDoc r)
788          throws RemoteException JavaDoc,
789                 TransactionNotPreparedException
790    {
791       if (log.isTraceEnabled())
792          log.trace("RecoveryCoordinator.replayCompletion, targetId=" +
793                 Long.toHexString(targetId));
794       
795       TxManager tm = (TxManager) TMUtil.getTransactionManager();
796       if (tm.isRecoveryPending())
797       {
798          if (log.isTraceEnabled())
799             log.trace("RecoveryCoordinator.replayCompletion called on" +
800                       " targetId=" + Long.toHexString(targetId) +
801                       " before recovery is complete.\n" +
802                       " Throwing RemoteException.");
803          throw new RemoteException JavaDoc("Transaction manager not ready.");
804       }
805       
806       LocalId localId = new LocalId(targetId);
807       TransactionImpl tx = (TransactionImpl)TMUtil.getTransaction(localId);
808       
809       if (tx == null)
810       {
811          log.trace("RemoteException in replayCompletion: " +
812                    "transaction not found");
813          throw new NoSuchObjectException JavaDoc("No transaction.");
814       }
815       
816       int status = tx.replayCompletion(r);
817       
818       if (status == javax.transaction.Status.STATUS_MARKED_ROLLBACK ||
819             status == javax.transaction.Status.STATUS_NO_TRANSACTION ||
820             status == javax.transaction.Status.STATUS_ROLLEDBACK ||
821             status == javax.transaction.Status.STATUS_ROLLING_BACK)
822       {
823          Runnable JavaDoc runnable = new Runnable JavaDoc()
824          {
825             public void run()
826             {
827                try
828                {
829                   r.rollback();
830                }
831                catch (Exception JavaDoc ignore)
832                {
833                   // We can ignore this exception. If the resource didn't get
834
// the rollback then it will eventually call replayCompletion
835
// again.
836
if (log.isTraceEnabled())
837                      log.trace("Ignoring exception in remote resource rollback",
838                                ignore);
839                }
840             }
841          };
842          Thread JavaDoc t = new Thread JavaDoc(runnable, "resourceRollbackThread");
843          
844          t.start();
845       }
846       return javaxToJBoss(status);
847    }
848
849    // TODO: This class does not need to implement ISynchronization
850
// ISynchronization methods --------------------------------------
851

852    public void beforeCompletion(long targetId)
853    {
854       // TODO: remove beforeCompletion
855
if (log.isTraceEnabled())
856          log.trace("Synchronization.beforeCompletion, targetId=" +
857                 Long.toHexString(targetId));
858    }
859
860    public void afterCompletion(long targetId)
861    {
862       // TODO: remove afterCompletion
863
if (log.isTraceEnabled())
864          log.trace("Synchronization.afterCompletion, targetId=" +
865                 Long.toHexString(targetId));
866    }
867
868    // CoordinatorFactory implementation -----------------------------
869

870    /**
871     * Creates a reference for the DTM object with the given interface and
872     * <code>localId</code>.
873     * @see org.jboss.tm.ProxyFactory#create(java.lang.Class, long)
874     */

875    public Coordinator createCoordinator(long localId)
876    {
877       return (Coordinator) RemoteProxy.create(Coordinator.class,
878                                               localId,
879                                               dtm.getLocators());
880    }
881
882    // ResourceFactory implementation --------------------------------
883

884    /**
885     * Creates a reference for the DTM resource with the given
886     * <code>localId</code>.
887     * @see org.jboss.tm.ResourceFactory#create(long)
888     */

889    public Resource JavaDoc createResource(long localId)
890    {
891       return (Resource JavaDoc) RemoteProxy.create(Resource JavaDoc.class,
892                                            localId,
893                                            dtm.getLocators());
894    }
895    
896    // StringRemoteRefConverter implementation -----------------------
897

898    /**
899     * Converts a stringfied reference to a remote resource back to a remote
900     * reference.
901     *
902     * @param strResource a stringfied reference to a remote resource
903     * @return a remote reference to the resource.
904     */

905    public Resource JavaDoc stringToResource(String JavaDoc strResource)
906    {
907       try
908       {
909          return (Resource JavaDoc) RemoteProxy.fromString(strResource);
910       }
911       catch (Exception JavaDoc e)
912       {
913          throw new RuntimeException JavaDoc(e);
914       }
915    }
916
917    /**
918     * Converts a stringfied reference to a remote recovery coordinator back
919     * to a remote reference.
920     *
921     * @param strRecCoordinator a stringfied reference to a remote recovery
922     * coordinator
923     * @return a remote reference to the recovery coordinator.
924     */

925    public RecoveryCoordinator stringToRecoveryCoordinator(
926                                                    String JavaDoc strRecCoordinator)
927    {
928       try
929       {
930          return (RecoveryCoordinator) RemoteProxy.fromString(strRecCoordinator);
931       }
932       catch (Exception JavaDoc e)
933       {
934          throw new RuntimeException JavaDoc(e);
935       }
936    }
937
938    /**
939     * Takes a remote reference to a resource and converts it to a string.
940     *
941     * @param res a remote reference to a resource
942     * @return a string that represents the remote resource.
943     */

944    public String JavaDoc resourceToString(Resource JavaDoc res)
945    {
946       return RemoteProxy.toString((Proxy JavaDoc)res);
947    }
948
949    /**
950     * Takes a remote reference to recovery coordinator and converts it to a
951     * string.
952     *
953     * @param recoveryCoord a remote reference to a recovery coordinator
954     * @return a string that represents the remote recovery coordinator.
955     */

956    public String JavaDoc recoveryCoordinatorToString(RecoveryCoordinator recoveryCoord)
957    {
958       return RemoteProxy.toString((Proxy JavaDoc)recoveryCoord);
959    }
960    
961    // Private -------------------------------------------------------
962

963    private static Status javaxToJBoss(int status)
964    {
965       return Status.fromInteger(status);
966    }
967 }
968
Popular Tags