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: /**
11: *
12: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
13: */
14:
15: public class ObjectFactorySupport implements ObjectFactory {
16: protected Class classType = null;
17:
18: public ObjectFactorySupport(Class classType) throws Exception {
19: setObjectClass(classType);
20: }
21:
22: public ObjectFactorySupport(String className) throws Exception {
23: Class claz = Thread.currentThread().getContextClassLoader()
24: .loadClass(className);
25: setObjectClass(claz);
26: }
27:
28: public Class getObjectClass() {
29: return classType;
30: }
31:
32: public PoolableObject makeObject() throws Exception {
33: return (PoolableObject) classType.newInstance();
34: }
35:
36: /**
37: * destroy a poolabled object
38: * @param object
39: */
40: public void destroyObject(PoolableObject object) throws Exception {
41: // object = null;
42: }
43:
44: public boolean validateObject(PoolableObject object) {
45: return classType.isAssignableFrom(object.getClass());
46: }
47:
48: private void setObjectClass(Class classType) throws Exception {
49: if (!validateClassType(classType)) {
50: throw new Exception("class " + classType.getName()
51: + " not derived from "
52: + PoolableObject.class.getName());
53: }
54: this .classType = classType;
55: }
56:
57: /**
58: * validate the classType is instanceof Poolable object
59: * @param classType
60: * @return
61: */
62: private boolean validateClassType(Class classType) {
63: if (PoolableObject.class.isAssignableFrom(classType)) {
64: return true;
65: }
66: return false;
67: }
68:
69: public int hashCode() {
70: return classType.hashCode();
71: }
72:
73: public boolean equals(Object obj) {
74: if (!(obj instanceof ObjectFactorySupport))
75: return false;
76: return classType.equals(((ObjectFactorySupport) obj).classType);
77: }
78:
79: }
|