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.pool;
018    
019    import javax.jms.JMSException;
020    
021    import org.apache.activemq.ActiveMQConnection;
022    import org.apache.activemq.ActiveMQSession;
023    import org.apache.activemq.AlreadyClosedException;
024    import org.apache.activemq.util.JMSExceptionSupport;
025    import org.apache.commons.pool.ObjectPool;
026    import org.apache.commons.pool.PoolableObjectFactory;
027    
028    /**
029     * Represents the session pool for a given JMS connection.
030     *
031     *
032     */
033    public class SessionPool implements PoolableObjectFactory {
034        private ConnectionPool connectionPool;
035        private SessionKey key;
036        private ObjectPool sessionPool;
037    
038        public SessionPool(ConnectionPool connectionPool, SessionKey key, ObjectPool sessionPool) {
039            this.connectionPool = connectionPool;
040            this.key = key;
041            this.sessionPool = sessionPool;
042            sessionPool.setFactory(this);
043        }
044    
045        public void close() throws Exception {
046            if (sessionPool != null) {
047                sessionPool.close();
048            }
049            sessionPool = null;
050        }
051    
052        public PooledSession borrowSession() throws JMSException {
053            try {
054                Object object = getSessionPool().borrowObject();
055                return (PooledSession)object;
056            } catch (JMSException e) {
057                throw e;
058            } catch (Exception e) {
059                throw JMSExceptionSupport.create(e);
060            }
061        }
062    
063        public void returnSession(PooledSession session) throws JMSException {
064            // lets check if we are already closed
065            getConnection();
066            try {
067                connectionPool.onSessionReturned(session);
068                getSessionPool().returnObject(session);
069            } catch (Exception e) {
070                throw JMSExceptionSupport.create("Failed to return session to pool: " + e, e);
071            }
072        }
073    
074        public void invalidateSession(PooledSession session) throws JMSException {
075            try {
076                connectionPool.onSessionInvalidated(session);
077                getSessionPool().invalidateObject(session);
078            } catch (Exception e) {
079                throw JMSExceptionSupport.create("Failed to invalidate session: " + e, e);
080            }
081        }
082    
083        // PoolableObjectFactory methods
084        // -------------------------------------------------------------------------
085        public Object makeObject() throws Exception {
086            return new PooledSession(createSession(), this);
087        }
088    
089        public void destroyObject(Object o) throws Exception {
090            PooledSession session = (PooledSession)o;
091            session.getInternalSession().close();
092        }
093    
094        public boolean validateObject(Object o) {
095            return true;
096        }
097    
098        public void activateObject(Object o) throws Exception {
099        }
100    
101        public void passivateObject(Object o) throws Exception {
102        }
103    
104        // Implemention methods
105        // -------------------------------------------------------------------------
106        protected ObjectPool getSessionPool() throws AlreadyClosedException {
107            if (sessionPool == null) {
108                throw new AlreadyClosedException();
109            }
110            return sessionPool;
111        }
112    
113        protected ActiveMQConnection getConnection() throws JMSException {
114            return connectionPool.getConnection();
115        }
116    
117        protected ActiveMQSession createSession() throws JMSException {
118            return (ActiveMQSession)getConnection().createSession(key.isTransacted(), key.getAckMode());
119        }
120    
121    }