javax.jcr.lock.Lock类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(10.8k)|赞(0)|评价(0)|浏览(153)

本文整理了Java中javax.jcr.lock.Lock类的一些代码示例,展示了Lock类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Lock类的具体详情如下:
包路径:javax.jcr.lock.Lock
类名称:Lock

Lock介绍

[英]Represents a lock placed on an item.
[中]表示放置在项目上的锁。

代码示例

代码示例来源:origin: apache/jackrabbit

/**
 * Creates a new lock info for the given JCR lock object.
 *
 * @param lock the lock.
 * @param idFactory the id factory.
 * @throws RepositoryException if an error occurs while the node from the
 * given lock or while creating the node id.
 */
private LockInfoImpl(Lock lock, IdFactoryImpl idFactory) throws RepositoryException {
  super(lock.getLockToken(), lock.getLockOwner(), lock.isDeep(),
      lock.isSessionScoped(), lock.getSecondsRemaining(), lock.isLockOwningSession(), 
      idFactory.createNodeId(lock.getNode()));
}

代码示例来源:origin: org.onehippo.cms7/hippo-repository-engine

@Override
public boolean isLive() throws RepositoryException {
  synchronized (session) {
    return lock.isLive();
  }
}

代码示例来源:origin: apache/jackrabbit

/**
 * Test {@link javax.jcr.lock.Lock#refresh()} on a released lock.
 */
public void testRefresh() throws RepositoryException {
  // refresh must succeed
  lock.refresh();
}

代码示例来源:origin: apache/jackrabbit

/**
 * Test {@link javax.jcr.lock.Lock#getSecondsRemaining()} 
 */
public void testGetSecondsRemaining() throws RepositoryException {
  if (lock.isLive()) {
    assertTrue("Seconds remaining must be a positive long.", lock.getSecondsRemaining() > 0);            
  } else {
    assertTrue("Seconds remaining must be a negative long.", lock.getSecondsRemaining() < 0);
  }
}

代码示例来源:origin: org.onehippo.cms7/hippo-repository-engine

private void setTimeout(final Lock lock, final long timeoutHint) throws RepositoryException {
  final Node lockNode = lock.getNode();
  if (timeoutHint != Long.MAX_VALUE) {
    lockNode.addMixin(NT_LOCKABLE);
    final Calendar timeout = Calendar.getInstance();
    final long timeoutTime = System.currentTimeMillis() + timeoutHint * 1000;
    timeout.setTimeInMillis(timeoutTime);
    lockNode.setProperty(HIPPO_LOCKEXPIRATIONTIME, timeout);
  } else {
    if (lockNode.hasProperty(HIPPO_LOCKEXPIRATIONTIME)) {
      lockNode.getProperty(HIPPO_LOCKEXPIRATIONTIME).remove();
    }
  }
  lockNode.getSession().save();
}

代码示例来源:origin: org.apache.jackrabbit/jackrabbit-jcr-commons

/**
 * Executes {@link #run} and unlocks the lockable node in any case, even
 * when an exception is thrown.
 *
 * @param lock The <code>Lock</code> to unlock in any case before returning.
 *
 * @return the object returned by {@link #run}.
 * @throws RepositoryException if an error occurs.
 */
private Object runAndUnlock(Lock lock) throws RepositoryException {
  Node node = lock.getNode();
  try {
    return run(node);
  } finally {
    node.getSession().getWorkspace().getLockManager().unlock(node.getPath());
  }
}

代码示例来源:origin: apache/jackrabbit

/**
 * Test locks are released when session logs out
 */
public void testImplicitUnlock() throws RepositoryException,
    NotExecutableException {
  Session other = getHelper().getReadWriteSession();
  try {
    Node testNode = (Node) other.getItem(testRootNode.getPath());
    Node lockedNode = testNode.addNode(nodeName1, testNodeType);
    other.save();
    assertLockable(lockedNode);
    Lock lock = getLockManager(other).lock(lockedNode.getPath(), isDeep(), isSessionScoped(), getTimeoutHint(), getLockOwner());
    other.logout();
    assertFalse(lock.isLive());
  } finally {
    if (other.isLive()) {
      other.logout();
    }
  }
}

代码示例来源:origin: pentaho/pentaho-platform

/**
 * Stores a lock token associated with the session's user.
 */
protected void addLockToken( final Session session, final PentahoJcrConstants pentahoJcrConstants, final Lock lock )
 throws RepositoryException {
 Node lockTokensNode = getOrCreateLockTokensNode( session, pentahoJcrConstants, lock );
 Node newLockTokenNode =
   lockTokensNode.addNode( lock.getNode().getIdentifier(), pentahoJcrConstants.getPHO_NT_LOCKTOKENSTORAGE() );
 newLockTokenNode.setProperty( pentahoJcrConstants.getPHO_LOCKEDNODEREF(), lock.getNode() );
 newLockTokenNode.setProperty( pentahoJcrConstants.getPHO_LOCKTOKEN(), lock.getLockToken() );
 session.save();
}

代码示例来源:origin: apache/jackrabbit

/**
 * Test expiration of the lock
 */
public synchronized void testOwnerHint()
    throws RepositoryException, NotExecutableException {
  lockedNode.unlock();
  lock = lockMgr.lock(lockedNode.getPath(), isDeep(), isSessionScoped(), Long.MAX_VALUE, "test");
  String owner = lock.getLockOwner();
  if (!"test".equals(lock.getLockOwner())) {
    throw new NotExecutableException();
  } else {
    assertTrue(lockedNode.hasProperty(Property.JCR_LOCK_OWNER));
    assertEquals("test", lockedNode.getProperty(Property.JCR_LOCK_OWNER).getString());
  }
}

代码示例来源:origin: com.bstek.urule/urule-console

private void lockCheck(Node node,User user) throws Exception{
  if(lockManager.isLocked(node.getPath())){
    String lockOwner=lockManager.getLock(node.getPath()).getLockOwner();
    if(lockOwner.equals(user.getUsername())){
      return;
    }
    throw new NodeLockException("【"+node.getName()+"】已被"+lockOwner+"锁定!");
  }
}

代码示例来源:origin: org.onehippo.cms7/hippo-repository-engine

private void computeAndWriteNodeDelta(final ConfigurationNode baselineNode,
                   final ConfigurationNode updateNode,
                   final Node targetNode,
                   final boolean isNew,
                   final boolean forceApply,
                   final List<UnprocessedReference> unprocessedReferences)
    throws RepositoryException, IOException {
  if (targetNode.isLocked()) {
    log.warn("Target node {} is locked, skipping its processing", targetNode.getPath());
    final LockManager lockManager = targetNode.getSession().getWorkspace().getLockManager();
    try {
      final Lock lock = lockManager.getLock(targetNode.getPath());
      if (lock.isDeep()) {
        return;
      }
    } catch (LockException ignored) {
    }
  } else {
    computeAndWritePrimaryTypeDelta(baselineNode, updateNode, targetNode, forceApply);
    computeAndWriteMixinTypesDelta(baselineNode, updateNode, targetNode, forceApply);
    computeAndWritePropertiesDelta(baselineNode, updateNode, targetNode, isNew, forceApply, unprocessedReferences);
  }
  computeAndWriteChildNodesDelta(baselineNode, updateNode, targetNode, forceApply, unprocessedReferences);
}

代码示例来源:origin: pentaho/pentaho-platform

/**
 * Removes a lock token so that it can never be associated with anyone's session again. (To be called after the
 * file has been unlocked and therefore the token associated with the lock is unnecessary.)
 */
public void removeLockToken( final Session session, final PentahoJcrConstants pentahoJcrConstants, final Lock lock )
 throws RepositoryException {
 Node lockTokensNode = getOrCreateLockTokensNode( session, pentahoJcrConstants, lock );
 NodeIterator nodes = lockTokensNode.getNodes( lock.getNode().getIdentifier() );
 if ( nodes.hasNext() ) {
  nodes.nextNode().remove();
 }
 session.save();
}

代码示例来源:origin: apache/jackrabbit

/**
 * Test Lock.isSessionScoped()
 */
public void testIsSessionScoped() throws RepositoryException,
    NotExecutableException {
  // create two lockable nodes
  Node n1 = testRootNode.addNode(nodeName1, testNodeType);
  ensureMixinType(n1, mixLockable);
  Node n2 = testRootNode.addNode(nodeName2, testNodeType);
  ensureMixinType(n2, mixLockable);
  testRootNode.getSession().save();
  // lock node 1 session-scoped
  Lock lock1 = n1.lock(false, true);
  assertTrue("Lock.isSessionScoped() must be true if the lock " +
      "is session-scoped",
      lock1.isSessionScoped());
  // lock node 2 open-scoped
  Lock lock2 = n2.lock(false, false);
  assertFalse("Lock.isSessionScoped() must be false if the lock " +
      "is open-scoped",
      lock2.isSessionScoped());
  n2.unlock();
}

代码示例来源:origin: apache/jackrabbit

public void execute(Session session, Node test) throws RepositoryException {
    // add versionable nodes
    for (int i = 0; i < NUM_OPERATIONS / CONCURRENCY; i++) {
      Node n = test.addNode("test" + i);
      n.addMixin(mixLockable);
      session.save();
      Lock l = n.lock(false, true);
      l.refresh();
      n.unlock();
    }
  }
}, CONCURRENCY);

代码示例来源:origin: apache/jackrabbit

lockedNode.unlock();
lock = lockMgr.lock(
    lockedNode.getPath(), isDeep(), isSessionScoped(), hint, null);
long remaining = lock.getSecondsRemaining();
if (remaining <= hint) {
  if (remaining > 0) {
  long secs = lock.getSecondsRemaining();
  assertTrue(
      "A released lock must return a negative number of seconds, was: " + secs,
  String message = "If the timeout hint is respected the lock"
    + " must be automatically released.";
  assertFalse(message, lock.isLive());
  assertFalse(message, lockedNode.isLocked());
  assertFalse(message, lockMgr.isLocked(lockedNode.getPath()));
  assertFalse(message, lockedNode.hasProperty(Property.JCR_LOCK_IS_DEEP));
  assertFalse(message, lockedNode.hasProperty(Property.JCR_LOCK_OWNER));

代码示例来源:origin: apache/jackrabbit

public void testRefreshAfterTokenTransfer2() throws Exception {
  String lockToken = lock.getLockToken();
  Node n2 = (Node) otherSession.getItem(lockedNode.getPath());
  try {
    superuser.removeLockToken(lockToken);
    otherSession.addLockToken(lockToken);
    n2.getLock().refresh();
  } finally {
    // move lock token back in order to have lock removed properly
    otherSession.removeLockToken(lockToken);
    superuser.addLockToken(lockToken);
  }
}

代码示例来源:origin: apache/jackrabbit

public void testLock2() throws RepositoryException, NotExecutableException {
  Node n = createLockableNode(testRootNode);
  modifyPrivileges(n.getPath(), PrivilegeRegistry.REP_WRITE, false);
  modifyPrivileges(n.getPath(), Privilege.JCR_LOCK_MANAGEMENT, true);
  Node n2 = getTestNode().getNode(nodeName1);
  // all lock operations must succeed
  Lock l = n2.lock(true, true);
  l.refresh();
  n2.unlock();
}

代码示例来源:origin: ModeShape/modeshape

@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());
}

代码示例来源:origin: org.apache/jackrabbit-ocm

/**
 * Throws {@link LockedException} id node is locked so alter nopde cannot be
 * done
 *
 * @param absPath
 *            abs path to node
 * @throws RepositoryException
 * @throws LockedException
 *             if node is locked
 */
protected void checkIfNodeLocked(final String absPath) throws RepositoryException, LockedException {
  Node node = getNode(absPath);
  // Node can hold lock or can be locked with precedencor
  if (node.isLocked()) {
    javax.jcr.lock.Lock lock = getLockManager().getLock(absPath);
    String lockOwner = lock.getLockOwner();
    if (!session.getUserID().equals(lockOwner)) {
      final String path = lock.getNode().getPath();
      throw new LockedException(lockOwner, path);
    }
  }
}

代码示例来源:origin: apache/jackrabbit

public void testGetNodeOnLockObtainedFromNewChild() throws RepositoryException {
  Node newChild = lockedNode.addNode(nodeName3, testNodeType);
  javax.jcr.lock.Lock lock = newChild.getLock();
  assertTrue("Lock.getNode() must return the lock holding node even if lock is obtained from child node.", lock.getNode().isSame(lockedNode));
}

相关文章