KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > edit > EditLockRegistry


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.modules.edit;
14
15 import java.util.Date JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import com.openedit.users.User;
20
21
22 /**
23  * This registry allows clients to claim, query, and release edit locks for paths.
24  *
25  * @author Eric Galluzzo
26  */

27 public class EditLockRegistry
28 {
29     /** Locks expire in 30 minutes. */
30     protected static final long LOCK_EXPIRATION_DURATION = 30 * 60 * 1000;
31     protected Map JavaDoc fieldPathToLockMap;
32
33     /**
34      * Constructor for EditLockRegistry.
35      */

36     public EditLockRegistry()
37     {
38         super();
39     }
40
41     /**
42      * Retrieve the owner of the lock for the specified path, if any.
43      *
44      * @param inPath The path for which to query the lock
45      *
46      * @return The user who has the given path locked, or <code>null</code> if none
47      */

48     public User getLockOwner(String JavaDoc inPath)
49     {
50         unlockIfExpired(inPath);
51
52         EditLock lock = getEditLock(inPath);
53
54         return (lock == null) ? null : lock.getLockedUser();
55     }
56
57     /**
58      * Determine whether the given path is locked by any user.
59      *
60      * @param inPath The path to query
61      *
62      * @return <code>true</code> if the path is locked, <code>false</code> if not
63      */

64     public boolean isLocked(String JavaDoc inPath)
65     {
66         unlockIfExpired(inPath);
67
68         return getPathToLockMap().containsKey(inPath);
69     }
70
71     /**
72      * Determine whether the given user can lock the given path via this edit lock registry.
73      *
74      * @param inPath The path
75      * @param inUser The user
76      *
77      * @return <code>true</code> if so, <code>false</code> if not
78      */

79     public boolean canLock(String JavaDoc inPath, User inUser)
80     {
81         if ((inPath == null) || (inUser == null))
82         {
83             return false;
84         }
85
86         unlockIfExpired(inPath);
87
88         User owner = getLockOwner(inPath);
89
90         boolean ok = (owner == null) || owner.equals(inUser);
91         if ( !ok )
92         {
93             
94         }
95         return ok;
96     }
97
98     /**
99      * Lock the given path with the given user, regardless of who already has the path locked.
100      *
101      * @param inPath The path to lock
102      * @param inUser The user with which to lock the path
103      */

104     public synchronized void forciblyLockPath(String JavaDoc inPath, User inUser)
105     {
106         forciblyLockPath(inPath, inUser, new Date JavaDoc());
107     }
108
109     /**
110      * Claim a lock for the given path with the given user.
111      *
112      * @param inPath The path to lock
113      * @param inUser The user with which to lock the path
114      *
115      * @throws AlreadyLockedException If the path is already locked by a different user
116      */

117     public synchronized void lockPath(String JavaDoc inPath, User inUser)
118         throws AlreadyLockedException
119     {
120         lockPath(inPath, inUser, new Date JavaDoc());
121     }
122
123     /**
124      * Release the lock for the given path, if the given user has claimed it.
125      *
126      * @param inPath The path to conditionally unlock
127      * @param inUser The user to check for
128      *
129      * @return <code>true</code> if the path was unlocked, <code>false</code> otherwise
130      */

131     public synchronized boolean unlockPath(String JavaDoc inPath, User inUser)
132     {
133         unlockIfExpired(inPath);
134
135         User oldUser = getLockOwner(inPath);
136
137         if ((oldUser != null) && oldUser.equals(inUser))
138         {
139             getPathToLockMap().remove(inPath);
140
141             return true;
142         }
143         else
144         {
145             return false;
146         }
147     }
148
149     /**
150      * Retrieve the edit lock for the given path, if any.
151      *
152      * @param inPath The path
153      *
154      * @return The edit lock, or <code>null</code> if the given path is not locked
155      */

156     protected EditLock getEditLock(String JavaDoc inPath)
157     {
158         if (inPath == null)
159         {
160             return null;
161         }
162
163         return (EditLock) getPathToLockMap().get(inPath);
164     }
165
166     /**
167      * Returns the map from locked paths to their {@link EditLock}s.
168      *
169      * @return Map
170      */

171     protected Map JavaDoc getPathToLockMap()
172     {
173         if (fieldPathToLockMap == null)
174         {
175             fieldPathToLockMap = new HashMap JavaDoc();
176         }
177
178         return fieldPathToLockMap;
179     }
180
181     /**
182      * Lock the given path with the given user, regardless of who already has the path locked, and
183      * setting the lock creation date to the given date.
184      *
185      * @param inPath The path to lock
186      * @param inUser The user with which to lock the path
187      * @param inDate The lock creation date
188      *
189      * @throws IllegalArgumentException DOCUMENT ME!
190      */

191     protected synchronized void forciblyLockPath(String JavaDoc inPath, User inUser, Date JavaDoc inDate)
192     {
193         if (inUser == null)
194         {
195             throw new IllegalArgumentException JavaDoc("Cannot claim an edit lock without a user");
196         }
197
198         if (inPath == null)
199         {
200             throw new IllegalArgumentException JavaDoc("Cannot claim a lock on a null path");
201         }
202
203         getPathToLockMap().put(inPath, new EditLock(inPath, inUser, inDate));
204     }
205
206     /**
207      * Claim a lock for the given path with the given user, setting the lock creation date to the
208      * given date.
209      *
210      * @param inPath The path to lock
211      * @param inUser The user with which to lock the path
212      * @param inDate The lock creation date
213      *
214      * @throws AlreadyLockedException If the path is already locked by a different user
215      * @throws IllegalArgumentException DOCUMENT ME!
216      */

217     protected synchronized void lockPath(String JavaDoc inPath, User inUser, Date JavaDoc inDate)
218         throws AlreadyLockedException
219     {
220         if (inUser == null)
221         {
222             throw new IllegalArgumentException JavaDoc("Cannot claim an edit lock without a user");
223         }
224
225         if (inPath == null)
226         {
227             throw new IllegalArgumentException JavaDoc("Cannot claim a lock on a null path");
228         }
229
230         unlockIfExpired(inPath);
231
232         User oldUser = getLockOwner(inPath);
233
234         if (!canLock(inPath, inUser))
235         {
236             throw new AlreadyLockedException(inPath, oldUser);
237         }
238
239         forciblyLockPath(inPath, inUser, inDate);
240     }
241
242     /**
243      * Remove the lock for the given path if it has expired.
244      *
245      * @param inPath The path whose lock to conditionally remove
246      */

247     protected void unlockIfExpired(String JavaDoc inPath)
248     {
249         EditLock lock = getEditLock(inPath);
250
251         if ((lock != null) && lock.isExpired())
252         {
253             getPathToLockMap().remove(inPath);
254         }
255     }
256
257     protected class EditLock
258     {
259         protected Date JavaDoc fieldCreationDate;
260         protected String JavaDoc fieldPath;
261         protected User fieldLockedUser;
262
263         /**
264          * Create a new edit lock. The path and user must be filled in later.
265          */

266         public EditLock()
267         {
268             this(null, null);
269         }
270
271         /**
272          * Create a new edit lock for the given path and user, locked at the present moment.
273          *
274          * @param inPath The path to lock
275          * @param inLockedUser The user locking the path
276          */

277         public EditLock(String JavaDoc inPath, User inLockedUser)
278         {
279             this(inPath, inLockedUser, new Date JavaDoc());
280         }
281
282         /**
283          * Create a new edit lock for the given path and user, created at the given date.
284          *
285          * @param inPath The path to lock
286          * @param inLockedUser The user locking the path
287          * @param inCreationDate The date at which this lock was created
288          */

289         public EditLock(String JavaDoc inPath, User inLockedUser, Date JavaDoc inCreationDate)
290         {
291             setPath(inPath);
292             setCreationDate(inCreationDate);
293             setLockedUser(inLockedUser);
294         }
295
296         /**
297          * Sets the date this lock was created.
298          *
299          * @param creationDate The creationDate to set
300          */

301         public void setCreationDate(Date JavaDoc creationDate)
302         {
303             fieldCreationDate = creationDate;
304         }
305
306         /**
307          * Returns the date this lock was created.
308          *
309          * @return Date
310          */

311         public Date JavaDoc getCreationDate()
312         {
313             return fieldCreationDate;
314         }
315
316         /**
317          * Determine whether this lock has expired.
318          *
319          * @return <code>true</code> if it has expired, <code>false</code> otherwise
320          */

321         public boolean isExpired()
322         {
323             return (new Date JavaDoc().getTime() - getCreationDate().getTime()) >= LOCK_EXPIRATION_DURATION;
324         }
325
326         /**
327          * Sets the user that has the path locked.
328          *
329          * @param lockedUser The user to set
330          */

331         public void setLockedUser(User lockedUser)
332         {
333             fieldLockedUser = lockedUser;
334         }
335
336         /**
337          * Returns the user that has the path locked.
338          *
339          * @return User
340          */

341         public User getLockedUser()
342         {
343             return fieldLockedUser;
344         }
345
346         /**
347          * Sets the locked path.
348          *
349          * @param path The path to set
350          */

351         public void setPath(String JavaDoc path)
352         {
353             fieldPath = path;
354         }
355
356         /**
357          * Returns the locked path.
358          *
359          * @return String
360          */

361         public String JavaDoc getPath()
362         {
363             return fieldPath;
364         }
365     }
366 }
367
Popular Tags