001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package org.quickserver.net.server.impl;
016:
017: import java.io.*;
018: import java.net.*;
019: import java.util.*;
020: import java.util.logging.*;
021: import java.util.regex.*;
022: import org.quickserver.net.server.*;
023: import org.quickserver.util.pool.*;
024:
025: /**
026: * Client Identifier interface.
027: * @author Akshathkumar Shetty
028: * @since 1.4.5
029: */
030: public abstract class BasicClientIdentifier implements ClientIdentifier {
031: private static final Logger logger = Logger
032: .getLogger(BasicClientIdentifier.class.getName());
033:
034: protected QSObjectPool clientHandlerPool;
035: protected QuickServer quickserver;
036:
037: public void setQuickServer(QuickServer quickserver) {
038: this .quickserver = quickserver;
039: }
040:
041: public void setClientHandlerPool(QSObjectPool clientHandlerPool) {
042: this .clientHandlerPool = clientHandlerPool;
043: }
044:
045: public Object getObjectToSynchronize() {
046: return clientHandlerPool.getObjectToSynchronize();
047: }
048:
049: public Iterator findAllClient() {
050: return clientHandlerPool.getAllActiveObjects();
051: }
052:
053: protected ClientIdentifiable getClientIdentifiable(
054: ClientHandler foundClientHandler) {
055: if (foundClientHandler == null)
056: return null;
057: if (foundClientHandler.isOpen() == false)
058: return null;
059: ClientData foundClientData = null;
060:
061: foundClientData = foundClientHandler.getClientData();
062: if (foundClientData == null)
063: throw new IllegalStateException(
064: "No ClientData was set! Can't find a client with out it.");
065: if (ClientIdentifiable.class.isInstance(foundClientData) == false)
066: throw new IllegalStateException(
067: "ClientData does not implement ClientIdentifiable! Can't find a client with out it.");
068: return (ClientIdentifiable) foundClientData;
069: }
070:
071: protected ClientHandler checkClientId(
072: ClientHandler foundClientHandler, String id) {
073: ClientIdentifiable data = getClientIdentifiable(foundClientHandler);
074: if (data == null)
075: return null;
076:
077: String foundId = data.getClientId();
078: //logger.finest("Found id: "+foundId+", id: "+id);
079: if (foundId == null) {
080: //throw new NullPointerException("Id returned by ClientData was null!");
081: logger
082: .finest("Id returned by ClientData was null! Client may not yet ready.. skipping");
083: return null;
084: }
085: if (foundId.equals(id) == false)
086: foundClientHandler = null;
087: return foundClientHandler;
088: }
089:
090: protected ClientHandler checkClientId(
091: ClientHandler foundClientHandler, Pattern pattern) {
092: ClientIdentifiable data = getClientIdentifiable(foundClientHandler);
093: if (data == null)
094: return null;
095:
096: String foundId = data.getClientId();
097: //logger.finest("Found id: "+foundId+", pattern: "+pattern);
098: if (foundId == null) {
099: //throw new NullPointerException("Id returned by ClientData was null!");
100: logger
101: .finest("Id returned by ClientData was null! Client may not yet ready.. skipping");
102: return null;
103: }
104: Matcher m = pattern.matcher(foundId);
105: if (m.matches() == false)
106: foundClientHandler = null;
107: return foundClientHandler;
108: }
109:
110: protected ClientHandler checkClientKey(
111: ClientHandler foundClientHandler, String key) {
112: ClientIdentifiable data = getClientIdentifiable(foundClientHandler);
113: if (data == null)
114: return null;
115:
116: String foundKey = data.getClientKey();
117: //logger.finest("Found key: "+foundKey+", key: "+key);
118: if (foundKey == null) {
119: //throw new NullPointerException("Key returned by ClientData was null!");
120: logger
121: .finest("Key returned by ClientData was null! Client may not yet ready.. skipping");
122: return null;
123: }
124: if (foundKey.equals(key) == false)
125: foundClientHandler = null;
126: return foundClientHandler;
127: }
128:
129: protected ClientHandler checkClientKey(
130: ClientHandler foundClientHandler, Pattern pattern) {
131: ClientIdentifiable data = getClientIdentifiable(foundClientHandler);
132: if (data == null)
133: return null;
134:
135: String foundKey = data.getClientKey();
136: //logger.finest("Found key: "+foundKey+", pattern: "+pattern);
137: if (foundKey == null) {
138: //throw new NullPointerException("Key returned by ClientData was null!");
139: logger
140: .finest("Key returned by ClientData was null! Client may not yet ready.. skipping");
141: return null;
142: }
143: Matcher m = pattern.matcher(foundKey);
144: if (m.matches() == false)
145: foundClientHandler = null;
146: return foundClientHandler;
147: }
148:
149: }
|