View Javadoc

1   /*
2    * $Id: MuleTransactionManagerFactoryBean.java 22147 2011-06-08 11:07:55Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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  package org.mule.config.spring.factories;
11  
12  import org.mule.api.MuleContext;
13  import org.mule.api.context.MuleContextAware;
14  
15  import javax.transaction.TransactionManager;
16  
17  import org.springframework.beans.factory.BeanCreationException;
18  import org.springframework.beans.factory.config.AbstractFactoryBean;
19  
20  /**
21   * <p>
22   * Creates a reference to the TransactionManager configured on the MuleContext. This
23   * is useful when you need to inject the TransactionManager into other objects such
24   * as XA data Sources.
25   * </p>
26   * <p>
27   * In order to use this factory bean you must have a transaction manager configured
28   * in the context i.e. <code><jbossts:transaction-manager/></code>
29   * </p>
30   */
31  public class MuleTransactionManagerFactoryBean extends AbstractFactoryBean<TransactionManager> implements MuleContextAware
32  {
33      private MuleContext muleContext;
34  
35      @Override
36      public void setMuleContext(MuleContext context)
37      {
38          muleContext = context;
39      }
40  
41      @Override
42      public Class<?> getObjectType()
43      {
44          return TransactionManager.class;
45      }
46  
47      @Override
48      protected TransactionManager createInstance() throws Exception
49      {
50          if (muleContext.getTransactionManager() == null)
51          {
52              throw new BeanCreationException("you must have a transaction manager configured inside the context when using " + getClass().getName());
53          }
54          return muleContext.getTransactionManager();
55      }
56  }