Coverage Report - org.mule.config.ChainedThreadingProfile
 
Classes in this File Line Coverage Branch Coverage Complexity
ChainedThreadingProfile
0%
0/52
0%
0/18
1.067
 
 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.config;
 8  
 
 9  
 import org.mule.api.MuleContext;
 10  
 import org.mule.api.config.ThreadingProfile;
 11  
 import org.mule.api.context.MuleContextAware;
 12  
 import org.mule.api.context.WorkManager;
 13  
 import org.mule.config.pool.ThreadPoolFactory;
 14  
 
 15  
 import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
 16  
 import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionHandler;
 17  
 import edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory;
 18  
 
 19  
 /**
 20  
  * This was written (perhaps too far in advance) with an eye to how we will manage default values
 21  
  * in a dynamic environment.  Since very little has been decided in that direction the correct
 22  
  * behaviour is unclear - changing the default value of "dynamic"
 23  
  * {@link org.mule.config.ChainedThreadingProfile#ChainedThreadingProfile(ThreadingProfile)}
 24  
  * will switch behaviour between dynamic and static chaining.
 25  
  *
 26  
  * <p>Note that within Spring, as far as I understand things, object creation is always ordered
 27  
  * so that dependencies are correctly resolved.  In that case, in a static scenario (or in a
 28  
  * dynamic one that rebuilds the instances) dynamic and static behaviour should be identical.</p>
 29  
  *
 30  
  * <p>Also, the "lazy" chaining is an optimisation - all hierarchies should be grounded in a final
 31  
  * default which is {@link ImmutableThreadingProfile} and, as such, return reliable
 32  
  * values (lazy would be necessary if this is not the case, since we must avoid evaluating
 33  
  * incomplete delegates).</p>
 34  
  */
 35  
 public class ChainedThreadingProfile implements ThreadingProfile
 36  
 {
 37  
 
 38  
     private Integer maxThreadsActive;
 39  
     private Integer maxThreadsIdle;
 40  
     private Integer maxBufferSize;
 41  
     private Long threadTTL;
 42  
     private Long threadWaitTimeout;
 43  
     private Integer poolExhaustedAction;
 44  
     private Boolean doThreading;
 45  
 
 46  0
     private ThreadPoolFactory poolFactory = ThreadPoolFactory.newInstance();
 47  0
     private WorkManagerFactory workManagerFactory = new ImmutableThreadingProfile.DefaultWorkManagerFactory();
 48  
     private RejectedExecutionHandler rejectedExecutionHandler;
 49  
     private ThreadFactory threadFactory;
 50  
 
 51  
     private ThreadingProfile delegate;
 52  
 
 53  
     private MuleContext muleContext;
 54  
 
 55  
     /**
 56  
      * Generate a mutable threading profile with fixed default values taken from
 57  
      * {@link #DEFAULT_THREADING_PROFILE}
 58  
      */
 59  
     public ChainedThreadingProfile()
 60  
     {
 61  
         // the default is itself constant, so dynanmic=true irrelevant
 62  0
         this(DEFAULT_THREADING_PROFILE);
 63  0
     }
 64  
 
 65  
     /**
 66  
      * Generate a mutable threading profile with dynamic default values taken from the
 67  
      * given delegate.
 68  
      *
 69  
      */
 70  
     public ChainedThreadingProfile(ThreadingProfile delegate)
 71  
     {
 72  0
         this(delegate, true);
 73  0
     }
 74  
 
 75  
     /**
 76  
      * Generate a mutable threading profile.  Default values are taken from the "delegate"
 77  
      * argument.  If dynamic is true then changes in the delegate instance are reflected in
 78  
      * this instance.
 79  
      * 
 80  
      * @param delegate Source of default values.
 81  
      * @param dynamic If true, changes in delegate are reflected in this instance
 82  
      */
 83  
     public ChainedThreadingProfile(ThreadingProfile delegate, boolean dynamic)
 84  0
     {
 85  0
         if (!dynamic)
 86  
         {
 87  
             // for static dependencies, we delegate to a fixed copy
 88  0
             delegate = new ImmutableThreadingProfile(delegate);
 89  
         }
 90  0
         this.delegate = delegate;
 91  0
     }
 92  
 
 93  
     public int getMaxThreadsActive()
 94  
     {
 95  0
         return null != maxThreadsActive ? maxThreadsActive : delegate.getMaxThreadsActive();
 96  
     }
 97  
 
 98  
     public int getMaxThreadsIdle()
 99  
     {
 100  0
         return null != maxThreadsIdle ? maxThreadsIdle : delegate.getMaxThreadsIdle();
 101  
     }
 102  
 
 103  
     public long getThreadTTL()
 104  
     {
 105  0
         return null != threadTTL ? threadTTL : delegate.getThreadTTL();
 106  
     }
 107  
 
 108  
     public long getThreadWaitTimeout()
 109  
     {
 110  0
         return null != threadWaitTimeout ? threadWaitTimeout : delegate.getThreadWaitTimeout();
 111  
     }
 112  
 
 113  
     public int getPoolExhaustedAction()
 114  
     {
 115  0
         return null != poolExhaustedAction ? poolExhaustedAction : delegate.getPoolExhaustedAction();
 116  
     }
 117  
 
 118  
     public RejectedExecutionHandler getRejectedExecutionHandler()
 119  
     {
 120  0
         return rejectedExecutionHandler;
 121  
     }
 122  
 
 123  
     public ThreadFactory getThreadFactory()
 124  
     {
 125  0
         return threadFactory;
 126  
     }
 127  
 
 128  
     public void setMaxThreadsActive(int maxThreadsActive)
 129  
     {
 130  0
         this.maxThreadsActive = maxThreadsActive;
 131  0
     }
 132  
 
 133  
     public void setMaxThreadsIdle(int maxThreadsIdle)
 134  
     {
 135  0
         this.maxThreadsIdle = maxThreadsIdle;
 136  0
     }
 137  
 
 138  
     public void setThreadTTL(long threadTTL)
 139  
     {
 140  0
         this.threadTTL = threadTTL;
 141  0
     }
 142  
 
 143  
     public void setThreadWaitTimeout(long threadWaitTimeout)
 144  
     {
 145  0
         this.threadWaitTimeout = threadWaitTimeout;
 146  0
     }
 147  
 
 148  
     public void setPoolExhaustedAction(int poolExhaustPolicy)
 149  
     {
 150  0
         this.poolExhaustedAction = poolExhaustPolicy;
 151  0
     }
 152  
 
 153  
     public void setRejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler)
 154  
     {
 155  0
         this.rejectedExecutionHandler = rejectedExecutionHandler;
 156  0
     }
 157  
 
 158  
     public void setThreadFactory(ThreadFactory threadFactory)
 159  
     {
 160  0
         this.threadFactory = threadFactory;
 161  0
     }
 162  
 
 163  
     public int getMaxBufferSize()
 164  
     {
 165  0
         return null != maxBufferSize ? maxBufferSize : delegate.getMaxBufferSize();
 166  
     }
 167  
 
 168  
     public void setMaxBufferSize(int maxBufferSize)
 169  
     {
 170  0
         this.maxBufferSize = maxBufferSize;
 171  0
     }
 172  
 
 173  
     public WorkManagerFactory getWorkManagerFactory()
 174  
     {
 175  0
         return workManagerFactory;
 176  
     }
 177  
 
 178  
     public void setWorkManagerFactory(WorkManagerFactory workManagerFactory)
 179  
     {
 180  0
         this.workManagerFactory = workManagerFactory;
 181  0
     }
 182  
 
 183  
     public WorkManager createWorkManager(String name, int shutdownTimeout)
 184  
     {
 185  
         // we deliberately don't instantiate the chained profile as we just want a cloned copy, not recursion
 186  0
         return workManagerFactory.createWorkManager(new ImmutableThreadingProfile(this), name, shutdownTimeout);
 187  
     }
 188  
 
 189  
     public ExecutorService createPool()
 190  
     {
 191  0
         return createPool(null);
 192  
     }
 193  
 
 194  
     public ExecutorService createPool(String name)
 195  
     {
 196  
         // we deliberately don't instantiate the chained profile as we just want a cloned copy, not recursion
 197  0
         return poolFactory.createPool(name, new ImmutableThreadingProfile(this));
 198  
     }
 199  
 
 200  
     public boolean isDoThreading()
 201  
     {
 202  0
         return null != doThreading ? doThreading : delegate.isDoThreading();
 203  
     }
 204  
 
 205  
     public void setDoThreading(boolean doThreading)
 206  
     {
 207  0
         this.doThreading = doThreading;
 208  0
     }
 209  
 
 210  
     public ThreadPoolFactory getPoolFactory()
 211  
     {
 212  0
         return poolFactory;
 213  
     }
 214  
 
 215  
     public MuleContext getMuleContext()
 216  
     {
 217  0
         return muleContext;
 218  
     }
 219  
 
 220  
     public void setMuleContext(MuleContext muleContext)
 221  
     {
 222  0
         this.muleContext = muleContext;
 223  
 
 224  
         // propagate mule context
 225  0
         if (this.workManagerFactory instanceof MuleContextAware)
 226  
         {
 227  0
             ((MuleContextAware) workManagerFactory).setMuleContext(muleContext);
 228  
         }
 229  
 
 230  0
         poolFactory.setMuleContext(muleContext);
 231  0
     }
 232  
 
 233  
     public String toString()
 234  
     {
 235  0
         return "ThreadingProfile{" + "maxThreadsActive=" + maxThreadsActive + ", maxThreadsIdle="
 236  
                         + maxThreadsIdle + ", maxBufferSize=" + maxBufferSize + ", threadTTL=" + threadTTL
 237  
                         + ", poolExhaustedAction=" + poolExhaustedAction + ", threadWaitTimeout="
 238  
                         + threadWaitTimeout + ", doThreading=" + doThreading + ", workManagerFactory="
 239  
                         + workManagerFactory + ", rejectedExecutionHandler=" + rejectedExecutionHandler
 240  
                         + ", threadFactory=" + threadFactory + "}";
 241  
     }
 242  
 
 243  
 }