Example #1
0
  @Override
  public LockResult acquireLock(
      final OLATResourceable ores, final OLATPrincipal requestor, final String locksubkey) {
    final String asset = OresHelper.createStringRepresenting(ores, locksubkey);

    final LockResult res =
        syncer.doInSync(
            ores,
            new SyncerCallback<LockResult>() {
              @Override
              public LockResult execute() {
                LockResultImpl lres;
                LockImpl li = clusterLockManager.findLock(asset);
                if (li == null) { // fine, we can lock it
                  li = clusterLockManager.createLockImpl(asset, requestor);
                  clusterLockManager.saveLock(li);
                  final LockEntry le =
                      new LockEntry(li.getAsset(), li.getCreationDate().getTime(), li.getOwner());
                  lres = new LockResultImpl(true, le);
                } else {
                  // already locked by a user.
                  // if that user is us, we can reacquire it
                  final LockEntry le =
                      new LockEntry(li.getAsset(), li.getCreationDate().getTime(), li.getOwner());
                  if (requestor.equals(li.getOwner())) {
                    // that's us -> success (asset, owner is the
                    // same, and we leave creationdate to when the
                    // lock was originally acquired, not
                    // when it was reacquired.
                    lres = new LockResultImpl(true, le);
                  } else {
                    log.info(
                        "Lock for resource: "
                            + asset
                            + " requested by "
                            + requestor
                            + " already held by "
                            + li.getOwner());
                    lres = new LockResultImpl(false, le);
                  }
                }
                return lres;
              }
            });

    return res;
  }
Example #2
0
 @Override
 public boolean isLocked(final OLATResourceable ores, final String locksubkey) {
   final String asset = OresHelper.createStringRepresenting(ores, locksubkey);
   final LockImpl li = clusterLockManager.findLock(asset);
   return (li != null);
 }