01: /* JFox, the OpenSource J2EE Application Server
02: *
03: * Copyright (C) 2002 huihoo.org
04: * Distributable under GNU LGPL license
05: * See the GNU Lesser General Public License for more details.
06: */
07:
08: package org.huihoo.jfox.pool;
09:
10: import java.util.HashMap;
11: import java.util.Map;
12: import java.util.Iterator;
13:
14: /**
15: *
16: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
17: */
18:
19: class PoolManager {
20:
21: private static Map pools = new HashMap();
22:
23: /**
24: * create a object pool with the poolable object factory
25: * @param factory
26: * @return
27: */
28: public static ObjectPool createPool(ObjectFactory factory) {
29: ObjectPool pool = new SimpleObjectPool(factory);
30: synchronized (pools) {
31: pools.put(factory.getClass(), pool);
32: }
33: return pool;
34: }
35:
36: /**
37: * obtain a object pool , when not exists, will create one
38: * @param factory
39: * @return
40: */
41: public static ObjectPool obtainPool(ObjectFactory factory) {
42: synchronized (pools) {
43: if (existsPool(factory.getClass())) {
44: return (ObjectPool) pools.get(factory.getClass());
45: } else {
46: return createPool(factory);
47: }
48: }
49: }
50:
51: public static boolean existsPool(Class factoryClass) {
52: synchronized (pools) {
53: return pools.containsKey(factoryClass);
54: }
55: }
56:
57: public static synchronized Iterator poolsIterator() {
58: return pools.values().iterator();
59: }
60:
61: public static void main(String[] args) {
62:
63: }
64: }
|