KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2006 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 javax.naming.NamingException JavaDoc;
20
21 import org.objectweb.jotm.Current;
22 import org.objectweb.jotm.Jotm;
23
24 import org.springframework.beans.factory.DisposableBean;
25 import org.springframework.beans.factory.FactoryBean;
26
27 /**
28  * FactoryBean that retrieves the JTA UserTransaction/TransactionManager for
29  * ObjectWeb's <a HREF="http://jotm.objectweb.org">JOTM</a>. Will retrieve
30  * an already active JOTM instance if found (e.g. if running in JOnAS),
31  * else create a new local JOTM instance.
32  *
33  * <p>With JOTM, the same object implements both the
34  * {@link javax.transaction.UserTransaction} and the
35  * {@link javax.transaction.TransactionManager} interface,
36  * as returned by this FactoryBean.
37  *
38  * <p>A local JOTM instance is well-suited for working in conjunction with
39  * ObjectWeb's <a HREF="http://xapool.experlog.com">XAPool</a>, e.g. with bean
40  * definitions like the following:
41  *
42  * <pre class="code">
43  * &lt;bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/&gt;
44  *
45  * &lt;bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"&gt;
46  * &lt;property name="userTransaction" ref="jotm"/&gt;
47  * &lt;/bean&gt;
48  *
49  * &lt;bean id="innerDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown"&gt;
50  * &lt;property name="transactionManager" ref="jotm"/&gt;
51  * &lt;property name="driverName" value="..."/&gt;
52  * &lt;property name="url" value="..."/&gt;
53  * &lt;property name="user" value="..."/&gt;
54  * &lt;property name="password" value="..."/&gt;
55  * &lt;/bean&gt;
56  *
57  * &lt;bean id="dataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown"&gt;
58  * &lt;property name="dataSource" ref="innerDataSource"/&gt;
59  * &lt;property name="user" value="..."/&gt;
60  * &lt;property name="password" value="..."/&gt;
61  * &lt;property name="maxSize" value="..."/&gt;
62  * &lt;/bean&gt;</pre>
63  *
64  * Note that Spring's {@link JtaTransactionManager} will automatically detect
65  * that the passed-in UserTransaction reference also implements the
66  * TransactionManager interface. Hence, it is not necessary to specify a
67  * separate reference for JtaTransactionManager's "transactionManager" property.
68  *
69  * <p>Implementation note: This FactoryBean uses JOTM's static access method
70  * to obtain the JOTM {@link org.objectweb.jotm.Current} object, which
71  * implements both the UserTransaction and the TransactionManager interface,
72  * as mentioned above.
73  *
74  * @author Juergen Hoeller
75  * @since 21.01.2004
76  * @see JtaTransactionManager#setUserTransaction
77  * @see JtaTransactionManager#setTransactionManager
78  * @see org.objectweb.jotm.Current
79  */

80 public class JotmFactoryBean implements FactoryBean, DisposableBean {
81
82     private Current jotmCurrent;
83
84     private Jotm jotm;
85
86
87     public JotmFactoryBean() throws NamingException JavaDoc {
88         // Check for already active JOTM instance.
89
this.jotmCurrent = Current.getCurrent();
90
91         // If none found, create new local JOTM instance.
92
if (this.jotmCurrent == null) {
93             // Only for use within the current Spring context:
94
// local, not bound to registry.
95
this.jotm = new Jotm(true, false);
96             this.jotmCurrent = Current.getCurrent();
97         }
98     }
99
100     /**
101      * Set the default transaction timeout for the JOTM instance.
102      * <p>Should only be called for a local JOTM instance,
103      * not when accessing an existing (shared) JOTM instance.
104      */

105     public void setDefaultTimeout(int defaultTimeout) {
106         this.jotmCurrent.setDefaultTimeout(defaultTimeout);
107     }
108
109     /**
110      * Return the JOTM instance created by this factory bean, if any.
111      * Will be <code>null</code> if an already active JOTM instance is used.
112      * <p>Application code should never need to access this.
113      */

114     public Jotm getJotm() {
115         return this.jotm;
116     }
117
118
119     public Object JavaDoc getObject() {
120         return this.jotmCurrent;
121     }
122
123     public Class JavaDoc getObjectType() {
124         return this.jotmCurrent.getClass();
125     }
126
127     public boolean isSingleton() {
128         return true;
129     }
130
131
132     /**
133      * Stop the local JOTM instance, if created by this FactoryBean.
134      */

135     public void destroy() {
136         if (this.jotm != null) {
137             this.jotm.stop();
138         }
139     }
140
141 }
142
Popular Tags