View Javadoc

1   /*
2    * Copyright (C) The Apache Software Foundation. All rights reserved.
3    *
4    * This software is published under the terms of the Apache Software License
5    * version 1.1, a copy of which has been included with this distribution in
6    * the LICENSE file.
7    *
8    * $Id: XAMessenger.java 155459 2005-02-26 13:24:44Z dirkv $
9    */
10  package org.apache.commons.messenger;
11  
12  import javax.jms.Session;
13  import javax.jms.XASession;
14  import javax.transaction.Transaction;
15  import javax.transaction.xa.XAResource;
16  
17  import org.apache.commons.logging.Log;
18  import org.apache.commons.logging.LogFactory;
19  
20  /** <p><code>XAMessenger</code> is a default implementation of
21   * Messenger which can also support XA transactions by enlisting and delisting
22   * XAResources.
23   * This is implemented as a seperate Messenger implementation to avoid the core
24   * Messenger having a dependency on JTA.
25   * .</p>
26   *
27   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
28   * @version $Revision: 155459 $
29   */
30  public class XAMessenger extends DefaultMessenger implements XACapable {
31      
32      /** Logger */
33      private static final Log log = LogFactory.getLog(XAMessenger.class);
34      
35      public XAMessenger() {
36      }
37      
38      // XACapable interface
39      //-------------------------------------------------------------------------
40          
41  	public void enlistResources(Transaction transaction) throws Exception {
42  		XAResource resource = getXAResource();
43  		if (resource != null) {
44  			transaction.enlistResource(resource);
45  		}
46  	}
47  
48  	public void delistResources(Transaction transaction, int flag) throws Exception {
49  		XAResource resource = getXAResource();
50  		if (resource != null) {
51  			transaction.delistResource(resource, flag);
52  		}
53  	}
54  
55      // Implementation methods
56      //-------------------------------------------------------------------------
57      
58      /**
59       * @return the XAResource associated with this Messenger if one exists
60       */
61  	protected XAResource getXAResource() throws Exception {
62  		Session session = getMessengerSession().getSession();
63  		if (session instanceof XASession) {
64  			XASession xaSession = (XASession) session;
65  			return xaSession.getXAResource();
66  		}
67  		return null;
68  	}
69  }