1   /*
2    * $Id: XABridgeComponent.java 10529 2008-01-25 05:58:36Z 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.test.integration.transaction;
12  
13  import org.mule.api.transaction.Transaction;
14  import org.mule.transaction.TransactionCoordination;
15  
16  import java.util.Map;
17  
18  /**
19   * Simple service that receives messages from jdbc or jms and just forward the
20   * interesting part.
21   */
22  public class XABridgeComponent
23  {
24  
25      public static boolean mayRollback = false;
26  
27      /**
28       * If <code>mayRollback</code> has been set to true, the service will mark
29       * the current transaction as rollback only on a 30 percent basis.
30       * 
31       * @throws Exception
32       */
33      protected void mayRollback() throws Exception
34      {
35          if (mayRollback)
36          {
37              Transaction tx = TransactionCoordination.getInstance().getTransaction();
38              if (tx != null)
39              {
40                  if (Math.random() < 0.3)
41                  {
42                      System.err.println("Marking transaction for rollback");
43                      tx.setRollbackOnly();
44                  }
45              }
46          }
47      }
48  
49      /**
50       * Receive the jdbc message and forward the <code>data</code> part. May mark
51       * the current transaction as rollback only.
52       * 
53       * @param msg
54       * @return
55       * @throws Exception
56       */
57      public Object onJdbcMessage(Map msg) throws Exception
58      {
59          mayRollback();
60          return msg.get("data").toString();
61      }
62  
63      /**
64       * Receive the content of the jms message and forward it. May mark the current
65       * transaction as rollback only.
66       * 
67       * @param msg
68       * @return
69       * @throws Exception
70       */
71      public Object onJmsMessage(String msg) throws Exception
72      {
73          mayRollback();
74          return msg;
75      }
76  
77  }