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  
  * $Id: DefaultLifecycleEnabledObjectPool.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.MuleContext;
 14  
 import org.mule.api.MuleException;
 15  
 import org.mule.api.component.JavaComponent;
 16  
 import org.mule.api.component.LifecycleAdapter;
 17  
 import org.mule.api.lifecycle.Disposable;
 18  
 import org.mule.api.lifecycle.Startable;
 19  
 import org.mule.api.lifecycle.Stoppable;
 20  
 import org.mule.api.object.ObjectFactory;
 21  
 import org.mule.component.PooledJavaComponent;
 22  
 import org.mule.config.PoolingProfile;
 23  
 
 24  
 import java.util.Iterator;
 25  
 import java.util.LinkedList;
 26  
 import java.util.List;
 27  
 
 28  
 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
 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  0
 public class DefaultLifecycleEnabledObjectPool extends CommonsPoolObjectPool implements LifecyleEnabledObjectPool
 41  
 {
 42  
     /**
 43  
      * logger used by this class
 44  
      */
 45  0
     protected static final Log logger = LogFactory.getLog(DefaultLifecycleEnabledObjectPool.class);
 46  
 
 47  0
     protected AtomicBoolean started = new AtomicBoolean(false);
 48  
 
 49  0
     private List items = new LinkedList();
 50  
 
 51  
     /**
 52  
      * @param objectFactory The object factory that should be used to create new
 53  
      *            {@link org.mule.api.component.LifecycleAdapter} instance for the pool
 54  
      * @param poolingProfile The pooling progile ot be used to configure pool
 55  
      * @param muleContext
 56  
      */
 57  
     public DefaultLifecycleEnabledObjectPool(ObjectFactory objectFactory, PoolingProfile poolingProfile, MuleContext muleContext)
 58  
     {
 59  0
         super(objectFactory, poolingProfile, muleContext);
 60  0
     }
 61  
 
 62  
     protected PoolableObjectFactory getPooledObjectFactory()
 63  
     {
 64  0
         return new LifecycleEnabledPoolabeObjectFactoryAdapter();
 65  
     }
 66  
 
 67  
     public void start() throws MuleException
 68  
     {
 69  0
         started.set(true);
 70  0
         synchronized (items)
 71  
         {
 72  0
             for (Iterator i = items.iterator(); i.hasNext();)
 73  
             {
 74  0
                 ((Startable) i.next()).start();
 75  
             }
 76  0
         }
 77  0
     }
 78  
 
 79  
     public void stop() throws MuleException
 80  
     {
 81  0
         started.set(false);
 82  0
         synchronized (items)
 83  
         {
 84  0
             for (Iterator i = items.iterator(); i.hasNext();)
 85  
             {
 86  0
                 ((Stoppable) i.next()).stop();
 87  
             }
 88  0
         }
 89  0
     }
 90  
 
 91  
     /**
 92  
      * Wraps org.mule.object.ObjectFactory with commons-pool PoolableObjectFactory
 93  
      */
 94  0
     class LifecycleEnabledPoolabeObjectFactoryAdapter implements PoolableObjectFactory
 95  
     {
 96  
 
 97  
         public void activateObject(Object obj) throws Exception
 98  
         {
 99  
             // nothing to do
 100  0
         }
 101  
 
 102  
         public void destroyObject(Object obj) throws Exception
 103  
         {
 104  
             // Only stop existing objects if they havn't already been stopped
 105  0
             if (started.get() && obj instanceof Stoppable)
 106  
             {
 107  0
                 ((Stoppable) obj).stop();
 108  
             }
 109  0
             if (obj instanceof Disposable)
 110  
             {
 111  0
                 ((Disposable) obj).dispose();
 112  
             }
 113  0
             synchronized (items)
 114  
             {
 115  0
                 items.remove(obj);
 116  0
             }
 117  0
         }
 118  
 
 119  
         public Object makeObject() throws Exception
 120  
         {
 121  0
             Object object = objectFactory.getInstance(muleContext);
 122  
             // Only start newly created objects if pool is started
 123  0
             if (started.get() && object instanceof Startable)
 124  
             {
 125  0
                 ((Startable) object).start();
 126  
             }
 127  0
             synchronized (items)
 128  
             {
 129  0
                 items.add(object);
 130  0
             }
 131  0
             return object;
 132  
         }
 133  
 
 134  
         public void passivateObject(Object obj) throws Exception
 135  
         {
 136  
             // nothing to do
 137  0
         }
 138  
 
 139  
         public boolean validateObject(Object obj)
 140  
         {
 141  0
             return true;
 142  
         }
 143  
     }
 144  
 }