1616import javax .enterprise .inject .Instance ;
1717import javax .inject .Inject ;
1818import java .util .Map ;
19- import java .util .Timer ;
20- import java .util .TimerTask ;
2119import java .util .concurrent .ConcurrentHashMap ;
2220
2321import static javax .ws .rs .core .Response .Status .FORBIDDEN ;
@@ -36,37 +34,31 @@ public class LockControllerImpl implements LockController {
3634 @ ConfigurationCache
3735 Instance <Configurations > configuration ;
3836
39- // :FIXME: avoid use of global state, not good for horizontal scaling. Switch to using a distributed cache
4037 private Map <String , ResourceEditLock > resourceLocks ;
4138
4239 @ PostConstruct
4340 void init () {
4441 resourceLocks = new ConcurrentHashMap <>();
45- long sleepTimeInMilli = 1000L * 60 * 5 ; // 5 minutes
46- Timer timer = new Timer (true );
47- TimerTask timerTask = new TimerTask () {
48- @ Override
49- public void run () {
50- lockListCleanup ();
51- }
52- };
53- timer .scheduleAtFixedRate (timerTask , sleepTimeInMilli , sleepTimeInMilli );
5442 }
5543
5644 @ Override
5745 public ResourceEditLock aquire (AppSecurityContext session , String resourceId ) {
46+
5847 ResourceEditLock resourceEditLock = resourceLocks .get (resourceId );
59- // if resourceEditLock has expired create new one
60- if (resourceEditLock != null ) {
61- if (System .currentTimeMillis () - resourceEditLock .getLockedAt () > configuration .get ().getEditLockMaxDuration ()) {
62- //Old ResourceEditLock has expired
63- resourceEditLock = new ResourceEditLock (configuration .get ().getEditLockMaxDuration (), resourceId , session .getPreferredUsername (), System .currentTimeMillis ());
64- resourceLocks .put (resourceId , resourceEditLock );
65- }
66- } else {
67- resourceEditLock = new ResourceEditLock (configuration .get ().getEditLockMaxDuration (), resourceId , session .getPreferredUsername (), System .currentTimeMillis ());
48+
49+ // Update the lock if either:
50+ //
51+ // 1. One does not exist for this resource
52+ // 2. The user name of an existing lock matches the current user name
53+ // 3. The lock has expired
54+ if (resourceEditLock == null
55+ || resourceEditLock .getLockedBy ().equalsIgnoreCase (session .getPreferredUsername ())
56+ || System .currentTimeMillis () - resourceEditLock .getLockedAt () > configuration .get ().getEditLockMaxDuration ()) {
57+
58+ resourceEditLock = new ResourceEditLock (resourceId , session .getPreferredUsername (), System .currentTimeMillis ());
6859 resourceLocks .put (resourceId , resourceEditLock );
6960 }
61+
7062 return resourceEditLock ;
7163 }
7264
@@ -103,7 +95,7 @@ public ResourceEditLock getLockForResource(AppSecurityContext session, String re
10395 ResourceEditLock resourceEditLock = resourceLocks .get (resourceId );
10496 // if resourceEditLock has expired create new one
10597 if (resourceEditLock == null || doCreate ) {
106- resourceEditLock = new ResourceEditLock (configuration . get (). getEditLockMaxDuration (), resourceId , session .getPreferredUsername (), System .currentTimeMillis ());
98+ resourceEditLock = new ResourceEditLock (resourceId , session .getPreferredUsername (), System .currentTimeMillis ());
10799 resourceLocks .put (resourceId , resourceEditLock );
108100 }
109101 return resourceEditLock ;
@@ -133,13 +125,4 @@ public void checkLockPermission(AppSecurityContext session, String resourceId, b
133125 }
134126 }
135127
136- private void lockListCleanup () {
137- resourceLocks .forEach ((key , value ) -> {
138- long timeDiff = System .currentTimeMillis () - value .getLockedAt ();
139- if (timeDiff > configuration .get ().getEditLockMaxDuration ()) {
140- log .debug ("lock cleanup time diff " + timeDiff + " lock" + value .toString ());
141- resourceLocks .remove (key , value );
142- }
143- });
144- }
145128}
0 commit comments