View Javadoc

1   /*** 
2    * Licensed under the Apache License, Version 2.0 (the "License"); 
3    * you may not use this file except in compliance with the License. 
4    * You may obtain a copy of the License at 
5    * 
6    * http://www.apache.org/licenses/LICENSE-2.0
7    * 
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, 
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
11   * See the License for the specific language governing permissions and 
12   * limitations under the License. 
13   * 
14   **/
15  package org.jencks.pool;
16  
17  import javax.jms.JMSException;
18  import javax.jms.XAConnection;
19  import javax.jms.XASession;
20  
21  import org.apache.commons.pool.PoolableObjectFactory;
22  import org.apache.commons.pool.ObjectPool;
23  import org.apache.commons.pool.impl.GenericObjectPool;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  public class XASessionPool implements PoolableObjectFactory
28  {
29    private static final Log log = LogFactory.getLog(XASessionPool.class);
30    
31    private XAConnection connection;
32    private ObjectPool sessionPool;
33  
34    public XASessionPool(final XAConnection connection)
35    {
36      this(connection, new GenericObjectPool(null, -1));
37    }
38  
39    public XASessionPool(final XAConnection connection,
40                         final ObjectPool sessionPool)
41    {
42      this.connection = connection;
43      this.sessionPool = sessionPool;
44      sessionPool.setFactory(this);
45    }
46  
47    public PooledSpringXASession borrowSession() throws JMSException
48    {
49      try {
50        if(log.isDebugEnabled()) log.debug("---->>>>> BORROWING JMS SESSION FROM POOL...");
51        Object object = sessionPool.borrowObject();
52        if(log.isDebugEnabled()) log.debug("---->>>>> BORROWED SESSION: " + object);
53        return (PooledSpringXASession) object;
54      }
55      catch (JMSException e) {
56        throw e;
57      }
58      catch (Exception e) {
59        final JMSException jmsException = new JMSException("Unhandled exception");
60        jmsException.initCause(e);
61        throw jmsException;
62      }
63    }
64    
65    
66  
67    // PoolableObjectFactory methods
68    //-------------------------------------------------------------------------
69    public Object makeObject() throws Exception
70    {
71      if(log.isDebugEnabled()) log.debug("---->>>>> CREATING NEW SESSION TO SATISFY REQUEST!!");
72      return new PooledSpringXASession(createSession(), sessionPool);
73    }
74  
75    public void destroyObject(Object o) throws Exception
76    {
77      if(log.isDebugEnabled()) log.debug("---->>>>> DESTROYING SESSION AND PERMANENTLY REMOVING FROM POOL: " + o);
78      PooledSpringXASession session = (PooledSpringXASession) o;
79      session.getActualSession().close();
80    }
81  
82    public boolean validateObject(Object o)
83    {
84      return true;
85    }
86  
87    public void activateObject(Object o) throws Exception
88    {
89    }
90  
91    public void passivateObject(Object o) throws Exception
92    {
93      if(log.isDebugEnabled()) log.debug("---->>>>> SESSION HAS BEEN RETURNED TO POOL: " + o);
94    }
95  
96    // Implemention methods
97    //-------------------------------------------------------------------------
98    protected XAConnection getConnection() throws JMSException
99    {
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 }