1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.impl;
24  
25  import com.liferay.portal.DuplicateLockException;
26  import com.liferay.portal.ExpiredLockException;
27  import com.liferay.portal.NoSuchLockException;
28  import com.liferay.portal.PortalException;
29  import com.liferay.portal.SystemException;
30  import com.liferay.portal.model.Lock;
31  import com.liferay.portal.model.User;
32  import com.liferay.portal.service.base.LockLocalServiceBaseImpl;
33  
34  import java.util.Date;
35  import java.util.List;
36  
37  /**
38   * <a href="LockLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class LockLocalServiceImpl extends LockLocalServiceBaseImpl {
43  
44      public void clear() throws SystemException {
45          lockPersistence.removeByExpirationDate(new Date());
46      }
47  
48      public Lock getLock(String className, long key)
49          throws PortalException, SystemException {
50  
51          return getLock(className, String.valueOf(key));
52      }
53  
54      public Lock getLock(String className, String key)
55          throws PortalException, SystemException {
56  
57          Lock lock = lockPersistence.findByC_K(className, key);
58  
59          if (lock.isExpired()) {
60              unlock(className, key);
61  
62              throw new ExpiredLockException();
63          }
64  
65          return lock;
66      }
67  
68      public boolean hasLock(long userId, String className, long key)
69          throws PortalException, SystemException {
70  
71          return hasLock(userId, className, String.valueOf(key));
72      }
73  
74      public boolean hasLock(long userId, String className, String key)
75          throws PortalException, SystemException {
76  
77          try {
78              Lock lock = getLock(className, key);
79  
80              if (lock.getUserId() == userId) {
81                  return true;
82              }
83          }
84          catch (ExpiredLockException ele) {
85          }
86          catch (NoSuchLockException nsle) {
87          }
88  
89          return false;
90      }
91  
92      public boolean isLocked(String className, long key)
93          throws PortalException, SystemException {
94  
95          return isLocked(className, String.valueOf(key));
96      }
97  
98      public boolean isLocked(String className, String key)
99          throws PortalException, SystemException {
100 
101         try {
102             getLock(className, key);
103 
104             return true;
105         }
106         catch (ExpiredLockException ele) {
107         }
108         catch (NoSuchLockException nsle) {
109         }
110 
111         return false;
112     }
113 
114     public Lock lock(
115             long userId, String className, long key, String owner,
116             boolean inheritable, long expirationTime)
117         throws PortalException, SystemException {
118 
119         return lock(
120             userId, className, String.valueOf(key), owner, inheritable,
121             expirationTime);
122     }
123 
124     public Lock lock(
125             long userId, String className, String key, String owner,
126             boolean inheritable, long expirationTime)
127         throws PortalException, SystemException {
128 
129         Date now = new Date();
130 
131         Lock lock = lockPersistence.fetchByC_K(className, key);
132 
133         if (lock != null) {
134             if (lock.isExpired()) {
135                 unlock(className, key);
136 
137                 lock = null;
138             }
139             else if (!lock.getOwner().equals(owner)) {
140                 throw new DuplicateLockException(lock);
141             }
142         }
143 
144         if (lock == null) {
145             User user = userPersistence.findByPrimaryKey(userId);
146 
147             long lockId = counterLocalService.increment();
148 
149             lock = lockPersistence.create(lockId);
150 
151             lock.setCompanyId(user.getCompanyId());
152             lock.setUserId(user.getUserId());
153             lock.setUserName(user.getFullName());
154             lock.setUserId(userId);
155             lock.setClassName(className);
156             lock.setKey(key);
157             lock.setOwner(owner);
158             lock.setInheritable(inheritable);
159         }
160 
161         lock.setCreateDate(now);
162         lock.setExpirationDate(new Date(now.getTime() + expirationTime));
163 
164         lockPersistence.update(lock, false);
165 
166         return lock;
167     }
168 
169     public Lock refresh(String uuid, long expirationTime)
170         throws PortalException, SystemException {
171 
172         Date now = new Date();
173 
174         List<Lock> locks = lockPersistence.findByUuid(uuid);
175 
176         Lock lock = null;
177 
178         if (locks.size() > 0) {
179             lock = locks.get(0);
180         }
181         else {
182             throw new NoSuchLockException();
183         }
184 
185         lock.setCreateDate(now);
186         lock.setExpirationDate(new Date(now.getTime() + expirationTime));
187 
188         lockPersistence.update(lock, false);
189 
190         return lock;
191     }
192 
193     public void unlock(String className, long key) throws SystemException {
194         unlock(className, String.valueOf(key));
195     }
196 
197     public void unlock(String className, String key) throws SystemException {
198         try {
199             lockPersistence.removeByC_K(className, key);
200         }
201         catch (NoSuchLockException nsle) {
202         }
203     }
204 
205 }