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.jms.pool;
018
019import javax.jms.JMSException;
020import javax.jms.MessageProducer;
021import javax.jms.QueueSender;
022import javax.jms.QueueSession;
023import javax.jms.Session;
024import javax.jms.TopicPublisher;
025import javax.jms.TopicSession;
026
027/**
028 * Used to store a pooled session instance and any resources that can
029 * be left open and carried along with the pooled instance such as the
030 * anonymous producer used for all MessageProducer instances created
031 * from this pooled session when enabled.
032 */
033public class SessionHolder {
034
035    private final Session session;
036    private MessageProducer producer;
037    private TopicPublisher publisher;
038    private QueueSender sender;
039
040    public SessionHolder(Session session) {
041        this.session = session;
042    }
043
044    public void close() throws JMSException {
045        try {
046            session.close();
047        } finally {
048            producer = null;
049            publisher = null;
050            sender = null;
051        }
052    }
053
054    public Session getSession() {
055        return session;
056    }
057
058    public MessageProducer getOrCreateProducer() throws JMSException {
059        if (producer == null) {
060            synchronized (this) {
061                if (producer == null) {
062                    producer = session.createProducer(null);
063                }
064            }
065        }
066
067        return producer;
068    }
069
070    public TopicPublisher getOrCreatePublisher() throws JMSException {
071        if (publisher == null) {
072            synchronized (this) {
073                if (publisher == null) {
074                    publisher = ((TopicSession) session).createPublisher(null);
075                }
076            }
077        }
078
079        return publisher;
080    }
081
082    public QueueSender getOrCreateSender() throws JMSException {
083        if (sender == null) {
084            synchronized (this) {
085                if (sender == null) {
086                    sender = ((QueueSession) session).createSender(null);
087                }
088            }
089        }
090
091        return sender;
092    }
093
094    @Override
095    public String toString() {
096        return session.toString();
097    }
098}