mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
[ACS-5756] Introduce locks to make sure the methods are not interfering with each other
This commit is contained in:
@@ -63,6 +63,11 @@ import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
@@ -501,40 +506,78 @@ class BlockingRemoteUserMapper implements RemoteUserMapper, ActivateableBean
|
||||
private volatile int timePassed;
|
||||
|
||||
private boolean isEnabled = true;
|
||||
private final ReadWriteLock lock = new ReentrantReadWriteLock();
|
||||
|
||||
@Override
|
||||
public String getRemoteUser(HttpServletRequest request)
|
||||
{
|
||||
long t1 = System.currentTimeMillis();
|
||||
doWithLock(lock.writeLock(), () -> {
|
||||
long t1 = System.currentTimeMillis();
|
||||
try
|
||||
{
|
||||
Thread.sleep(BLOCKING_FOR_MILLIS);
|
||||
} catch (InterruptedException ie)
|
||||
{
|
||||
wasInterrupted = true;
|
||||
} finally
|
||||
{
|
||||
timePassed = (int) (System.currentTimeMillis() - t1);
|
||||
}
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void doWithLock(Lock lock, Runnable action)
|
||||
{
|
||||
getWithLock(lock, () ->
|
||||
{
|
||||
action.run();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
private static <T> T getWithLock(Lock lock, Supplier<T> supplier)
|
||||
{
|
||||
final boolean locked;
|
||||
try
|
||||
{
|
||||
Thread.sleep(BLOCKING_FOR_MILLIS);
|
||||
locked = lock.tryLock(BLOCKING_FOR_MILLIS * 2, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
catch (InterruptedException ie)
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
wasInterrupted = true;
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("Unexpected InterruptedException while acquiring " + lock);
|
||||
}
|
||||
|
||||
if (!locked) throw new IllegalStateException("Unexpected failure while acquiring " + lock);
|
||||
|
||||
try
|
||||
{
|
||||
return supplier.get();
|
||||
}
|
||||
finally
|
||||
{
|
||||
timePassed = (int) (System.currentTimeMillis() - t1);
|
||||
lock.unlock();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isWasInterrupted()
|
||||
{
|
||||
return wasInterrupted;
|
||||
return getWithLock(lock.readLock(), () -> wasInterrupted);
|
||||
}
|
||||
|
||||
public int getTimePassed()
|
||||
{
|
||||
return timePassed;
|
||||
return getWithLock(lock.readLock(), () -> timePassed);
|
||||
}
|
||||
|
||||
public void reset()
|
||||
{
|
||||
wasInterrupted = false;
|
||||
timePassed = 0;
|
||||
doWithLock(lock.writeLock(), () ->
|
||||
{
|
||||
wasInterrupted = false;
|
||||
timePassed = 0;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user