01: /*
02: * This file is part of the QuickServer library
03: * Copyright (C) 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;
16:
17: import org.quickserver.net.server.ClientHandler;
18: import org.quickserver.net.server.impl.*;
19: import org.apache.commons.pool.BasePoolableObjectFactory;
20:
21: /**
22: * A factory for creating {@link org.quickserver.net.server.ClientHandler}
23: * instances.
24: * @author Akshathkumar Shetty
25: * @since 1.3
26: */
27: public class ClientHandlerObjectFactory extends
28: BasePoolableObjectFactory {
29: private static int instanceCount = 0;
30: private int id = -1;
31: private boolean blocking = true;
32:
33: public ClientHandlerObjectFactory(boolean blocking) {
34: super ();
35: id = ++instanceCount;
36: this .blocking = blocking;
37: }
38:
39: //Creates an instance that can be returned by the pool.
40: public Object makeObject() {
41: if (blocking)
42: return new BlockingClientHandler(id);
43: else
44: return new NonBlockingClientHandler(id);
45: }
46:
47: //Uninitialize an instance to be returned to the pool.
48: public void passivateObject(Object obj) {
49: ClientHandler ch = (ClientHandler) obj;
50: ch.clean();
51: }
52:
53: //Reinitialize an instance to be returned by the pool.
54: public void activateObject(Object obj) {
55: }
56:
57: //Destroys an instance no longer needed by the pool.
58: public void destroyObject(Object obj) {
59: if (obj == null)
60: return;
61: passivateObject(obj);
62: obj = null;
63: }
64:
65: //Ensures that the instance is safe to be returned by the pool.
66: public boolean validateObject(Object obj) {
67: if (obj == null)
68: return false;
69:
70: BasicClientHandler ch = (BasicClientHandler) obj;
71: if (ch.getInstanceCount() == id)
72: return true;
73: else {
74: return false;
75: }
76: }
77: }
|