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