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.util.*;
019: import java.util.logging.*;
020: import org.quickserver.util.pool.*;
021: import org.quickserver.net.server.*;
022: import java.util.regex.*;
023:
024: /**
025: * Optimistic Client Identifier implementation.
026: * @author Akshathkumar Shetty
027: * @since 1.4.5
028: */
029: public class OptimisticClientIdentifier extends BasicClientIdentifier {
030: private static final Logger logger = Logger
031: .getLogger(OptimisticClientIdentifier.class.getName());
032: private ClientIdentifier backupClientIdentifier;
033: private static final int MAX_TRY_COUNT = 4;
034:
035: public ClientHandler findFirstClientById(String id) {
036: return findFirstClientById(id, 0);
037: }
038:
039: private ClientHandler findFirstClientById(String id, int callCount) {
040: ClientHandler foundClientHandler = null;
041: try {
042: Iterator iterator = findAllClient();
043: while (iterator.hasNext()) {
044: foundClientHandler = checkClientId(
045: (ClientHandler) iterator.next(), id);
046:
047: if (foundClientHandler != null)
048: break;
049: }//endof while
050: } catch (ConcurrentModificationException e) {
051: if (callCount < MAX_TRY_COUNT) {
052: //start over again.
053: foundClientHandler = findFirstClientById(id,
054: ++callCount);
055: } else {
056: logger.finest("Going for backup..");
057: foundClientHandler = getBackupClientIdentifier()
058: .findFirstClientById(id);
059: }
060: }
061: return foundClientHandler;
062: }
063:
064: public Iterator findAllClientById(String pattern) {
065: return findAllClientById(pattern, 0);
066: }
067:
068: private Iterator findAllClientById(String pattern, int callCount) {
069: ArrayList list = new ArrayList();
070: Pattern p = Pattern.compile(pattern);
071: ClientHandler foundClientHandler = null;
072:
073: try {
074: Iterator iterator = findAllClient();
075: while (iterator.hasNext()) {
076: foundClientHandler = checkClientId(
077: (ClientHandler) iterator.next(), p);
078:
079: if (foundClientHandler != null)
080: list.add(foundClientHandler);
081: }//endof while
082: } catch (ConcurrentModificationException e) {
083: if (callCount < MAX_TRY_COUNT) {
084: //start over again.
085: list = null;
086: return findAllClientById(pattern, ++callCount);
087: } else {
088: logger.finest("Going for backup..");
089: return getBackupClientIdentifier().findAllClientById(
090: pattern);
091: }
092: }
093: return list.iterator();
094: }
095:
096: public ClientHandler findClientByKey(String key) {
097: return findClientByKey(key, 0);
098: }
099:
100: private ClientHandler findClientByKey(String key, int callCount) {
101: ClientHandler foundClientHandler = null;
102: try {
103: Iterator iterator = findAllClient();
104: while (iterator.hasNext()) {
105: foundClientHandler = checkClientKey(
106: (ClientHandler) iterator.next(), key);
107:
108: if (foundClientHandler != null)
109: break;
110: }//endof while
111: } catch (ConcurrentModificationException e) {
112: if (callCount < MAX_TRY_COUNT) {
113: //start over again.
114: foundClientHandler = findClientByKey(key, ++callCount);
115: } else {
116: logger.finest("Going for backup..");
117: foundClientHandler = getBackupClientIdentifier()
118: .findClientByKey(key);
119: }
120: }
121: return foundClientHandler;
122: }
123:
124: public Iterator findAllClientByKey(String pattern) {
125: return findAllClientByKey(pattern, 0);
126: }
127:
128: private Iterator findAllClientByKey(String pattern, int callCount) {
129: ArrayList list = new ArrayList();
130: Pattern p = Pattern.compile(pattern);
131: ClientHandler foundClientHandler = null;
132:
133: try {
134: Iterator iterator = findAllClient();
135: while (iterator.hasNext()) {
136: foundClientHandler = checkClientKey(
137: (ClientHandler) iterator.next(), p);
138:
139: if (foundClientHandler != null)
140: list.add(foundClientHandler);
141: foundClientHandler = null;
142: }//endof while
143: } catch (ConcurrentModificationException e) {
144: if (callCount < MAX_TRY_COUNT) {
145: //start over again.
146: list = null;
147: return findAllClientByKey(pattern, ++callCount);
148: } else {
149: logger.finest("Going for backup..");
150: return getBackupClientIdentifier().findAllClientByKey(
151: pattern);
152: }
153: }
154: return list.iterator();
155: }
156:
157: private synchronized ClientIdentifier getBackupClientIdentifier() {
158: if (backupClientIdentifier == null) {
159: backupClientIdentifier = new SyncClientIdentifier();
160: backupClientIdentifier
161: .setClientHandlerPool(clientHandlerPool);
162: }
163: return backupClientIdentifier;
164: }
165: }
|