1 package org.apache.fulcrum.pool;
2
3 import java.util.HashMap;
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import java.util.Map;
25
26 import org.apache.avalon.framework.activity.Disposable;
27 import org.apache.avalon.framework.activity.Initializable;
28 import org.apache.avalon.framework.configuration.Configurable;
29 import org.apache.avalon.framework.configuration.Configuration;
30 import org.apache.avalon.framework.logger.AbstractLogEnabled;
31 import org.apache.avalon.framework.service.ServiceManager;
32 import org.apache.avalon.framework.service.Serviceable;
33 import org.apache.fulcrum.factory.FactoryException;
34 import org.apache.fulcrum.factory.FactoryService;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class DefaultPoolService extends AbstractLogEnabled
54 implements PoolService, Serviceable, Disposable, Initializable, Configurable {
55
56
57
58 public static final String POOL_CAPACITY = "capacity";
59
60
61
62
63 private int poolCapacity = DEFAULT_POOL_CAPACITY;
64
65
66
67
68 private HashMap<String, PoolBuffer> poolRepository = new HashMap<>();
69 private Map<String, Integer> capacityMap;
70 private FactoryService factoryService;
71 private ServiceManager manager;
72
73
74
75
76
77
78
79
80
81 public <T> T getInstance(String className) throws PoolException
82 {
83 try
84 {
85 T instance = pollInstance(className, null, null);
86 return instance == null ? getFactory().getInstance(className) : instance;
87 }
88 catch (FactoryException fe)
89 {
90 throw new PoolException(fe);
91 }
92 }
93
94
95
96
97
98
99
100
101
102
103
104 public Object getInstance(String className, ClassLoader loader) throws PoolException
105 {
106 try
107 {
108 Object instance = pollInstance(className, null, null);
109 return instance == null ? getFactory().getInstance(className, loader) : instance;
110 }
111 catch (FactoryException fe)
112 {
113 throw new PoolException(fe);
114 }
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128
129 public Object getInstance(String className, Object[] params, String[] signature) throws PoolException
130 {
131 try
132 {
133 Object instance = pollInstance(className, params, signature);
134 return instance == null ? getFactory().getInstance(className, params, signature) : instance;
135 }
136 catch (FactoryException fe)
137 {
138 throw new PoolException(fe);
139 }
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 public Object getInstance(String className, ClassLoader loader, Object[] params, String[] signature)
157 throws PoolException
158 {
159 try
160 {
161 Object instance = pollInstance(className, params, signature);
162 return instance == null ? getFactory().getInstance(className, loader, params, signature) : instance;
163 }
164 catch (FactoryException fe)
165 {
166 throw new PoolException(fe);
167 }
168 }
169
170
171
172
173
174
175
176
177 public boolean isLoaderSupported(String className) throws FactoryException
178 {
179 return getFactory().isLoaderSupported(className);
180 }
181
182
183
184
185
186
187
188
189
190
191 @SuppressWarnings("unchecked")
192 public <T> T getInstance(Class<?> clazz) throws PoolException
193 {
194 try
195 {
196 T instance = pollInstance(clazz.getName(), null, null);
197 return instance == null ? (T) factoryService.getInstance(clazz) : instance;
198 }
199 catch (FactoryException fe)
200 {
201 throw new PoolException(fe);
202 }
203 }
204
205
206
207
208
209
210
211
212
213
214
215 public <T> T getInstance(Class<?> clazz, Object params[], String signature[]) throws PoolException
216 {
217 try
218 {
219 T instance = pollInstance(clazz.getName(), params, signature);
220
221
222
223 return instance == null ? getFactory().getInstance(clazz.toString(), params, signature) : instance;
224
225 }
226 catch (FactoryException fe)
227 {
228 throw new PoolException(fe);
229 }
230 }
231
232
233
234
235
236
237
238
239
240 @SuppressWarnings("unchecked")
241 public boolean putInstance(Object instance)
242 {
243 if (instance != null)
244 {
245 HashMap<String, PoolBuffer> repository = poolRepository;
246 String className = instance.getClass().getName();
247 PoolBuffer pool = (PoolBuffer) repository.get(className);
248 if (pool == null)
249 {
250 pool = new PoolBuffer(getCapacity(className));
251 repository = (HashMap<String, PoolBuffer>) repository.clone();
252 repository.put(className, pool);
253 poolRepository = repository;
254 if (instance instanceof ArrayCtorRecyclable)
255 {
256 pool.setArrayCtorRecyclable(true);
257 }
258 }
259 return pool.offer(instance);
260 } else {
261 return false;
262 }
263 }
264
265
266
267
268
269
270 public int getCapacity(String className)
271 {
272 PoolBuffer pool = (PoolBuffer) poolRepository.get(className);
273 if (pool == null)
274 {
275
276 int capacity = poolCapacity;
277 if (capacityMap != null)
278 {
279 Integer cap = (Integer) capacityMap.get(className);
280 if (cap != null)
281 {
282 capacity = cap.intValue();
283 }
284 }
285 return capacity;
286 } else {
287 return pool.capacity();
288 }
289 }
290
291
292
293
294
295
296
297
298 @SuppressWarnings("unchecked")
299 public void setCapacity(String className, int capacity)
300 {
301 HashMap<String, PoolBuffer> repository = poolRepository;
302 repository = repository != null ? (HashMap<String, PoolBuffer>) repository.clone()
303 : new HashMap<String, PoolBuffer>();
304 repository.put(className, new PoolBuffer(capacity));
305 poolRepository = repository;
306 }
307
308
309
310
311
312
313 public int getSize(String className)
314 {
315 PoolBuffer pool = (PoolBuffer) poolRepository.get(className);
316 return pool != null ? pool.size() : 0;
317 }
318
319
320
321
322
323
324 @SuppressWarnings("unchecked")
325 public void clearPool(String className)
326 {
327 HashMap<String, PoolBuffer> repository = poolRepository;
328 if (repository.get(className) != null)
329 {
330 repository = (HashMap<String, PoolBuffer>) repository.clone();
331 repository.remove(className);
332 poolRepository = repository;
333 }
334 }
335
336
337
338
339 public void clearPool()
340 {
341 poolRepository = new HashMap<String, PoolBuffer>();
342 }
343
344
345
346
347
348
349
350
351
352
353 private <T> T pollInstance(String className, Object[] params, String[] signature) throws PoolException
354 {
355 PoolBuffer pool = (PoolBuffer) poolRepository.get(className);
356 return pool != null ? pool.poll(params, signature, factoryService) : null;
357 }
358
359
360
361
362
363
364 protected FactoryService getFactory()
365 {
366 return factoryService;
367 }
368
369
370
371
372
373
374 public void configure(Configuration conf)
375 {
376 final Configuration capacities = conf.getChild(POOL_CAPACITY, false);
377 if (capacities != null)
378 {
379 Configuration defaultConf = capacities.getChild("default");
380 int capacity = defaultConf.getValueAsInteger(DEFAULT_POOL_CAPACITY);
381 if (capacity <= 0)
382 {
383 throw new IllegalArgumentException("Capacity must be >0");
384 }
385 poolCapacity = capacity;
386 Configuration[] nameVal = capacities.getChildren();
387 for (int i = 0; i < nameVal.length; i++)
388 {
389 String key = nameVal[i].getName();
390 if (!"default".equals(key))
391 {
392 capacity = nameVal[i].getValueAsInteger(poolCapacity);
393 if (capacity < 0)
394 {
395 capacity = poolCapacity;
396 }
397
398 if (capacityMap == null)
399 {
400 capacityMap = new HashMap<String, Integer>();
401 }
402
403 capacityMap.put(key, capacity);
404 }
405 }
406 }
407 }
408
409
410
411
412
413
414
415
416 public void service(ServiceManager manager)
417 {
418 this.manager = manager;
419 }
420
421
422
423
424
425
426
427 public void initialize() throws Exception
428 {
429 try
430 {
431 factoryService = (FactoryService) manager.lookup(FactoryService.ROLE);
432 }
433 catch (Exception e)
434 {
435 throw new Exception("DefaultPoolService.initialize: Failed to get a Factory object", e);
436 }
437 }
438
439
440
441
442 public void dispose()
443 {
444 if (factoryService != null)
445 {
446 manager.release(factoryService);
447 }
448 factoryService = null;
449 manager = null;
450 }
451 }