View Javadoc

1   /*
2    * $Id: VMTransaction.java 10489 2008-01-23 17:53:38Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.vm;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.transaction.TransactionException;
15  import org.mule.config.i18n.CoreMessages;
16  import org.mule.transaction.AbstractSingleResourceTransaction;
17  import org.mule.transaction.IllegalTransactionStateException;
18  import org.mule.util.queue.QueueManager;
19  import org.mule.util.queue.QueueSession;
20  import org.mule.util.xa.ResourceManagerException;
21  
22  public class VMTransaction extends AbstractSingleResourceTransaction
23  {
24  
25      public VMTransaction(MuleContext muleContext) throws TransactionException
26      {
27          QueueManager qm = muleContext.getQueueManager();
28          QueueSession qs = qm.getQueueSession();
29          bindResource(qm, qs);
30      }
31  
32      public void bindResource(Object key, Object resource) throws TransactionException
33      {
34          if (!(key instanceof QueueManager) || !(resource instanceof QueueSession))
35          {
36              throw new IllegalTransactionStateException(
37                  CoreMessages.transactionCanOnlyBindToResources("QueueManager/QueueSession"));
38          }
39          super.bindResource(key, resource);
40      }
41  
42      protected void doBegin() throws TransactionException
43      {
44          try
45          {
46              ((QueueSession)resource).begin();
47          }
48          catch (ResourceManagerException e)
49          {
50              throw new TransactionException(CoreMessages.cannotStartTransaction("VMTransaction"), e);
51          }
52      }
53  
54      protected void doCommit() throws TransactionException
55      {
56          try
57          {
58              ((QueueSession)resource).commit();
59          }
60          catch (ResourceManagerException e)
61          {
62              throw new TransactionException(CoreMessages.transactionCommitFailed(), e);
63          }
64      }
65  
66      protected void doRollback() throws TransactionException
67      {
68          try
69          {
70              ((QueueSession)resource).rollback();
71          }
72          catch (ResourceManagerException e)
73          {
74              throw new TransactionException(CoreMessages.transactionRollbackFailed(), e);
75          }
76      }
77  
78  }