1
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
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 }