Search in sources :

Example 31 with LockException

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

the class AddNodeTest method testAddNodeOnLocked.

/**
     * Writing to a locked node must throw LockException even if the lock
     * isn't detected withing Jcr2Spi.
     *
     * @throws Exception
     * @see <a href="https://issues.apache.org/jira/browse/JCR-2585">JCR-2585</a>
     */
public void testAddNodeOnLocked() throws Exception {
    Session s = getHelper().getSuperuserSession();
    try {
        Node node = s.getNode(testRootNode.getPath());
        Node n = node.addNode(nodeName1);
        n.setProperty(propertyName1, "value");
        testRootNode.lock(true, true);
        s.save();
    } catch (LockException e) {
    // success
    } finally {
        s.logout();
    }
}
Also used : LockException(javax.jcr.lock.LockException) Node(javax.jcr.Node) Session(javax.jcr.Session)

Example 32 with LockException

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

the class ContentLoaderService method getBundleContentInfo.

/**
     * Return the bundle content info and make an exclusive lock.
     * @param session
     * @param bundle
     * @return The map of bundle content info or null.
     * @throws RepositoryException
     */
public Map<String, Object> getBundleContentInfo(final Session session, final Bundle bundle, boolean create) throws RepositoryException {
    final String nodeName = bundle.getSymbolicName();
    final Node parentNode = (Node) session.getItem(BUNDLE_CONTENT_NODE);
    if (!parentNode.hasNode(nodeName)) {
        if (!create) {
            return null;
        }
        try {
            final Node bcNode = parentNode.addNode(nodeName, "nt:unstructured");
            bcNode.addMixin("mix:lockable");
            parentNode.save();
        } catch (RepositoryException re) {
            // for concurrency issues (running in a cluster) we ignore exceptions
            this.log.warn("Unable to create node " + nodeName, re);
            session.refresh(true);
        }
    }
    final Node bcNode = parentNode.getNode(nodeName);
    if (bcNode.isLocked()) {
        return null;
    }
    try {
        bcNode.lock(false, true);
    } catch (LockException le) {
        return null;
    }
    final Map<String, Object> info = new HashMap<String, Object>();
    if (bcNode.hasProperty(PROPERTY_CONTENT_LOADED_AT)) {
        info.put(PROPERTY_CONTENT_LOADED_AT, bcNode.getProperty(PROPERTY_CONTENT_LOADED_AT).getDate());
    }
    if (bcNode.hasProperty(PROPERTY_CONTENT_LOADED)) {
        info.put(PROPERTY_CONTENT_LOADED, bcNode.getProperty(PROPERTY_CONTENT_LOADED).getBoolean());
    } else {
        info.put(PROPERTY_CONTENT_LOADED, false);
    }
    if (bcNode.hasProperty(PROPERTY_UNINSTALL_PATHS)) {
        final Value[] values = bcNode.getProperty(PROPERTY_UNINSTALL_PATHS).getValues();
        final String[] s = new String[values.length];
        for (int i = 0; i < values.length; i++) {
            s[i] = values[i].getString();
        }
        info.put(PROPERTY_UNINSTALL_PATHS, s);
    }
    return info;
}
Also used : LockException(javax.jcr.lock.LockException) HashMap(java.util.HashMap) Node(javax.jcr.Node) Value(javax.jcr.Value) RepositoryException(javax.jcr.RepositoryException)

Example 33 with LockException

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

the class SetValueLockExceptionTest method doTestSetValueLockException.

/**
     * Tests if a LockException is thrown if a value is added to a property of a locked node.
     *
     * @param type The possible argument types.
     * @throws RepositoryException
     */
public void doTestSetValueLockException(int type) throws RepositoryException {
    // lock if not yet locked
    if (!testNode.holdsLock()) {
        testNode.lock(false, false);
        superuser.save();
    }
    // another session
    Session session = getHelper().getReadWriteSession();
    try {
        Node node = (Node) session.getItem(testNode.getPath());
        Property prop = null;
        switch(type) {
            case PropertyType.BINARY:
                ByteArrayInputStream in = new ByteArrayInputStream(binaryValue);
                prop = node.getProperty(binaryProp);
                prop.setValue(in);
                break;
            case PropertyType.BOOLEAN:
                prop = node.getProperty(booleanProp);
                prop.setValue(booleanValue);
                break;
            case PropertyType.DATE:
                prop = node.getProperty(dateProp);
                prop.setValue(dateValue);
                break;
            case PropertyType.DOUBLE:
                prop = node.getProperty(doubleProp);
                prop.setValue(doubleValue);
                break;
            case PropertyType.LONG:
                prop = node.getProperty(longProp);
                prop.setValue(longValue);
                break;
            case PropertyType.REFERENCE:
                prop = node.getProperty(referenceProp);
                if (referenceNode != null) {
                    prop.setValue(referenceNode);
                }
                break;
            case PropertyType.STRING:
                prop = node.getProperty(stringProp);
                prop.setValue(stringValue);
                break;
            case TYPE_VALUE:
                prop = node.getProperty(stringProp);
                Value value = session.getValueFactory().createValue(stringValue);
                prop.setValue(value);
                break;
            case TYPE_MULTIVAL:
                prop = node.getProperty(multiStringProp);
                Value[] values = { session.getValueFactory().createValue(stringValue), session.getValueFactory().createValue(stringValue), session.getValueFactory().createValue(stringValue) };
                prop.setValue(values);
                break;
            case TYPE_MULTSTRING:
                prop = node.getProperty(multiStringProp);
                String[] strVals = { stringValue, stringValue, stringValue };
                prop.setValue(strVals);
                break;
        }
        session.save();
        fail("Property.setValue should throw a LockException " + "if the parent node holds a Lock.");
    } catch (LockException le) {
    // ok
    } finally {
        session.logout();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) LockException(javax.jcr.lock.LockException) Node(javax.jcr.Node) Value(javax.jcr.Value) Property(javax.jcr.Property) Session(javax.jcr.Session)

Example 34 with LockException

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

the class AbstractLockTest method testGetLockOnChild.

public void testGetLockOnChild() throws RepositoryException {
    if (isDeep()) {
        // get lock must succeed even if child is not lockable.
        Lock lock = childNode.getLock();
        assertNotNull(lock);
        assertTrue("Lock.getNode() must return the lock holding node", lockedNode.isSame(lock.getNode()));
        Lock lock2 = lockMgr.getLock(childNode.getPath());
        assertNotNull(lock2);
        assertTrue("Lock.getNode() must return the lock holding node", lockedNode.isSame(lock2.getNode()));
    } else {
        try {
            childNode.getLock();
            fail("Node.getLock() must throw if node is not locked.");
        } catch (LockException e) {
        // success
        }
        try {
            lockMgr.getLock(childNode.getPath());
            fail("LockManager.getLock(String) must throw if node is not locked.");
        } catch (LockException e) {
        // success
        }
    }
}
Also used : LockException(javax.jcr.lock.LockException) Lock(javax.jcr.lock.Lock)

Example 35 with LockException

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

the class LockManagerTest method testLockTransfer3.

public void testLockTransfer3() throws Exception {
    assertLockable(testNode);
    boolean sessionScoped = false;
    Lock l = lockMgr.lock(testPath, true, sessionScoped, Long.MAX_VALUE, null);
    String ltoken = l.getLockToken();
    Session other = getHelper().getReadWriteSession();
    LockManager otherLockMgr = getLockManager(other);
    try {
        lockMgr.removeLockToken(ltoken);
        otherLockMgr.addLockToken(ltoken);
        lockMgr.removeLockToken(ltoken);
        fail("Removing a token that has been transfered to another manager must fail.");
    } catch (LockException e) {
    // success
    } finally {
        otherLockMgr.removeLockToken(ltoken);
        lockMgr.addLockToken(ltoken);
        other.logout();
    }
}
Also used : LockManager(javax.jcr.lock.LockManager) LockException(javax.jcr.lock.LockException) Lock(javax.jcr.lock.Lock) 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