Skip to content

Commit fcb9dd1

Browse files
committed
JtaTransactionManagerFactoryBean as an equivalent to <tx:jta-transaction-manager/>
Issue: SPR-12197
1 parent 6f1acdd commit fcb9dd1

File tree

2 files changed

+105
-30
lines changed

2 files changed

+105
-30
lines changed

spring-tx/src/main/java/org/springframework/transaction/config/JtaTransactionManagerBeanDefinitionParser.java

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,46 +21,23 @@
2121
import org.springframework.beans.factory.support.AbstractBeanDefinition;
2222
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
2323
import org.springframework.beans.factory.xml.ParserContext;
24-
import org.springframework.util.ClassUtils;
2524

2625
/**
27-
* Parser for the &lt;tx:jta-transaction-manager/&gt; element,
28-
* autodetecting BEA WebLogic and IBM WebSphere.
26+
* Parser for the &lt;tx:jta-transaction-manager/&gt; XML configuration element,
27+
* autodetecting WebLogic and WebSphere servers and exposing the corresponding
28+
* {@link org.springframework.transaction.jta.JtaTransactionManager} subclass.
2929
*
3030
* @author Juergen Hoeller
3131
* @author Christian Dupuis
3232
* @since 2.5
33+
* @see org.springframework.transaction.jta.WebLogicJtaTransactionManager
34+
* @see org.springframework.transaction.jta.WebSphereUowTransactionManager
3335
*/
3436
public class JtaTransactionManagerBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
3537

36-
private static final String WEBLOGIC_JTA_TRANSACTION_MANAGER_CLASS_NAME =
37-
"org.springframework.transaction.jta.WebLogicJtaTransactionManager";
38-
39-
private static final String WEBSPHERE_TRANSACTION_MANAGER_CLASS_NAME =
40-
"org.springframework.transaction.jta.WebSphereUowTransactionManager";
41-
42-
private static final String JTA_TRANSACTION_MANAGER_CLASS_NAME =
43-
"org.springframework.transaction.jta.JtaTransactionManager";
44-
45-
46-
private static final boolean weblogicPresent = ClassUtils.isPresent(
47-
"weblogic.transaction.UserTransaction", JtaTransactionManagerBeanDefinitionParser.class.getClassLoader());
48-
49-
private static final boolean webspherePresent = ClassUtils.isPresent(
50-
"com.ibm.wsspi.uow.UOWManager", JtaTransactionManagerBeanDefinitionParser.class.getClassLoader());
51-
52-
5338
@Override
5439
protected String getBeanClassName(Element element) {
55-
if (weblogicPresent) {
56-
return WEBLOGIC_JTA_TRANSACTION_MANAGER_CLASS_NAME;
57-
}
58-
else if (webspherePresent) {
59-
return WEBSPHERE_TRANSACTION_MANAGER_CLASS_NAME;
60-
}
61-
else {
62-
return JTA_TRANSACTION_MANAGER_CLASS_NAME;
63-
}
40+
return JtaTransactionManagerFactoryBean.resolveJtaTransactionManagerClassName();
6441
}
6542

6643
@Override
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2002-2014 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+
17+
package org.springframework.transaction.config;
18+
19+
import org.springframework.beans.BeanUtils;
20+
import org.springframework.beans.factory.FactoryBean;
21+
import org.springframework.transaction.jta.JtaTransactionManager;
22+
import org.springframework.util.ClassUtils;
23+
24+
/**
25+
* A {@link FactoryBean} equivalent to the &lt;tx:jta-transaction-manager/&gt; XML element,
26+
* autodetecting WebLogic and WebSphere servers and exposing the corresponding
27+
* {@link org.springframework.transaction.jta.JtaTransactionManager} subclass.
28+
*
29+
* @author Juergen Hoeller
30+
* @since 4.1.1
31+
* @see org.springframework.transaction.jta.WebLogicJtaTransactionManager
32+
* @see org.springframework.transaction.jta.WebSphereUowTransactionManager
33+
*/
34+
public class JtaTransactionManagerFactoryBean implements FactoryBean<JtaTransactionManager> {
35+
36+
private static final String WEBLOGIC_JTA_TRANSACTION_MANAGER_CLASS_NAME =
37+
"org.springframework.transaction.jta.WebLogicJtaTransactionManager";
38+
39+
private static final String WEBSPHERE_TRANSACTION_MANAGER_CLASS_NAME =
40+
"org.springframework.transaction.jta.WebSphereUowTransactionManager";
41+
42+
private static final String JTA_TRANSACTION_MANAGER_CLASS_NAME =
43+
"org.springframework.transaction.jta.JtaTransactionManager";
44+
45+
46+
private static final boolean weblogicPresent = ClassUtils.isPresent(
47+
"weblogic.transaction.UserTransaction", JtaTransactionManagerFactoryBean.class.getClassLoader());
48+
49+
private static final boolean webspherePresent = ClassUtils.isPresent(
50+
"com.ibm.wsspi.uow.UOWManager", JtaTransactionManagerFactoryBean.class.getClassLoader());
51+
52+
53+
private final JtaTransactionManager transactionManager;
54+
55+
56+
@SuppressWarnings("unchecked")
57+
public JtaTransactionManagerFactoryBean() {
58+
String className = resolveJtaTransactionManagerClassName();
59+
try {
60+
Class<? extends JtaTransactionManager> clazz = (Class<? extends JtaTransactionManager>)
61+
JtaTransactionManagerFactoryBean.class.getClassLoader().loadClass(className);
62+
this.transactionManager = BeanUtils.instantiate(clazz);
63+
}
64+
catch (ClassNotFoundException ex) {
65+
throw new IllegalStateException("Failed to load JtaTransactionManager class: " + className, ex);
66+
}
67+
}
68+
69+
70+
@Override
71+
public JtaTransactionManager getObject() {
72+
return this.transactionManager;
73+
}
74+
75+
@Override
76+
public Class<?> getObjectType() {
77+
return (this.transactionManager != null ? this.transactionManager.getClass() : JtaTransactionManager.class);
78+
}
79+
80+
@Override
81+
public boolean isSingleton() {
82+
return true;
83+
}
84+
85+
86+
static String resolveJtaTransactionManagerClassName() {
87+
if (weblogicPresent) {
88+
return WEBLOGIC_JTA_TRANSACTION_MANAGER_CLASS_NAME;
89+
}
90+
else if (webspherePresent) {
91+
return WEBSPHERE_TRANSACTION_MANAGER_CLASS_NAME;
92+
}
93+
else {
94+
return JTA_TRANSACTION_MANAGER_CLASS_NAME;
95+
}
96+
}
97+
98+
}

0 commit comments

Comments
 (0)