Search in sources :

Example 51 with LockException

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();
    }
}
Also used : LockException(javax.jcr.lock.LockException) Node(javax.jcr.Node) Session(javax.jcr.Session)

Example 52 with LockException

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();
    }
}
Also used : InputSource(org.xml.sax.InputSource) ImportHandler(org.apache.jackrabbit.jcr2spi.xml.ImportHandler) SAXParser(javax.xml.parsers.SAXParser) InvalidSerializedDataException(javax.jcr.InvalidSerializedDataException) RepositoryException(javax.jcr.RepositoryException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ItemExistsException(javax.jcr.ItemExistsException) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) NoSuchWorkspaceException(javax.jcr.NoSuchWorkspaceException) VersionException(javax.jcr.version.VersionException) InvalidSerializedDataException(javax.jcr.InvalidSerializedDataException) InvalidItemStateException(javax.jcr.InvalidItemStateException) ItemNotFoundException(javax.jcr.ItemNotFoundException) LoginException(javax.jcr.LoginException) SAXException(org.xml.sax.SAXException) AccessControlException(java.security.AccessControlException) AccessDeniedException(javax.jcr.AccessDeniedException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException) LockException(javax.jcr.lock.LockException) NamespaceException(javax.jcr.NamespaceException) IOException(java.io.IOException) UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) MalformedPathException(org.apache.jackrabbit.spi.commons.conversion.MalformedPathException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 53 with LockException

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();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) LockException(javax.jcr.lock.LockException) Node(javax.jcr.Node) Lock(javax.jcr.lock.Lock) Session(javax.jcr.Session)

Example 54 with LockException

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();
    }
}
Also used : LockException(javax.jcr.lock.LockException) Node(javax.jcr.Node) Value(javax.jcr.Value) Property(javax.jcr.Property) Session(javax.jcr.Session)

Example 55 with LockException

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();
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) LockException(javax.jcr.lock.LockException) Node(javax.jcr.Node) Session(javax.jcr.Session)

Aggregations

LockException (javax.jcr.lock.LockException)61 Node (javax.jcr.Node)33 Session (javax.jcr.Session)24 Lock (javax.jcr.lock.Lock)13 RepositoryException (javax.jcr.RepositoryException)11 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)11 NodeImpl (org.apache.jackrabbit.core.NodeImpl)7 LockManager (javax.jcr.lock.LockManager)6 NodeId (org.apache.jackrabbit.core.id.NodeId)6 ItemNotFoundException (javax.jcr.ItemNotFoundException)5 PathMap (org.apache.jackrabbit.spi.commons.name.PathMap)5 RetentionManager (javax.jcr.retention.RetentionManager)4 SessionImpl (org.apache.jackrabbit.core.SessionImpl)4 Value (javax.jcr.Value)3 Path (org.apache.jackrabbit.spi.Path)3 IOException (java.io.IOException)2 InvalidItemStateException (javax.jcr.InvalidItemStateException)2 PathNotFoundException (javax.jcr.PathNotFoundException)2 Property (javax.jcr.Property)2 UnsupportedRepositoryOperationException (javax.jcr.UnsupportedRepositoryOperationException)2