1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
34 | |
|
35 | |
|
36 | |
public class CommonsPoolProxyPool implements ObjectPool |
37 | |
{ |
38 | |
|
39 | |
|
40 | |
|
41 | 0 | protected static final Log logger = LogFactory.getLog(CommonsPoolProxyPool.class); |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
protected GenericObjectPool pool; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
protected ObjectFactory factory; |
52 | |
|
53 | |
private List components; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public CommonsPoolProxyPool(MuleDescriptor descriptor, UMOModel model, ObjectFactory factory, PoolingProfile pp) |
62 | 0 | { |
63 | 0 | this.factory = factory; |
64 | |
|
65 | 0 | GenericObjectPool.Config config = new GenericObjectPool.Config(); |
66 | 0 | config.maxIdle = pp.getMaxIdle(); |
67 | 0 | config.maxActive = pp.getMaxActive(); |
68 | 0 | config.maxWait = pp.getMaxWait(); |
69 | 0 | config.whenExhaustedAction = (byte) pp.getExhaustedAction(); |
70 | |
|
71 | 0 | init(descriptor, model, config); |
72 | 0 | } |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public CommonsPoolProxyPool(MuleDescriptor descriptor, UMOModel model, GenericObjectPool.Config config) |
79 | 0 | { |
80 | 0 | init(descriptor, model, config); |
81 | 0 | } |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
private void init(MuleDescriptor descriptor, UMOModel model, GenericObjectPool.Config config) |
88 | |
{ |
89 | 0 | components = new ArrayList(); |
90 | |
|
91 | 0 | if (factory == null) |
92 | |
{ |
93 | 0 | setFactory(new CommonsPoolProxyFactory(descriptor, model)); |
94 | |
} |
95 | |
|
96 | 0 | pool = new GenericObjectPool((PoolableObjectFactory) factory, config); |
97 | |
|
98 | 0 | if (factory instanceof CommonsPoolProxyFactory) |
99 | |
{ |
100 | 0 | ((CommonsPoolProxyFactory) factory).setPool(this); |
101 | |
} |
102 | 0 | } |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
public Object borrowObject() throws Exception |
110 | |
{ |
111 | 0 | return pool.borrowObject(); |
112 | |
} |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
public void returnObject(Object object) throws Exception |
120 | |
{ |
121 | 0 | pool.returnObject(object); |
122 | 0 | } |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public int getSize() |
130 | |
{ |
131 | 0 | return pool.getNumActive(); |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public int getMaxSize() |
140 | |
{ |
141 | 0 | return pool.getMaxActive(); |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
public void setFactory(ObjectFactory factory) |
150 | |
{ |
151 | 0 | this.factory = factory; |
152 | 0 | } |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
public void clearPool() |
160 | |
{ |
161 | 0 | synchronized (components) |
162 | |
{ |
163 | 0 | for (Iterator i = components.iterator(); i.hasNext();) |
164 | |
{ |
165 | 0 | ((Disposable) i.next()).dispose(); |
166 | |
} |
167 | 0 | components.clear(); |
168 | 0 | } |
169 | 0 | pool.clear(); |
170 | 0 | } |
171 | |
|
172 | |
public void onAdd(Object proxy) |
173 | |
{ |
174 | 0 | synchronized (components) |
175 | |
{ |
176 | 0 | components.add(proxy); |
177 | 0 | } |
178 | 0 | } |
179 | |
|
180 | |
public void onRemove(Object proxy) |
181 | |
{ |
182 | 0 | synchronized (components) |
183 | |
{ |
184 | 0 | final boolean wasRemoved = components.remove(proxy); |
185 | 0 | if (wasRemoved) |
186 | |
{ |
187 | 0 | ((Disposable) proxy).dispose(); |
188 | |
} |
189 | 0 | } |
190 | 0 | } |
191 | |
|
192 | |
public void start() throws UMOException |
193 | |
{ |
194 | 0 | synchronized (components) |
195 | |
{ |
196 | 0 | for (Iterator i = components.iterator(); i.hasNext();) |
197 | |
{ |
198 | 0 | ((Startable) i.next()).start(); |
199 | |
} |
200 | 0 | } |
201 | 0 | } |
202 | |
|
203 | |
public void stop() throws UMOException |
204 | |
{ |
205 | 0 | synchronized (components) |
206 | |
{ |
207 | 0 | for (Iterator i = components.iterator(); i.hasNext();) |
208 | |
{ |
209 | 0 | ((Stoppable) i.next()).stop(); |
210 | |
} |
211 | 0 | } |
212 | 0 | } |
213 | |
|
214 | |
} |