jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r803604 - in /jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock: LockImpl.java LockInfo.java LockManagerImpl.java XAEnvironment.java XALockManager.java
Date Wed, 12 Aug 2009 17:11:54 GMT
Author: jukka
Date: Wed Aug 12 17:11:54 2009
New Revision: 803604

URL: http://svn.apache.org/viewvc?rev=803604&view=rev
Log:
JCR-1590: JSR 283: Locking

Add tracking of timeout information. Work in progress...

Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockInfo.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockManagerImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XAEnvironment.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XALockManager.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockImpl.java?rev=803604&r1=803603&r2=803604&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockImpl.java
Wed Aug 12 17:11:54 2009
@@ -109,29 +109,40 @@
             info.throwLockException(
                     "Lock is not live any more",
                     (SessionImpl) node.getSession());
-        }
-        if (!isLockOwningSession()) {
+        } else if (!isLockOwningSession()) {
             info.throwLockException(
                     "Session does not hold lock.",
                     (SessionImpl) node.getSession());
+        } else {
+            // make sure the current session has sufficient privileges to refresh
+            // the lock.
+            SessionImpl session = (SessionImpl) node.getSession();
+            session.getAccessManager().checkPermission(
+                    node.getPrimaryPath(), Permission.LOCK_MNGMT);
+
+            // Update the lock timeout
+            info.updateTimeoutTime();
         }
-        // make sure the current session has sufficient privileges to refresh
-        // the lock.
-        SessionImpl s = (SessionImpl) node.getSession();
-        s.getAccessManager().checkPermission(node.getPrimaryPath(), Permission.LOCK_MNGMT);
-
-        // TODO: TOBEFIXED for 2.0
-        // TODO  - add refresh if timeout is supported -> see #getSecondsRemaining
-        // since a lock has no expiration date no other action is required
     }
 
     //--------------------------------------------------< new JSR 283 methods >
 
-    /**
-     * @see javax.jcr.lock.Lock#getSecondsRemaining()
-     */
+    /** {@inheritDoc} */
     public long getSecondsRemaining() {
-        return info.getSecondsRemaining();
+        if (!info.isLive()) {
+            return -1;
+        } else {
+            return Long.MAX_VALUE;
+        }
+
+        // TODO JCR-1590: Disabled until locks get unlocked when they timeout
+//        long timeout = info.getTimeoutTime();
+//        if (timeout == Long.MAX_VALUE) {
+//            return Long.MAX_VALUE;
+//        }
+//
+//        long now = (System.currentTimeMillis() + 999) / 1000; // round up
+//        return Math.max(timeout - now, 1); // must always be positive
     }
 
     /**

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockInfo.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockInfo.java?rev=803604&r1=803603&r2=803604&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockInfo.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockInfo.java
Wed Aug 12 17:11:54 2009
@@ -29,14 +29,11 @@
 public abstract class LockInfo {
 
     /**
-     * Constant for the undefined or infinite timeout.
+     * The biggest possible timeout hint value (in seconds), used to avoid
+     * overflows when calculating the timeout. 100 years should be plenty
+     * enough for anyone...
      */
-    static final long TIMEOUT_INFINITE = Long.MAX_VALUE;
-
-    /**
-     * Constant for the expired timeout.
-     */
-    static final long TIMEOUT_EXPIRED = -1;
+    private static final long MAXIMUM_TIMEOUT = 100L * 365L * 24L * 60L * 60L;
 
     /**
      * Lock holder node id. Used also as the lock token.
@@ -59,7 +56,20 @@
     private final String lockOwner;
 
     /**
-     * Flag indicating whether this lock is live
+     * Lock timeout hint (in seconds) given when the lock was created.
+     * Guaranteed to be between 0 and {@link #MAXIMUM_TIMEOUT}. If the value
+     * is 0, then this lock will not timeout.
+     */
+    private final long timeoutHint;
+
+    /**
+     * Time (in seconds since epoch) when this lock will timeout. Set to
+     * {@link Long#MAX_VALUE} if this lock will not timeout.
+     */
+    private long timeoutTime;
+
+    /**
+     * Flag indicating whether this lock is live. See also {@link #timeoutTime}.
      */
     private boolean live;
 
@@ -75,7 +85,7 @@
      * @param sessionScoped whether lock token is session scoped
      * @param deep          whether lock is deep
      * @param lockOwner     owner of lock
-     * @param timeoutHint   the timeoutHint
+     * @param timeoutHint   lock timeout hint in seconds
      */
     protected LockInfo(
             NodeId id, boolean sessionScoped, boolean deep,
@@ -84,7 +94,18 @@
         this.sessionScoped = sessionScoped;
         this.deep = deep;
         this.lockOwner = lockOwner;
-        // TODO: TOBEFIXED for 2.0 respect and deal with timeout hint.
+        this.timeoutHint = timeoutHint;
+
+        updateTimeoutTime();
+    }
+
+    protected LockInfo(LockInfo that) {
+        this.id = that.id;
+        this.sessionScoped = that.sessionScoped;
+        this.deep = that.deep;
+        this.lockOwner = that.lockOwner;
+        this.timeoutHint = that.timeoutHint;
+        this.timeoutTime = that.timeoutTime;
     }
 
     /**
@@ -189,14 +210,35 @@
     }
 
     /**
-     * Return the number of seconds remaining until the lock expires.
+     * Returns the timeout hint given when the lock was created.
      *
-     * @return number of seconds remaining until the lock expires.
+     * @return timeout hint (in seconds)
      */
-    public long getSecondsRemaining() {
-        // TODO: TOBEFIXED for 2.0
-        // TODO  - add support for timeout specified by the API user -> LockManager#lock
-        return isLive() ? TIMEOUT_INFINITE : TIMEOUT_EXPIRED;
+    public long getTimeoutHint() {
+        return timeoutHint;
+    }
+
+    /**
+     * Returns the time when this lock will expire. 
+     *
+     * @return timeout time in seconds after epoch
+     */
+    public long getTimeoutTime() {
+        return timeoutTime;
+    }
+
+    /**
+     * Updates the timeout time of this lock based on current time and
+     * the timeout hint specified for this lock. The timeout time is always
+     * rounded up.
+     */
+    public void updateTimeoutTime() {
+        if (timeoutHint > 0 && timeoutHint <= MAXIMUM_TIMEOUT) {
+            long now = (System.currentTimeMillis() + 999) / 1000; // round up
+            this.timeoutTime = now + timeoutHint;
+        } else {
+            this.timeoutTime = Long.MAX_VALUE;
+        }
     }
 
     /**

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockManagerImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockManagerImpl.java?rev=803604&r1=803603&r2=803604&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockManagerImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockManagerImpl.java
Wed Aug 12 17:11:54 2009
@@ -224,7 +224,8 @@
             InternalLockInfo info = new InternalLockInfo(
                     id, false,
                     node.getProperty(NameConstants.JCR_LOCKISDEEP).getBoolean(),
-                    node.getProperty(NameConstants.JCR_LOCKOWNER).getString());
+                    node.getProperty(NameConstants.JCR_LOCKOWNER).getString(),
+                    Long.MAX_VALUE);
             info.setLive(true);
             lockMap.put(path, info);
         } catch (RepositoryException e) {
@@ -464,7 +465,7 @@
      */
     public Lock lock(NodeImpl node, boolean isDeep, boolean isSessionScoped)
             throws LockException, RepositoryException {
-        return lock(node, isDeep, isSessionScoped, LockInfo.TIMEOUT_INFINITE, null);
+        return lock(node, isDeep, isSessionScoped, Long.MAX_VALUE, null);
     }
 
     public Lock lock(NodeImpl node, boolean isDeep, boolean isSessionScoped, long timoutHint,
String ownerInfo)
@@ -1171,19 +1172,6 @@
          * @param sessionScoped whether lock token is session scoped
          * @param deep          whether lock is deep
          * @param lockOwner     owner of lock
-         */
-        public InternalLockInfo(NodeId lockToken, boolean sessionScoped,
-                                boolean deep, String lockOwner) {
-            this(lockToken, sessionScoped, deep, lockOwner, TIMEOUT_INFINITE);
-        }
-
-        /**
-         * Create a new instance of this class.
-         *
-         * @param lockToken     lock token
-         * @param sessionScoped whether lock token is session scoped
-         * @param deep          whether lock is deep
-         * @param lockOwner     owner of lock
          * @param timeoutHint
          */
         public InternalLockInfo(NodeId lockToken, boolean sessionScoped,
@@ -1262,7 +1250,8 @@
             Path path = getPath(sysSession, nodeId);
 
             // create lock token
-            InternalLockInfo info = new InternalLockInfo(nodeId, false, isDeep, lockOwner);
+            InternalLockInfo info = new InternalLockInfo(
+                    nodeId, false, isDeep, lockOwner, Long.MAX_VALUE);
             info.setLive(true);
             lockMap.put(path, info);
 

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XAEnvironment.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XAEnvironment.java?rev=803604&r1=803603&r2=803604&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XAEnvironment.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XAEnvironment.java
Wed Aug 12 17:11:54 2009
@@ -110,7 +110,7 @@
      */
     public LockInfo lock(NodeImpl node, boolean isDeep, boolean isSessionScoped)
             throws LockException, RepositoryException {
-        return lock(node, isDeep, isSessionScoped, LockInfo.TIMEOUT_INFINITE, null);
+        return lock(node, isDeep, isSessionScoped, Long.MAX_VALUE, null);
     }
 
     /**
@@ -444,7 +444,7 @@
         public XALockInfo(
                 NodeImpl node,
                 boolean sessionScoped, boolean deep, String lockOwner) {
-            super(node.getNodeId(), sessionScoped, deep, lockOwner, TIMEOUT_INFINITE);
+            super(node.getNodeId(), sessionScoped, deep, lockOwner, Long.MAX_VALUE);
             this.node = node;
         }
 
@@ -453,9 +453,7 @@
          * unlock operation on some existing lock information.
          */
         public XALockInfo(NodeImpl node, LockInfo info) {
-            super(info.getId(), info.isSessionScoped(), info.isDeep(),
-                    info.getLockOwner(), info.getSecondsRemaining());
-
+            super(info);
             this.node = node;
             this.isUnlock = true;
         }
@@ -478,7 +476,7 @@
             } else {
                 LockInfo internalLock = lockMgr.internalLock(
                         node, isDeep(), isSessionScoped(),
-                        getSecondsRemaining(), getLockOwner());
+                        getTimeoutTime(), getLockOwner());
                 LockInfo xaEnvLock = getLockInfo(node);
                 // Check if the lockToken has been removed in the transaction ...
                 if (xaEnvLock != null && xaEnvLock.getLockHolder() == null) {
@@ -494,7 +492,7 @@
             if (isUnlock) {
                 lockMgr.internalLock(
                         node, isDeep(), isSessionScoped(),
-                        getSecondsRemaining(), getLockOwner());
+                        getTimeoutHint(), getLockOwner());
             } else {
                 lockMgr.internalUnlock(node);
             }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XALockManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XALockManager.java?rev=803604&r1=803603&r2=803604&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XALockManager.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XALockManager.java
Wed Aug 12 17:11:54 2009
@@ -65,7 +65,7 @@
      */
     public Lock lock(NodeImpl node, boolean isDeep, boolean isSessionScoped)
             throws LockException, RepositoryException {
-        return lock(node, isDeep, isSessionScoped, LockInfo.TIMEOUT_INFINITE, null);
+        return lock(node, isDeep, isSessionScoped, Long.MAX_VALUE, null);
     }
 
     /**



Mime
View raw message