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