1 12 13 package com.openedit.modules.edit; 14 15 import java.util.Date ; 16 import java.util.HashMap ; 17 import java.util.Map ; 18 19 import com.openedit.users.User; 20 21 22 27 public class EditLockRegistry 28 { 29 30 protected static final long LOCK_EXPIRATION_DURATION = 30 * 60 * 1000; 31 protected Map fieldPathToLockMap; 32 33 36 public EditLockRegistry() 37 { 38 super(); 39 } 40 41 48 public User getLockOwner(String inPath) 49 { 50 unlockIfExpired(inPath); 51 52 EditLock lock = getEditLock(inPath); 53 54 return (lock == null) ? null : lock.getLockedUser(); 55 } 56 57 64 public boolean isLocked(String inPath) 65 { 66 unlockIfExpired(inPath); 67 68 return getPathToLockMap().containsKey(inPath); 69 } 70 71 79 public boolean canLock(String inPath, User inUser) 80 { 81 if ((inPath == null) || (inUser == null)) 82 { 83 return false; 84 } 85 86 unlockIfExpired(inPath); 87 88 User owner = getLockOwner(inPath); 89 90 boolean ok = (owner == null) || owner.equals(inUser); 91 if ( !ok ) 92 { 93 94 } 95 return ok; 96 } 97 98 104 public synchronized void forciblyLockPath(String inPath, User inUser) 105 { 106 forciblyLockPath(inPath, inUser, new Date ()); 107 } 108 109 117 public synchronized void lockPath(String inPath, User inUser) 118 throws AlreadyLockedException 119 { 120 lockPath(inPath, inUser, new Date ()); 121 } 122 123 131 public synchronized boolean unlockPath(String inPath, User inUser) 132 { 133 unlockIfExpired(inPath); 134 135 User oldUser = getLockOwner(inPath); 136 137 if ((oldUser != null) && oldUser.equals(inUser)) 138 { 139 getPathToLockMap().remove(inPath); 140 141 return true; 142 } 143 else 144 { 145 return false; 146 } 147 } 148 149 156 protected EditLock getEditLock(String inPath) 157 { 158 if (inPath == null) 159 { 160 return null; 161 } 162 163 return (EditLock) getPathToLockMap().get(inPath); 164 } 165 166 171 protected Map getPathToLockMap() 172 { 173 if (fieldPathToLockMap == null) 174 { 175 fieldPathToLockMap = new HashMap (); 176 } 177 178 return fieldPathToLockMap; 179 } 180 181 191 protected synchronized void forciblyLockPath(String inPath, User inUser, Date inDate) 192 { 193 if (inUser == null) 194 { 195 throw new IllegalArgumentException ("Cannot claim an edit lock without a user"); 196 } 197 198 if (inPath == null) 199 { 200 throw new IllegalArgumentException ("Cannot claim a lock on a null path"); 201 } 202 203 getPathToLockMap().put(inPath, new EditLock(inPath, inUser, inDate)); 204 } 205 206 217 protected synchronized void lockPath(String inPath, User inUser, Date inDate) 218 throws AlreadyLockedException 219 { 220 if (inUser == null) 221 { 222 throw new IllegalArgumentException ("Cannot claim an edit lock without a user"); 223 } 224 225 if (inPath == null) 226 { 227 throw new IllegalArgumentException ("Cannot claim a lock on a null path"); 228 } 229 230 unlockIfExpired(inPath); 231 232 User oldUser = getLockOwner(inPath); 233 234 if (!canLock(inPath, inUser)) 235 { 236 throw new AlreadyLockedException(inPath, oldUser); 237 } 238 239 forciblyLockPath(inPath, inUser, inDate); 240 } 241 242 247 protected void unlockIfExpired(String inPath) 248 { 249 EditLock lock = getEditLock(inPath); 250 251 if ((lock != null) && lock.isExpired()) 252 { 253 getPathToLockMap().remove(inPath); 254 } 255 } 256 257 protected class EditLock 258 { 259 protected Date fieldCreationDate; 260 protected String fieldPath; 261 protected User fieldLockedUser; 262 263 266 public EditLock() 267 { 268 this(null, null); 269 } 270 271 277 public EditLock(String inPath, User inLockedUser) 278 { 279 this(inPath, inLockedUser, new Date ()); 280 } 281 282 289 public EditLock(String inPath, User inLockedUser, Date inCreationDate) 290 { 291 setPath(inPath); 292 setCreationDate(inCreationDate); 293 setLockedUser(inLockedUser); 294 } 295 296 301 public void setCreationDate(Date creationDate) 302 { 303 fieldCreationDate = creationDate; 304 } 305 306 311 public Date getCreationDate() 312 { 313 return fieldCreationDate; 314 } 315 316 321 public boolean isExpired() 322 { 323 return (new Date ().getTime() - getCreationDate().getTime()) >= LOCK_EXPIRATION_DURATION; 324 } 325 326 331 public void setLockedUser(User lockedUser) 332 { 333 fieldLockedUser = lockedUser; 334 } 335 336 341 public User getLockedUser() 342 { 343 return fieldLockedUser; 344 } 345 346 351 public void setPath(String path) 352 { 353 fieldPath = path; 354 } 355 356 361 public String getPath() 362 { 363 return fieldPath; 364 } 365 } 366 } 367 | Popular Tags |