KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > chatserver > ChatData


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 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 chatserver;
16
17 import org.quickserver.net.server.*;
18
19 import org.quickserver.util.pool.PoolableObject;
20 import org.apache.commons.pool.BasePoolableObjectFactory;
21 import org.apache.commons.pool.PoolableObjectFactory;
22
23 import java.util.*;
24 import java.io.*;
25 import java.nio.*;
26 import java.util.logging.*;
27
28 /**
29  *
30  * @author Akshathkumar Shetty
31  */

32 public class ChatData implements ClientData, ClientIdentifiable, PoolableObject {
33     private static final Logger logger =
34         Logger.getLogger(ChatData.class.getName());
35
36     private static Set usernameList = new HashSet();
37
38     private String JavaDoc username = null;
39     private String JavaDoc room = null;
40     private String JavaDoc info = null;
41
42     //for auth
43
private String JavaDoc lastAsked = null;
44     private byte password[] = null;
45
46     public void setLastAsked(String JavaDoc lastAsked) {
47         this.lastAsked = lastAsked;
48     }
49     public String JavaDoc getLastAsked() {
50         return lastAsked;
51     }
52
53     public void setPassword(byte[] password) {
54         this.password = password;
55     }
56     public byte[] getPassword() {
57         return password;
58     }
59
60     public boolean registerUsername(String JavaDoc username) {
61         return usernameList.add(username);
62     }
63     public void deregisterUsername(String JavaDoc username) {
64         usernameList.remove(username);
65     }
66     public void setUsername(String JavaDoc username) {
67         this.username = username;
68     }
69     public String JavaDoc getUsername() {
70         return username;
71     }
72
73     public void setRoom(String JavaDoc room) {
74         this.room = room;
75     }
76     public String JavaDoc getRoom() {
77         return room;
78     }
79
80     public String JavaDoc getClientId() {
81         return username;
82     }
83
84     public String JavaDoc getClientKey() {
85         if(room==null)
86             return username;
87         else
88             return username+"@"+room;
89     }
90
91     public void setClientInfo(String JavaDoc info) {
92         this.info = info;
93     }
94     public String JavaDoc getClientInfo() {
95         return getClientKey()+" - "+info;
96     }
97
98     public String JavaDoc toString() {
99         return getClientInfo();
100     }
101
102     //-- PoolableObject
103
public void clean() {
104         usernameList.remove(username);
105         username = null;
106         room = null;
107         info = null;
108         lastAsked = null;
109     }
110
111     public boolean isPoolable() {
112         return true;
113     }
114
115     public PoolableObjectFactory getPoolableObjectFactory() {
116         return new BasePoolableObjectFactory() {
117             public Object JavaDoc makeObject() {
118                 return new ChatData();
119             }
120             public void passivateObject(Object JavaDoc obj) {
121                 ChatData ed = (ChatData)obj;
122                 ed.clean();
123             }
124             public void destroyObject(Object JavaDoc obj) {
125                 if(obj==null) return;
126                 passivateObject(obj);
127                 obj = null;
128             }
129             public boolean validateObject(Object JavaDoc obj) {
130                 if(obj==null)
131                     return false;
132                 else
133                     return true;
134             }
135         };
136     }
137 }
138
139
140
Popular Tags