View Javadoc

1   /*
2    * Copyright 2007 Hippo
3    *
4    * Licensed under the Apache License, Version 2.0 (the  "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" 
12   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  package nl.hippo.portal.locking;
17  
18  import org.apache.commons.transaction.locking.LockException;
19  import org.apache.commons.transaction.locking.ReadWriteUpgradeLockManager;
20  
21  /***
22   * @version $Id:CMSRequestLockImpl.java 19178 2007-02-06 10:52:55Z adouma $
23   *
24   */
25  public class RequestLockImpl implements RequestLock
26  {
27      private static final String LOCK_RESOURCE = "RequestLock";
28      
29      private ReadWriteUpgradeLockManager manager;
30      private Long id;
31      private long readLockTimeout;
32      private long writeLockTimeout;
33      
34      
35      public RequestLockImpl(ReadWriteUpgradeLockManager manager, Long id, long readLockTimeout, long writeLockTimeout)
36      {
37          this.manager = manager;
38          this.id = id;
39          this.readLockTimeout = readLockTimeout;
40          this.writeLockTimeout = writeLockTimeout;
41      }
42  
43      /* (non-Javadoc)
44       * @see nl.hippo.portal.cms.locking.CMSRequestLock#readLock()
45       */
46      public boolean readLock()
47      {
48          try
49          {
50              manager.readLock(id, LOCK_RESOURCE);
51              if ( readLockTimeout > 0 )
52              {
53                  manager.startGlobalTimeout(id, readLockTimeout);
54              }
55          }
56          catch (LockException le)
57          {
58              return false;
59          }
60          return true;
61      }
62  
63      /* (non-Javadoc)
64       * @see nl.hippo.portal.cms.locking.CMSRequestLock#upgradeLock()
65       */
66      public boolean upgradeLock()
67      {
68          try
69          {
70              manager.upgradeLock(id, LOCK_RESOURCE);
71              if ( writeLockTimeout > 0 )
72              {
73                  manager.startGlobalTimeout(id, writeLockTimeout);
74              }
75          }
76          catch (LockException le)
77          {
78              return false;
79          }
80          return true;
81      }
82  
83      /* (non-Javadoc)
84       * @see nl.hippo.portal.cms.locking.CMSRequestLock#writeLock()
85       */
86      public boolean writeLock()
87      {
88          try
89          {
90              manager.writeLock(id, LOCK_RESOURCE);
91              if ( writeLockTimeout > 0 )
92              {
93                  manager.startGlobalTimeout(id, writeLockTimeout);
94              }
95          }
96          catch (LockException le)
97          {
98              return false;
99          }
100         return true;
101     }
102 
103     /* (non-Javadoc)
104      * @see nl.hippo.portal.cms.locking.CMSRequestLock#release()
105      */
106     public void release()
107     {
108         manager.release(id, LOCK_RESOURCE);
109     }
110 }