1 13 package org.jahia.services.lock; 14 15 import java.util.HashMap ; 16 17 import org.apache.log4j.Logger; 18 import org.jahia.content.ContentObject; 19 import org.jahia.content.JahiaObject; 20 import org.jahia.content.ObjectKey; 21 import org.jahia.exceptions.JahiaException; 22 import org.jahia.registries.ServicesRegistry; 23 import org.jahia.services.cache.Cache; 24 import org.jahia.services.cache.CacheEntry; 25 import org.jahia.services.usermanager.JahiaUser; 26 27 42 public class LockRegistry extends LockService { 43 44 public final static String OWNER = "owner"; 45 public final static String ID = "id"; 46 public final static String STEALED = "stealed"; 47 public final static String TIME_REMAINING = "timeRemaining"; 48 public final static String TIMEOUT = "timeout"; 49 50 56 public synchronized static LockRegistry getInstance () { 57 if (lockRegistryInstance == null) { 58 lockRegistryInstance = new LockRegistry(); 59 } 60 return lockRegistryInstance; 61 } 62 63 73 public boolean acquire (LockKey lockKey, JahiaUser owner, 74 String lockID, int timeout) { 75 if (LockPrerequisites.getInstance().isLockAcquirable(lockKey, owner, 76 lockID, false)) { 77 Lock lock = getLock(lockKey); 78 if (lock == null) { 79 lock = new Lock(owner, lockID, timeout); 80 if (lockKey.getId() != 0) { 81 putLock(lockKey, lock); 82 } 83 return true; 84 } else if (lock.hasExpired()) { 85 lock.resetTimeout(timeout); 86 lock.setOwner(owner); 87 lock.setID(lockID); 88 if (getLock(lockKey) == null) { 92 if (lockKey.getId() != 0) { 93 putLock(lockKey, lock); 94 } 95 } 96 return true; 97 } else if (lock.getOwner().getUserKey().equals(owner.getUserKey())) { 98 lock.resetTimeout(timeout); 99 return true; 100 } 101 } 102 return false; 103 } 104 105 114 public boolean isAcquireable (LockKey lockKey, JahiaUser owner, 115 String lockID) { 116 if (LockPrerequisites.getInstance().isLockAcquirable(lockKey, owner, 117 lockID, true)) { 118 return true; 119 } else { 120 return false; 121 } 122 } 123 124 137 public boolean reserve (LockKey lockKey, JahiaUser owner, 138 String lockID, int timeout, int delay) { 139 return false; 140 } 141 142 149 public void release (LockKey lockKey, JahiaUser owner, 150 String lockID) { 151 if (canRelease(lockKey, owner, lockID)) { 152 removeLock(lockKey); 153 LockPrerequisites.getInstance().resetPrerequisite(lockKey); 154 } 155 } 156 157 163 public HashMap getInfo (LockKey lockKey) { 164 Lock lock = getLock(lockKey); 165 if (lock == null) { 166 return null; 167 } 168 HashMap lockInfo = new HashMap (); 169 lockInfo.put(OWNER, lock.getOwner()); 170 lockInfo.put(ID, lock.getID()); 171 lockInfo.put(STEALED, new Boolean (lock.isStealed())); 172 lockInfo.put(TIME_REMAINING, getTimeRemaining(lockKey)); 173 lockInfo.put(TIMEOUT, new Long (lock.getTimeout())); 174 return lockInfo; 175 } 176 177 183 public Long getTimeRemaining (LockKey lockKey) { 184 Lock lock = getLock(lockKey); 185 if (lock == null) { 186 return null; 187 } 188 return new Long (lock.getTimeRemaining()); 189 } 190 191 200 public void steal(LockKey lockKey, JahiaUser newOwner, String lockID) { 201 if (hasAdminRights(lockKey, newOwner)) { 202 synchronized (this) { 203 Lock lock = getLock(lockKey); 204 lock.setOwner(newOwner); 205 lock.setID(lockID); 206 lock.setStealed(true); 207 putLock(lockKey, lock); 208 LockPrerequisites.getInstance().resetPrerequisite(lockKey); 209 } 210 } 211 } 212 213 224 public void nuke(LockKey lockKey, JahiaUser owner, String lockID) { 225 if (hasAdminRights(lockKey, owner)) { 226 synchronized (this) { 227 removeLock(lockKey); 228 LockPrerequisites.getInstance().resetPrerequisite(lockKey); 229 } 230 } 231 } 232 233 239 public boolean isStealed (LockKey lockKey) { 240 Lock lock = getLock(lockKey); 241 if (lock == null) { 242 return false; 243 } 244 return lock.isStealed(); 245 } 246 247 253 public boolean isAlreadyAcquired(LockKey lockKey) { 254 Lock lock = getLock(lockKey); 255 if (lock == null) { 256 return false; } 258 if (lock.hasExpired()) { 259 synchronized (this) { 260 removeLock(lockKey); 261 LockPrerequisites.getInstance().resetPrerequisite(lockKey); 262 } 263 return false; 264 } 265 return true; 266 } 267 268 276 public boolean isStealedInContext (LockKey lockKey, 277 JahiaUser owner, String lockID) { 278 if (isStealed(lockKey) && 279 !isAlreadyAcquiredInContext(lockKey, owner, lockID)) { 280 return true; 281 } 282 return false; 283 } 284 285 294 public boolean isAlreadyAcquiredInContext (LockKey lockKey, 295 JahiaUser owner, String lockID) { 296 Lock lock = getLock(lockKey); 297 if (lock == null) { 298 return false; } 300 if (!isAlreadyAcquired(lockKey)) { 301 return false; 302 } 303 if (lock.getOwner().getUserKey().equals(owner.getUserKey())) { 304 return true; 305 } 306 return false; 307 } 308 309 317 public boolean canRelease (LockKey lockKey, JahiaUser owner, 318 String lockID) { 319 Lock lock = getLock(lockKey); 320 if (lock == null) { 321 return true; } 323 if (lock.hasExpired()) { 324 synchronized (this) { 325 removeLock(lockKey); 326 LockPrerequisites.getInstance().resetPrerequisite(lockKey); 327 } 328 return true; 329 } 330 if (lock.getOwner().getUserKey().equals(owner.getUserKey())) { 331 return true; 332 } 333 return false; 334 } 335 336 343 public boolean hasAdminRights (LockKey lockKey, 344 JahiaUser owner) { 345 ObjectKey objectKey = lockKey.getObjectKey(); 346 if (objectKey != null) { 348 try { 349 ContentObject contentObject = (ContentObject) JahiaObject. 350 getInstance(objectKey); 351 if (contentObject.checkAdminAccess(owner)) { 352 return true; 353 } 354 } catch (ClassNotFoundException cnfe) { 355 logger.debug("Object '" + lockKey.getType() + "' not found !", 356 cnfe); 357 } 358 } 359 else if (owner.isAdminMember(0) || 361 owner.isAdminMember(owner.getSiteID())) { 362 return true; 363 } 364 return false; 365 } 366 367 373 public synchronized void purgeLocks() { 374 LockPrerequisites.getInstance().flush(); 375 removeAllLocks(); 376 } 377 378 381 private LockRegistry () { 382 try { 383 lockCache = ServicesRegistry.getInstance().getJahiaCacheServiceBis(). 384 createCache("LockCache"); 385 } catch (JahiaException je) { 386 logger.error ("Error while creating lock cache", je); 387 } 388 logger.debug("Lock registry has been instanciated"); 389 } 390 391 private void putLock (LockKey lockKey, Lock lock) { 392 if (lockCache.get(lockKey) != null) { 393 try { 394 LockDB.getInstance().updateLock(lockKey, lock); 395 } catch (JahiaException je) { 396 logger.error("Error while updating lock " + lockKey + 397 "in database", je); 398 } 399 } else { 400 try { 401 LockDB.getInstance().createLock(lockKey, lock); 402 } catch (JahiaException je) { 403 logger.error("Error while creating the lock " + lockKey + 404 "in the database", je); 405 } 406 } 407 lockCache.put(lockKey, lock); 408 } 409 410 private Lock getLock (LockKey lockKey) { 411 CacheEntry cacheEntry = lockCache.getCacheEntry(lockKey); 412 if (cacheEntry != null) { 413 418 return (Lock) cacheEntry.getObject(); 419 } else { 420 try { 421 Lock lock = LockDB.getInstance().getLock(lockKey); 422 lockCache.put(lockKey, lock); 425 return lock; 426 } catch (JahiaException je) { 427 logger.error("Error while retrieving lock " + lockKey + 428 " from database", je); 429 return null; 430 } 431 } 432 } 433 434 private void removeLock (LockKey lockKey) { 435 lockCache.remove(lockKey); 436 try { 437 LockDB.getInstance().removeLock(lockKey); 438 } catch (JahiaException je) { 439 logger.error("Error while removing lock " + lockKey + 440 " from the database ", je); 441 } 442 } 443 444 private void removeAllLocks() { 445 try { 446 LockDB.getInstance().removeAllLocks(); 447 } catch (JahiaException je) { 448 logger.error("Error while removing all locks from the database ", je); 449 } 450 lockCache.flush(); 451 } 452 453 private static LockRegistry lockRegistryInstance; 454 455 private Cache lockCache; 456 457 private static Logger logger = Logger.getLogger(LockRegistry.class); 458 459 } 460 | Popular Tags |