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