KickJava   Java API By Example, From Geeks To Geeks.

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


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

29 public class OptimisticClientIdentifier extends BasicClientIdentifier {
30     private static final Logger logger = Logger.getLogger(OptimisticClientIdentifier.class.getName());
31     private ClientIdentifier backupClientIdentifier;
32     private static final int MAX_TRY_COUNT = 4;
33
34     public ClientHandler findFirstClientById(String JavaDoc id) {
35         return findFirstClientById(id, 0);
36     }
37
38     private ClientHandler findFirstClientById(String JavaDoc id, int callCount) {
39         ClientHandler foundClientHandler = null;
40         try {
41             Iterator iterator = findAllClient();
42             while(iterator.hasNext()) {
43                 foundClientHandler = checkClientId(
44                     (ClientHandler) iterator.next(), id);
45
46                 if(foundClientHandler!=null) break;
47             }//endof while
48
} catch(ConcurrentModificationException e) {
49             if(callCount<MAX_TRY_COUNT) {
50                 //start over again.
51
foundClientHandler = findFirstClientById(id, ++callCount);
52             } else {
53                 logger.finest("Going for backup..");
54                 foundClientHandler = getBackupClientIdentifier().findFirstClientById(id);
55             }
56         }
57         return foundClientHandler;
58     }
59
60     public Iterator findAllClientById(String JavaDoc pattern) {
61         return findAllClientById(pattern, 0);
62     }
63     private Iterator findAllClientById(String JavaDoc pattern, int callCount) {
64         ArrayList list = new ArrayList();
65         Pattern p = Pattern.compile(pattern);
66         ClientHandler foundClientHandler = null;
67         
68         try {
69             Iterator iterator = findAllClient();
70             while(iterator.hasNext()) {
71                 foundClientHandler = checkClientId(
72                     (ClientHandler) iterator.next(), p);
73
74                 if(foundClientHandler!=null)
75                     list.add(foundClientHandler);
76             }//endof while
77
} catch(ConcurrentModificationException e) {
78             if(callCount<MAX_TRY_COUNT) {
79                 //start over again.
80
list = null;
81                 return findAllClientById(pattern, ++callCount);
82             } else {
83                 logger.finest("Going for backup..");
84                 return getBackupClientIdentifier().findAllClientById(pattern);
85             }
86         }
87         return list.iterator();
88     }
89
90     public ClientHandler findClientByKey(String JavaDoc key) {
91         return findClientByKey(key, 0);
92     }
93     private ClientHandler findClientByKey(String JavaDoc key, int callCount) {
94         ClientHandler foundClientHandler = null;
95         try {
96             Iterator iterator = findAllClient();
97             while(iterator.hasNext()) {
98                 foundClientHandler = checkClientKey(
99                     (ClientHandler) iterator.next(), key);
100
101                 if(foundClientHandler!=null) break;
102             }//endof while
103
} catch(ConcurrentModificationException e) {
104             if(callCount<MAX_TRY_COUNT) {
105                 //start over again.
106
foundClientHandler = findClientByKey(key, ++callCount);
107             } else {
108                 logger.finest("Going for backup..");
109                 foundClientHandler = getBackupClientIdentifier().findClientByKey(key);
110             }
111         }
112         return foundClientHandler;
113     }
114
115     public Iterator findAllClientByKey(String JavaDoc pattern) {
116         return findAllClientByKey(pattern, 0);
117     }
118     private Iterator findAllClientByKey(String JavaDoc pattern, int callCount) {
119         ArrayList list = new ArrayList();
120         Pattern p = Pattern.compile(pattern);
121         ClientHandler foundClientHandler = null;
122
123         try {
124             Iterator iterator = findAllClient();
125             while(iterator.hasNext()) {
126                 foundClientHandler = checkClientKey(
127                     (ClientHandler) iterator.next(), p);
128             
129                 if(foundClientHandler!=null)
130                     list.add(foundClientHandler);
131                 foundClientHandler = null;
132             }//endof while
133
} catch(ConcurrentModificationException e) {
134             if(callCount<MAX_TRY_COUNT) {
135                 //start over again.
136
list = null;
137                 return findAllClientByKey(pattern, ++callCount);
138             } else {
139                 logger.finest("Going for backup..");
140                 return getBackupClientIdentifier().findAllClientByKey(pattern);
141             }
142         }
143         return list.iterator();
144     }
145
146     private synchronized ClientIdentifier getBackupClientIdentifier() {
147         if(backupClientIdentifier==null) {
148             backupClientIdentifier = new SyncClientIdentifier();
149             backupClientIdentifier.setClientHandlerPool(clientHandlerPool);
150         }
151         return backupClientIdentifier;
152     }
153 }
154
Popular Tags