/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 

package org.springframework.transaction.jta;

import java.lang.reflect.InvocationTargetException;

import javax.naming.NamingException;

import org.objectweb.jotm.Current;
import org.objectweb.jotm.Jotm;

import org.springframework.beans.factory.FactoryBean;

/**
 * FactoryBean that retrieves the JTA UserTransaction/TransactionManager for
 * ObjectWeb's <a href="http://jotm.objectweb.org">JOTM</a>. Will retrieve an
 * already active JOTM instance if found (e.g. if running in JOnAS), else create
 * a new local JOTM instance. The same object implements both the UserTransaction
 * and the TransactionManager interface, as returned by this FactoryBean.
 *
 * <p>A local JOTM instance is well-suited for working in conjunction with
 * ObjectWeb's <a href="http://xapool.experlog.com">XAPool</a>, e.g. with bean
 * definitions like the following:
 *
 * <p><code>
 * &lt;bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/&gt;<br>
 * <br>
 * &lt;bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"&gt;<br>
 * &nbsp;&nbsp;&lt;property name="userTransaction"&gt;&lt;ref local="jotm"/&gt;&lt;/property&gt;<br>
 * &lt;/bean&gt;<br>
 * <br>
 * &lt;bean id="innerDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource"&gt;<br>
 * &nbsp;&nbsp;&lt;property name="transactionManager"&gt;&lt;ref local="jotm"/&gt;&lt;/property&gt;<br>
 * &nbsp;&nbsp;&lt;property name="driverName"&gt;...&lt;/property&gt;<br>
 * &nbsp;&nbsp;&lt;property name="url"&gt;...&lt;/property&gt;<br>
 * &lt;/bean&gt;<br>
 * <br>
 * &lt;bean id="dataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"&gt;<br>
 * &nbsp;&nbsp;&lt;property name="dataSource"&gt;&lt;ref local="innerDataSource"/&gt;&lt;/property&gt;<br>
 * &nbsp;&nbsp;&lt;property name="maxSize"&gt;...&lt;/property&gt;<br>
 * &lt;/bean&gt;
 * </code>
 *
 * <p>Uses JOTM's static access method to obtain the JOTM Current object, which
 * implements both the UserTransaction and the TransactionManager interface.
 *
 * @author Juergen Hoeller
 * @since 21.01.2004
 * @see JtaTransactionManager#setUserTransaction
 * @see JtaTransactionManager#setTransactionManager
 * @see org.objectweb.jotm.Current
 */
public class JotmFactoryBean implements FactoryBean {

	private Current jotmCurrent;

	public JotmFactoryBean() throws ClassNotFoundException, NoSuchMethodException,
	    IllegalAccessException, InvocationTargetException, NamingException {

		// check for already active JOTM instance
		this.jotmCurrent = Current.getCurrent();

		// if none found, create new local JOTM instance
		if (this.jotmCurrent == null) {
			new Jotm(true, false);
			this.jotmCurrent = Current.getCurrent();
		}
	}

	public Object getObject() {
		return this.jotmCurrent;
	}

	public Class getObjectType() {
		return this.jotmCurrent.getClass();
	}

	public boolean isSingleton() {
		return true;
	}

}

@@ -824,16 +824,73 @@
 .Jotm;%0A%0A
+import org.springframework.beans.factory.DisposableBean;%0A
 import o
@@ -935,16 +935,16 @@
 ryBean;%0A
-
 %0A/**%0A * 
@@ -1958,32 +1958,58 @@
 ardXADataSource%22
+ destroy-method=%22shutdown%22
 &gt;%3Cbr%3E%0A * &nbs
@@ -2353,24 +2353,50 @@
 lDataSource%22
+ destroy-method=%22shutdown%22
 &gt;%3Cbr%3E%0A * 
@@ -3000,18 +3000,55 @@
 toryBean
- %7B
+, DisposableBean %7B%0A%0A%09private Jotm jotm;
 %0A%0A%09priva
@@ -3403,16 +3403,28 @@
 l) %7B%0A%09%09%09
+this.jotm = 
 new Jotm
@@ -3486,24 +3486,264 @@
 ();%0A%09%09%7D%0A%09%7D%0A%0A
+%09/**%0A%09 * Return the JOTM instance created by this factory bean, if any.%0A%09 * Will be null if an already active JOTM instance is used.%0A%09 * %3Cp%3EApplication code should never need to access this.%0A%09 */%0A%09public Jotm getJotm() %7B%0A%09%09return jotm;%0A%09%7D%0A%0A
 %09public Obje
@@ -3892,24 +3892,24 @@
 ngleton() %7B%0A
-
 %09%09return tru
@@ -3915,10 +3915,91 @@
 ue;%0A%09%7D%0A%0A
+%09public void destroy() %7B%0A%09%09if (this.jotm != null) %7B%0A%09%09%09this.jotm.stop();%0A%09%09%7D%0A%09%7D%0A%0A
 %7D%0A

implement DisposableBean to stop a locally created JOTM instance on context shutdown