001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  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 */
017package org.apache.activemq.ra;
018
019/**
020 * A helper class used to provide access to the current active
021 * {@link InboundContext} instance being used to process a message in the
022 * current thread so that messages can be produced using the same session.
023 * 
024 * 
025 */
026public final class InboundContextSupport {
027
028    private static final ThreadLocal<InboundContext> THREAD_LOCAL = new ThreadLocal<InboundContext>();
029
030    private InboundContextSupport() {
031    }
032
033    /**
034     * Returns the current {@link InboundContext} used by the current thread
035     * which is processing a message. This allows us to access the current
036     * Session to send a message using the same underlying session to avoid
037     * unnecessary XA or to use regular JMS transactions while using message
038     * driven POJOs.
039     * 
040     * @return
041     */
042    public static InboundContext getActiveSessionAndProducer() {
043        return THREAD_LOCAL.get();
044    }
045
046    /**
047     * Registers the session and producer which should be called before the
048     * {@link javax.resource.spi.endpoint.MessageEndpoint#beforeDelivery(java.lang.reflect.Method)}
049     * method is called.
050     * 
051     * @param sessionAndProducer
052     */
053    public static void register(InboundContext sessionAndProducer) {
054        THREAD_LOCAL.set(sessionAndProducer);
055    }
056
057    /**
058     * Unregisters the session and producer which should be called after the
059     * {@link javax.resource.spi.endpoint.MessageEndpoint#afterDelivery()}
060     * method is called.
061     * 
062     * @param sessionAndProducer
063     */
064    public static void unregister(InboundContext sessionAndProducer) {
065        THREAD_LOCAL.set(null);
066    }
067}