View Javadoc

1   /*
2    * $Id: PoolingProfile.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.config;
12  
13  import org.mule.config.pool.CommonsPoolFactory;
14  import org.mule.umo.model.UMOPoolFactory;
15  import org.mule.util.MapUtils;
16  import org.mule.util.ObjectPool;
17  
18  import java.util.Map;
19  
20  import org.apache.commons.collections.map.CaseInsensitiveMap;
21  
22  /**
23   * <code>PoolingProfile</code> is a configuration object used to define the object
24   * pooling parameters for the component it is associated with.
25   */
26  
27  public class PoolingProfile
28  {
29  
30      /**
31       * Tells the object pool not to initialise any components on startup.
32       */
33      public static final int INITIALISE_NONE = 0;
34      /** @deprecated use INITIALISE_NONE instead */
35      public static final int POOL_INITIALISE_NO_COMPONENTS = INITIALISE_NONE;
36  
37      /**
38       * Tells the object pool only to initialise one component on startup.
39       */
40      public static final int INITIALISE_ONE = 1;
41      /** @deprecated use INITIALISE_ONE instead */
42      public static final int POOL_INITIALISE_ONE_COMPONENT = INITIALISE_ONE;
43  
44      /**
45       * Tells the object pool to initialise all components on startup.
46       */
47      public static final int INITIALISE_ALL = 2;
48      /** @deprecated use INITIALISE_ALL instead */
49      public static final int POOL_INITIALISE_ALL_COMPONENTS = INITIALISE_ALL;
50  
51      /**
52       * Controls the maximum number of Mule UMOs that can be borrowed from a component
53       * pool at one time. When non-positive, there is no limit to the number of
54       * components that may be active at one time. When maxActive is exceeded, the
55       * pool is said to be exhausted. You can specify this value on the descriptor
56       * declaration. If none is set this value will be used.
57       */
58      public static final int DEFAULT_MAX_POOL_ACTIVE = ObjectPool.DEFAULT_MAX_SIZE;
59  
60      /**
61       * Controls the maximum number of Mule UMOs that can sit idle in the pool at any
62       * time. When non-positive, there is no limit to the number of Mule UMOs that may
63       * be idle at one time. You can specify this value on the descriptor declaration.
64       * If none is set this value will be used. If this value is not set then a system
65       * default of '5' will be used.
66       */
67      public static final int DEFAULT_MAX_POOL_IDLE = ObjectPool.DEFAULT_MAX_SIZE;
68  
69      /**
70       * When the threadPoolExhaustedAction is set to WHEN_EXHAUSTED_WAIT this can
71       * specify the maximum milliseconds the pool should block before throwing a
72       * NoSuchElementException
73       */
74      public static final long DEFAULT_MAX_POOL_WAIT = ObjectPool.DEFAULT_MAX_WAIT;
75  
76      /**
77       * Specifies the behaviour of the Mule UMO pool when the pool is exhausted:
78       * <ul>
79       * <li>WHEN_EXHAUSTED_FAIL : will throw a NoSuchElementException</li>
80       * <li>WHEN_EXHAUSTED_WAIT : will block (invoke Object.wait(long) until a new or
81       * idle object is available.</li>
82       * <li>WHEN_EXHAUSTED_GROW : will create a new Mule and return it (essentially
83       * making maxActive meaningless).</li>
84       * </ul>
85       * If a positive maxWait value is supplied, it will block for at most that many
86       * milliseconds, after which a NoSuchElementException will be thrown. If maxWait
87       * is non-positive, it will block indefinitely.
88       */
89      public static final int DEFAULT_POOL_EXHAUSTED_ACTION = ObjectPool.WHEN_EXHAUSTED_GROW;
90  
91      /**
92       * Determines how components in a pool should be initialised. The possible values
93       * are:
94       * <ul>
95       * <li>INITIALISE_NONE : Will not load any components in the pool on startup</li>
96       * <li>INITIALISE_ONE : Will load only the first component in the pool on
97       * startup</li>
98       * <li>INITIALISE_ALL : Will load all components in the pool on startup</li>
99       * </ul>
100      */
101     public static final int DEFAULT_POOL_INITIALISATION_POLICY = INITIALISE_ONE;
102 
103     // map pool exhaustion strings to their respective values
104     private static final Map POOL_EXHAUSTED_ACTIONS = new CaseInsensitiveMap()
105     {
106         private static final long serialVersionUID = 1L;
107 
108         // static initializer
109         {
110             // if the values were an actual enum in ObjectPool we could iterate
111             // properly.. :/
112 
113             Integer value = new Integer(ObjectPool.WHEN_EXHAUSTED_WAIT);
114             this.put("WHEN_EXHAUSTED_WAIT", value);
115             this.put("WAIT", value);
116             // TODO HH: remove for 2.0 (only keep WAIT)
117             this.put("BLOCK", value);
118 
119             value = new Integer(ObjectPool.WHEN_EXHAUSTED_FAIL);
120             this.put("WHEN_EXHAUSTED_FAIL", value);
121             this.put("FAIL", value);
122 
123             value = new Integer(ObjectPool.WHEN_EXHAUSTED_GROW);
124             this.put("WHEN_EXHAUSTED_GROW", value);
125             this.put("GROW", value);
126         }
127     };
128 
129     // map pool initialisation policy strings to their respective values
130     private static final Map POOL_INITIALISATION_POLICIES = new CaseInsensitiveMap()
131     {
132         private static final long serialVersionUID = 1L;
133 
134         // static initializer
135         {
136             Integer value = new Integer(INITIALISE_NONE);
137             this.put("INITIALISE_NONE", value);
138 
139             value = new Integer(INITIALISE_ONE);
140             this.put("INITIALISE_ONE", value);
141             // TODO HH: remove for 2.0 (only keep INITIALISE_ONE)
142             this.put("INITIALISE_FIRST", value);
143 
144             value = new Integer(INITIALISE_ALL);
145             this.put("INITIALISE_ALL", value);
146         }
147     };
148 
149     private int maxActive = DEFAULT_MAX_POOL_ACTIVE;
150 
151     private int maxIdle = DEFAULT_MAX_POOL_IDLE;
152 
153     private long maxWait = DEFAULT_MAX_POOL_WAIT;
154 
155     private int exhaustedAction = DEFAULT_POOL_EXHAUSTED_ACTION;
156 
157     private int initialisationPolicy = DEFAULT_POOL_INITIALISATION_POLICY;
158 
159     private UMOPoolFactory poolFactory = new CommonsPoolFactory();
160 
161     public PoolingProfile()
162     {
163         super();
164     }
165 
166     public PoolingProfile(PoolingProfile pp)
167     {
168         this.maxActive = pp.getMaxActive();
169         this.maxIdle = pp.getMaxIdle();
170         this.maxWait = pp.getMaxWait();
171         this.exhaustedAction = pp.getExhaustedAction();
172         this.initialisationPolicy = pp.getInitialisationPolicy();
173         if (pp.getPoolFactory() != null)
174         {
175             poolFactory = pp.getPoolFactory();
176         }
177     }
178 
179     public PoolingProfile(int maxActive,
180                           int maxIdle,
181                           long maxWait,
182                           int exhaustedAction,
183                           int initialisationPolicy)
184     {
185         this.maxActive = maxActive;
186         this.maxIdle = maxIdle;
187         this.maxWait = maxWait;
188         this.exhaustedAction = exhaustedAction;
189         this.initialisationPolicy = initialisationPolicy;
190     }
191 
192     /**
193      * @return max number of Mule UMOs that can be idle in a component
194      */
195     public int getMaxIdle()
196     {
197         return maxIdle;
198     }
199 
200     /**
201      * @return max number of Mule UMOs that can be active in a component
202      */
203     public int getMaxActive()
204     {
205         return maxActive;
206     }
207 
208     /**
209      * @return time in miilisconds to wait for a Mule UMO to be available in a
210      *         component when the pool of Mule UMOs is exhausted and the
211      *         PoolExhaustedAction is set to WHEN_EXHAUSTED_BLOCK
212      */
213     public long getMaxWait()
214     {
215         return maxWait;
216     }
217 
218     /**
219      * @return the action when the Mule UMO pool is exhaused for a component
220      */
221     public int getExhaustedAction()
222     {
223         return exhaustedAction;
224     }
225 
226     public int getInitialisationPolicy()
227     {
228         return initialisationPolicy;
229     }
230 
231     public void setInitialisationPolicy(int policy)
232     {
233         initialisationPolicy = policy;
234     }
235 
236     public void setMaxIdle(int maxIdle)
237     {
238         this.maxIdle = maxIdle;
239     }
240 
241     public void setMaxActive(int maxActive)
242     {
243         this.maxActive = maxActive;
244     }
245 
246     public void setMaxWait(long maxWait)
247     {
248         this.maxWait = maxWait;
249     }
250 
251     public void setExhaustedAction(int exhaustedAction)
252     {
253         this.exhaustedAction = exhaustedAction;
254     }
255 
256     public void setExhaustedActionString(String poolExhaustedAction)
257     {
258         this.exhaustedAction = MapUtils.getIntValue(POOL_EXHAUSTED_ACTIONS, poolExhaustedAction,
259             ObjectPool.DEFAULT_EXHAUSTED_ACTION);
260     }
261 
262     public void setInitialisationPolicyString(String policy)
263     {
264         this.initialisationPolicy = MapUtils.getIntValue(POOL_INITIALISATION_POLICIES, policy,
265             DEFAULT_POOL_INITIALISATION_POLICY);
266     }
267 
268     public UMOPoolFactory getPoolFactory()
269     {
270         return poolFactory;
271     }
272 
273     public void setPoolFactory(UMOPoolFactory poolFactory)
274     {
275         this.poolFactory = poolFactory;
276     }
277 
278 }