01: /*
02: * $Id: JmsTransaction.java 10489 2008-01-23 17:53:38Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.transport.jms;
12:
13: import org.mule.api.transaction.TransactionException;
14: import org.mule.config.i18n.CoreMessages;
15: import org.mule.config.i18n.MessageFactory;
16: import org.mule.transaction.AbstractSingleResourceTransaction;
17: import org.mule.transaction.IllegalTransactionStateException;
18: import org.mule.transport.jms.i18n.JmsMessages;
19:
20: import javax.jms.Connection;
21: import javax.jms.JMSException;
22: import javax.jms.Session;
23:
24: /**
25: * <code>JmsTransaction</code> is a wrapper for a JMS local transaction. This
26: * object holds the JMS session and controls when the transaction is committed or
27: * rolled back.
28: */
29: public class JmsTransaction extends AbstractSingleResourceTransaction {
30:
31: public void bindResource(Object key, Object resource)
32: throws TransactionException {
33: if (!(key instanceof Connection)
34: || !(resource instanceof Session)) {
35: throw new IllegalTransactionStateException(
36: CoreMessages
37: .transactionCanOnlyBindToResources("javax.jms.Connection/javax.jms.Session"));
38: }
39:
40: Session session = (Session) resource;
41: try {
42: if (!session.getTransacted()) {
43: throw new IllegalTransactionStateException(JmsMessages
44: .sessionShouldBeTransacted());
45: }
46: } catch (JMSException e) {
47: throw new IllegalTransactionStateException(CoreMessages
48: .transactionCannotReadState(), e);
49: }
50:
51: super .bindResource(key, resource);
52: }
53:
54: protected void doBegin() throws TransactionException {
55: // do nothing
56: }
57:
58: protected void doCommit() throws TransactionException {
59: try {
60: ((Session) resource).commit();
61: } catch (JMSException e) {
62: throw new TransactionException(CoreMessages
63: .transactionCommitFailed(), e);
64: }
65: }
66:
67: protected void doRollback() throws TransactionException {
68: if (resource != null) {
69: try {
70: ((Session) resource).rollback();
71: } catch (JMSException e) {
72: throw new TransactionException(CoreMessages
73: .transactionRollbackFailed(), e);
74: }
75: } else {
76: throw new TransactionException(
77: MessageFactory
78: .createStaticMessage("No resource has been bound to this transaction"));
79: }
80: }
81: }
|