View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.springframework.transaction.jta;
17  
18  import javax.naming.NamingException;
19  import javax.transaction.SystemException;
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   * {@link FactoryBean} that retrieves the JTA UserTransaction/TransactionManager
29   * for 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 {
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 		// The following is a JOTM oddity: should be used for demarcation transaction only,
108 		// but is required here in order to actually get rid of JOTM's default (60 seconds).
109 		try {
110 			this.jotmCurrent.setTransactionTimeout(defaultTimeout);
111 		}
112 		catch (SystemException ex) {
113 			// should never happen
114 		}
115 	}
116 
117 
118 	/**
119 	 * Return the JOTM instance created by this factory bean, if any.
120 	 * Will be <code>null</code> if an already active JOTM instance is used.
121 	 * <p>Application code should never need to access this.
122 	 */
123 	public Jotm getJotm() {
124 		return this.jotm;
125 	}
126 
127 	public Object getObject() {
128 		return this.jotmCurrent;
129 	}
130 
131 	public Class getObjectType() {
132 		return this.jotmCurrent.getClass();
133 	}
134 
135 	public boolean isSingleton() {
136 		return true;
137 	}
138 
139 
140 	/**
141 	 * Stop the local JOTM instance, if created by this FactoryBean.
142 	 */
143 	public void destroy() {
144 		if (this.jotm != null) {
145 			this.jotm.stop();
146 		}
147 	}
148 
149 }