KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
10  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
11  */

12
13 public class SimpleObjectFactory implements ObjectFactory {
14     protected Class JavaDoc classType = null;
15
16     public SimpleObjectFactory(Class JavaDoc classType) throws Exception JavaDoc {
17         setObjectClass(classType);
18     }
19
20     public Class JavaDoc getObjectClass() {
21         return classType;
22     }
23
24     public PoolableObject makeObject() throws Exception JavaDoc {
25         return (PoolableObject) classType.newInstance();
26     }
27
28     /**
29      * destroy a poolabled object
30      *
31      * @param object
32      */

33     public void destroyObject(PoolableObject object) throws Exception JavaDoc {
34 // object = null;
35
}
36
37     public boolean validateObject(PoolableObject obj) {
38         return obj.isAvailable() && classType.isAssignableFrom(obj.getClass());
39     }
40
41     private void setObjectClass(Class JavaDoc classType) throws Exception JavaDoc {
42         if(!validateClassType(classType)) {
43             throw new Exception JavaDoc("class " + classType.getName() + " not derived from " + PoolableObject.class.getName());
44         }
45         this.classType = classType;
46     }
47
48     /**
49      * validate the classType is instanceof Poolable object
50      *
51      * @param classType
52      * @return
53      */

54     private boolean validateClassType(Class JavaDoc classType) {
55         if(PoolableObject.class.isAssignableFrom(classType)) {
56             return true;
57         }
58         return false;
59     }
60
61     public int hashCode() {
62         return classType.hashCode();
63     }
64
65     public boolean equals(Object JavaDoc obj) {
66         if(!(obj instanceof SimpleObjectFactory)) return false;
67         return classType.equals(((SimpleObjectFactory) obj).classType);
68     }
69
70 }
71
72
Popular Tags