Coverage Report - org.mule.util.pool.DefaultLifecycleEnabledObjectPool
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultLifecycleEnabledObjectPool
94%
16/17
75%
3/4
1.556
DefaultLifecycleEnabledObjectPool$LifecycleEnabledPoolabeObjectFactoryAdaptor
89%
17/19
40%
4/10
1.556
 
 1  
 /*
 2  
  * $Id: DefaultLifecycleEnabledObjectPool.java 11517 2008-03-31 21:34:19Z 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.util.pool;
 12  
 
 13  
 import org.mule.api.MuleException;
 14  
 import org.mule.api.component.JavaComponent;
 15  
 import org.mule.api.component.LifecycleAdapter;
 16  
 import org.mule.api.lifecycle.Disposable;
 17  
 import org.mule.api.lifecycle.Startable;
 18  
 import org.mule.api.lifecycle.Stoppable;
 19  
 import org.mule.api.object.ObjectFactory;
 20  
 import org.mule.component.PooledJavaComponent;
 21  
 import org.mule.config.PoolingProfile;
 22  
 
 23  
 import java.util.Iterator;
 24  
 import java.util.LinkedList;
 25  
 import java.util.List;
 26  
 
 27  
 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
 28  
 
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 31  
 import org.apache.commons.pool.PoolableObjectFactory;
 32  
 
 33  
 /**
 34  
  * A LifecyleEnabledObjectPool implementation for pooling {@link LifecycleAdapter}
 35  
  * instances for implementations of {@link JavaComponent} that require
 36  
  * {@link LifecycleAdapter} pooling such as {@link PooledJavaComponent}.
 37  
  * 
 38  
  * @see PooledJavaComponent
 39  
  */
 40  104
 public class DefaultLifecycleEnabledObjectPool extends CommonsPoolObjectPool implements LifecyleEnabledObjectPool
 41  
 {
 42  
     /**
 43  
      * logger used by this class
 44  
      */
 45  2
     protected static final Log logger = LogFactory.getLog(DefaultLifecycleEnabledObjectPool.class);
 46  
 
 47  26
     protected AtomicBoolean started = new AtomicBoolean(false);
 48  
 
 49  26
     private List items = new LinkedList();
 50  
 
 51  
     /**
 52  
      * @param objectFactory The object factory that should be used to create new
 53  
      *            {@link LifecycleAdapter} instance for the pool
 54  
      * @param poolingProfile The pooling progile ot be used to configure pool
 55  
      */
 56  
     public DefaultLifecycleEnabledObjectPool(ObjectFactory objectFactory, PoolingProfile poolingProfile)
 57  
     {
 58  26
         super(objectFactory, poolingProfile);
 59  26
     }
 60  
 
 61  
     protected PoolableObjectFactory getPooledObjectFactory()
 62  
     {
 63  26
         return new LifecycleEnabledPoolabeObjectFactoryAdaptor();
 64  
     }
 65  
 
 66  
     public void start() throws MuleException
 67  
     {
 68  26
         synchronized (items)
 69  
         {
 70  26
             for (Iterator i = items.iterator(); i.hasNext();)
 71  
             {
 72  0
                 ((Startable) i.next()).start();
 73  
             }
 74  26
         }
 75  26
     }
 76  
 
 77  
     public void stop() throws MuleException
 78  
     {
 79  8
         synchronized (items)
 80  
         {
 81  8
             for (Iterator i = items.iterator(); i.hasNext();)
 82  
             {
 83  8
                 ((Stoppable) i.next()).stop();
 84  
             }
 85  8
         }
 86  8
     }
 87  
 
 88  
     /**
 89  
      * Wraps org.mule.object.ObjectFactory with commons-pool PoolableObjectFactory
 90  
      */
 91  26
     class LifecycleEnabledPoolabeObjectFactoryAdaptor implements PoolableObjectFactory
 92  
     {
 93  
 
 94  
         public void activateObject(Object obj) throws Exception
 95  
         {
 96  
             // nothing to do
 97  52
         }
 98  
 
 99  
         public void destroyObject(Object obj) throws Exception
 100  
         {
 101  4
             if (started.get() && obj instanceof Stoppable)
 102  
             {
 103  0
                 ((Stoppable) obj).stop();
 104  
             }
 105  4
             if (obj instanceof Disposable)
 106  
             {
 107  4
                 ((Disposable) obj).dispose();
 108  
             }
 109  4
             synchronized (items)
 110  
             {
 111  4
                 items.remove(obj);
 112  4
             }
 113  4
         }
 114  
 
 115  
         public Object makeObject() throws Exception
 116  
         {
 117  48
             Object object = objectFactory.getInstance();
 118  48
             if (!started.get() && object instanceof Startable)
 119  
             {
 120  48
                 ((Startable) object).start();
 121  
             }
 122  48
             synchronized (items)
 123  
             {
 124  48
                 items.add(object);
 125  48
             }
 126  48
             return object;
 127  
         }
 128  
 
 129  
         public void passivateObject(Object obj) throws Exception
 130  
         {
 131  
             // nothing to do
 132  8
         }
 133  
 
 134  
         public boolean validateObject(Object obj)
 135  
         {
 136  0
             return true;
 137  
         }
 138  
     }
 139  
 }