001    /**
002     * 
003     * Copyright 2005 LogicBlaze, Inc.
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.jencks;
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    
023    import javax.jms.Message;
024    import javax.jms.MessageListener;
025    import javax.resource.ResourceException;
026    import javax.resource.spi.LocalTransaction;
027    import javax.resource.spi.endpoint.MessageEndpoint;
028    import java.lang.reflect.Method;
029    
030    /**
031     * Performs a local transaction while processing the message
032     *
033     * @version $Revision: 1.1.1.1 $
034     */
035    public class LocalTransactionEndpoint implements MessageEndpoint, MessageListener {
036    
037        private static final Log log = LogFactory.getLog(LocalTransactionEndpoint.class);
038    
039        private MessageListener messageListener;
040        private LocalTransaction localTransaction;
041    
042        public LocalTransactionEndpoint(MessageListener messageListener, LocalTransaction localTransaction) {
043            this.messageListener = messageListener;
044            this.localTransaction = localTransaction;
045        }
046    
047        public void beforeDelivery(Method method) throws NoSuchMethodException, ResourceException {
048            getLocalTransaction().begin();
049        }
050    
051        public void afterDelivery() throws ResourceException {
052            getLocalTransaction().commit();
053        }
054    
055        public void release() {
056            if (localTransaction != null) {
057                try {
058                    localTransaction.rollback();
059                }
060                catch (ResourceException e) {
061                    log.warn("Failed to rollback local transaction: " + e, e);
062                }
063                localTransaction = null;
064            }
065        }
066    
067        public void onMessage(Message message) {
068            messageListener.onMessage(message);
069        }
070    
071    
072        /**
073         * A getter which will return the current local transaction or throw a new exception
074         * if this endpoint has already been released.
075         */
076        protected LocalTransaction getLocalTransaction() throws ResourceException {
077            if (localTransaction == null) {
078                throw new ResourceException("This endpoint has already been released via a call to release() you cannot deliver messages to me");
079            }
080            return localTransaction;
081        }
082    
083    }