KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ejb > StatelessObjectPool


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.ejb;
8
9 import org.jfox.ioc.common.AbstractComponent;
10 import org.jfox.pool.AbstractObjectPool;
11 import org.jfox.pool.ObjectFactory;
12 import org.jfox.pool.ObjectPool;
13 import org.jfox.pool.PoolableObject;
14 import org.jfox.pool.SimpleObjectPool;
15
16 /**
17  * 用来存储 StatelessSessionBean 的对象池,该池的后端使用一个 SimpleObjectPool
18  *
19  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
20  */

21
22 public class StatelessObjectPool extends AbstractComponent implements ObjectPool, EJBObjectPool {
23     private AbstractObjectPool pool = null;
24     private EJBObjectFactory factory = null;
25
26
27     public StatelessObjectPool() {
28
29     }
30
31     public void setEJBObjectFactory(EJBObjectFactory factory) {
32         if(factory == null) throw new NullPointerException JavaDoc("factory is null.");
33         this.factory = factory;
34     }
35
36     public synchronized PoolableObject createObject() throws Exception JavaDoc {
37         return retrieveObject();
38     }
39
40     public synchronized PoolableObject retrieveObject() throws Exception JavaDoc {
41         // not need call ejbCreate, bucket will call it after get instance from the pool
42
return pool.retrieveObject();
43     }
44
45     /**
46      * 如果数目过多,则返回给 pool,但是先得调用 ejbRemove,释放资源
47      *
48      * @param obj
49      * @return
50      */

51     public boolean restoreObject(PoolableObject obj) {
52         try {
53             return pool.restoreObject(obj);
54         }
55         catch(Exception JavaDoc e) {
56             logger.warn("restoreObject " + obj + " failed, exception is: " + e);
57             return false;
58         }
59     }
60
61     /**
62      * 如果池中对象小于 max, 存到池中,池满,则调用 ejbRemove,并返回上级池
63      *
64      * @param obj
65      * @return
66      */

67     public boolean removeObject(PoolableObject obj) {
68         try {
69             return pool.restoreObject(obj);
70         }
71         catch(Exception JavaDoc e) {
72             logger.warn("removeObject " + obj + " failed, exception is: " + e);
73             return false;
74         }
75     }
76
77     public void clear() {
78         pool.clear();
79     }
80
81     public ObjectFactory getObjectFactory() {
82         return factory;
83     }
84
85     public String JavaDoc getObjectClass() {
86         return factory.getObjectClass().getName();
87     }
88
89     public int getWorking() {
90         return pool.getWorking();
91     }
92
93     public int getRest() {
94         return pool.getRest();
95     }
96
97     protected void doInit() throws Exception JavaDoc {
98         if(factory == null) throw new NullPointerException JavaDoc("factory is null.");
99         pool = new SimpleObjectPool(factory, 0, 10);
100         pool.init();
101     }
102
103     protected void doDestroy() throws Exception JavaDoc {
104         pool.destroy();
105     }
106
107     public int getInitNum() {
108         return pool.getInitNum();
109     }
110
111     public int getMaxRest() {
112         return pool.getMaxRest();
113     }
114
115     public static void main(String JavaDoc[] args) {
116
117     }
118 }
Popular Tags