View Javadoc

1   /*
2    * $Id: CommonsPoolProxyPool.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.pool;
12  
13  import org.mule.config.PoolingProfile;
14  import org.mule.impl.MuleDescriptor;
15  import org.mule.umo.UMOException;
16  import org.mule.umo.lifecycle.Disposable;
17  import org.mule.umo.lifecycle.Startable;
18  import org.mule.umo.lifecycle.Stoppable;
19  import org.mule.umo.model.UMOModel;
20  import org.mule.util.ObjectFactory;
21  import org.mule.util.ObjectPool;
22  
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.commons.pool.PoolableObjectFactory;
30  import org.apache.commons.pool.impl.GenericObjectPool;
31  
32  /**
33   * <code>CommonsPoolProxyPool</code> is pool used to store MuleProxy objects. This
34   * pool is a jakarta commons-pool implementation.
35   */
36  public class CommonsPoolProxyPool implements ObjectPool
37  {
38      /**
39       * logger used by this class
40       */
41      protected static final Log logger = LogFactory.getLog(CommonsPoolProxyPool.class);
42  
43      /**
44       * The pool that holds the MuleProxy objects
45       */
46      protected GenericObjectPool pool;
47  
48      /**
49       * the factory used to create objects for the pool
50       */
51      protected ObjectFactory factory;
52  
53      private List components;
54  
55      /**
56       * Creates a new pool and an Object factory with the UMODescriptor
57       *
58       * @param descriptor the descriptor to use when constructing MuleProxy objects in
59       *            the pool
60       */
61      public CommonsPoolProxyPool(MuleDescriptor descriptor, UMOModel model, ObjectFactory factory, PoolingProfile pp)
62          {
63          this.factory = factory;
64  
65          GenericObjectPool.Config config = new GenericObjectPool.Config();
66          config.maxIdle = pp.getMaxIdle();
67          config.maxActive = pp.getMaxActive();
68          config.maxWait = pp.getMaxWait();
69          config.whenExhaustedAction = (byte) pp.getExhaustedAction();
70  
71          init(descriptor, model, config);
72      }
73  
74      /**
75       * @param descriptor the UMO descriptor to pool
76       * @param config the config to use when configuring the pool
77       */
78      public CommonsPoolProxyPool(MuleDescriptor descriptor, UMOModel model, GenericObjectPool.Config config)
79      {
80          init(descriptor, model, config);
81      }
82  
83      /**
84       * @param descriptor the UMO descriptor to pool
85       * @param config the config to use when configuring the pool
86       */
87      private void init(MuleDescriptor descriptor, UMOModel model, GenericObjectPool.Config config)
88      {
89          components = new ArrayList();
90  
91          if (factory == null)
92          {
93              setFactory(new CommonsPoolProxyFactory(descriptor, model));
94          }
95  
96          pool = new GenericObjectPool((PoolableObjectFactory) factory, config);
97  
98          if (factory instanceof CommonsPoolProxyFactory)
99          {
100             ((CommonsPoolProxyFactory) factory).setPool(this);
101         }
102     }
103 
104     /*
105      * (non-Javadoc)
106      *
107      * @see org.mule.model.pool.ObjectPool#borrowObject()
108      */
109     public Object borrowObject() throws Exception
110     {
111         return pool.borrowObject();
112     }
113 
114     /*
115      * (non-Javadoc)
116      *
117      * @see org.mule.model.pool.ObjectPool#returnObject(java.lang.Object)
118      */
119     public void returnObject(Object object) throws Exception
120     {
121         pool.returnObject(object);
122     }
123 
124     /*
125      * (non-Javadoc)
126      *
127      * @see org.mule.model.pool.ObjectPool#getSize()
128      */
129     public int getSize()
130     {
131         return pool.getNumActive();
132     }
133 
134     /*
135      * (non-Javadoc)
136      *
137      * @see org.mule.model.pool.ObjectPool#getMaxSize()
138      */
139     public int getMaxSize()
140     {
141         return pool.getMaxActive();
142     }
143 
144     /*
145      * (non-Javadoc)
146      *
147      * @see org.mule.model.pool.ObjectPool#setFactory(org.mule.model.pool.ProxyFactory)
148      */
149     public void setFactory(ObjectFactory factory)
150     {
151         this.factory = factory;
152     }
153 
154     /*
155      * (non-Javadoc)
156      *
157      * @see org.mule.model.pool.ObjectPool#clearPool()
158      */
159     public void clearPool()
160     {
161         synchronized (components)
162         {
163             for (Iterator i = components.iterator(); i.hasNext();)
164             {
165                 ((Disposable) i.next()).dispose();
166             }
167             components.clear();
168         }
169         pool.clear();
170     }
171 
172     public void onAdd(Object proxy)
173     {
174         synchronized (components)
175         {
176             components.add(proxy);
177         }
178     }
179 
180     public void onRemove(Object proxy)
181     {
182         synchronized (components)
183         {
184             final boolean wasRemoved = components.remove(proxy);
185             if (wasRemoved)
186             {
187                 ((Disposable) proxy).dispose();
188             }
189         }
190     }
191 
192     public void start() throws UMOException
193     {
194         synchronized (components)
195         {
196             for (Iterator i = components.iterator(); i.hasNext();)
197             {
198                 ((Startable) i.next()).start();
199             }
200         }
201     }
202 
203     public void stop() throws UMOException
204     {
205         synchronized (components)
206         {
207             for (Iterator i = components.iterator(); i.hasNext();)
208             {
209                 ((Stoppable) i.next()).stop();
210             }
211         }
212     }
213 
214 }