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