KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > util > pool > ClientHandlerObjectFactory


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
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 BasePoolableObjectFactory {
28     private static int instanceCount = 0;
29     private int id = -1;
30     private boolean blocking = true;
31
32     public ClientHandlerObjectFactory(boolean blocking) {
33         super();
34         id = ++instanceCount;
35         this.blocking = blocking;
36     }
37
38     //Creates an instance that can be returned by the pool.
39
public Object JavaDoc makeObject() {
40         if(blocking)
41             return new BlockingClientHandler(id);
42         else
43             return new NonBlockingClientHandler(id);
44     }
45
46     //Uninitialize an instance to be returned to the pool.
47
public void passivateObject(Object JavaDoc obj) {
48         ClientHandler ch = (ClientHandler)obj;
49         ch.clean();
50     }
51
52     //Reinitialize an instance to be returned by the pool.
53
public void activateObject(Object JavaDoc obj) {
54     }
55     
56     //Destroys an instance no longer needed by the pool.
57
public void destroyObject(Object JavaDoc obj) {
58         if(obj==null) return;
59         passivateObject(obj);
60         obj = null;
61     }
62
63     //Ensures that the instance is safe to be returned by the pool.
64
public boolean validateObject(Object JavaDoc obj) {
65         if(obj==null)
66             return false;
67         
68         BasicClientHandler ch = (BasicClientHandler)obj;
69         if(ch.getInstanceCount()==id)
70             return true;
71         else {
72             return false;
73         }
74     }
75 }
76
Popular Tags