001 /** 002 * 003 * Copyright 2004 Protique Ltd 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * 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 **/ 018 package org.activemq.pool; 019 020 import org.apache.commons.pool.ObjectPool; 021 import org.apache.commons.pool.PoolableObjectFactory; 022 import org.apache.commons.pool.impl.GenericObjectPool; 023 import org.activemq.ActiveMQSession; 024 import org.activemq.ActiveMQConnection; 025 import org.activemq.AlreadyClosedException; 026 import org.activemq.util.JMSExceptionHelper; 027 028 import javax.jms.JMSException; 029 030 /** 031 * Represents the session pool for a given JMS connection. 032 * 033 * @version $Revision: 1.1 $ 034 */ 035 public class SessionPool implements PoolableObjectFactory { 036 private ActiveMQConnection connection; 037 private SessionKey key; 038 private ObjectPool sessionPool; 039 040 public SessionPool(ActiveMQConnection connection, SessionKey key) { 041 this(connection, key, new GenericObjectPool()); 042 } 043 044 public SessionPool(ActiveMQConnection connection, SessionKey key, ObjectPool sessionPool) { 045 this.connection = connection; 046 this.key = key; 047 this.sessionPool = sessionPool; 048 sessionPool.setFactory(this); 049 } 050 051 public PooledSession borrowSession() throws JMSException { 052 try { 053 Object object = sessionPool.borrowObject(); 054 return (PooledSession) object; 055 } 056 catch (JMSException e) { 057 throw e; 058 } 059 catch (Exception e) { 060 throw JMSExceptionHelper.newJMSException(e); 061 } 062 } 063 064 // PoolableObjectFactory methods 065 //------------------------------------------------------------------------- 066 public Object makeObject() throws Exception { 067 return new PooledSession(createSession(), sessionPool); 068 } 069 070 public void destroyObject(Object o) throws Exception { 071 PooledSession session = (PooledSession) o; 072 session.getSession().close(); 073 } 074 075 public boolean validateObject(Object o) { 076 return true; 077 } 078 079 public void activateObject(Object o) throws Exception { 080 } 081 082 public void passivateObject(Object o) throws Exception { 083 } 084 085 // Implemention methods 086 //------------------------------------------------------------------------- 087 protected ActiveMQConnection getConnection() throws JMSException { 088 if (connection == null) { 089 throw new AlreadyClosedException(); 090 } 091 return connection; 092 } 093 094 protected ActiveMQSession createSession() throws JMSException { 095 return (ActiveMQSession) getConnection().createSession(key.isTransacted(), key.getAckMode()); 096 } 097 098 099 }