1 18 package sync4j.framework.server; 19 20 import java.util.Map ; 21 import java.util.HashMap ; 22 import java.util.ArrayList ; 23 24 import sync4j.framework.logging.Sync4jLogger; 25 26 import java.util.logging.Logger ; 27 import java.util.logging.Level ; 28 29 import sync4j.framework.security.Sync4jPrincipal; 30 31 import org.apache.commons.lang.builder.ToStringBuilder; 32 33 39 public class ClientMapping { 40 41 private HashMap clientMapping = new HashMap (); 43 44 private Sync4jPrincipal principal = null; 46 47 private String dbURI; 49 50 private boolean modified = false; 52 private ArrayList modifiedKeys = new ArrayList (); 53 54 private boolean deleted = false; 56 private ArrayList deletedKeys = new ArrayList (); 57 58 private transient Logger log = Sync4jLogger.getLogger(); 61 62 64 70 public ClientMapping(Sync4jPrincipal principal, String dbURI) { 71 this.principal = principal; 72 this.dbURI = dbURI ; 73 } 74 75 77 80 public boolean isModified() { 81 return modified; 82 } 83 84 87 public boolean isDeleted() { 88 return deleted; 89 } 90 91 95 public void initializeFromMapping(Map mapping) { 96 resetMapping(); 97 clientMapping.putAll(mapping); 98 } 99 100 104 public String [] getDeletedLuids() { 105 if (deleted) { 106 return (String [])deletedKeys.toArray(new String [deletedKeys.size()]); 107 } 108 109 return new String [0]; 110 } 111 112 116 public String [] getModifiedLuids() { 117 if (modified) { 118 return (String [])modifiedKeys.toArray(new String [modifiedKeys.size()]); 119 } 120 121 return new String [0]; 122 } 123 124 129 public Map getMapping() { 130 Map ret = new HashMap (); 131 ret.putAll(clientMapping); 132 return ret; 133 } 134 135 139 public String getClientDeviceId() { 140 return principal.getDeviceId(); 141 } 142 143 148 public Sync4jPrincipal getPrincipal() { 149 return principal; 150 } 151 152 157 public String getDbURI() { 158 return dbURI; 159 } 160 161 166 private void addMapping(String guid, String luid) { 167 168 if (log.isLoggable(Level.FINEST)) { 169 log.finest("Adding mapping LUID-GUID " + luid + "-" + guid); 170 } 171 172 String oldLuid = (String )clientMapping.put(guid, luid); 177 modifiedKeys.add(luid); modified = true; 178 179 if (oldLuid != null) { 180 removeDeletedKey(oldLuid); 181 removeModifiedKey(oldLuid); 182 } 183 184 removeDeletedKey(luid); 189 } 190 191 196 public String getMappedValueForLuid(String luid) { 197 String result = null; 198 199 if (clientMapping.containsValue(luid)) { 200 java.util.Set keys = clientMapping.keySet(); 201 java.util.Iterator itKeys = keys.iterator(); 202 while (itKeys.hasNext()) { 203 String key = (String )itKeys.next(); 204 String value = (String )clientMapping.get(key); 205 if (value.equals(luid)) { 206 return key; 207 } 208 } 209 } 210 211 if (log.isLoggable(Level.FINEST)) { 212 log.finest("No mapping found for LUID: " + luid); 213 } 214 return result; 215 } 216 217 222 public String getMappedValueForGuid(String guid) { 223 String result = null; 224 if ((result = (String ) clientMapping.get(guid)) == null) { 225 if (log.isLoggable(Level.FINEST)) { 226 log.finest("No mapping found for GUID: " + guid); 227 } 228 } 229 return result; 230 } 231 232 236 public void removeMappedValuesForLuid(String luid) { 237 String guid = getMappedValueForLuid(luid); 238 if (guid != null) { 239 clientMapping.remove(guid); 240 addDeletedKey(luid); 241 } 242 } 243 244 248 public void removeMappedValuesForGuid(String guid) { 249 String luid = getMappedValueForGuid(guid); 250 if (luid != null) { 251 clientMapping.remove(guid); 252 addDeletedKey(luid); 253 } 254 } 255 256 264 public void updateMapping(String guid, String luid) { 265 266 if (log.isLoggable(Level.FINEST)) { 267 log.finest("Updating mapping LUID-GUID " + luid + "-" + guid); 268 } 269 270 if (clientMapping.containsKey(guid) == false) { 274 addMapping(guid, luid); 275 return; 276 } 277 278 String oldLuid = (String )clientMapping.put(guid, luid); 283 modifiedKeys.add(luid); modified = true; 284 285 if (oldLuid != null) { 286 removeDeletedKey(oldLuid); 287 removeModifiedKey(oldLuid); 288 } 289 290 removeDeletedKey(luid); 295 } 296 297 302 public void clearMappings() { 303 304 deletedKeys.addAll(clientMapping.values()); 305 deletedKeys.addAll(modifiedKeys); 306 307 clientMapping.clear(); 308 309 modified = false; 310 deleted = true; 311 } 312 313 public String toString() { 314 ToStringBuilder sb = new ToStringBuilder(this); 315 316 sb.append("clientMapping", clientMapping); 317 sb.append("modifiedKeys" , modifiedKeys ); 318 sb.append("deletedKeys" , deletedKeys ); 319 320 return sb.toString(); 321 } 322 323 326 public void resetModifiedKeys() { 327 if (modified) { 329 modifiedKeys.clear(); 330 modified = false; 331 } 332 if (deleted) { 334 deletedKeys.clear(); 335 deleted = false; 336 } 337 } 338 339 341 345 private void resetMapping() { 346 if (!clientMapping.isEmpty()) { 348 clientMapping.clear(); 349 } 350 351 resetModifiedKeys(); 352 } 353 354 358 private void removeDeletedKey(String luid) { 359 if (deleted && deletedKeys.contains(luid)) { 360 if (log.isLoggable(Level.FINEST)) { 361 log.finest("Removing deleted LUID: " + luid); 362 } 363 deletedKeys.remove(luid); 364 if (deletedKeys.size() == 0) { 365 deleted = false; 366 } 367 } 368 } 369 370 374 private void removeModifiedKey(String luid) { 375 if (modified && modifiedKeys.contains(luid)) { 376 if (log.isLoggable(Level.FINEST)) { 377 log.finest("Removing deleted LUID: " + luid); 378 } 379 modifiedKeys.remove(luid); 380 if (modifiedKeys.size() == 0) { 381 modified = false; 382 } 383 } 384 } 385 386 390 private void addDeletedKey(String luid) { 391 removeModifiedKey(luid); 392 deleted = true; 393 deletedKeys.add(luid); 394 } 395 } 396 | Popular Tags |