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