use of javax.jcr.lock.LockException in project jackrabbit by apache.
the class WorkspaceCopyTest method testCopyNodesLocked.
/**
* A LockException is thrown if a lock prevents the copy.
*/
public void testCopyNodesLocked() throws RepositoryException, NotExecutableException {
// we assume repository supports locking
String dstAbsPath = node2.getPath() + "/" + node1.getName();
// get other session
Session otherSession = getHelper().getReadWriteSession();
try {
// get lock target node in destination wsp through other session
Node lockTarget = (Node) otherSession.getItem(node2.getPath());
// add mixin "lockable" to be able to lock the node
ensureMixinType(lockTarget, mixLockable);
lockTarget.getParent().save();
// lock dst parent node using other session
lockTarget.lock(true, true);
try {
workspace.copy(node1.getPath(), dstAbsPath);
fail("LockException was expected.");
} catch (LockException e) {
// successful
} finally {
lockTarget.unlock();
}
} finally {
otherSession.logout();
}
}
use of javax.jcr.lock.LockException in project jackrabbit by apache.
the class SessionImpl method importXML.
/**
* @see javax.jcr.Session#importXML(String, java.io.InputStream, int)
*/
@Override
public void importXML(String parentAbsPath, InputStream in, int uuidBehavior) throws IOException, PathNotFoundException, ItemExistsException, ConstraintViolationException, VersionException, InvalidSerializedDataException, LockException, RepositoryException {
// NOTE: checks are performed by 'getImportContentHandler'
ImportHandler handler = (ImportHandler) getImportContentHandler(parentAbsPath, uuidBehavior);
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
SAXParser parser = factory.newSAXParser();
parser.parse(new InputSource(in), handler);
} catch (SAXException se) {
// check for wrapped repository exception
Exception e = se.getException();
if (e != null && e instanceof RepositoryException) {
throw (RepositoryException) e;
} else {
String msg = "failed to parse XML stream";
log.debug(msg);
throw new InvalidSerializedDataException(msg, se);
}
} catch (ParserConfigurationException e) {
throw new RepositoryException("SAX parser configuration error", e);
} finally {
// JCR-2903
in.close();
}
}
use of javax.jcr.lock.LockException in project jackrabbit by apache.
the class XATest method testAddRemoveLockToken.
/**
* Test add and remove lock tokens in a transaction
* @throws Exception
*/
public void testAddRemoveLockToken() throws Exception {
// create new node and lock it
UserTransaction utx = new UserTransactionImpl(superuser);
utx.begin();
// add node that is both lockable and referenceable, save
Node rootNode = superuser.getRootNode();
Node n = rootNode.addNode(nodeName1);
n.addMixin(mixLockable);
n.addMixin(mixReferenceable);
rootNode.save();
String uuid = n.getUUID();
// lock this new node
Lock lock = n.lock(true, false);
String lockToken = lock.getLockToken();
// 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));
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));
// commit
utx.commit();
// refresh Lock Info
lock = n.getLock();
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));
Session other = getHelper().getSuperuserSession();
try {
// start new Transaction and try to add lock token
utx = new UserTransactionImpl(other);
utx.begin();
Node otherNode = other.getNodeByUUID(uuid);
assertTrue("Node not locked", otherNode.isLocked());
try {
otherNode.setProperty(propertyName1, "foo");
fail("Lock exception should be thrown");
} catch (LockException e) {
// expected
}
// add lock token
other.addLockToken(lockToken);
// refresh Lock Info
lock = otherNode.getLock();
// assert: session must hold lock token
assertTrue("session must hold lock token", containsLockToken(other, lock.getLockToken()));
otherNode.unlock();
assertFalse("Node is locked", otherNode.isLocked());
otherNode.setProperty(propertyName1, "foo");
other.save();
utx.commit();
} finally {
other.logout();
}
}
use of javax.jcr.lock.LockException in project jackrabbit by apache.
the class SessionRemoveItemTest method testRemoveLockedChildItem.
public void testRemoveLockedChildItem() throws RepositoryException, NotExecutableException {
// add a child property and a child node to test deep lock effect.
Node childN = removeNode.addNode(nodeName2);
Value v = getJcrValue(superuser, RepositoryStub.PROP_PROP_VALUE2, RepositoryStub.PROP_PROP_TYPE2, "propvalue2");
Property childP = removeNode.setProperty(propertyName2, v);
removeNode.save();
ensureMixinType(removeNode, mixLockable);
removeNode.save();
// make sure the test node is locked.
removeNode.lock(true, true);
Session testSession = null;
try {
testSession = getHelper().getReadWriteSession();
try {
testSession.removeItem(childN.getPath());
testSession.save();
fail("Locked child node cannot be removed by another session.");
} catch (LockException e) {
// success
}
try {
testSession.removeItem(childP.getPath());
testSession.save();
fail("Locked child node cannot be removed by another session.");
} catch (LockException e) {
// success
}
} finally {
if (testSession != null) {
testSession.logout();
}
removeNode.unlock();
}
}
use of javax.jcr.lock.LockException in project jackrabbit by apache.
the class SessionTest method testMoveLockException.
/**
* Calls {@link javax.jcr.Session#move(String src, String dest)} where
* the parent node of src is locked.
* <p>
* Should throw a {@link LockException} immediately or on save.
*/
public void testMoveLockException() throws NotExecutableException, RepositoryException {
Session session = superuser;
if (!isSupported(Repository.OPTION_LOCKING_SUPPORTED)) {
throw new NotExecutableException("Locking is not supported.");
}
// create a node that is lockable
Node lockableNode = testRootNode.addNode(nodeName1, testNodeType);
// or try to make it lockable if it is not
ensureMixinType(lockableNode, mixLockable);
// add a sub node (the one that is tried to move later on)
Node srcNode = lockableNode.addNode(nodeName1, testNodeType);
testRootNode.getSession().save();
// remove first slash of path to get rel path to root
String pathRelToRoot = lockableNode.getPath().substring(1);
// access node through another session to lock it
Session session2 = getHelper().getSuperuserSession();
try {
Node node2 = session2.getRootNode().getNode(pathRelToRoot);
node2.lock(true, true);
try {
String destPath = testRoot + "/" + nodeName2;
session.move(srcNode.getPath(), destPath);
testRootNode.getSession().save();
fail("A LockException is thrown either immediately or on save if a lock prevents the move.");
} catch (LockException e) {
// success
}
} finally {
session2.logout();
}
}
Aggregations