|
1 |
| -/** |
2 |
| - * Copyright 2009-2015 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 |
| - * http://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 javax.inject.Provider; |
24 |
| -import javax.transaction.TransactionManager; |
25 |
| -import javax.transaction.xa.XAResource; |
26 |
| - |
27 |
| -import org.apache.ibatis.logging.Log; |
28 |
| -import org.apache.ibatis.logging.LogFactory; |
29 |
| -import org.apache.ibatis.transaction.TransactionFactory; |
30 |
| -import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; |
31 |
| -import org.apache.ibatis.transaction.managed.ManagedTransactionFactory; |
32 |
| -import org.mybatis.guice.transactional.Transactional; |
33 |
| -import org.mybatis.guice.transactional.TransactionalMethodInterceptor; |
34 |
| -import org.mybatis.guice.transactional.TxTransactionalMethodInterceptor; |
35 |
| -import org.mybatis.guice.transactional.XASqlSessionManagerProvider; |
36 |
| - |
37 |
| -public abstract class MyBatisJtaModule extends MyBatisModule { |
38 |
| - private final Log log = LogFactory.getLog(getClass()); |
39 |
| - |
40 |
| - private TransactionManager transactionManager; |
41 |
| - private Class<? extends Provider<? extends XAResource>> xaResourceProvider = XASqlSessionManagerProvider.class; |
42 |
| - |
43 |
| - public MyBatisJtaModule() { |
44 |
| - } |
45 |
| - |
46 |
| - public MyBatisJtaModule(TransactionManager transactionManager) { |
47 |
| - this.transactionManager = transactionManager; |
48 |
| - } |
49 |
| - |
50 |
| - @Override |
51 |
| - protected void bindTransactionInterceptors() { |
52 |
| - TransactionManager manager = getTransactionManager(); |
53 |
| - |
54 |
| - if (manager == null) { |
55 |
| - log.debug("bind default transaction interceptors"); |
56 |
| - super.bindTransactionInterceptors(); |
57 |
| - } else { |
58 |
| - log.debug("bind XA transaction interceptors"); |
59 |
| - |
60 |
| - // transactional interceptor |
61 |
| - TransactionalMethodInterceptor interceptor = new TransactionalMethodInterceptor(); |
62 |
| - requestInjection(interceptor); |
63 |
| - |
64 |
| - // jta transactional interceptor |
65 |
| - TxTransactionalMethodInterceptor interceptorTx = new TxTransactionalMethodInterceptor(); |
66 |
| - requestInjection(interceptorTx); |
67 |
| - bind(XAResource.class).toProvider(xaResourceProvider); |
68 |
| - |
69 |
| - bind(TransactionManager.class).toInstance(manager); |
70 |
| - |
71 |
| - bindInterceptor(any(), not(DECLARED_BY_OBJECT).and(annotatedWith(Transactional.class)), interceptorTx, interceptor); |
72 |
| - // Intercept classes annotated with Transactional, but avoid "double" |
73 |
| - // interception when a mathod is also annotated inside an annotated |
74 |
| - // class. |
75 |
| - bindInterceptor(annotatedWith(Transactional.class), not(DECLARED_BY_OBJECT).and(not(annotatedWith(Transactional.class))), interceptorTx, interceptor); |
76 |
| - } |
77 |
| - } |
78 |
| - |
79 |
| - protected TransactionManager getTransactionManager() { |
80 |
| - return transactionManager; |
81 |
| - } |
82 |
| - |
83 |
| - protected void setTransactionManager(TransactionManager transactionManager) { |
84 |
| - this.transactionManager = transactionManager; |
85 |
| - } |
86 |
| - |
87 |
| - protected void bindDefaultTransactionProvider() { |
88 |
| - Class<? extends TransactionFactory> factoryType = getTransactionManager() == null ? |
89 |
| - JdbcTransactionFactory.class : ManagedTransactionFactory.class; |
90 |
| - |
91 |
| - bindTransactionFactoryType(factoryType); |
92 |
| - } |
93 |
| - |
94 |
| - protected void bindXAResourceProvider(Class<? extends Provider<? extends XAResource>> xaResourceProvider) { |
95 |
| - checkArgument(xaResourceProvider != null, "Parameter 'xaResourceProvider' must be not null"); |
96 |
| - this.xaResourceProvider = xaResourceProvider; |
97 |
| - } |
98 |
| - |
99 |
| - protected static class ProviderImpl<T> implements Provider<T> { |
100 |
| - private T wrapper; |
101 |
| - |
102 |
| - public ProviderImpl(T wrapper) { |
103 |
| - this.wrapper = wrapper; |
104 |
| - } |
105 |
| - |
106 |
| - @Override |
107 |
| - public T get() { |
108 |
| - return wrapper; |
109 |
| - } |
110 |
| - |
111 |
| - } |
112 |
| -} |
| 1 | +/** |
| 2 | + * Copyright 2009-2015 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 | + * http://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 javax.inject.Provider; |
| 24 | +import javax.transaction.TransactionManager; |
| 25 | +import javax.transaction.xa.XAResource; |
| 26 | + |
| 27 | +import org.apache.ibatis.logging.Log; |
| 28 | +import org.apache.ibatis.logging.LogFactory; |
| 29 | +import org.apache.ibatis.transaction.TransactionFactory; |
| 30 | +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; |
| 31 | +import org.apache.ibatis.transaction.managed.ManagedTransactionFactory; |
| 32 | +import org.mybatis.guice.transactional.Transactional; |
| 33 | +import org.mybatis.guice.transactional.TransactionalMethodInterceptor; |
| 34 | +import org.mybatis.guice.transactional.TxTransactionalMethodInterceptor; |
| 35 | +import org.mybatis.guice.transactional.XASqlSessionManagerProvider; |
| 36 | + |
| 37 | +public abstract class MyBatisJtaModule extends MyBatisModule { |
| 38 | + private final Log log = LogFactory.getLog(getClass()); |
| 39 | + |
| 40 | + private TransactionManager transactionManager; |
| 41 | + private Class<? extends Provider<? extends XAResource>> xaResourceProvider = XASqlSessionManagerProvider.class; |
| 42 | + |
| 43 | + public MyBatisJtaModule() { |
| 44 | + } |
| 45 | + |
| 46 | + public MyBatisJtaModule(TransactionManager transactionManager) { |
| 47 | + this.transactionManager = transactionManager; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + protected void bindTransactionInterceptors() { |
| 52 | + TransactionManager manager = getTransactionManager(); |
| 53 | + |
| 54 | + if (manager == null) { |
| 55 | + log.debug("bind default transaction interceptors"); |
| 56 | + super.bindTransactionInterceptors(); |
| 57 | + } else { |
| 58 | + log.debug("bind XA transaction interceptors"); |
| 59 | + |
| 60 | + // transactional interceptor |
| 61 | + TransactionalMethodInterceptor interceptor = new TransactionalMethodInterceptor(); |
| 62 | + requestInjection(interceptor); |
| 63 | + |
| 64 | + // jta transactional interceptor |
| 65 | + TxTransactionalMethodInterceptor interceptorTx = new TxTransactionalMethodInterceptor(); |
| 66 | + requestInjection(interceptorTx); |
| 67 | + bind(XAResource.class).toProvider(xaResourceProvider); |
| 68 | + |
| 69 | + bind(TransactionManager.class).toInstance(manager); |
| 70 | + |
| 71 | + bindInterceptor(any(), not(DECLARED_BY_OBJECT).and(annotatedWith(Transactional.class)), interceptorTx, interceptor); |
| 72 | + // Intercept classes annotated with Transactional, but avoid "double" |
| 73 | + // interception when a mathod is also annotated inside an annotated |
| 74 | + // class. |
| 75 | + bindInterceptor(annotatedWith(Transactional.class), not(DECLARED_BY_OBJECT).and(not(annotatedWith(Transactional.class))), interceptorTx, interceptor); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + protected TransactionManager getTransactionManager() { |
| 80 | + return transactionManager; |
| 81 | + } |
| 82 | + |
| 83 | + protected void setTransactionManager(TransactionManager transactionManager) { |
| 84 | + this.transactionManager = transactionManager; |
| 85 | + } |
| 86 | + |
| 87 | + protected void bindDefaultTransactionProvider() { |
| 88 | + Class<? extends TransactionFactory> factoryType = getTransactionManager() == null ? |
| 89 | + JdbcTransactionFactory.class : ManagedTransactionFactory.class; |
| 90 | + |
| 91 | + bindTransactionFactoryType(factoryType); |
| 92 | + } |
| 93 | + |
| 94 | + protected void bindXAResourceProvider(Class<? extends Provider<? extends XAResource>> xaResourceProvider) { |
| 95 | + checkArgument(xaResourceProvider != null, "Parameter 'xaResourceProvider' must be not null"); |
| 96 | + this.xaResourceProvider = xaResourceProvider; |
| 97 | + } |
| 98 | + |
| 99 | + protected static class ProviderImpl<T> implements Provider<T> { |
| 100 | + private T wrapper; |
| 101 | + |
| 102 | + public ProviderImpl(T wrapper) { |
| 103 | + this.wrapper = wrapper; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public T get() { |
| 108 | + return wrapper; |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | +} |
0 commit comments