use of java.util.concurrent.locks.Lock in project che by eclipse.
the class PreferenceManager method update.
/**
* Updates the preferences of the user by merging given {@code preferences}
* with the existing preferences. If user doesn't have any preferences
* then the given {@code preferences} will be associated with the user.
*
* @param userId
* the user whose preferences should be updated
* @param preferences
* preferences update
* @return all the user's preferences including the update
* @throws NullPointerException
* when either {@code userId} or {@code preferences} is null
* @throws ServerException
* when any error occurs
*/
public Map<String, String> update(String userId, Map<String, String> preferences) throws ServerException {
requireNonNull(userId, "Required non-null user id");
requireNonNull(preferences, "Required non-null preferences");
// Holding reference to prevent garbage collection
// this reentrantLock helps to avoid race-conditions when parallel updates are applied
final Lock reentrantLock = UPDATE_REENTRANT_LOCKS.get(userId);
reentrantLock.lock();
try {
final Map<String, String> found = preferenceDao.getPreferences(userId);
found.putAll(preferences);
preferenceDao.setPreferences(userId, found);
return found;
} finally {
reentrantLock.unlock();
}
}
use of java.util.concurrent.locks.Lock in project che by eclipse.
the class PreferenceManager method remove.
/**
* Removes the preferences with the given {@code names}.
*
* @param userId
* the id of the user to remove preferences
* @param names
* the names to remove
* @throws NullPointerException
* when either {@code userId} or {@code names} is null
* @throws ServerException
* when any error occurs
*/
public void remove(String userId, List<String> names) throws ServerException {
requireNonNull(userId, "Required non-null user id");
requireNonNull(names, "Required non-null preference names");
// Holding reference to prevent garbage collection
// this reentrantLock helps to avoid race-conditions when parallel updates are applied
final Lock reentrantLock = UPDATE_REENTRANT_LOCKS.get(userId);
reentrantLock.lock();
try {
final Map<String, String> preferences = preferenceDao.getPreferences(userId);
names.forEach(preferences::remove);
preferenceDao.setPreferences(userId, preferences);
} finally {
reentrantLock.unlock();
}
}
use of java.util.concurrent.locks.Lock in project druid by druid-io.
the class NamespaceLookupExtractorFactory method close.
@Override
public boolean close() {
final Lock writeLock = startStopSync.writeLock();
try {
writeLock.lockInterruptibly();
} catch (InterruptedException e) {
throw Throwables.propagate(e);
}
try {
if (entry == null) {
LOG.warn("Not started! [%s]", extractorID);
return true;
}
entry.close();
entry = null;
return true;
} finally {
writeLock.unlock();
}
}
use of java.util.concurrent.locks.Lock in project druid by druid-io.
the class NamespaceLookupExtractorFactory method get.
// Grab the latest snapshot from the CacheScheduler's entry
@Override
public LookupExtractor get() {
final Lock readLock = startStopSync.readLock();
try {
readLock.lockInterruptibly();
} catch (InterruptedException e) {
throw Throwables.propagate(e);
}
try {
if (entry == null) {
throw new ISE("Factory [%s] not started", extractorID);
}
final CacheScheduler.CacheState cacheState = entry.getCacheState();
if (cacheState instanceof CacheScheduler.NoCache) {
final String noCacheReason = ((CacheScheduler.NoCache) cacheState).name();
throw new ISE("%s: %s, extractorID = %s", entry, noCacheReason, extractorID);
}
CacheScheduler.VersionedCache versionedCache = (CacheScheduler.VersionedCache) cacheState;
Map<String, String> map = versionedCache.getCache();
final byte[] v = StringUtils.toUtf8(versionedCache.getVersion());
final byte[] id = StringUtils.toUtf8(extractorID);
return new MapLookupExtractor(map, isInjective()) {
@Override
public byte[] getCacheKey() {
return ByteBuffer.allocate(CLASS_CACHE_KEY.length + id.length + 1 + v.length + 1 + 1).put(CLASS_CACHE_KEY).put(id).put((byte) 0xFF).put(v).put((byte) 0xFF).put(isOneToOne() ? (byte) 1 : (byte) 0).array();
}
};
} finally {
readLock.unlock();
}
}
use of java.util.concurrent.locks.Lock in project tomcat by apache.
the class Util method getExpressionFactory.
/**
* Provides a per class loader cache of ExpressionFactory instances without
* pinning any in memory as that could trigger a memory leak.
*/
static ExpressionFactory getExpressionFactory() {
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
CacheValue cacheValue = null;
ExpressionFactory factory = null;
if (tccl == null) {
cacheValue = nullTcclFactory;
} else {
CacheKey key = new CacheKey(tccl);
cacheValue = factoryCache.get(key);
if (cacheValue == null) {
CacheValue newCacheValue = new CacheValue();
cacheValue = factoryCache.putIfAbsent(key, newCacheValue);
if (cacheValue == null) {
cacheValue = newCacheValue;
}
}
}
final Lock readLock = cacheValue.getLock().readLock();
readLock.lock();
try {
factory = cacheValue.getExpressionFactory();
} finally {
readLock.unlock();
}
if (factory == null) {
final Lock writeLock = cacheValue.getLock().writeLock();
writeLock.lock();
try {
factory = cacheValue.getExpressionFactory();
if (factory == null) {
factory = ExpressionFactory.newInstance();
cacheValue.setExpressionFactory(factory);
}
} finally {
writeLock.unlock();
}
}
return factory;
}
Aggregations