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    /**
027     * A simple pool of JMS Session objects intended for use by Queue browsers.
028     * 
029     * @version $Revision: 569067 $
030     */
031    public class SessionPool {
032    
033        private ConnectionFactory connectionFactory;
034        private Connection connection;
035        private LinkedList<Session> sessions = new LinkedList<Session>();
036    
037        public Connection getConnection() throws JMSException {
038            if (checkConnection()) {
039                return connection;
040            }
041    
042            synchronized (this) {
043                connection = getConnectionFactory().createConnection();
044                connection.start();
045                return connection;
046            }
047        }
048    
049        private boolean checkConnection() {
050            if (connection == null) {
051                return false;
052            }
053    
054            try {
055                connection.getMetaData();
056                return true;
057            } catch (JMSException e) {
058                return false;
059            }
060        }
061    
062        public void setConnection(Connection connection) {
063            this.connection = connection;
064        }
065    
066        public ConnectionFactory getConnectionFactory() {
067            if (connectionFactory == null) {
068                throw new IllegalStateException("No ConnectionFactory has been set for the session pool");
069            }
070            return connectionFactory;
071        }
072    
073        public void setConnectionFactory(ConnectionFactory connectionFactory) {
074            this.connectionFactory = connectionFactory;
075        }
076    
077        public Session borrowSession() throws JMSException {
078            Session answer = null;
079            synchronized (sessions) {
080                if (sessions.isEmpty()) {
081                    answer = createSession();
082                } else {
083                    answer = sessions.removeLast();
084                }
085            }
086            return answer;
087        }
088    
089        public void returnSession(Session session) {
090            if (session != null) {
091                synchronized (sessions) {
092                    sessions.add(session);
093                }
094            }
095        }
096    
097        protected Session createSession() throws JMSException {
098            return getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE);
099        }
100    
101    }