KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > transaction > jta > WebSphereTransactionManagerFactoryBean


1 /*
2  * Copyright 2002-2005 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.jta;
18
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21
22 import javax.transaction.TransactionManager JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import org.springframework.beans.factory.FactoryBean;
28 import org.springframework.transaction.TransactionSystemException;
29
30 /**
31  * FactoryBean that retrieves the JTA TransactionManager for IBM's
32  * WebSphere application servers (versions 6, 5.1, 5.0 and 4).
33  *
34  * <p>Uses WebSphere's static access methods to obtain the JTA TransactionManager,
35  * which is different for WebSphere 5.1+, 5.0 and 4.
36  *
37  * <p>In combination with Spring's JtaTransactionManager, this FactoryBean
38  * can be used to enable transaction suspension (PROPAGATION_REQUIRES_NEW,
39  * PROPAGATION_NOT_SUPPORTED) on WebSphere:
40  *
41  * <pre>
42  * &lt;bean id="wsJtaTm" class="org.springframework.transaction.jta.WebSphereTransactionManagerFactoryBean"/&gt;
43  *
44  * &lt;bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"&gt;
45  * &lt;property name="transactionManager ref="wsJtaTm"/&gt;
46  * &lt;/bean&gt;</pre>
47  *
48  * Note that Spring's JtaTransactionManager will continue to use the JTA
49  * UserTransaction for standard transaction demarcation, as defined by
50  * standard J2EE. It will only use the provided WebSphere TransactionManager
51  * in case of actual transaction suspension needs.
52  *
53  * @author Juergen Hoeller
54  * @since 21.01.2004
55  * @see JtaTransactionManager#setTransactionManager
56  * @see com.ibm.ws.Transaction.TransactionManagerFactory#getTransactionManager
57  * @see com.ibm.ejs.jts.jta.JTSXA#getTransactionManager
58  * @see com.ibm.ejs.jts.jta.TransactionManagerFactory#getTransactionManager
59  */

60 public class WebSphereTransactionManagerFactoryBean implements FactoryBean {
61
62     private static final String JavaDoc FACTORY_CLASS_5_1 = "com.ibm.ws.Transaction.TransactionManagerFactory";
63
64     private static final String JavaDoc FACTORY_CLASS_5_0 = "com.ibm.ejs.jts.jta.TransactionManagerFactory";
65
66     private static final String JavaDoc FACTORY_CLASS_4 = "com.ibm.ejs.jts.jta.JTSXA";
67
68
69     protected final Log logger = LogFactory.getLog(getClass());
70
71     private final TransactionManager JavaDoc transactionManager;
72
73
74     /**
75      * This constructor retrieves the WebSphere TransactionManager factory class,
76      * so we can get access to the JTA TransactionManager.
77      */

78     public WebSphereTransactionManagerFactoryBean() throws TransactionSystemException {
79         Class JavaDoc clazz;
80         try {
81             logger.debug("Trying WebSphere 5.1+: " + FACTORY_CLASS_5_1);
82             clazz = Class.forName(FACTORY_CLASS_5_1);
83             logger.info("Found WebSphere 5.1+: " + FACTORY_CLASS_5_1);
84         }
85         catch (ClassNotFoundException JavaDoc ex) {
86             logger.debug("Could not find WebSphere 5.1/6.0 TransactionManager factory class", ex);
87             try {
88                 logger.debug("Trying WebSphere 5.0: " + FACTORY_CLASS_5_0);
89                 clazz = Class.forName(FACTORY_CLASS_5_0);
90                 logger.info("Found WebSphere 5.0: " + FACTORY_CLASS_5_0);
91             }
92             catch (ClassNotFoundException JavaDoc ex2) {
93                 logger.debug("Could not find WebSphere 5.0 TransactionManager factory class", ex2);
94                 try {
95                     logger.debug("Trying WebSphere 4: " + FACTORY_CLASS_4);
96                     clazz = Class.forName(FACTORY_CLASS_4);
97                     logger.info("Found WebSphere 4: " + FACTORY_CLASS_4);
98                 }
99                 catch (ClassNotFoundException JavaDoc ex3) {
100                     logger.debug("Could not find WebSphere 4 TransactionManager factory class", ex3);
101                     throw new TransactionSystemException(
102                             "Could not find any WebSphere TransactionManager factory class, " +
103                             "neither for WebSphere version 5.1+ nor 5.0 nor 4");
104                 }
105             }
106         }
107
108         try {
109             Method JavaDoc method = clazz.getMethod("getTransactionManager", (Class JavaDoc[]) null);
110             this.transactionManager = (TransactionManager JavaDoc) method.invoke(null, (Object JavaDoc[]) null);
111         }
112         catch (InvocationTargetException JavaDoc ex) {
113             throw new TransactionSystemException(
114                     "WebSphere's TransactionManagerFactory.getTransactionManager method failed", ex.getTargetException());
115         }
116         catch (Exception JavaDoc ex) {
117             throw new TransactionSystemException(
118                     "Could not access WebSphere's TransactionManagerFactory.getTransactionManager method", ex);
119         }
120     }
121     
122
123     public Object JavaDoc getObject() {
124         return this.transactionManager;
125     }
126
127     public Class JavaDoc getObjectType() {
128         return this.transactionManager.getClass();
129     }
130
131     public boolean isSingleton() {
132         return true;
133     }
134
135 }
136
Popular Tags