001 /** 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 * 014 **/ 015 package org.jencks.pool; 016 017 import javax.jms.JMSException; 018 import javax.jms.XAConnection; 019 import javax.jms.XASession; 020 021 import org.apache.commons.pool.PoolableObjectFactory; 022 import org.apache.commons.pool.ObjectPool; 023 import org.apache.commons.pool.impl.GenericObjectPool; 024 import org.apache.commons.logging.Log; 025 import org.apache.commons.logging.LogFactory; 026 027 public class XASessionPool implements PoolableObjectFactory 028 { 029 private static final Log log = LogFactory.getLog(XASessionPool.class); 030 031 private XAConnection connection; 032 private ObjectPool sessionPool; 033 034 public XASessionPool(final XAConnection connection) 035 { 036 this(connection, new GenericObjectPool(null, -1)); 037 } 038 039 public XASessionPool(final XAConnection connection, 040 final ObjectPool sessionPool) 041 { 042 this.connection = connection; 043 this.sessionPool = sessionPool; 044 sessionPool.setFactory(this); 045 } 046 047 public PooledSpringXASession borrowSession() throws JMSException 048 { 049 try { 050 if(log.isDebugEnabled()) log.debug("---->>>>> BORROWING JMS SESSION FROM POOL..."); 051 Object object = sessionPool.borrowObject(); 052 if(log.isDebugEnabled()) log.debug("---->>>>> BORROWED SESSION: " + object); 053 return (PooledSpringXASession) object; 054 } 055 catch (JMSException e) { 056 throw e; 057 } 058 catch (Exception e) { 059 final JMSException jmsException = new JMSException("Unhandled exception"); 060 jmsException.initCause(e); 061 throw jmsException; 062 } 063 } 064 065 066 067 // PoolableObjectFactory methods 068 //------------------------------------------------------------------------- 069 public Object makeObject() throws Exception 070 { 071 if(log.isDebugEnabled()) log.debug("---->>>>> CREATING NEW SESSION TO SATISFY REQUEST!!"); 072 return new PooledSpringXASession(createSession(), sessionPool); 073 } 074 075 public void destroyObject(Object o) throws Exception 076 { 077 if(log.isDebugEnabled()) log.debug("---->>>>> DESTROYING SESSION AND PERMANENTLY REMOVING FROM POOL: " + o); 078 PooledSpringXASession session = (PooledSpringXASession) o; 079 session.getActualSession().close(); 080 } 081 082 public boolean validateObject(Object o) 083 { 084 return true; 085 } 086 087 public void activateObject(Object o) throws Exception 088 { 089 } 090 091 public void passivateObject(Object o) throws Exception 092 { 093 if(log.isDebugEnabled()) log.debug("---->>>>> SESSION HAS BEEN RETURNED TO POOL: " + o); 094 } 095 096 // Implemention methods 097 //------------------------------------------------------------------------- 098 protected XAConnection getConnection() throws JMSException 099 { 100 if (this.connection == null) { 101 throw new JMSException("Already closed"); 102 } 103 return connection; 104 } 105 106 protected XASession createSession() throws JMSException 107 { 108 return getConnection().createXASession(); 109 } 110 111 112 }