use of javax.jcr.lock.LockException in project jackrabbit by apache.
the class LockTest method testAddRemoveLockToken.
/**
* Test lock token functionality
*/
public void testAddRemoveLockToken() throws Exception {
// create new node
Node n = testRootNode.addNode(nodeName1, testNodeType);
ensureMixinType(n, mixLockable);
testRootNode.getSession().save();
// lock node and get lock token
Lock lock = n.lock(false, false);
String lockToken = lock.getLockToken();
try {
// assert: session must get a non-null lock token
assertNotNull("session must get a non-null lock token", lockToken);
// assert: session must hold lock token
assertTrue("session must hold lock token", containsLockToken(superuser, lockToken));
// remove lock token
superuser.removeLockToken(lockToken);
String nlt = lock.getLockToken();
assertTrue("freshly obtained lock token must either be null or the same as the one returned earlier", nlt == null || nlt.equals(lockToken));
// assert: session must still hold lock token
assertFalse("session must not hold lock token", containsLockToken(superuser, lockToken));
// assert: session unable to modify node
try {
n.addNode(nodeName2, testNodeType);
fail("session unable to modify node");
} catch (LockException e) {
// expected
}
// add lock token
superuser.addLockToken(lockToken);
// assert: session must get a non-null lock token
assertNotNull("session must get a non-null lock token", lock.getLockToken());
// assert: session must hold lock token
assertTrue("session must hold lock token", containsLockToken(superuser, lock.getLockToken()));
// assert: session able to modify node
n.addNode(nodeName2, testNodeType);
} finally {
// make sure lock token is added even if test fail
superuser.addLockToken(lockToken);
}
}
use of javax.jcr.lock.LockException in project jackrabbit by apache.
the class LockManagerImpl method externalUnlock.
/**
* {@inheritDoc}
*/
public void externalUnlock(NodeId nodeId) throws RepositoryException {
acquire();
try {
Path path = getPath(sysSession, nodeId);
PathMap.Element<LockInfo> element = lockMap.map(path, true);
if (element == null) {
throw new LockException("Node not locked: " + path.toString());
}
LockInfo info = element.get();
if (info == null) {
throw new LockException("Node not locked: " + path.toString());
}
element.set(null);
info.setLive(false);
save();
} finally {
release();
}
}
use of javax.jcr.lock.LockException in project jackrabbit by apache.
the class XAEnvironment method removeLockToken.
/**
* Remove lock token from this environment.
* @param session
* @param lt lock token
* @throws RepositoryException
*/
public void removeLockToken(SessionImpl session, String lt) throws RepositoryException {
try {
NodeId id = LockInfo.parseLockToken(lt);
NodeImpl node = (NodeImpl) session.getItemManager().getItem(id);
LockInfo info = getLockInfo(node);
if (info != null) {
if (info.isLockHolder(session)) {
info.setLockHolder(null);
} else if (info.getLockHolder() != null) {
String msg = "Cannot remove lock token: lock held by other session.";
log.warn(msg);
throw new LockException(msg);
}
}
// inform SessionLockManager
getSessionLockManager(session).lockTokenRemoved(lt);
} catch (IllegalArgumentException e) {
String msg = "Bad lock token: " + e.getMessage();
log.warn(msg);
throw new LockException(msg);
}
}
use of javax.jcr.lock.LockException in project jackrabbit by apache.
the class XAEnvironment method addLockToken.
/**
* Add lock token to this environment.
* @param session
* @param lt lock token
* @throws RepositoryException
*/
public void addLockToken(SessionImpl session, String lt) throws RepositoryException {
try {
NodeId id = LockInfo.parseLockToken(lt);
NodeImpl node = (NodeImpl) session.getItemManager().getItem(id);
LockInfo info = getLockInfo(node);
if (info != null && !info.isLockHolder(session)) {
if (info.getLockHolder() == null) {
info.setLockHolder(session);
} else {
String msg = "Cannot add lock token: lock already held by other session.";
log.warn(msg);
throw new LockException(msg);
}
}
// inform SessionLockManager
getSessionLockManager(session).lockTokenAdded(lt);
} catch (IllegalArgumentException e) {
String msg = "Bad lock token: " + e.getMessage();
log.warn(msg);
throw new LockException(msg);
}
}
use of javax.jcr.lock.LockException in project jackrabbit by apache.
the class XAEnvironment method lock.
/**
* Lock some node.
* @param node node to lock
* @param isDeep <code>true</code> to deep lock this node;
* <code>false</code> otherwise
* @param isSessionScoped <code>true</code> if lock should be session scoped;
* <code>false</code> otherwise
* @param timeoutHint
* @param ownerInfo
* @throws LockException if node is already locked
* @throws RepositoryException if an error occurs
*/
public LockInfo lock(NodeImpl node, boolean isDeep, boolean isSessionScoped, long timeoutHint, String ownerInfo) throws LockException, RepositoryException {
NodeId id = node.getNodeId();
// check negative set first
XALockInfo info = unlockedNodesMap.get(id);
if (info != null) {
// if settings are compatible, this is effectively a no-op
if (info.isDeep() == isDeep && info.isSessionScoped() == isSessionScoped) {
unlockedNodesMap.remove(id);
operations.remove(info);
return lockMgr.getLockInfo(id);
}
}
// verify node is not already locked.
if (isLocked(node)) {
throw new LockException("Node locked.");
}
// create a new lock info for this node
String lockOwner = (ownerInfo != null) ? ownerInfo : node.getSession().getUserID();
info = new XALockInfo(node, isSessionScoped, isDeep, timeoutHint, lockOwner);
SessionImpl session = (SessionImpl) node.getSession();
info.setLockHolder(session);
info.setLive(true);
LockManagerImpl.getSessionLockManager(session).lockTokenAdded(info.getLockToken());
lockedNodesMap.put(id, info);
operations.add(info);
return info;
}
Aggregations