001    /*
002     * Copyright (C) The Apache Software Foundation. All rights reserved.
003     *
004     * This software is published under the terms of the Apache Software License
005     * version 1.1, a copy of which has been included with this distribution in
006     * the LICENSE file.
007     *
008     * $Id: XAMessenger.java 155459 2005-02-26 13:24:44Z dirkv $
009     */
010    package org.apache.commons.messenger;
011    
012    import javax.jms.Session;
013    import javax.jms.XASession;
014    import javax.transaction.Transaction;
015    import javax.transaction.xa.XAResource;
016    
017    import org.apache.commons.logging.Log;
018    import org.apache.commons.logging.LogFactory;
019    
020    /** <p><code>XAMessenger</code> is a default implementation of
021     * Messenger which can also support XA transactions by enlisting and delisting
022     * XAResources.
023     * This is implemented as a seperate Messenger implementation to avoid the core
024     * Messenger having a dependency on JTA.
025     * .</p>
026     *
027     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
028     * @version $Revision: 155459 $
029     */
030    public class XAMessenger extends DefaultMessenger implements XACapable {
031        
032        /** Logger */
033        private static final Log log = LogFactory.getLog(XAMessenger.class);
034        
035        public XAMessenger() {
036        }
037        
038        // XACapable interface
039        //-------------------------------------------------------------------------
040            
041            public void enlistResources(Transaction transaction) throws Exception {
042                    XAResource resource = getXAResource();
043                    if (resource != null) {
044                            transaction.enlistResource(resource);
045                    }
046            }
047    
048            public void delistResources(Transaction transaction, int flag) throws Exception {
049                    XAResource resource = getXAResource();
050                    if (resource != null) {
051                            transaction.delistResource(resource, flag);
052                    }
053            }
054    
055        // Implementation methods
056        //-------------------------------------------------------------------------
057        
058        /**
059         * @return the XAResource associated with this Messenger if one exists
060         */
061            protected XAResource getXAResource() throws Exception {
062                    Session session = getMessengerSession().getSession();
063                    if (session instanceof XASession) {
064                            XASession xaSession = (XASession) session;
065                            return xaSession.getXAResource();
066                    }
067                    return null;
068            }
069    }