1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.util.pool; |
12 | |
|
13 | |
import org.mule.api.lifecycle.Disposable; |
14 | |
import org.mule.api.lifecycle.InitialisationException; |
15 | |
import org.mule.api.object.ObjectFactory; |
16 | |
import org.mule.config.PoolingProfile; |
17 | |
import org.mule.config.i18n.MessageFactory; |
18 | |
|
19 | |
import java.util.ArrayList; |
20 | |
import java.util.List; |
21 | |
|
22 | |
import org.apache.commons.logging.Log; |
23 | |
import org.apache.commons.logging.LogFactory; |
24 | |
import org.apache.commons.pool.PoolableObjectFactory; |
25 | |
import org.apache.commons.pool.impl.GenericObjectPool; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class CommonsPoolObjectPool implements ObjectPool |
33 | |
{ |
34 | |
|
35 | |
|
36 | |
|
37 | 2 | protected static final Log logger = LogFactory.getLog(CommonsPoolObjectPool.class); |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
protected GenericObjectPool pool; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
protected ObjectFactory objectFactory; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
protected PoolingProfile poolingProfile; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public CommonsPoolObjectPool(ObjectFactory objectFactory, PoolingProfile poolingProfile) |
61 | 26 | { |
62 | 26 | this.objectFactory = objectFactory; |
63 | 26 | this.poolingProfile = poolingProfile; |
64 | 26 | } |
65 | |
|
66 | |
public void initialise() throws InitialisationException |
67 | |
{ |
68 | 26 | GenericObjectPool.Config config = new GenericObjectPool.Config(); |
69 | |
|
70 | 26 | if (poolingProfile != null) |
71 | |
{ |
72 | 26 | config.maxIdle = poolingProfile.getMaxIdle(); |
73 | 26 | config.maxActive = poolingProfile.getMaxActive(); |
74 | 26 | config.maxWait = poolingProfile.getMaxWait(); |
75 | 26 | config.whenExhaustedAction = (byte) poolingProfile.getExhaustedAction(); |
76 | |
} |
77 | |
|
78 | 26 | pool = new GenericObjectPool(getPooledObjectFactory(), config); |
79 | |
|
80 | |
try |
81 | |
{ |
82 | 26 | applyInitialisationPolicy(); |
83 | |
} |
84 | 0 | catch (Exception e) |
85 | |
{ |
86 | 0 | throw new InitialisationException(e, this); |
87 | 26 | } |
88 | 26 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
protected PoolableObjectFactory getPooledObjectFactory() |
97 | |
{ |
98 | 0 | return new PoolabeObjectFactoryAdaptor(); |
99 | |
} |
100 | |
|
101 | |
protected void applyInitialisationPolicy() throws Exception |
102 | |
{ |
103 | 26 | if (poolingProfile != null) |
104 | |
{ |
105 | 26 | int numToBorrow = 0; |
106 | 26 | int initPolicy = poolingProfile.getInitialisationPolicy(); |
107 | |
|
108 | 26 | if (initPolicy == PoolingProfile.INITIALISE_ALL) |
109 | |
{ |
110 | 0 | numToBorrow = poolingProfile.getMaxActive(); |
111 | |
} |
112 | 26 | else if (initPolicy == PoolingProfile.INITIALISE_ONE) |
113 | |
{ |
114 | 0 | numToBorrow = 1; |
115 | |
} |
116 | |
|
117 | 26 | List holderList = new ArrayList(numToBorrow); |
118 | |
try |
119 | |
{ |
120 | 26 | for (int t = 0; t < numToBorrow; t++) |
121 | |
{ |
122 | 0 | holderList.add(objectFactory.getInstance()); |
123 | |
} |
124 | |
} |
125 | |
finally |
126 | |
{ |
127 | 26 | for (int t = 0; t < holderList.size(); t++) |
128 | |
{ |
129 | 0 | Object obj = holderList.get(t); |
130 | 0 | if (obj != null) |
131 | |
{ |
132 | 0 | this.returnObject(obj); |
133 | |
} |
134 | |
} |
135 | 26 | } |
136 | |
} |
137 | 26 | } |
138 | |
|
139 | |
public Object borrowObject() throws Exception |
140 | |
{ |
141 | 56 | if (pool != null) |
142 | |
{ |
143 | 56 | return pool.borrowObject(); |
144 | |
} |
145 | |
else |
146 | |
{ |
147 | 0 | throw new InitialisationException( |
148 | |
MessageFactory.createStaticMessage("Object pool has not been initialized."), this); |
149 | |
} |
150 | |
} |
151 | |
|
152 | |
public void returnObject(Object object) |
153 | |
{ |
154 | 8 | if (pool != null) |
155 | |
{ |
156 | |
try |
157 | |
{ |
158 | 8 | pool.returnObject(object); |
159 | |
} |
160 | 0 | catch (Exception ex) |
161 | |
{ |
162 | |
|
163 | |
|
164 | |
|
165 | 8 | } |
166 | |
} |
167 | 8 | } |
168 | |
|
169 | |
public int getNumActive() |
170 | |
{ |
171 | 34 | return pool.getNumActive(); |
172 | |
} |
173 | |
|
174 | |
public int getMaxActive() |
175 | |
{ |
176 | 0 | return pool.getMaxActive(); |
177 | |
} |
178 | |
|
179 | |
public void dispose() |
180 | |
{ |
181 | 0 | if (pool != null) |
182 | |
{ |
183 | |
try |
184 | |
{ |
185 | 0 | pool.close(); |
186 | |
} |
187 | 0 | catch (Exception e) |
188 | |
{ |
189 | |
|
190 | |
} |
191 | |
finally |
192 | |
{ |
193 | 0 | pool = null; |
194 | 0 | } |
195 | |
} |
196 | 0 | } |
197 | |
|
198 | |
public void clear() |
199 | |
{ |
200 | 0 | if (pool != null) |
201 | |
{ |
202 | 0 | pool.clear(); |
203 | |
} |
204 | |
|
205 | 0 | } |
206 | |
|
207 | |
public void close() |
208 | |
{ |
209 | 8 | if (pool != null) |
210 | |
{ |
211 | |
try |
212 | |
{ |
213 | 8 | pool.close(); |
214 | |
} |
215 | 0 | catch (Exception e) |
216 | |
{ |
217 | |
|
218 | |
} |
219 | |
finally |
220 | |
{ |
221 | 8 | pool = null; |
222 | 8 | } |
223 | |
} |
224 | |
|
225 | 8 | } |
226 | |
|
227 | |
public void setObjectFactory(ObjectFactory objectFactory) |
228 | |
{ |
229 | 0 | this.objectFactory = objectFactory; |
230 | 0 | } |
231 | |
|
232 | |
public ObjectFactory getObjectFactory() |
233 | |
{ |
234 | 2 | return objectFactory; |
235 | |
} |
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | 0 | class PoolabeObjectFactoryAdaptor implements PoolableObjectFactory |
241 | |
{ |
242 | |
|
243 | |
public void activateObject(Object obj) throws Exception |
244 | |
{ |
245 | |
|
246 | 0 | } |
247 | |
|
248 | |
public void destroyObject(Object obj) throws Exception |
249 | |
{ |
250 | 0 | if (obj instanceof Disposable) |
251 | |
{ |
252 | 0 | ((Disposable) obj).dispose(); |
253 | |
} |
254 | 0 | } |
255 | |
|
256 | |
public Object makeObject() throws Exception |
257 | |
{ |
258 | 0 | return objectFactory.getInstance(); |
259 | |
} |
260 | |
|
261 | |
public void passivateObject(Object obj) throws Exception |
262 | |
{ |
263 | |
|
264 | 0 | } |
265 | |
|
266 | |
public boolean validateObject(Object obj) |
267 | |
{ |
268 | 0 | return true; |
269 | |
} |
270 | |
|
271 | |
} |
272 | |
|
273 | |
} |