KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > pool > PoolManager


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.pool;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collections JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import org.jfox.ioc.ComponentContext;
16 import org.jfox.ioc.ext.ActiveComponent;
17
18 /**
19  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
20  */

21
22 public class PoolManager implements ActiveComponent{
23
24     private static Map JavaDoc pools = new HashMap JavaDoc();
25     private static Map JavaDoc indexablePools = new HashMap JavaDoc();
26
27     /**
28      * obtain a object pool , when not exists, will create one
29      *
30      * @param factory
31      * @return
32      */

33     public ObjectPool obtainPool(ObjectFactory factory) {
34         synchronized(pools) {
35             if(existsPool(factory.getClass())) {
36                 return (ObjectPool) pools.get(factory.getClass());
37             }
38             else {
39                 ObjectPool pool = new SimpleObjectPool(factory);
40                 synchronized(pools) {
41                     pools.put(factory.getClass(), pool);
42                 }
43                 return pool;
44             }
45         }
46     }
47
48     public boolean existsPool(Class JavaDoc factoryClass) {
49         synchronized(pools) {
50             return pools.containsKey(factoryClass);
51         }
52     }
53
54     /**
55      * obtain a object pool , when not exists, will create one
56      *
57      * @param factory
58      * @return
59      */

60     public IndexableObjectPool obtainIndexablePool(ObjectFactory factory) {
61         synchronized(indexablePools) {
62             if(existsPool(factory.getClass())) {
63                 return (IndexableObjectPool) indexablePools.get(factory.getClass());
64             }
65             else {
66                 IndexableObjectPool pool = new SimpleIndexableObjectPool(factory);
67                 synchronized(indexablePools) {
68                     indexablePools.put(factory.getClass(), pool);
69                 }
70                 return pool;
71             }
72         }
73     }
74
75     public boolean existsIndexablePool(Class JavaDoc factoryClass) {
76         synchronized(indexablePools) {
77             return indexablePools.containsKey(factoryClass);
78         }
79     }
80
81     public synchronized List JavaDoc pools() {
82         List JavaDoc allPools = new ArrayList JavaDoc(pools.size() + indexablePools.size());
83         allPools.addAll(pools.values());
84         allPools.addAll(indexablePools.values());
85         return Collections.unmodifiableList(allPools);
86     }
87
88     public void setComponentContext(ComponentContext ctx) {
89     }
90
91     public static void main(String JavaDoc[] args) {
92
93     }
94 }
95
Popular Tags