01: /*
02: * This file is part of the QuickServer library
03: * Copyright (C) 2003-2005 QuickServer.org
04: *
05: * Use, modification, copying and distribution of this software is subject to
06: * the terms and conditions of the GNU Lesser General Public License.
07: * You should have received a copy of the GNU LGP License along with this
08: * library; if not, you can download a copy from <http://www.quickserver.org/>.
09: *
10: * For questions, suggestions, bug-reports, enhancement-requests etc.
11: * visit http://www.quickserver.org
12: *
13: */
14:
15: package org.quickserver.util.pool.thread;
16:
17: import org.apache.commons.pool.BasePoolableObjectFactory;
18:
19: /**
20: * A factory for creating {@link org.quickserver.util.pool.thread.ClientThread}
21: * instances.
22: * @author Akshathkumar Shetty
23: * @since 1.3
24: */
25: public class ThreadObjectFactory extends BasePoolableObjectFactory {
26: private ClientPool pool;
27: private static int instanceCount = 0;
28: private int id = -1;
29:
30: public ThreadObjectFactory() {
31: super ();
32: id = ++instanceCount;
33: }
34:
35: public void setClientPool(ClientPool pool) {
36: this .pool = pool;
37: }
38:
39: //Creates an instance that can be returned by the pool.
40: public Object makeObject() {
41: ClientThread ct = new ClientThread(pool, id);
42: ct.start();
43: return ct;
44: }
45:
46: //Uninitialize an instance to be returned to the pool.
47: public void passivateObject(Object obj) {
48: ((ClientThread) obj).clean();
49: }
50:
51: //Reinitialize an instance to be returned by the pool.
52: public void activateObject(Object obj) {
53: }
54:
55: //Destroys an instance no longer needed by the pool.
56: public void destroyObject(Object obj) {
57: if (obj == null)
58: return;
59: Thread thread = (Thread) obj;
60: thread.interrupt();
61: thread = null;
62: }
63:
64: //Ensures that the instance is safe to be returned by the pool.
65: public boolean validateObject(Object obj) {
66: if (obj == null)
67: return false;
68: Thread thread = (Thread) obj;
69: return thread.isAlive();
70: }
71: }
|