001: /*
002: * $Id: JmsClientAcknowledgeTransaction.java 10489 2008-01-23 17:53:38Z 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.transport.jms;
012:
013: import org.mule.api.transaction.TransactionException;
014: import org.mule.config.i18n.CoreMessages;
015: import org.mule.transaction.AbstractSingleResourceTransaction;
016: import org.mule.transaction.IllegalTransactionStateException;
017: import org.mule.transport.jms.i18n.JmsMessages;
018:
019: import javax.jms.Connection;
020: import javax.jms.JMSException;
021: import javax.jms.Message;
022: import javax.jms.Session;
023:
024: /**
025: * <code>JmsClientAcknowledgeTransaction</code> is a transaction implementation of
026: * performing a message acknowledgement. There is no notion of rollback with client
027: * acknowledgement, but this transaction can be useful for controlling how messages
028: * are consumed from a destination.
029: */
030: public class JmsClientAcknowledgeTransaction extends
031: AbstractSingleResourceTransaction {
032: private volatile Message message;
033:
034: public void setMessage(Message message) {
035: this .message = message;
036: }
037:
038: /*
039: * (non-Javadoc)
040: *
041: * @see org.mule.transaction.AbstractSingleResourceTransaction#doBegin()
042: */
043: protected void doBegin() throws TransactionException {
044: // nothing to do
045: }
046:
047: /*
048: * (non-Javadoc)
049: *
050: * @see org.mule.transaction.AbstractSingleResourceTransaction#doCommit()
051: */
052: protected void doCommit() throws TransactionException {
053: try {
054: if (message == null) {
055: throw new IllegalTransactionStateException(JmsMessages
056: .noMessageBoundForAck());
057: }
058: message.acknowledge();
059: } catch (JMSException e) {
060: throw new IllegalTransactionStateException(CoreMessages
061: .transactionCommitFailed(), e);
062: }
063: }
064:
065: /*
066: * (non-Javadoc)
067: *
068: * @see org.mule.transaction.AbstractSingleResourceTransaction#doRollback()
069: */
070: protected void doRollback() throws TransactionException {
071: // If a message has been bound, rollback is forbidden
072: if (message != null) {
073: throw new UnsupportedOperationException(
074: "Jms Client Acknowledge doesn't support rollback");
075: }
076: }
077:
078: /*
079: * (non-Javadoc)
080: *
081: * @see org.mule.api.Transaction#bindResource(java.lang.Object,
082: * java.lang.Object)
083: */
084: public void bindResource(Object key, Object resource)
085: throws TransactionException {
086: if (key instanceof Message) {
087: this .message = (Message) key;
088: return;
089: }
090: if (!(key instanceof Connection)
091: || !(resource instanceof Session)) {
092: throw new IllegalTransactionStateException(
093: CoreMessages
094: .transactionCanOnlyBindToResources("javax.jms.Connection/javax.jms.Session"));
095: }
096:
097: Session session = (Session) resource;
098: try {
099: if (session.getTransacted()) {
100: throw new IllegalTransactionStateException(JmsMessages
101: .sessionShouldNotBeTransacted());
102: }
103: } catch (JMSException e) {
104: throw new IllegalTransactionStateException(CoreMessages
105: .transactionCannotReadState(), e);
106: }
107:
108: super.bindResource(key, resource);
109: }
110: }
|