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.web;
018
019import java.util.LinkedList;
020
021import javax.jms.Connection;
022import javax.jms.ConnectionFactory;
023import javax.jms.JMSException;
024import javax.jms.Session;
025
026import org.apache.activemq.ActiveMQSession;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * A simple pool of JMS Session objects intended for use by Queue browsers.
032 */
033public class SessionPool {
034
035    private static final Logger LOG = LoggerFactory.getLogger(SessionPool.class);
036
037    private ConnectionFactory connectionFactory;
038    private Connection connection;
039    private final LinkedList<Session> sessions = new LinkedList<Session>();
040
041    public Connection getConnection() throws JMSException {
042        if (checkConnection()) {
043            return connection;
044        }
045
046        synchronized (this) {
047            try {
048                connection = getConnectionFactory().createConnection();
049                connection.start();
050                return connection;
051            } catch (JMSException jmsEx) {
052                LOG.debug("Caught exception while attempting to get a new Connection.", jmsEx);
053                connection.close();
054                connection = null;
055                throw jmsEx;
056            }
057        }
058    }
059
060    private boolean checkConnection() {
061        if (connection == null) {
062            return false;
063        }
064
065        try {
066            connection.getMetaData();
067            return true;
068        } catch (JMSException e) {
069            return false;
070        }
071    }
072
073    public void setConnection(Connection connection) {
074        this.connection = connection;
075    }
076
077    public ConnectionFactory getConnectionFactory() {
078        if (connectionFactory == null) {
079            throw new IllegalStateException("No ConnectionFactory has been set for the session pool");
080        }
081        return connectionFactory;
082    }
083
084    public void setConnectionFactory(ConnectionFactory connectionFactory) {
085        this.connectionFactory = connectionFactory;
086    }
087
088    public Session borrowSession() throws JMSException {
089        Session answer = null;
090        synchronized (sessions) {
091            if (sessions.isEmpty()) {
092                LOG.trace("Creating a new session.");
093                answer = createSession();
094            } else {
095                LOG.trace("Serving session from the pool.");
096                answer = sessions.removeLast();
097            }
098        }
099        return answer;
100    }
101
102    public void returnSession(Session session) {
103        if (session != null && !((ActiveMQSession) session).isClosed()) {
104            synchronized (sessions) {
105                LOG.trace("Returning session to the pool.");
106                sessions.add(session);
107            }
108        } else {
109            LOG.debug("Session closed or null, not returning to the pool.");
110        }
111    }
112
113    protected Session createSession() throws JMSException {
114        return getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE);
115    }
116}