Coverage Report - org.mule.config.pool.CommonsPoolProxyPool
 
Classes in this File Line Coverage Branch Coverage Complexity
CommonsPoolProxyPool
0%
0/54
0%
0/6
1.462
 
 1  
 /*
 2  
  * $Id: CommonsPoolProxyPool.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 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.config.pool;
 12  
 
 13  
 import org.mule.config.PoolingProfile;
 14  
 import org.mule.impl.MuleDescriptor;
 15  
 import org.mule.umo.UMOException;
 16  
 import org.mule.umo.lifecycle.Disposable;
 17  
 import org.mule.umo.lifecycle.Startable;
 18  
 import org.mule.umo.lifecycle.Stoppable;
 19  
 import org.mule.umo.model.UMOModel;
 20  
 import org.mule.util.ObjectFactory;
 21  
 import org.mule.util.ObjectPool;
 22  
 
 23  
 import java.util.ArrayList;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 
 27  
 import org.apache.commons.logging.Log;
 28  
 import org.apache.commons.logging.LogFactory;
 29  
 import org.apache.commons.pool.PoolableObjectFactory;
 30  
 import org.apache.commons.pool.impl.GenericObjectPool;
 31  
 
 32  
 /**
 33  
  * <code>CommonsPoolProxyPool</code> is pool used to store MuleProxy objects. This
 34  
  * pool is a jakarta commons-pool implementation.
 35  
  */
 36  
 public class CommonsPoolProxyPool implements ObjectPool
 37  
 {
 38  
     /**
 39  
      * logger used by this class
 40  
      */
 41  0
     protected static final Log logger = LogFactory.getLog(CommonsPoolProxyPool.class);
 42  
 
 43  
     /**
 44  
      * The pool that holds the MuleProxy objects
 45  
      */
 46  
     protected GenericObjectPool pool;
 47  
 
 48  
     /**
 49  
      * the factory used to create objects for the pool
 50  
      */
 51  
     protected ObjectFactory factory;
 52  
 
 53  
     private List components;
 54  
 
 55  
     /**
 56  
      * Creates a new pool and an Object factory with the UMODescriptor
 57  
      *
 58  
      * @param descriptor the descriptor to use when constructing MuleProxy objects in
 59  
      *            the pool
 60  
      */
 61  
     public CommonsPoolProxyPool(MuleDescriptor descriptor, UMOModel model, ObjectFactory factory, PoolingProfile pp)
 62  0
         {
 63  0
         this.factory = factory;
 64  
 
 65  0
         GenericObjectPool.Config config = new GenericObjectPool.Config();
 66  0
         config.maxIdle = pp.getMaxIdle();
 67  0
         config.maxActive = pp.getMaxActive();
 68  0
         config.maxWait = pp.getMaxWait();
 69  0
         config.whenExhaustedAction = (byte) pp.getExhaustedAction();
 70  
 
 71  0
         init(descriptor, model, config);
 72  0
     }
 73  
 
 74  
     /**
 75  
      * @param descriptor the UMO descriptor to pool
 76  
      * @param config the config to use when configuring the pool
 77  
      */
 78  
     public CommonsPoolProxyPool(MuleDescriptor descriptor, UMOModel model, GenericObjectPool.Config config)
 79  0
     {
 80  0
         init(descriptor, model, config);
 81  0
     }
 82  
 
 83  
     /**
 84  
      * @param descriptor the UMO descriptor to pool
 85  
      * @param config the config to use when configuring the pool
 86  
      */
 87  
     private void init(MuleDescriptor descriptor, UMOModel model, GenericObjectPool.Config config)
 88  
     {
 89  0
         components = new ArrayList();
 90  
 
 91  0
         if (factory == null)
 92  
         {
 93  0
             setFactory(new CommonsPoolProxyFactory(descriptor, model));
 94  
         }
 95  
 
 96  0
         pool = new GenericObjectPool((PoolableObjectFactory) factory, config);
 97  
 
 98  0
         if (factory instanceof CommonsPoolProxyFactory)
 99  
         {
 100  0
             ((CommonsPoolProxyFactory) factory).setPool(this);
 101  
         }
 102  0
     }
 103  
 
 104  
     /*
 105  
      * (non-Javadoc)
 106  
      *
 107  
      * @see org.mule.model.pool.ObjectPool#borrowObject()
 108  
      */
 109  
     public Object borrowObject() throws Exception
 110  
     {
 111  0
         return pool.borrowObject();
 112  
     }
 113  
 
 114  
     /*
 115  
      * (non-Javadoc)
 116  
      *
 117  
      * @see org.mule.model.pool.ObjectPool#returnObject(java.lang.Object)
 118  
      */
 119  
     public void returnObject(Object object) throws Exception
 120  
     {
 121  0
         pool.returnObject(object);
 122  0
     }
 123  
 
 124  
     /*
 125  
      * (non-Javadoc)
 126  
      *
 127  
      * @see org.mule.model.pool.ObjectPool#getSize()
 128  
      */
 129  
     public int getSize()
 130  
     {
 131  0
         return pool.getNumActive();
 132  
     }
 133  
 
 134  
     /*
 135  
      * (non-Javadoc)
 136  
      *
 137  
      * @see org.mule.model.pool.ObjectPool#getMaxSize()
 138  
      */
 139  
     public int getMaxSize()
 140  
     {
 141  0
         return pool.getMaxActive();
 142  
     }
 143  
 
 144  
     /*
 145  
      * (non-Javadoc)
 146  
      *
 147  
      * @see org.mule.model.pool.ObjectPool#setFactory(org.mule.model.pool.ProxyFactory)
 148  
      */
 149  
     public void setFactory(ObjectFactory factory)
 150  
     {
 151  0
         this.factory = factory;
 152  0
     }
 153  
 
 154  
     /*
 155  
      * (non-Javadoc)
 156  
      *
 157  
      * @see org.mule.model.pool.ObjectPool#clearPool()
 158  
      */
 159  
     public void clearPool()
 160  
     {
 161  0
         synchronized (components)
 162  
         {
 163  0
             for (Iterator i = components.iterator(); i.hasNext();)
 164  
             {
 165  0
                 ((Disposable) i.next()).dispose();
 166  
             }
 167  0
             components.clear();
 168  0
         }
 169  0
         pool.clear();
 170  0
     }
 171  
 
 172  
     public void onAdd(Object proxy)
 173  
     {
 174  0
         synchronized (components)
 175  
         {
 176  0
             components.add(proxy);
 177  0
         }
 178  0
     }
 179  
 
 180  
     public void onRemove(Object proxy)
 181  
     {
 182  0
         synchronized (components)
 183  
         {
 184  0
             final boolean wasRemoved = components.remove(proxy);
 185  0
             if (wasRemoved)
 186  
             {
 187  0
                 ((Disposable) proxy).dispose();
 188  
             }
 189  0
         }
 190  0
     }
 191  
 
 192  
     public void start() throws UMOException
 193  
     {
 194  0
         synchronized (components)
 195  
         {
 196  0
             for (Iterator i = components.iterator(); i.hasNext();)
 197  
             {
 198  0
                 ((Startable) i.next()).start();
 199  
             }
 200  0
         }
 201  0
     }
 202  
 
 203  
     public void stop() throws UMOException
 204  
     {
 205  0
         synchronized (components)
 206  
         {
 207  0
             for (Iterator i = components.iterator(); i.hasNext();)
 208  
             {
 209  0
                 ((Stoppable) i.next()).stop();
 210  
             }
 211  0
         }
 212  0
     }
 213  
 
 214  
 }