001: /*
002: * $Id: SpringTransactionFactory.java 10789 2008-02-12 20:04:43Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.module.spring.transaction;
012:
013: import org.mule.api.MuleContext;
014: import org.mule.api.transaction.Transaction;
015: import org.mule.api.transaction.TransactionException;
016: import org.mule.api.transaction.TransactionFactory;
017: import org.mule.transaction.AbstractSingleResourceTransaction;
018:
019: import org.springframework.jdbc.datasource.ConnectionHolder;
020: import org.springframework.jms.connection.JmsResourceHolder;
021: import org.springframework.transaction.PlatformTransactionManager;
022: import org.springframework.transaction.TransactionStatus;
023: import org.springframework.transaction.support.TransactionSynchronizationManager;
024:
025: /**
026: * TODO: document this class
027: */
028: public class SpringTransactionFactory implements TransactionFactory {
029:
030: private PlatformTransactionManager manager;
031:
032: public SpringTransactionFactory() {
033: super ();
034: }
035:
036: public Transaction beginTransaction(MuleContext muleContext)
037: throws TransactionException {
038: Transaction tx = new SpringTransaction();
039: tx.begin();
040: return tx;
041: }
042:
043: public boolean isTransacted() {
044: return true;
045: }
046:
047: /**
048: * @return Returns the manager.
049: */
050: synchronized public PlatformTransactionManager getManager() {
051: return manager;
052: }
053:
054: /**
055: * @param manager The manager to set.
056: */
057: synchronized public void setManager(
058: PlatformTransactionManager manager) {
059: this .manager = manager;
060: }
061:
062: /**
063: * TODO: document this class
064: */
065: public class SpringTransaction extends
066: AbstractSingleResourceTransaction {
067: protected final TransactionStatus status;
068:
069: public SpringTransaction() {
070: status = manager.getTransaction(null);
071: }
072:
073: protected void doBegin() throws TransactionException {
074: // nothing to do
075: }
076:
077: protected void doCommit() throws TransactionException {
078: manager.commit(status);
079: }
080:
081: protected void doRollback() throws TransactionException {
082: manager.rollback(status);
083: }
084:
085: public Object getResource(Object key) {
086: Object res = TransactionSynchronizationManager
087: .getResource(key);
088: if (res != null) {
089: if (!(res instanceof ConnectionHolder)) {
090: if (res instanceof JmsResourceHolder) {
091: return ((JmsResourceHolder) res)
092: .getConnection();
093: }
094: } else {
095: return ((ConnectionHolder) res).getConnection();
096: }
097: }
098: return res;
099: }
100:
101: public boolean hasResource(Object key) {
102: return getResource(key) != null;
103: }
104:
105: public void bindResource(Object key, Object resource)
106: throws TransactionException {
107: throw new UnsupportedOperationException();
108: }
109:
110: public void setRollbackOnly() {
111: super.setRollbackOnly();
112: status.setRollbackOnly();
113: }
114:
115: }
116:
117: }
|