1. /*
  2. * Copyright 2002-2004 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.springframework.transaction.jta;
  17. import java.lang.reflect.InvocationTargetException;
  18. import javax.naming.NamingException;
  19. import org.objectweb.jotm.Current;
  20. import org.objectweb.jotm.Jotm;
  21. import org.springframework.beans.factory.DisposableBean;
  22. import org.springframework.beans.factory.FactoryBean;
  23. /**
  24. * FactoryBean that retrieves the JTA UserTransaction/TransactionManager for
  25. * ObjectWeb's <a href="http://jotm.objectweb.org">JOTM</a>. Will retrieve an
  26. * already active JOTM instance if found (e.g. if running in JOnAS), else create
  27. * a new local JOTM instance. The same object implements both the UserTransaction
  28. * and the TransactionManager interface, as returned by this FactoryBean.
  29. *
  30. * <p>A local JOTM instance is well-suited for working in conjunction with
  31. * ObjectWeb's <a href="http://xapool.experlog.com">XAPool</a>, e.g. with bean
  32. * definitions like the following:
  33. *
  34. * <pre>
  35. * <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
  36. *
  37. * <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
  38. * <property name="userTransaction"><ref local="jotm"/></property>
  39. * </bean>
  40. *
  41. * <bean id="innerDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
  42. * <property name="transactionManager"><ref local="jotm"/></property>
  43. * <property name="driverName">...</property>
  44. * <property name="url">...</property>
  45. * </bean>
  46. *
  47. * <bean id="dataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
  48. * <property name="dataSource"><ref local="innerDataSource"/></property>
  49. * <property name="maxSize">...</property>
  50. * </bean></pre>
  51. *
  52. * Uses JOTM's static access method to obtain the JOTM Current object, which
  53. * implements both the UserTransaction and the TransactionManager interface.
  54. *
  55. * @author Juergen Hoeller
  56. * @since 21.01.2004
  57. * @see JtaTransactionManager#setUserTransaction
  58. * @see JtaTransactionManager#setTransactionManager
  59. * @see org.objectweb.jotm.Current
  60. */
  61. public class JotmFactoryBean implements FactoryBean, DisposableBean {
  62. private Current jotmCurrent;
  63. private Jotm jotm;
  64. public JotmFactoryBean() throws ClassNotFoundException, NoSuchMethodException,
  65. IllegalAccessException, InvocationTargetException, NamingException {
  66. // check for already active JOTM instance
  67. this.jotmCurrent = Current.getCurrent();
  68. // if none found, create new local JOTM instance
  69. if (this.jotmCurrent == null) {
  70. this.jotm = new Jotm(true, false);
  71. this.jotmCurrent = Current.getCurrent();
  72. }
  73. }
  74. /**
  75. * Return the JOTM instance created by this factory bean, if any.
  76. * Will be null if an already active JOTM instance is used.
  77. * <p>Application code should never need to access this.
  78. */
  79. public Jotm getJotm() {
  80. return jotm;
  81. }
  82. public Object getObject() {
  83. return this.jotmCurrent;
  84. }
  85. public Class getObjectType() {
  86. return this.jotmCurrent.getClass();
  87. }
  88. public boolean isSingleton() {
  89. return true;
  90. }
  91. public void destroy() {
  92. if (this.jotm != null) {
  93. this.jotm.stop();
  94. }
  95. }
  96. }