protected void lock( Node node, boolean isDeep, boolean isSessionScoped ) throws RepositoryException { session.getWorkspace().getLockManager().lock(node.getPath(), isDeep, isSessionScoped, 1L, "owner"); } }
@SuppressWarnings( "synthetic-access" ) public Lock lockFor( final JcrSession session ) throws RepositoryException { final AbstractJcrNode node = session.node(lockedNodeKey, null); final JcrLockManager lockManager = session.workspace().getLockManager(); return new Lock() { @Override
@SuppressWarnings( "synthetic-access" ) public Lock lockFor( final JcrSession session ) throws RepositoryException { final AbstractJcrNode node = session.node(lockedNodeKey, null); final JcrLockManager lockManager = session.workspace().getLockManager(); return new Lock() { @Override
@Test @FixFor( "MODE-1407" ) public void shouldTestLockingTest_testRemoveLockFromNode() throws Exception { // setup String node1 = "node1"; Node lockable = getRoot().addNode(node1, UNSTRUCTURED); lockable.addMixin(LOCK_MIXIN); save(); session.getWorkspace().getLockManager().lock(lockable.getPath(), false, true, 1L, "me"); // register listener SimpleListener listener = addListener(2, Event.PROPERTY_REMOVED, null, false, null, null, false); // lock node (no save needed) session.getWorkspace().getLockManager().unlock(lockable.getPath()); // event handling listener.waitForEvents(); removeListener(listener); // tests checkResults(listener); assertTrue("No event created for " + LOCK_OWNER, containsPath(listener, lockable.getPath() + '/' + LOCK_OWNER)); assertTrue("No event created for " + LOCK_IS_DEEP, containsPath(listener, lockable.getPath() + '/' + LOCK_IS_DEEP)); }
@Test @FixFor( "MODE-2633" ) public void shouldExpireOpenScopedLocks() throws Exception { // Create a new lockable node Node node = session.getRootNode().addNode("test"); node.addMixin("mix:lockable"); session.save(); // Lock the node JcrLockManager lockManager = session.getWorkspace().getLockManager(); lockManager.lock(node.getPath(), false, false, 1, null); assertTrue(node.isLocked()); // Wait enough time for the lock to be expired Thread.sleep(TimeUnit.SECONDS.toMillis(2)); // The old lock should still be there even though it's expired assertFalse(node.isLocked()); assertFalse(lockManager.getLock(node.getPath()).isLive()); // Check that a new lock can be obtained lockManager.lock(node.getPath(), false, false, 10, null); assertTrue(node.isLocked()); }
@Test @FixFor( "MODE-1407" ) public void shouldTestLockingTest_testAddLockToNode() throws Exception { // setup String node1 = "node1"; Node lockable = getRoot().addNode(node1, UNSTRUCTURED); lockable.addMixin(LOCK_MIXIN); save(); // register listener SimpleListener listener = addListener(2, 2, Event.PROPERTY_ADDED, getRoot().getPath(), true, null, null, false); // lock node (no save needed) session.getWorkspace().getLockManager().lock(lockable.getPath(), false, true, 1L, "me"); // event handling listener.waitForEvents(); removeListener(listener); // tests checkResults(listener); assertTrue("No event created for " + LOCK_OWNER, containsPath(listener, lockable.getPath() + '/' + LOCK_OWNER)); assertTrue("No event created for " + LOCK_IS_DEEP, containsPath(listener, lockable.getPath() + '/' + LOCK_IS_DEEP)); }
@Test @FixFor( "MODE-2641" ) public void shouldProvideTimeoutForOpenScopedLocks() throws Exception { // Create a new lockable node Node node = session.getRootNode().addNode("test"); node.addMixin("mix:lockable"); session.save(); String path = node.getPath(); // Lock the node with an open scoped lock int timeout = 2; JcrLockManager lockManager = session.getWorkspace().getLockManager(); Lock lock = lockManager.lock(node.getPath(), false, false, timeout, null); assertTrue(node.isLocked()); long secondsRemaining = lock.getSecondsRemaining(); assertTrue("Expected a valid value for seconds remaining", secondsRemaining <= timeout && secondsRemaining > 0); // Look at the same lock from another session session.logout(); session = repository.login(); lockManager = session.getWorkspace().getLockManager(); lock = lockManager.getLock(path); secondsRemaining = lock.getSecondsRemaining(); assertTrue("Expected a valid value for seconds remaining", secondsRemaining <= timeout && secondsRemaining > 0); Thread.sleep(TimeUnit.SECONDS.toMillis(2)); assertEquals("Expected a negative value because the lock should have expired", Long.MIN_VALUE, lock.getSecondsRemaining()); }
sessionLockedNode1.addMixin("mix:lockable"); locker1.save(); locker1.getWorkspace().getLockManager().lock(sessionLockedNode1.getPath(), false, true, 1, "me"); sessionLockedNode2.addMixin("mix:lockable"); locker2.save(); locker2.getWorkspace().getLockManager().lock(sessionLockedNode2.getPath(), false, true, 1, "me");
@Test @FixFor( "MODE-2641" ) public void shouldNotProvideTimeoutForSessionScopedLocks() throws Exception { // Create a new lockable node Node node = session.getRootNode().addNode("test"); node.addMixin("mix:lockable"); session.save(); String path = node.getPath(); // Lock the node with a session scoped lock int timeout = 2; JcrLockManager lockManager = session.getWorkspace().getLockManager(); Lock lock = lockManager.lock(node.getPath(), false, true, timeout, null); assertTrue(node.isLocked()); assertEquals("Session scoped locks should not have timeout information", Long.MAX_VALUE, lock.getSecondsRemaining()); JcrSession otherSession = repository.login(); try { lockManager = otherSession.getWorkspace().getLockManager(); lock = lockManager.getLock(path); assertTrue(otherSession.getNode(path).isLocked()); assertEquals("Session scoped locks should not have timeout information", Long.MAX_VALUE, lock.getSecondsRemaining()); } finally { otherSession.logout(); } }
@Test @FixFor( "MODE-2424" ) public void shouldAllowAddingMixinOnLockedNodeForLockOwner() throws Exception { final AbstractJcrNode testNode = session.getRootNode().addNode("test"); final String path = testNode.getPath(); testNode.addMixin("mix:lockable"); session.save(); session.getWorkspace().getLockManager().lock(path, false, true, Long.MAX_VALUE, session.getUserID()); testNode.addMixin("mix:created"); session.save(); }
@Test @FixFor( "MODE-2642" ) public void shouldLockNodeWithinTransaction() throws Exception { Node node = session.getRootNode().addNode("test"); node.addMixin("mix:lockable"); session.save(); startTransaction(); JcrLockManager lockManager = session.getWorkspace().getLockManager(); Lock lock = lockManager.lock(node.getPath(), false, false, Long.MAX_VALUE, null); assertTrue(lock.isLive()); assertTrue("Node should be locked", node.isLocked()); commitTransaction(); assertTrue(node.isLocked()); }
@Test @FixFor( "MODE-2450" ) public void shouldCleanupCorruptedLocks() throws Exception { final AbstractJcrNode testNode = session.getRootNode().addNode("test"); final String path = testNode.getPath(); testNode.addMixin("mix:lockable"); session.save(); final org.modeshape.jcr.RepositoryLockManager.Lock lock = (RepositoryLockManager.Lock) session.getWorkspace().getLockManager().lock(path, false, false, Long.MAX_VALUE, session.getUserID()); Assert.assertNotNull(lock); session.logout(); //forcibly remove the lock node from the system area... String lockKey = lock.lockKey().toString(); assertTrue(runInTransaction(() -> repository.documentStore().remove(lockKey))); //and then force a refresh RepositoryLockManager lockManager = repository.lockManager(); lockManager.refreshFromSystem(); //check that the lock has been removed session = repository.login(); assertFalse(session.getWorkspace().getLockManager().isLocked("/test")); // issue another refresh and verify the node is still unlocked lockManager.refreshFromSystem(); assertFalse(session.getWorkspace().getLockManager().isLocked("/test")); }
locker1.getWorkspace().getLockManager().lock(sessionLockedNode1.getPath(), false, true, 1, "me"); assertLocking(locker1, "/sessionLockedNode1", true); locker1.getWorkspace().getLockManager().lock(openLockedNode.getPath(), false, false, 1, "me"); assertLocking(locker1, "/openLockedNode", true); locker2.save(); locker2.getWorkspace().getLockManager().lock(sessionLockedNode2.getPath(), false, true, Long.MAX_VALUE, "me"); assertEquals(Long.MAX_VALUE, locker2.getWorkspace().getLockManager().getLock("/sessionLockedNode2").getSecondsRemaining()); assertEquals(Long.MAX_VALUE, locker1.getWorkspace().getLockManager().getLock("/sessionLockedNode2").getSecondsRemaining()); assertLocking(reader, "/sessionLockedNode2", true); assertEquals(Long.MAX_VALUE, locker2.getWorkspace().getLockManager().getLock("/sessionLockedNode2").getSecondsRemaining());
@Test @FixFor( "MODE-2342" ) public void lockTokensShouldBeRemovedFromSessionUponLogout() throws Exception { final AbstractJcrNode testNode = session.getRootNode().addNode("test"); final String path = testNode.getPath(); testNode.addMixin("mix:lockable"); session.save(); final Lock lock = session.getWorkspace().getLockManager().lock(path, false, false, Long.MAX_VALUE, session.getUserID()); final String token = lock.getLockToken(); Assert.assertNotNull(token); session.logout(); Session session2 = repository.login(); final LockManager lockManager = session2.getWorkspace().getLockManager(); lockManager.addLockToken(token); Assert.assertTrue("New session should now own the lock.", lockManager.getLock(path).isLockOwningSession()); }
@FixFor( "MODE-2623" ) @Test public void shouldAllowLockUnlockWithinTransaction() throws Exception { final String path = "/test"; Node parent = session.getRootNode().addNode("test"); parent.addMixin("mix:lockable"); session.save(); startTransaction(); LockManager lockMgr = session.getWorkspace().getLockManager(); lockMgr.lock(path, true, true, Long.MAX_VALUE, session.getUserID()); lockMgr.unlock(path); commitTransaction(); assertFalse(session.getNode(path).isLocked()); }
@Test @FixFor("MODE-2654") public void shouldNotCopyLocks()throws Exception { Node parentNode = session.getRootNode().addNode("node"); Node childNode = parentNode.addNode("child"); childNode.addMixin("mix:lockable"); session.save(); JcrWorkspace workspace = session.getWorkspace(); JcrLockManager lockManager = workspace.getLockManager(); lockManager.lock("/node/child", false, false, Long.MAX_VALUE, null); assertTrue(session.getNode("/node/child").isLocked()); workspace.copy("/node", "/newPath"); assertFalse(session.getNode("/newPath").isLocked()); assertFalse(session.getNode("/newPath/child").isLocked()); lockManager.lock("/newPath/child", false, false, Long.MAX_VALUE, null); assertTrue(session.getNode("/newPath/child").isLocked()); } }