Coverage Report - org.mule.util.pool.CommonsPoolObjectPool
 
Classes in this File Line Coverage Branch Coverage Complexity
CommonsPoolObjectPool
61%
38/62
38%
9/24
2.111
CommonsPoolObjectPool$PoolabeObjectFactoryAdaptor
0%
0/8
0%
0/2
2.111
 
 1  
 /*
 2  
  * $Id: CommonsPoolObjectPool.java 12269 2008-07-10 04:19:03Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.util.pool;
 12  
 
 13  
 import org.mule.api.lifecycle.Disposable;
 14  
 import org.mule.api.lifecycle.InitialisationException;
 15  
 import org.mule.api.object.ObjectFactory;
 16  
 import org.mule.config.PoolingProfile;
 17  
 import org.mule.config.i18n.MessageFactory;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.List;
 21  
 
 22  
 import org.apache.commons.logging.Log;
 23  
 import org.apache.commons.logging.LogFactory;
 24  
 import org.apache.commons.pool.PoolableObjectFactory;
 25  
 import org.apache.commons.pool.impl.GenericObjectPool;
 26  
 
 27  
 /**
 28  
  * <code>CommonsPoolProxyPool</code> is an implementation of {@link ObjectPool}
 29  
  * that internally uses the commons-pool {@link GenericObjectPool} and uses a
 30  
  * {@link ObjectFactory} for creating new pooled instances.
 31  
  */
 32  
 public class CommonsPoolObjectPool implements ObjectPool
 33  
 {
 34  
     /**
 35  
      * logger used by this class
 36  
      */
 37  2
     protected static final Log logger = LogFactory.getLog(CommonsPoolObjectPool.class);
 38  
 
 39  
     /**
 40  
      * The pool
 41  
      */
 42  
     protected GenericObjectPool pool;
 43  
 
 44  
     /**
 45  
      * The ObjectFactory used to create new pool instances
 46  
      */
 47  
     protected ObjectFactory objectFactory;
 48  
 
 49  
     /**
 50  
      * The pooling profile used to configure and initialise pool
 51  
      */
 52  
     protected PoolingProfile poolingProfile;
 53  
 
 54  
     /**
 55  
      * Creates a new pool and an Object factory with the UMODescriptor
 56  
      * 
 57  
      * @param descriptor the descriptor to use when constructing MuleProxy objects in
 58  
      *            the pool
 59  
      */
 60  
     public CommonsPoolObjectPool(ObjectFactory objectFactory, PoolingProfile poolingProfile)
 61  26
     {
 62  26
         this.objectFactory = objectFactory;
 63  26
         this.poolingProfile = poolingProfile;
 64  26
     }
 65  
 
 66  
     public void initialise() throws InitialisationException
 67  
     {
 68  26
         GenericObjectPool.Config config = new GenericObjectPool.Config();
 69  
 
 70  26
         if (poolingProfile != null)
 71  
         {
 72  26
             config.maxIdle = poolingProfile.getMaxIdle();
 73  26
             config.maxActive = poolingProfile.getMaxActive();
 74  26
             config.maxWait = poolingProfile.getMaxWait();
 75  26
             config.whenExhaustedAction = (byte) poolingProfile.getExhaustedAction();
 76  
         }
 77  
 
 78  26
         pool = new GenericObjectPool(getPooledObjectFactory(), config);
 79  
 
 80  
         try
 81  
         {
 82  26
             applyInitialisationPolicy();
 83  
         }
 84  0
         catch (Exception e)
 85  
         {
 86  0
             throw new InitialisationException(e, this);
 87  26
         }
 88  26
     }
 89  
 
 90  
     /**
 91  
      * Template method to be overridden by implementations that do more than just
 92  
      * invoke objectFactory
 93  
      * 
 94  
      * @return
 95  
      */
 96  
     protected PoolableObjectFactory getPooledObjectFactory()
 97  
     {
 98  0
         return new PoolabeObjectFactoryAdaptor();
 99  
     }
 100  
 
 101  
     protected void applyInitialisationPolicy() throws Exception
 102  
     {
 103  26
         if (poolingProfile != null)
 104  
         {
 105  26
             int numToBorrow = 0;
 106  26
             int initPolicy = poolingProfile.getInitialisationPolicy();
 107  
 
 108  26
             if (initPolicy == PoolingProfile.INITIALISE_ALL)
 109  
             {
 110  0
                 numToBorrow = poolingProfile.getMaxActive();
 111  
             }
 112  26
             else if (initPolicy == PoolingProfile.INITIALISE_ONE)
 113  
             {
 114  0
                 numToBorrow = 1;
 115  
             }
 116  
 
 117  26
             List holderList = new ArrayList(numToBorrow);
 118  
             try
 119  
             {
 120  26
                 for (int t = 0; t < numToBorrow; t++)
 121  
                 {
 122  0
                     holderList.add(objectFactory.getInstance());
 123  
                 }
 124  
             }
 125  
             finally
 126  
             {
 127  26
                 for (int t = 0; t < holderList.size(); t++)
 128  
                 {
 129  0
                     Object obj = holderList.get(t);
 130  0
                     if (obj != null)
 131  
                     {
 132  0
                         this.returnObject(obj);
 133  
                     }
 134  
                 }
 135  26
             }
 136  
         }
 137  26
     }
 138  
 
 139  
     public Object borrowObject() throws Exception
 140  
     {
 141  56
         if (pool != null)
 142  
         {
 143  56
             return pool.borrowObject();
 144  
         }
 145  
         else
 146  
         {
 147  0
             throw new InitialisationException(
 148  
                 MessageFactory.createStaticMessage("Object pool has not been initialized."), this);
 149  
         }
 150  
     }
 151  
 
 152  
     public void returnObject(Object object)
 153  
     {
 154  8
         if (pool != null)
 155  
         {
 156  
             try
 157  
             {
 158  8
                 pool.returnObject(object);
 159  
             }
 160  0
             catch (Exception ex)
 161  
             {
 162  
                 // declared Exception is never thrown from pool; this is a known bug
 163  
                 // in
 164  
                 // the pool API
 165  8
             }
 166  
         }
 167  8
     }
 168  
 
 169  
     public int getNumActive()
 170  
     {
 171  34
         return pool.getNumActive();
 172  
     }
 173  
 
 174  
     public int getMaxActive()
 175  
     {
 176  0
         return pool.getMaxActive();
 177  
     }
 178  
 
 179  
     public void dispose()
 180  
     {
 181  0
         if (pool != null)
 182  
         {
 183  
             try
 184  
             {
 185  0
                 pool.close();
 186  
             }
 187  0
             catch (Exception e)
 188  
             {
 189  
                 // close() never throws - wrong method signature
 190  
             }
 191  
             finally
 192  
             {
 193  0
                 pool = null;
 194  0
             }
 195  
         }
 196  0
     }
 197  
 
 198  
     public void clear()
 199  
     {
 200  0
         if (pool != null)
 201  
         {
 202  0
             pool.clear();
 203  
         }
 204  
 
 205  0
     }
 206  
 
 207  
     public void close()
 208  
     {
 209  8
         if (pool != null)
 210  
         {
 211  
             try
 212  
             {
 213  8
                 pool.close();
 214  
             }
 215  0
             catch (Exception e)
 216  
             {
 217  
                 // close() never throws - wrong method signature
 218  
             }
 219  
             finally
 220  
             {
 221  8
                 pool = null;
 222  8
             }
 223  
         }
 224  
 
 225  8
     }
 226  
 
 227  
     public void setObjectFactory(ObjectFactory objectFactory)
 228  
     {
 229  0
         this.objectFactory = objectFactory;
 230  0
     }
 231  
 
 232  
     public ObjectFactory getObjectFactory()
 233  
     {
 234  2
         return objectFactory;
 235  
     }
 236  
 
 237  
     /**
 238  
      * Wraps org.mule.object.ObjectFactory with commons-pool PoolableObjectFactory
 239  
      */
 240  0
     class PoolabeObjectFactoryAdaptor implements PoolableObjectFactory
 241  
     {
 242  
 
 243  
         public void activateObject(Object obj) throws Exception
 244  
         {
 245  
             // nothing to do
 246  0
         }
 247  
 
 248  
         public void destroyObject(Object obj) throws Exception
 249  
         {
 250  0
             if (obj instanceof Disposable)
 251  
             {
 252  0
                 ((Disposable) obj).dispose();
 253  
             }
 254  0
         }
 255  
 
 256  
         public Object makeObject() throws Exception
 257  
         {
 258  0
             return objectFactory.getInstance();
 259  
         }
 260  
 
 261  
         public void passivateObject(Object obj) throws Exception
 262  
         {
 263  
             // nothing to do
 264  0
         }
 265  
 
 266  
         public boolean validateObject(Object obj)
 267  
         {
 268  0
             return true;
 269  
         }
 270  
 
 271  
     }
 272  
 
 273  
 }