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: XACapableAdapter.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 /**
021 * <p><code>XACapableAdapter</code> is an adapter that implements
022 * XACapable for a given Messenger
023 * </p>
024 *
025 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
026 * @version $Revision: 155459 $
027 */
028 public class XACapableAdapter implements XACapable {
029
030 /** Logger */
031 private static final Log log = LogFactory.getLog(XACapableAdapter.class);
032
033 private Messenger messenger;
034
035 public XACapableAdapter(Messenger messenger) {
036 this.messenger = messenger;
037 }
038
039 // XACapable interface
040 //-------------------------------------------------------------------------
041
042 public void enlistResources(Transaction transaction) throws Exception {
043 XAResource resource = getXAResource();
044 if (resource != null) {
045 transaction.enlistResource(resource);
046 }
047 }
048
049 public void delistResources(Transaction transaction, int flag) throws Exception {
050 XAResource resource = getXAResource();
051 if (resource != null) {
052 transaction.delistResource(resource, flag);
053 }
054 }
055
056 // Implementation methods
057 //-------------------------------------------------------------------------
058
059 /**
060 * @return the XAResource associated with this Messenger if one exists
061 */
062 protected XAResource getXAResource() throws Exception {
063 Session session = messenger.getSession();
064 if (session instanceof XASession) {
065 XASession xaSession = (XASession) session;
066 return xaSession.getXAResource();
067 }
068 else {
069 log.warn(
070 "Messenger: " + messenger
071 + " cannot take part in an XA transaction as it does not have an XASession."
072 + " session: " + session
073 );
074 }
075 return null;
076 }
077 }