Class Locked

  • Direct Known Subclasses:
    LockedWrapper

    public abstract class Locked
    extends Object
    Locked is a utility to synchronize modifications on a lockable node. The modification is applied while the lock on the node is held, thus ensuring that the modification will never fail with an InvalidItemStateException. This utility can be used with any JCR Repository, not just Jackrabbit.

    The following example shows how this utility can be used to implement a persistent counter:

     Node counter = ...;
     long nextValue = ((Long) new Locked() {
         protected Object run(Node counter) throws RepositoryException {
             Property seqProp = counter.getProperty("value");
             long value = seqProp.getLong();
             seqProp.setValue(++value);
             seqProp.save();
             return new Long(value);
         }
     }.with(counter, false)).longValue();
     
    If you specify a timeout you need to check the return value whether the run method could be executed within the timeout period:
     Node counter = ...;
     Object ret = new Locked() {
         protected Object run(Node counter) throws RepositoryException {
             Property seqProp = counter.getProperty("value");
             long value = seqProp.getLong();
             seqProp.setValue(++value);
             seqProp.save();
             return new Long(value);
         }
     }.with(counter, false);
     if (ret == Locked.TIMED_OUT) {
         // do whatever you think is appropriate in this case
     } else {
         // get the value
         long nextValue = ((Long) ret).longValue();
     }