KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > config > pool > CommonsPoolProxyPool


1 /*
2  * $Id: CommonsPoolProxyPool.java 4259 2006-12-14 03:12:07Z aperepel $
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 MuleSource MPL
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.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.apache.commons.pool.PoolableObjectFactory;
16 import org.apache.commons.pool.impl.GenericObjectPool;
17 import org.mule.impl.MuleDescriptor;
18 import org.mule.umo.UMOException;
19 import org.mule.umo.lifecycle.Disposable;
20 import org.mule.umo.lifecycle.Startable;
21 import org.mule.umo.lifecycle.Stoppable;
22 import org.mule.util.ObjectFactory;
23 import org.mule.util.ObjectPool;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * <code>CommonsPoolProxyPool</code> is pool used to store MuleProxy objects. This
31  * pool is a jakarta commons-pool implementation.
32  */

33 public class CommonsPoolProxyPool implements ObjectPool
34 {
35     /**
36      * logger used by this class
37      */

38     protected static final Log logger = LogFactory.getLog(CommonsPoolProxyPool.class);
39
40     /**
41      * The pool that holds the MuleProxy objects
42      */

43     protected GenericObjectPool pool;
44
45     /**
46      * the factory used to create objects for the pool
47      */

48     protected ObjectFactory factory;
49
50     private List JavaDoc components;
51
52     /**
53      * Creates a new pool and an Object factory with the UMODescriptor
54      *
55      * @param descriptor the descriptor to use when constructing MuleProxy objects in
56      * the pool
57      */

58     public CommonsPoolProxyPool(MuleDescriptor descriptor, ObjectFactory factory)
59     {
60         this.factory = factory;
61
62         GenericObjectPool.Config config = new GenericObjectPool.Config();
63         config.maxIdle = descriptor.getPoolingProfile().getMaxIdle();
64         config.maxActive = descriptor.getPoolingProfile().getMaxActive();
65         config.maxWait = descriptor.getPoolingProfile().getMaxWait();
66         config.whenExhaustedAction = (byte)descriptor.getPoolingProfile().getExhaustedAction();
67
68         init(descriptor, config);
69     }
70
71     /**
72      * @param descriptor the UMO descriptor to pool
73      * @param config the config to use when configuring the pool
74      */

75     public CommonsPoolProxyPool(MuleDescriptor descriptor, GenericObjectPool.Config config)
76     {
77         init(descriptor, config);
78     }
79
80     /**
81      * @param descriptor the UMO descriptor to pool
82      * @param config the config to use when configuring the pool
83      */

84     private void init(MuleDescriptor descriptor, GenericObjectPool.Config config)
85     {
86         components = new ArrayList JavaDoc();
87
88         if (factory == null)
89         {
90             setFactory(new CommonsPoolProxyFactory(descriptor));
91         }
92
93         pool = new GenericObjectPool((PoolableObjectFactory)factory, config);
94
95         if (factory instanceof CommonsPoolProxyFactory)
96         {
97             ((CommonsPoolProxyFactory)factory).setPool(this);
98         }
99     }
100
101     /*
102      * (non-Javadoc)
103      *
104      * @see org.mule.model.pool.ObjectPool#borrowObject()
105      */

106     public Object JavaDoc borrowObject() throws Exception JavaDoc
107     {
108         return pool.borrowObject();
109     }
110
111     /*
112      * (non-Javadoc)
113      *
114      * @see org.mule.model.pool.ObjectPool#returnObject(java.lang.Object)
115      */

116     public void returnObject(Object JavaDoc object) throws Exception JavaDoc
117     {
118         pool.returnObject(object);
119     }
120
121     /*
122      * (non-Javadoc)
123      *
124      * @see org.mule.model.pool.ObjectPool#getSize()
125      */

126     public int getSize()
127     {
128         return pool.getNumActive();
129     }
130
131     /*
132      * (non-Javadoc)
133      *
134      * @see org.mule.model.pool.ObjectPool#getMaxSize()
135      */

136     public int getMaxSize()
137     {
138         return pool.getMaxActive();
139     }
140
141     /*
142      * (non-Javadoc)
143      *
144      * @see org.mule.model.pool.ObjectPool#setFactory(org.mule.model.pool.ProxyFactory)
145      */

146     public void setFactory(ObjectFactory factory)
147     {
148         this.factory = factory;
149     }
150
151     /*
152      * (non-Javadoc)
153      *
154      * @see org.mule.model.pool.ObjectPool#clearPool()
155      */

156     public void clearPool()
157     {
158         synchronized (components)
159         {
160             for (Iterator JavaDoc i = components.iterator(); i.hasNext();)
161             {
162                 ((Disposable)i.next()).dispose();
163             }
164             components.clear();
165         }
166         pool.clear();
167     }
168
169     public void onAdd(Object JavaDoc proxy)
170     {
171         synchronized (components)
172         {
173             components.add(proxy);
174         }
175     }
176
177     public void onRemove(Object JavaDoc proxy)
178     {
179         synchronized (components)
180         {
181             final boolean wasRemoved = components.remove(proxy);
182             if (wasRemoved)
183             {
184                 ((Disposable)proxy).dispose();
185             }
186         }
187     }
188
189     public void start() throws UMOException
190     {
191         synchronized (components)
192         {
193             for (Iterator JavaDoc i = components.iterator(); i.hasNext();)
194             {
195                 ((Startable)i.next()).start();
196             }
197         }
198     }
199
200     public void stop() throws UMOException
201     {
202         synchronized (components)
203         {
204             for (Iterator JavaDoc i = components.iterator(); i.hasNext();)
205             {
206                 ((Stoppable)i.next()).stop();
207             }
208         }
209     }
210
211 }
212
Popular Tags