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