View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.guice;
17  
18  import static com.google.inject.matcher.Matchers.annotatedWith;
19  import static com.google.inject.matcher.Matchers.any;
20  import static com.google.inject.matcher.Matchers.not;
21  import static org.mybatis.guice.Preconditions.checkArgument;
22  
23  import jakarta.inject.Provider;
24  import jakarta.transaction.TransactionManager;
25  
26  import javax.transaction.xa.XAResource;
27  
28  import org.apache.ibatis.logging.Log;
29  import org.apache.ibatis.logging.LogFactory;
30  import org.apache.ibatis.transaction.TransactionFactory;
31  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
32  import org.apache.ibatis.transaction.managed.ManagedTransactionFactory;
33  import org.mybatis.guice.transactional.Transactional;
34  import org.mybatis.guice.transactional.TransactionalMethodInterceptor;
35  import org.mybatis.guice.transactional.TxTransactionalMethodInterceptor;
36  import org.mybatis.guice.transactional.XASqlSessionManagerProvider;
37  
38  public abstract class MyBatisJtaModule extends MyBatisModule {
39    private final Log log = LogFactory.getLog(getClass());
40  
41    private TransactionManager transactionManager;
42    private Class<? extends Provider<? extends XAResource>> xaResourceProvider = XASqlSessionManagerProvider.class;
43  
44    public MyBatisJtaModule() {
45    }
46  
47    public MyBatisJtaModule(TransactionManager transactionManager) {
48      this.transactionManager = transactionManager;
49    }
50  
51    @Override
52    protected void bindTransactionInterceptors() {
53      TransactionManager manager = getTransactionManager();
54  
55      if (manager == null) {
56        log.debug("bind default transaction interceptors");
57        super.bindTransactionInterceptors();
58      } else {
59        log.debug("bind XA transaction interceptors");
60  
61        // transactional interceptor
62        TransactionalMethodInterceptor interceptor = new TransactionalMethodInterceptor();
63        requestInjection(interceptor);
64  
65        // jta transactional interceptor
66        TxTransactionalMethodInterceptor interceptorTx = new TxTransactionalMethodInterceptor();
67        requestInjection(interceptorTx);
68        bind(XAResource.class).toProvider(xaResourceProvider);
69  
70        bind(TransactionManager.class).toInstance(manager);
71  
72        bindInterceptor(any(), not(DECLARED_BY_OBJECT).and(annotatedWith(Transactional.class)), interceptorTx,
73            interceptor);
74        // Intercept classes annotated with Transactional, but avoid "double"
75        // interception when a mathod is also annotated inside an annotated
76        // class.
77        bindInterceptor(annotatedWith(Transactional.class),
78            not(DECLARED_BY_OBJECT).and(not(annotatedWith(Transactional.class))), interceptorTx, interceptor);
79      }
80    }
81  
82    protected TransactionManager getTransactionManager() {
83      return transactionManager;
84    }
85  
86    protected void setTransactionManager(TransactionManager transactionManager) {
87      this.transactionManager = transactionManager;
88    }
89  
90    protected void bindDefaultTransactionProvider() {
91      Class<? extends TransactionFactory> factoryType = getTransactionManager() == null ? JdbcTransactionFactory.class
92          : ManagedTransactionFactory.class;
93  
94      bindTransactionFactoryType(factoryType);
95    }
96  
97    protected void bindXAResourceProvider(Class<? extends Provider<? extends XAResource>> xaResourceProvider) {
98      checkArgument(xaResourceProvider != null, "Parameter 'xaResourceProvider' must be not null");
99      this.xaResourceProvider = xaResourceProvider;
100   }
101 
102   protected static class ProviderImpl<T> implements Provider<T> {
103     private T wrapper;
104 
105     public ProviderImpl(T wrapper) {
106       this.wrapper = wrapper;
107     }
108 
109     @Override
110     public T get() {
111       return wrapper;
112     }
113 
114   }
115 }