use of javax.jcr.lock.Lock in project jackrabbit by apache.
the class SessionScopedLockTest method testImplicitUnlock2.
/**
* Test locks are released when session logs out
*/
public void testImplicitUnlock2() 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);
LockManager lMgr = getLockManager(other);
Lock lock = lMgr.lock(lockedNode.getPath(), isDeep(), isSessionScoped(), getTimeoutHint(), getLockOwner());
// access the locked noded added by another session
testRootNode.refresh(false);
Node n = (Node) superuser.getItem(lockedNode.getPath());
// remove lock implicit by logout lock-holding session
other.logout();
// check if superuser session is properly informed about the unlock
assertFalse(n.isLocked());
assertFalse(n.holdsLock());
try {
n.getLock();
fail("Upon logout of the session a session-scoped lock must be gone.");
} catch (LockException e) {
// ok
}
} finally {
if (other.isLive()) {
other.logout();
}
}
}
use of javax.jcr.lock.Lock in project jackrabbit by apache.
the class LockManagerImpl method lock.
/**
* @see LockStateManager#lock(NodeState,boolean,boolean,long,String)
*/
public Lock lock(NodeState nodeState, boolean isDeep, boolean isSessionScoped, long timeoutHint, String ownerHint) throws RepositoryException {
// retrieve node first
Node lhNode;
Item item = itemManager.getItem(nodeState.getHierarchyEntry());
if (item.isNode()) {
lhNode = (Node) item;
} else {
throw new RepositoryException("Internal error: ItemManager returned Property from NodeState");
}
// execute the operation
LockOperation op = LockOperation.create(nodeState, isDeep, isSessionScoped, timeoutHint, ownerHint);
wspManager.execute(op);
Lock lock = new LockImpl(new LockState(nodeState, op.getLockInfo()), lhNode);
return lock;
}
use of javax.jcr.lock.Lock in project jackrabbit by apache.
the class LockManagerImpl method getLocks.
/**
* {@inheritDoc}
*/
public Lock[] getLocks(SessionImpl session) throws RepositoryException {
acquire();
LockInfo[] infos = getLockInfos(session);
try {
Lock[] locks = new Lock[infos.length];
for (int i = 0; i < infos.length; i++) {
NodeImpl holder = (NodeImpl) session.getItemManager().getItem(infos[i].getId());
locks[i] = new LockImpl(infos[i], holder);
}
return locks;
} finally {
release();
}
}
use of javax.jcr.lock.Lock in project jackrabbit by apache.
the class Locked method with.
/**
* Executes the method {@link #run} within the scope of a lock held on
* <code>lockable</code>.
*
* @param lockable the node where the lock is obtained from.
* @param isDeep <code>true</code> if <code>lockable</code> will be locked
* deep.
* @param timeout time in milliseconds to wait at most to acquire the lock.
* @param isSessionScoped <code>true</code> if the lock is session scoped.
* @return the object returned by {@link #run} or {@link #TIMED_OUT} if the
* lock on <code>lockable</code> could not be acquired within the
* specified timeout.
* @throws IllegalArgumentException if <code>timeout</code> is negative or
* <code>lockable</code> is not
* <i>mix:lockable</i>.
* @throws RepositoryException if {@link #run} throws an exception.
* @throws UnsupportedRepositoryOperationException
* if this repository does not support
* locking.
* @throws InterruptedException if this thread is interrupted while
* waiting for the lock on node
* <code>lockable</code>.
*/
public Object with(Node lockable, boolean isDeep, long timeout, boolean isSessionScoped) throws UnsupportedRepositoryOperationException, RepositoryException, InterruptedException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout must be >= 0");
}
Session session = lockable.getSession();
EventListener listener = null;
try {
// check whether the lockable can be locked at all
String mix = session.getNamespacePrefix(MIX);
if (!lockable.isNodeType(mix + ":lockable")) {
throw new IllegalArgumentException("Node is not lockable");
}
Lock lock = tryLock(lockable, isDeep, timeout, isSessionScoped);
if (lock != null) {
return runAndUnlock(lock);
}
if (timeout == 0) {
return TIMED_OUT;
}
long timelimit;
if (timeout == Long.MAX_VALUE) {
timelimit = Long.MAX_VALUE;
} else {
timelimit = System.currentTimeMillis() + timeout;
}
// node is locked by other session -> register event listener if possible
if (isObservationSupported(session)) {
ObservationManager om = session.getWorkspace().getObservationManager();
listener = new EventListener() {
public void onEvent(EventIterator events) {
synchronized (Locked.this) {
Locked.this.notify();
}
}
};
om.addEventListener(listener, Event.PROPERTY_REMOVED, lockable.getPath(), false, null, null, true);
}
// the current thread when the lockable node is possibly unlocked
for (; ; ) {
synchronized (this) {
lock = tryLock(lockable, isDeep, timeout, isSessionScoped);
if (lock != null) {
return runAndUnlock(lock);
} else {
// check timeout
if (System.currentTimeMillis() > timelimit) {
return TIMED_OUT;
}
if (listener != null) {
// event listener *should* wake us up, however
// there is a chance that removal of the lockOwner
// property is notified before the node is acutally
// unlocked. therefore we use a safety net to wait
// at most 1000 millis.
this.wait(Math.min(1000, timeout));
} else {
// repository does not support observation
// wait at most 50 millis then retry
this.wait(Math.min(50, timeout));
}
}
}
}
} finally {
if (listener != null) {
session.getWorkspace().getObservationManager().removeEventListener(listener);
}
}
}
use of javax.jcr.lock.Lock in project jackrabbit by apache.
the class DavResourceImpl method getLock.
/**
* @see DavResource#getLock(Type, Scope)
*/
public ActiveLock getLock(Type type, Scope scope) {
ActiveLock lock = null;
if (exists() && Type.WRITE.equals(type) && Scope.EXCLUSIVE.equals(scope)) {
// try to retrieve the repository lock information first
try {
if (node.isLocked()) {
Lock jcrLock = node.getLock();
if (jcrLock != null && jcrLock.isLive()) {
lock = new JcrActiveLock(jcrLock);
String lockroot = locator.getFactory().createResourceLocator(locator.getPrefix(), locator.getWorkspacePath(), jcrLock.getNode().getPath(), false).getHref(false);
lock.setLockroot(lockroot);
}
}
} catch (RepositoryException e) {
// LockException (no lock applies) >> should never occur
// RepositoryException, AccessDeniedException or another error >> ignore
}
// could not retrieve a jcr-lock. test if a simple webdav lock is present.
if (lock == null) {
lock = lockManager.getLock(type, scope, this);
}
}
return lock;
}
Aggregations