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