KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > net > server > impl > BasicClientIdentifier


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 org.quickserver.net.server.impl;
16
17 import java.io.*;
18 import java.net.*;
19 import java.util.*;
20 import java.util.logging.*;
21 import java.util.regex.*;
22 import org.quickserver.net.server.*;
23 import org.quickserver.util.pool.*;
24
25 /**
26  * Client Identifier interface.
27  * @author Akshathkumar Shetty
28  * @since 1.4.5
29  */

30 public abstract class BasicClientIdentifier implements ClientIdentifier{
31     private static final Logger logger = Logger.getLogger(BasicClientIdentifier.class.getName());
32
33     protected QSObjectPool clientHandlerPool;
34     protected QuickServer quickserver;
35
36     public void setQuickServer(QuickServer quickserver) {
37         this.quickserver = quickserver;
38     }
39
40     public void setClientHandlerPool(QSObjectPool clientHandlerPool) {
41         this.clientHandlerPool = clientHandlerPool;
42     }
43
44     public Object JavaDoc getObjectToSynchronize() {
45         return clientHandlerPool.getObjectToSynchronize();
46     }
47
48     public Iterator findAllClient() {
49         return clientHandlerPool.getAllActiveObjects();
50     }
51
52     protected ClientIdentifiable getClientIdentifiable(
53             ClientHandler foundClientHandler) {
54         if(foundClientHandler==null) return null;
55         if(foundClientHandler.isOpen()==false) return null;
56         ClientData foundClientData = null;
57
58         foundClientData = foundClientHandler.getClientData();
59         if(foundClientData==null)
60             throw new IllegalStateException JavaDoc("No ClientData was set! Can't find a client with out it.");
61         if(ClientIdentifiable.class.isInstance(foundClientData)==false)
62             throw new IllegalStateException JavaDoc("ClientData does not implement ClientIdentifiable! Can't find a client with out it.");
63         return (ClientIdentifiable) foundClientData;
64     }
65
66     protected ClientHandler checkClientId(ClientHandler foundClientHandler,
67             String JavaDoc id) {
68         ClientIdentifiable data = getClientIdentifiable(foundClientHandler);
69         if(data==null) return null;
70
71         String JavaDoc foundId = data.getClientId();
72         //logger.finest("Found id: "+foundId+", id: "+id);
73
if(foundId==null) {
74             //throw new NullPointerException("Id returned by ClientData was null!");
75
logger.finest("Id returned by ClientData was null! Client may not yet ready.. skipping");
76             return null;
77         }
78         if(foundId.equals(id)==false)
79             foundClientHandler = null;
80         return foundClientHandler;
81     }
82
83     protected ClientHandler checkClientId(ClientHandler foundClientHandler,
84             Pattern pattern) {
85         ClientIdentifiable data = getClientIdentifiable(foundClientHandler);
86         if(data==null) return null;
87
88         String JavaDoc foundId = data.getClientId();
89         //logger.finest("Found id: "+foundId+", pattern: "+pattern);
90
if(foundId==null) {
91             //throw new NullPointerException("Id returned by ClientData was null!");
92
logger.finest("Id returned by ClientData was null! Client may not yet ready.. skipping");
93             return null;
94         }
95         Matcher m = pattern.matcher(foundId);
96         if(m.matches()==false)
97             foundClientHandler = null;
98         return foundClientHandler;
99     }
100
101     protected ClientHandler checkClientKey(ClientHandler foundClientHandler,
102             String JavaDoc key) {
103         ClientIdentifiable data = getClientIdentifiable(foundClientHandler);
104         if(data==null) return null;
105
106         String JavaDoc foundKey = data.getClientKey();
107         //logger.finest("Found key: "+foundKey+", key: "+key);
108
if(foundKey==null) {
109             //throw new NullPointerException("Key returned by ClientData was null!");
110
logger.finest("Key returned by ClientData was null! Client may not yet ready.. skipping");
111             return null;
112         }
113         if(foundKey.equals(key)==false)
114             foundClientHandler = null;
115         return foundClientHandler;
116     }
117
118     protected ClientHandler checkClientKey(ClientHandler foundClientHandler,
119             Pattern pattern) {
120         ClientIdentifiable data = getClientIdentifiable(foundClientHandler);
121         if(data==null) return null;
122
123         String JavaDoc foundKey = data.getClientKey();
124         //logger.finest("Found key: "+foundKey+", pattern: "+pattern);
125
if(foundKey==null) {
126             //throw new NullPointerException("Key returned by ClientData was null!");
127
logger.finest("Key returned by ClientData was null! Client may not yet ready.. skipping");
128             return null;
129         }
130         Matcher m = pattern.matcher(foundKey);
131         if(m.matches()==false)
132             foundClientHandler = null;
133         return foundClientHandler;
134     }
135
136 }
137
Popular Tags