001    /**
002     *
003     * Copyright 2004 Protique Ltd
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     * http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     *
017     **/
018    package org.activemq;
019    
020    import javax.jms.JMSException;
021    import javax.jms.QueueSession;
022    import javax.jms.Session;
023    import javax.jms.TopicSession;
024    import javax.jms.TransactionInProgressException;
025    import javax.jms.XAQueueSession;
026    import javax.jms.XATopicSession;
027    import javax.transaction.xa.XAResource;
028    
029    /**
030     * The XASession interface extends the capability of Session by adding access
031     * to a JMS provider's support for the  Java Transaction API (JTA) (optional).
032     * This support takes the form of a javax.transaction.xa.XAResource object.
033     * The functionality of this object closely resembles that defined by the
034     * standard X/Open XA Resource interface.
035     * <p/>
036     * An application server controls the transactional assignment of an XASession
037     * by obtaining its XAResource. It uses the XAResource to assign the session
038     * to a transaction, prepare and commit work on the transaction, and so on.
039     * <p/>
040     * An XAResource provides some fairly sophisticated facilities for
041     * interleaving work on multiple transactions, recovering a list of
042     * transactions in progress, and so on. A JTA aware JMS provider must fully
043     * implement this functionality. This could be done by using the services of a
044     * database that supports XA, or a JMS provider may choose to implement this
045     * functionality from scratch.
046     * <p/>
047     * A client of the application server is given what it thinks is a regular
048     * JMS Session. Behind the scenes, the application server controls the
049     * transaction management of the underlying XASession.
050     * <p/>
051     * The XASession interface is optional. JMS providers are not required to
052     * support this interface. This interface is for use by JMS providers to
053     * support transactional environments. Client programs are strongly encouraged
054     * to use the transactional support  available in their environment, rather
055     * than use these XA  interfaces directly.
056     *
057     * @version $Revision: 1.1.1.1 $
058     * @see javax.jms.Session
059     * @see javax.jms.QueueSession
060     * @see javax.jms.TopicSession
061     * @see javax.jms.XASession
062     */
063    public class ActiveMQXASession extends ActiveMQSession implements QueueSession, TopicSession, XAQueueSession, XATopicSession {
064    
065        public ActiveMQXASession(ActiveMQXAConnection theConnection, int theAcknowlegeMode) throws JMSException {
066            super(theConnection, theAcknowlegeMode);
067        }
068    
069        public boolean getTransacted() throws JMSException {
070            return true;
071        }
072    
073        public void rollback() throws JMSException {
074            throw new TransactionInProgressException("Cannot rollback() inside an XASession");
075        }
076    
077        public void commit() throws JMSException {
078            throw new TransactionInProgressException("Cannot commit() inside an XASession");
079        }
080    
081        public Session getSession() throws JMSException {
082            return this;
083        }
084    
085        public XAResource getXAResource() {
086            return getTransactionContext();
087        }
088    
089        public QueueSession getQueueSession() throws JMSException {
090            return new ActiveMQQueueSession(this);
091        }
092    
093        public TopicSession getTopicSession() throws JMSException {
094            return new ActiveMQTopicSession(this);
095        }
096    
097        /**
098         * This is called before transacted work is done by
099         * the session.  XA Work can only be done when this
100         * XA resource is associated with an Xid.
101         *
102         * @throws JMSException not associated with an Xid
103         */
104        protected void doStartTransaction() throws JMSException {
105            if (!getTransactionContext().isInXATransaction()) {
106                throw new JMSException("Session's XAResource has not been enlisted in a distributed transaction.");
107            }
108        }
109    
110    }