2019-09-16 12:00:49 +02:00
|
|
|
package org.gcube.gcat.persistence.ckan;
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import javax.cache.Cache;
|
|
|
|
import javax.cache.CacheManager;
|
|
|
|
import javax.cache.Caching;
|
|
|
|
import javax.cache.configuration.MutableConfiguration;
|
|
|
|
import javax.cache.expiry.CreatedExpiryPolicy;
|
|
|
|
import javax.cache.expiry.Duration;
|
|
|
|
import javax.cache.spi.CachingProvider;
|
|
|
|
|
2021-12-01 17:12:04 +01:00
|
|
|
import org.gcube.common.authorization.utils.manager.SecretManager;
|
2022-02-28 15:43:40 +01:00
|
|
|
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
|
2019-09-16 14:48:18 +02:00
|
|
|
|
2022-04-05 17:32:03 +02:00
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
2019-09-16 12:00:49 +02:00
|
|
|
public abstract class CKANUserCache {
|
|
|
|
|
|
|
|
private static final CacheManager cacheManager;
|
|
|
|
|
|
|
|
private static final MutableConfiguration<String,CKANUser> userCacheConfiguration;
|
|
|
|
|
|
|
|
static {
|
|
|
|
CachingProvider provider = Caching.getCachingProvider();
|
|
|
|
cacheManager = provider.getCacheManager();
|
|
|
|
|
|
|
|
userCacheConfiguration = new MutableConfiguration<String,CKANUser>().setTypes(String.class, CKANUser.class)
|
|
|
|
.setStoreByValue(false)
|
2019-09-16 14:55:57 +02:00
|
|
|
.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MINUTES, 15)));
|
2019-09-16 12:00:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private CKANUserCache() {
|
|
|
|
}
|
|
|
|
|
2022-04-21 16:48:27 +02:00
|
|
|
public synchronized static CKANUser getCurrrentCKANUser() {
|
2022-02-28 15:43:40 +01:00
|
|
|
SecretManager secretManager = SecretManagerProvider.instance.get();
|
2022-02-28 10:28:48 +01:00
|
|
|
String context = secretManager.getContext();
|
2022-05-10 15:59:04 +02:00
|
|
|
Cache<String,CKANUser> userCache = cacheManager.getCache(context);
|
2019-09-16 14:48:18 +02:00
|
|
|
if(userCache == null) {
|
2019-09-27 17:24:34 +02:00
|
|
|
userCache = cacheManager.createCache(context, userCacheConfiguration);
|
2019-09-16 12:00:49 +02:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:28:48 +01:00
|
|
|
String gcubeUsername = secretManager.getUser().getUsername();
|
2019-09-16 14:48:18 +02:00
|
|
|
CKANUser ckanUser = userCache.get(gcubeUsername);
|
|
|
|
if(ckanUser == null) {
|
2019-09-16 12:00:49 +02:00
|
|
|
ckanUser = new CKANUser();
|
|
|
|
ckanUser.retrieve();
|
2019-09-16 14:48:18 +02:00
|
|
|
userCache.put(gcubeUsername, ckanUser);
|
2019-09-16 12:00:49 +02:00
|
|
|
}
|
|
|
|
return ckanUser;
|
|
|
|
}
|
|
|
|
|
2022-04-21 16:48:27 +02:00
|
|
|
public synchronized static void removeUserFromCache() {
|
2022-04-21 17:44:34 +02:00
|
|
|
SecretManager secretManager = SecretManagerProvider.instance.get();
|
|
|
|
String gcubeUsername = secretManager.getUser().getUsername();
|
|
|
|
removeUserFromCache(gcubeUsername);
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized static void removeUserFromCache(String gcubeUsername) {
|
2022-04-21 16:43:24 +02:00
|
|
|
SecretManager secretManager = SecretManagerProvider.instance.get();
|
|
|
|
String context = secretManager.getContext();
|
2022-05-10 15:59:04 +02:00
|
|
|
Cache<String,CKANUser> userCache = cacheManager.getCache(context);
|
2022-04-21 16:43:24 +02:00
|
|
|
if(userCache != null) {
|
|
|
|
userCache.remove(gcubeUsername);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 15:59:04 +02:00
|
|
|
public synchronized static void emptyUsersCache() {
|
2022-04-21 17:18:02 +02:00
|
|
|
SecretManager secretManager = SecretManagerProvider.instance.get();
|
|
|
|
String context = secretManager.getContext();
|
2022-05-10 15:59:04 +02:00
|
|
|
cacheManager.destroyCache(context);
|
2022-04-21 17:18:02 +02:00
|
|
|
}
|
|
|
|
|
2019-09-16 12:00:49 +02:00
|
|
|
@Override
|
|
|
|
protected void finalize() throws Throwable {
|
|
|
|
super.finalize();
|
|
|
|
cacheManager.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|