use of org.olat.core.util.coordinate.LockEntry in project OpenOLAT by OpenOLAT.
the class LockController method event.
/**
* @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest,
* org.olat.core.gui.control.Controller, org.olat.core.gui.control.Event)
*/
public void event(UserRequest ureq, Controller source, Event event) {
if (source == tableCtr) {
if (event.getCommand().equals(Table.COMMANDLINK_ROWACTION_CLICKED)) {
TableEvent te = (TableEvent) event;
LockEntry lockToRelease = locksTableModel.getObject(te.getRowId());
dialogController = activateYesNoDialog(ureq, null, translate("lock.release.sure"), dialogController);
dialogController.setUserObject(lockToRelease);
}
} else if (source == dialogController) {
if (DialogBoxUIFactory.isYesEvent(event)) {
LockEntry lockToRelease = (LockEntry) dialogController.getUserObject();
MultiUserEvent mue = new LockRemovedEvent(lockToRelease);
OLATResourceable lockEntryOres = OresHelper.createOLATResourceableInstance(LockEntry.class, 0l);
CoordinatorManager.getInstance().getCoordinator().getEventBus().fireEventToListenersOf(mue, lockEntryOres);
CoordinatorManager.getInstance().getCoordinator().getLocker().releaseLockEntry(lockToRelease);
lockToRelease = null;
resetTableModel();
}
}
}
use of org.olat.core.util.coordinate.LockEntry in project OpenOLAT by OpenOLAT.
the class ClusterLocker method acquireLock.
@Override
public LockResult acquireLock(final OLATResourceable ores, final Identity requestor, final String locksubkey) {
final String asset = OresHelper.createStringRepresenting(ores, locksubkey);
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);
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
LockEntry le = new LockEntry(li.getAsset(), li.getCreationDate().getTime(), li.getOwner());
if (requestor.getName().equals(li.getOwner().getName())) {
// 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 {
lres = new LockResultImpl(false, le);
}
}
return lres;
}
});
return res;
}
use of org.olat.core.util.coordinate.LockEntry in project OpenOLAT by OpenOLAT.
the class LockTest method testCreateDeleteAcquire.
@Test
public void testCreateDeleteAcquire() {
// some setup
List<Identity> identities = new ArrayList<Identity>();
for (int i = 0; i < MAX_COUNT + MAX_USERS_MORE; i++) {
Identity i1 = JunitTestHelper.createAndPersistIdentityAsRndUser("lock-");
identities.add(i1);
}
dbInstance.closeSession();
Identity ident = identities.get(0);
Identity ident2 = identities.get(1);
OLATResourceable ores = OresHelper.createOLATResourceableInstanceWithoutCheck(LockTest.class.getName(), new Long(123456789));
// ------------------ test the clusterlockmanager ----------------------
// create a lock
String asset = OresHelper.createStringRepresenting(ores, "locktest");
LockImpl li = clusterLockManager.createLockImpl(asset, ident);
clusterLockManager.saveLock(li);
dbInstance.closeSession();
// find it
LockImpl l2 = clusterLockManager.findLock(asset);
assertNotNull(l2);
assertEquals(li.getKey(), l2.getKey());
// delete it
int deletedLock = clusterLockManager.deleteLock(asset, ident);
dbInstance.closeSession();
Assert.assertEquals(1, deletedLock);
// may not find it again
LockImpl l3 = clusterLockManager.findLock(asset);
assertNull(l3);
// ------------------ test the clusterlocker ----------------------
// access the cluster locker explicitely
Locker cl = clusterCoordinator.getLocker();
// acquire
LockResult res1 = cl.acquireLock(ores, ident, "abc");
assertTrue(res1.isSuccess());
dbInstance.closeSession();
// reacquire same identity (get from db)
LockResult res11 = cl.acquireLock(ores, ident, "abc");
long lock1Ac = res11.getLockAquiredTime();
assertTrue(res11.isSuccess());
assertTrue(lock1Ac > 0);
dbInstance.closeSession();
// acquire by another identity must fail
LockResult res2 = cl.acquireLock(ores, ident2, "abc");
assertFalse(res2.isSuccess());
dbInstance.closeSession();
// reacquire same identity
LockResult res3 = cl.acquireLock(ores, ident, "abc");
assertTrue(res3.isSuccess());
dbInstance.closeSession();
// make sure it is not locked anymore
boolean lo3 = cl.isLocked(ores, "abc");
assertTrue(lo3);
// test the admin
List<LockEntry> entries = cl.adminOnlyGetLockEntries();
assertEquals(1, entries.size());
LockEntry le = entries.get(0);
// must be original owner
assertEquals(le.getOwner().getName(), ident.getName());
// release lock
cl.releaseLock(res3);
dbInstance.closeSession();
// test the admin
entries = cl.adminOnlyGetLockEntries();
assertEquals(0, entries.size());
// make sure it is not locked anymore
boolean lo = cl.isLocked(ores, "abc");
assertFalse(lo);
}
use of org.olat.core.util.coordinate.LockEntry in project openolat by klemens.
the class LockTest method testCreateDeleteAcquire.
@Test
public void testCreateDeleteAcquire() {
// some setup
List<Identity> identities = new ArrayList<Identity>();
for (int i = 0; i < MAX_COUNT + MAX_USERS_MORE; i++) {
Identity i1 = JunitTestHelper.createAndPersistIdentityAsRndUser("lock-");
identities.add(i1);
}
dbInstance.closeSession();
Identity ident = identities.get(0);
Identity ident2 = identities.get(1);
OLATResourceable ores = OresHelper.createOLATResourceableInstanceWithoutCheck(LockTest.class.getName(), new Long(123456789));
// ------------------ test the clusterlockmanager ----------------------
// create a lock
String asset = OresHelper.createStringRepresenting(ores, "locktest");
LockImpl li = clusterLockManager.createLockImpl(asset, ident);
clusterLockManager.saveLock(li);
dbInstance.closeSession();
// find it
LockImpl l2 = clusterLockManager.findLock(asset);
assertNotNull(l2);
assertEquals(li.getKey(), l2.getKey());
// delete it
int deletedLock = clusterLockManager.deleteLock(asset, ident);
dbInstance.closeSession();
Assert.assertEquals(1, deletedLock);
// may not find it again
LockImpl l3 = clusterLockManager.findLock(asset);
assertNull(l3);
// ------------------ test the clusterlocker ----------------------
// access the cluster locker explicitely
Locker cl = clusterCoordinator.getLocker();
// acquire
LockResult res1 = cl.acquireLock(ores, ident, "abc");
assertTrue(res1.isSuccess());
dbInstance.closeSession();
// reacquire same identity (get from db)
LockResult res11 = cl.acquireLock(ores, ident, "abc");
long lock1Ac = res11.getLockAquiredTime();
assertTrue(res11.isSuccess());
assertTrue(lock1Ac > 0);
dbInstance.closeSession();
// acquire by another identity must fail
LockResult res2 = cl.acquireLock(ores, ident2, "abc");
assertFalse(res2.isSuccess());
dbInstance.closeSession();
// reacquire same identity
LockResult res3 = cl.acquireLock(ores, ident, "abc");
assertTrue(res3.isSuccess());
dbInstance.closeSession();
// make sure it is not locked anymore
boolean lo3 = cl.isLocked(ores, "abc");
assertTrue(lo3);
// test the admin
List<LockEntry> entries = cl.adminOnlyGetLockEntries();
assertEquals(1, entries.size());
LockEntry le = entries.get(0);
// must be original owner
assertEquals(le.getOwner().getName(), ident.getName());
// release lock
cl.releaseLock(res3);
dbInstance.closeSession();
// test the admin
entries = cl.adminOnlyGetLockEntries();
assertEquals(0, entries.size());
// make sure it is not locked anymore
boolean lo = cl.isLocked(ores, "abc");
assertFalse(lo);
}
use of org.olat.core.util.coordinate.LockEntry in project openolat by klemens.
the class LockController method event.
/**
* @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest,
* org.olat.core.gui.control.Controller, org.olat.core.gui.control.Event)
*/
public void event(UserRequest ureq, Controller source, Event event) {
if (source == tableCtr) {
if (event.getCommand().equals(Table.COMMANDLINK_ROWACTION_CLICKED)) {
TableEvent te = (TableEvent) event;
LockEntry lockToRelease = locksTableModel.getObject(te.getRowId());
dialogController = activateYesNoDialog(ureq, null, translate("lock.release.sure"), dialogController);
dialogController.setUserObject(lockToRelease);
}
} else if (source == dialogController) {
if (DialogBoxUIFactory.isYesEvent(event)) {
LockEntry lockToRelease = (LockEntry) dialogController.getUserObject();
MultiUserEvent mue = new LockRemovedEvent(lockToRelease);
OLATResourceable lockEntryOres = OresHelper.createOLATResourceableInstance(LockEntry.class, 0l);
CoordinatorManager.getInstance().getCoordinator().getEventBus().fireEventToListenersOf(mue, lockEntryOres);
CoordinatorManager.getInstance().getCoordinator().getLocker().releaseLockEntry(lockToRelease);
lockToRelease = null;
resetTableModel();
}
}
}
Aggregations