Search in sources :

Example 11 with LockException

use of javax.jcr.lock.LockException in project jackrabbit by apache.

the class NodeSetPrimaryTypeTest method testLocked.

/**
     * Tests if <code>Node.setPrimaryType(String)</code> throws a
     * <code>LockException</code> if <code>Node</code> is locked.
     */
public void testLocked() throws NotExecutableException, RepositoryException {
    Session session = testRootNode.getSession();
    if (!isSupported(Repository.OPTION_LOCKING_SUPPORTED)) {
        throw new NotExecutableException("Locking is not supported.");
    }
    // create a node that is lockable
    Node node = testRootNode.addNode(nodeName1, testNodeType);
    // or try to make it lockable if it is not
    ensureMixinType(node, mixLockable);
    testRootNode.getSession().save();
    String primaryTypeName = getPrimaryTypeName(session, node);
    if (primaryTypeName == null) {
        throw new NotExecutableException("No testable node type found");
    }
    // remove first slash of path to get rel path to root
    String pathRelToRoot = node.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 {
            // implementation specific: either throw LockException upon
            // addMixin or upon save.
            node.setPrimaryType(primaryTypeName);
            node.save();
            fail("Node.setPrimaryType(String) must throw a LockException if the node is locked.");
        } catch (LockException e) {
        // success
        }
        // unlock to remove node at tearDown()
        node2.unlock();
    } finally {
        session2.logout();
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) LockException(javax.jcr.lock.LockException) Node(javax.jcr.Node) Session(javax.jcr.Session)

Example 12 with LockException

use of javax.jcr.lock.LockException in project jackrabbit by apache.

the class ExceptionConverter method generate.

public static RepositoryException generate(DavException davExc, int methodCode, String name) {
    String msg = davExc.getMessage();
    if (davExc.hasErrorCondition()) {
        try {
            Element error = davExc.toXml(DomUtil.createDocument());
            if (DomUtil.matches(error, DavException.XML_ERROR, DavConstants.NAMESPACE)) {
                if (DomUtil.hasChildElement(error, "exception", null)) {
                    Element exc = DomUtil.getChildElement(error, "exception", null);
                    if (DomUtil.hasChildElement(exc, "message", null)) {
                        msg = DomUtil.getChildText(exc, "message", null);
                    }
                    if (DomUtil.hasChildElement(exc, "class", null)) {
                        Class<?> cl = Class.forName(DomUtil.getChildText(exc, "class", null));
                        Constructor<?> excConstr = cl.getConstructor(String.class);
                        if (excConstr != null) {
                            Object o = excConstr.newInstance(msg);
                            if (o instanceof PathNotFoundException && methodCode == DavMethods.DAV_POST) {
                                // see JCR-2536
                                return new InvalidItemStateException(msg);
                            } else if (o instanceof RepositoryException) {
                                return (RepositoryException) o;
                            } else if (o instanceof Exception) {
                                return new RepositoryException(msg, (Exception) o);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            return new RepositoryException(e);
        }
    }
    // make sure an exception is generated
    switch(davExc.getErrorCode()) {
        // TODO: mapping DAV_error to jcr-exception is ambiguous. to be improved
        case DavServletResponse.SC_NOT_FOUND:
            switch(methodCode) {
                case DavMethods.DAV_DELETE:
                case DavMethods.DAV_MKCOL:
                case DavMethods.DAV_PUT:
                case DavMethods.DAV_POST:
                    // been made.
                    return new InvalidItemStateException(msg, davExc);
                default:
                    return new ItemNotFoundException(msg, davExc);
            }
        case DavServletResponse.SC_LOCKED:
            return new LockException(msg, davExc);
        case DavServletResponse.SC_METHOD_NOT_ALLOWED:
            return new ConstraintViolationException(msg, davExc);
        case DavServletResponse.SC_CONFLICT:
            return new InvalidItemStateException(msg, davExc);
        case DavServletResponse.SC_PRECONDITION_FAILED:
            return new LockException(msg, davExc);
        case DavServletResponse.SC_NOT_IMPLEMENTED:
            if (methodCode > 0 && name != null) {
                return new UnsupportedRepositoryOperationException("Missing implementation: Method " + name + " could not be executed", davExc);
            } else {
                return new UnsupportedRepositoryOperationException("Missing implementation", davExc);
            }
        default:
            return new RepositoryException(msg, davExc);
    }
}
Also used : UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) InvalidItemStateException(javax.jcr.InvalidItemStateException) Element(org.w3c.dom.Element) RepositoryException(javax.jcr.RepositoryException) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) ItemNotFoundException(javax.jcr.ItemNotFoundException) PathNotFoundException(javax.jcr.PathNotFoundException) UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) DavException(org.apache.jackrabbit.webdav.DavException) RepositoryException(javax.jcr.RepositoryException) LockException(javax.jcr.lock.LockException) InvalidItemStateException(javax.jcr.InvalidItemStateException) LockException(javax.jcr.lock.LockException) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) PathNotFoundException(javax.jcr.PathNotFoundException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 13 with LockException

use of javax.jcr.lock.LockException in project jackrabbit by apache.

the class LockedWrapperTest method testTimeout.

public void testTimeout() throws RepositoryException, InterruptedException {
    final Node lockable = testRootNode.addNode("testTimeout" + System.currentTimeMillis());
    lockable.addMixin(mixLockable);
    superuser.save();
    ExecutorService e = Executors.newFixedThreadPool(2);
    Future<?> lockMaster = e.submit(buildNewTimeoutCallable(lockable.getPath(), true));
    Future<?> lockSlave = e.submit(buildNewTimeoutCallable(lockable.getPath(), false));
    e.shutdown();
    try {
        lockMaster.get();
        lockSlave.get();
    } catch (ExecutionException ex) {
        if (ex.getCause().getClass().isAssignableFrom(LockException.class)) {
            return;
        }
        fail(ex.getMessage());
    }
    fail("was expecting a LockException");
}
Also used : LockException(javax.jcr.lock.LockException) Node(javax.jcr.Node) ExecutorService(java.util.concurrent.ExecutorService) ExecutionException(java.util.concurrent.ExecutionException)

Example 14 with LockException

use of javax.jcr.lock.LockException in project jackrabbit by apache.

the class VersionTest method testLockJcr2.

/**
     * Tests if <code>Version.lock(boolean, boolean)</code> throws a {@link
     * LockException}
     */
public void testLockJcr2() throws Exception {
    LockManager lockManager = version.getSession().getWorkspace().getLockManager();
    String path = version.getPath();
    try {
        lockManager.lock(path, true, true, 60, "");
        fail("Version should not be lockable: Version.lock(true,true) did not throw a LockException");
    } catch (LockException success) {
    }
    try {
        lockManager.lock(path, true, false, 60, "");
        fail("Version should not be lockable: Version.lock(true,false) did not throw a LockException");
    } catch (LockException success) {
    }
    try {
        lockManager.lock(path, false, true, 60, "");
        fail("Version should not be lockable: Version.lock(false,true) did not throw a LockException");
    } catch (LockException success) {
    }
    try {
        lockManager.lock(path, false, false, 60, "");
        fail("Version should not be lockable: Version.lock(false,false) did not throw a LockException");
    } catch (LockException success) {
    }
}
Also used : LockManager(javax.jcr.lock.LockManager) LockException(javax.jcr.lock.LockException)

Example 15 with LockException

use of javax.jcr.lock.LockException in project jackrabbit by apache.

the class RetentionPolicyTest method testSetRetentionPolicyOnLockedNode.

public void testSetRetentionPolicyOnLockedNode() throws NotExecutableException, RepositoryException {
    String childPath = getLockedChildNode().getPath();
    // get another session.
    Session otherS = getHelper().getSuperuserSession();
    try {
        RetentionManager rmgr = getRetentionManager(otherS);
        rmgr.setRetentionPolicy(childPath, getApplicableRetentionPolicy());
        otherS.save();
        fail("Setting a retention policy on a locked node must throw LockException.");
    } catch (LockException e) {
    // success
    } finally {
        otherS.logout();
        if (retentionMgr.getRetentionPolicy(childPath) != null) {
            retentionMgr.removeRetentionPolicy(childPath);
        }
        superuser.save();
    }
}
Also used : RetentionManager(javax.jcr.retention.RetentionManager) LockException(javax.jcr.lock.LockException) 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