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