Coverage Report - org.mule.component.PooledJavaComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
PooledJavaComponent
67%
18/27
50%
1/2
1.062
PooledJavaComponent$LifeCycleAdaptorFactory
20%
2/10
N/A
1.062
 
 1  
 /*
 2  
  * $Id: PooledJavaComponent.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.component;
 12  
 
 13  
 import org.mule.api.MuleException;
 14  
 import org.mule.api.component.LifecycleAdapter;
 15  
 import org.mule.api.lifecycle.InitialisationCallback;
 16  
 import org.mule.api.lifecycle.InitialisationException;
 17  
 import org.mule.api.model.EntryPointResolverSet;
 18  
 import org.mule.api.object.ObjectFactory;
 19  
 import org.mule.api.routing.NestedRouterCollection;
 20  
 import org.mule.config.PoolingProfile;
 21  
 import org.mule.util.pool.DefaultLifecycleEnabledObjectPool;
 22  
 import org.mule.util.pool.LifecyleEnabledObjectPool;
 23  
 
 24  
 /**
 25  
  * <code>PooledJavaComponent</code> implements pooling.
 26  
  */
 27  
 public class PooledJavaComponent extends AbstractJavaComponent
 28  
 {
 29  
 
 30  
     protected PoolingProfile poolingProfile;
 31  
     protected LifecyleEnabledObjectPool lifecycleAdapterPool;
 32  
 
 33  
     public PooledJavaComponent()
 34  
     {
 35  0
         super();
 36  0
     }
 37  
 
 38  
     public PooledJavaComponent(ObjectFactory objectFactory)
 39  
     {
 40  0
         this(objectFactory, null);
 41  0
     }
 42  
 
 43  
     public PooledJavaComponent(ObjectFactory objectFactory, PoolingProfile poolingProfile)
 44  
     {
 45  24
         super(objectFactory);
 46  24
         this.poolingProfile = poolingProfile;
 47  24
     }
 48  
 
 49  
     public PooledJavaComponent(ObjectFactory objectFactory,
 50  
                                PoolingProfile poolingProfile,
 51  
                                EntryPointResolverSet entryPointResolverSet,
 52  
                                NestedRouterCollection nestedRouterCollection)
 53  
     {
 54  0
         super(objectFactory, entryPointResolverSet, nestedRouterCollection);
 55  0
         this.poolingProfile = poolingProfile;
 56  0
     }
 57  
 
 58  
     protected LifecycleAdapter borrowComponentLifecycleAdaptor() throws Exception
 59  
     {
 60  56
         return (LifecycleAdapter) lifecycleAdapterPool.borrowObject();
 61  
     }
 62  
 
 63  
     protected void returnComponentLifecycleAdaptor(LifecycleAdapter lifecycleAdapter)
 64  
     {
 65  8
         lifecycleAdapterPool.returnObject(lifecycleAdapter);
 66  8
     }
 67  
 
 68  
     protected void doStart() throws MuleException
 69  
     {
 70  26
         super.doStart();
 71  
         // Wrap pool's objectFactory with a LifeCycleAdaptor factory so we pool
 72  
         // LifeCycleAdaptor's and not just pojo instances.
 73  26
         lifecycleAdapterPool = new DefaultLifecycleEnabledObjectPool(new LifeCycleAdaptorFactory(), poolingProfile);
 74  26
         lifecycleAdapterPool.initialise();
 75  26
         lifecycleAdapterPool.start();
 76  26
     }
 77  
 
 78  
     protected void doStop() throws MuleException
 79  
     {
 80  8
         super.doStop();
 81  8
         if (lifecycleAdapterPool != null)
 82  
         {
 83  8
             lifecycleAdapterPool.stop();
 84  8
             lifecycleAdapterPool.close();
 85  8
             lifecycleAdapterPool = null;
 86  
         }
 87  8
     }
 88  
 
 89  
     public void setPoolingProfile(PoolingProfile poolingProfile)
 90  
     {
 91  
         // TODO What happens if this is set while component is started? Should we i)
 92  
         // do nothing ii) issue warning iii) stop/start the pool
 93  
         // (!!) iv) throw exception?
 94  0
         this.poolingProfile = poolingProfile;
 95  0
     }
 96  
 
 97  
     public PoolingProfile getPoolingProfile()
 98  
     {
 99  6
         return poolingProfile;
 100  
     }
 101  
 
 102  
     /**
 103  
      * <code>LifeCycleAdaptorFactory</code> wraps the Component' s
 104  
      * {@link ObjectFactory}. The LifeCycleAdaptorFactory "getInstance()" method
 105  
      * creates a new {@link LifecycleAdapter} wrapping the object instance obtained
 106  
      * for the component instance {@link ObjectFactory} set on the {@link Component}.<br/>
 107  
      * This allows us to keep {@link LifecycleAdapter} creation in the Component and
 108  
      * out of the {@link DefaultLifecyleEnabledObjectPool} and to use the generic
 109  
      * {@link ObjectPool} interface.
 110  
      */
 111  26
     protected class LifeCycleAdaptorFactory implements ObjectFactory
 112  
     {
 113  
 
 114  
         public Object getInstance() throws Exception
 115  
         {
 116  50
             return createLifeCycleAdaptor();
 117  
         }
 118  
 
 119  
         public Class getObjectClass()
 120  
         {
 121  0
             return LifecycleAdapter.class;
 122  
         }
 123  
 
 124  
         public void initialise() throws InitialisationException
 125  
         {
 126  0
             objectFactory.initialise();
 127  0
         }
 128  
 
 129  
         public void dispose()
 130  
         {
 131  0
             objectFactory.dispose();
 132  0
         }
 133  
 
 134  
         public void addObjectInitialisationCallback(InitialisationCallback callback)
 135  
         {
 136  0
             objectFactory.addObjectInitialisationCallback(callback);
 137  0
         }
 138  
 
 139  
         public boolean isSingleton()
 140  
         {
 141  0
             return false;
 142  
         }
 143  
     }
 144  
 
 145  
 }