1 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 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 id) { 35 return findFirstClientById(id, 0); 36 } 37 38 private ClientHandler findFirstClientById(String 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 } } catch(ConcurrentModificationException e) { 49 if(callCount<MAX_TRY_COUNT) { 50 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 pattern) { 61 return findAllClientById(pattern, 0); 62 } 63 private Iterator findAllClientById(String 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 } } catch(ConcurrentModificationException e) { 78 if(callCount<MAX_TRY_COUNT) { 79 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 key) { 91 return findClientByKey(key, 0); 92 } 93 private ClientHandler findClientByKey(String 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 } } catch(ConcurrentModificationException e) { 104 if(callCount<MAX_TRY_COUNT) { 105 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 pattern) { 116 return findAllClientByKey(pattern, 0); 117 } 118 private Iterator findAllClientByKey(String 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 } } catch(ConcurrentModificationException e) { 134 if(callCount<MAX_TRY_COUNT) { 135 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 |