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