Added user cache

This commit is contained in:
Luca Frosini 2019-09-16 12:00:49 +02:00
parent d59786d415
commit 63246f7cce
12 changed files with 144 additions and 88 deletions

35
pom.xml
View File

@ -87,21 +87,18 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<!--
<dependency>
<groupId>org.gcube.data-catalogue</groupId>
<artifactId>ckan-util-library</artifactId>
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
</dependency>
-->
<dependency> <dependency>
<groupId>org.glassfish.jersey.containers</groupId> <groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId> <artifactId>jersey-container-servlet</artifactId>
</dependency> </dependency>
<!-- Required with jersey 2.27 <dependency> <groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId> </dependency> --> <!-- Required with jersey 2.27
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
-->
<dependency> <dependency>
<groupId>org.glassfish.jersey.media</groupId> <groupId>org.glassfish.jersey.media</groupId>
@ -129,6 +126,22 @@
</dependency> </dependency>
<!-- ehCAChe -->
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.5.2</version>
<scope>runtime</scope>
</dependency>
<!-- END ehCAChe -->
<!-- Libraries explicitly forced to provided --> <!-- Libraries explicitly forced to provided -->
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>

View File

@ -1,25 +0,0 @@
package org.gcube.gcat.exception;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ScienceCatalogueException extends Exception {
/**
* Generated Serial Version UID
*/
private static final long serialVersionUID = -5449813222333935588L;
public ScienceCatalogueException(String message) {
super(message);
}
public ScienceCatalogueException(Throwable cause) {
super(cause);
}
public ScienceCatalogueException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -171,7 +171,7 @@ public class CKANPackage extends CKAN {
CKANUser ckanUser = CKANUser.getCurrentCKANUser(); CKANUser ckanUser = CKANUserCache.getCurrrentCKANUser();
objectNode.put(AUTHOR_KEY, ckanUser.getName()); objectNode.put(AUTHOR_KEY, ckanUser.getName());
objectNode.put(AUTHOR_EMAIL_KEY, ckanUser.getPortalUser().getEMail()); objectNode.put(AUTHOR_EMAIL_KEY, ckanUser.getPortalUser().getEMail());

View File

@ -103,21 +103,6 @@ public class CKANUser extends CKAN {
} }
} }
private static final InheritableThreadLocal<CKANUser> currentCkanUser = new InheritableThreadLocal<CKANUser>() {
@Override
protected CKANUser initialValue() {
CKANUser ckanUser = new CKANUser();
ckanUser.retrieve();
return ckanUser;
}
};
public static CKANUser getCurrentCKANUser() {
return currentCkanUser.get();
}
protected PortalUser portalUser; protected PortalUser portalUser;
protected Role role; protected Role role;
@ -229,7 +214,7 @@ public class CKANUser extends CKAN {
return toBeUpdated; return toBeUpdated;
} }
private void retrieve() { protected void retrieve() {
setApiKey(CKANUtility.getSysAdminAPI()); setApiKey(CKANUtility.getSysAdminAPI());
try { try {
if(name==null || name.compareTo("")==0) { if(name==null || name.compareTo("")==0) {

View File

@ -0,0 +1,66 @@
package org.gcube.gcat.persistence.ckan;
import java.util.HashMap;
import java.util.Map;
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;
public abstract class CKANUserCache {
private static final CacheManager cacheManager;
private static final String USERS_CACHE = "userCache";
private static final MutableConfiguration<String,CKANUser> userCacheConfiguration;
private static final Map<String,Cache<String,CKANUser>> userCachePerCkanInstance;
static {
CachingProvider provider = Caching.getCachingProvider();
cacheManager = provider.getCacheManager();
userCacheConfiguration = new MutableConfiguration<String,CKANUser>().setTypes(String.class, CKANUser.class)
.setStoreByValue(false)
.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.HOURS, 3)));
userCachePerCkanInstance = new HashMap<String,Cache<String,CKANUser>>();
}
private CKANUserCache() {
}
public static CKANUser getCurrrentCKANUser() {
String ckanURL = CKANInstance.getInstance().getCKANURL();
Cache<String,CKANUser> userCache = userCachePerCkanInstance.get(ckanURL);
if(userCache==null) {
userCache = cacheManager.createCache(USERS_CACHE, userCacheConfiguration);
userCachePerCkanInstance.put(ckanURL, userCache);
}
String ckanUserName = CKANUser.getCKANUsername();
CKANUser ckanUser = userCache.get(ckanUserName);
if(ckanUser==null) {
ckanUser = new CKANUser();
ckanUser.retrieve();
}
return ckanUser;
}
@Override
protected void finalize() throws Throwable {
super.finalize();
cacheManager.close();
}
}

View File

@ -17,7 +17,7 @@ public class CKANUtility {
public static String getApiKey() { public static String getApiKey() {
try { try {
CKANUser ckanUser = CKANUser.getCurrentCKANUser(); CKANUser ckanUser = CKANUserCache.getCurrrentCKANUser();
return ckanUser.getApiKey(); return ckanUser.getApiKey();
}catch (Exception e) { }catch (Exception e) {
throw new InternalServerErrorException(e); throw new InternalServerErrorException(e);

View File

@ -73,7 +73,7 @@ public class PortalUser {
public JsonNode getOAuthUserProfile() { public JsonNode getOAuthUserProfile() {
if(oAuthUserProfile == null) { if(oAuthUserProfile == null) {
try { try {
String socialServiceBasePath = SocialServiceDiscovery.getCurrentScopeSocialServiceBasePath(); String socialServiceBasePath = SocialService.getSocialService().getServiceBasePath();
GXHTTPStringRequest gxhttpStringRequest = HTTPUtility.createGXHTTPStringRequest(socialServiceBasePath, GXHTTPStringRequest gxhttpStringRequest = HTTPUtility.createGXHTTPStringRequest(socialServiceBasePath,
SOCIAL_SERVICE_GET_OAUTH_USER_PROFILE_PATH, false); SOCIAL_SERVICE_GET_OAUTH_USER_PROFILE_PATH, false);

View File

@ -10,7 +10,7 @@ import javax.ws.rs.core.MediaType;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.gcat.persistence.ckan.CKANInstance; import org.gcube.gcat.persistence.ckan.CKANInstance;
import org.gcube.gcat.persistence.ckan.CKANUser; import org.gcube.gcat.persistence.ckan.CKANUserCache;
import org.gcube.gcat.utils.Constants; import org.gcube.gcat.utils.Constants;
import org.gcube.gcat.utils.ContextUtility; import org.gcube.gcat.utils.ContextUtility;
import org.gcube.gcat.utils.HTTPUtility; import org.gcube.gcat.utils.HTTPUtility;
@ -128,9 +128,9 @@ public class SocialPost extends Thread {
public void sendSocialPost(boolean notifyUsers) { public void sendSocialPost(boolean notifyUsers) {
try { try {
String fullName = CKANUser.getCurrentCKANUser().getPortalUser().getFullName(); String fullName = CKANUserCache.getCurrrentCKANUser().getPortalUser().getFullName();
String basePath = SocialServiceDiscovery.getCurrentScopeSocialServiceBasePath(); String basePath = SocialService.getSocialService().getServiceBasePath();
if(basePath == null) { if(basePath == null) {
logger.info("Unable to write a post because there is no social networking service available"); logger.info("Unable to write a post because there is no social networking service available");
return; return;

View File

@ -16,55 +16,40 @@ import org.slf4j.LoggerFactory;
/** /**
* Discover the Social Networking Service in the Infrastructure. * Discover the Social Networking Service in the Infrastructure.
* @author Costantino Perciante (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
public class SocialService {
// TODO Add a context based cache
public class SocialServiceDiscovery {
private static final String RESOURCE = "jersey-servlet"; private static final String RESOURCE = "jersey-servlet";
private static final String SERVICE_NAME = "SocialNetworking"; private static final String SERVICE_NAME = "SocialNetworking";
private static final String SERVICE_CLASSE = "Portal"; private static final String SERVICE_CLASSE = "Portal";
private static final String SOCIAL_SERVICE_HA_PROXY_URL = "https://api.d4science.org/rest"; private static Logger logger = LoggerFactory.getLogger(SocialService.class);
private static Logger logger = LoggerFactory.getLogger(SocialServiceDiscovery.class);
private String serviceBasePath; private String serviceBasePath;
// Map<String contextName, String basePath> // Map<String contextFullName, SocialService socialService>
private static Map<String, String> socialServiceBasePathPerContext; private static Map<String, SocialService> socialServicePerContext;
static { static {
socialServiceBasePathPerContext = new HashMap<>(); socialServicePerContext = new HashMap<>();
} }
public static String getCurrentScopeSocialServiceBasePath() throws Exception { public static SocialService getSocialService() throws Exception {
String contex = ContextUtility.getCurrentContext(); String contex = ContextUtility.getCurrentContext();
String serviceBasePath = socialServiceBasePathPerContext.get(contex); SocialService socialService = socialServicePerContext.get(contex);
if(serviceBasePath==null) { if(socialService==null) {
SocialServiceDiscovery gcoreEndpointReaderSNL = new SocialServiceDiscovery(); socialService = new SocialService();
serviceBasePath = gcoreEndpointReaderSNL.getServiceBasePath(); socialServicePerContext.put(contex, socialService);
socialServiceBasePathPerContext.put(contex, serviceBasePath);
} }
return serviceBasePath; return socialService;
} }
/** /**
* Discover the gcore endpoint for the social networking service. * Discover the gcore endpoint for the social networking service.
* @throws Exception the exception * @throws Exception the exception
*/ */
private SocialServiceDiscovery() throws Exception { private SocialService() throws Exception {
getServiceBasePathViaServiceEndpoint(); getServiceBasePathViaGCoreEndpoint();
if(serviceBasePath==null) {
getServiceBasePathViaGCoreEndpoint();
}
}
protected void getServiceBasePathViaServiceEndpoint() throws Exception {
// TODO Discover on IS
serviceBasePath = SOCIAL_SERVICE_HA_PROXY_URL;
} }
protected void getServiceBasePathViaGCoreEndpoint() throws Exception { protected void getServiceBasePathViaGCoreEndpoint() throws Exception {

View File

@ -9,11 +9,19 @@ public class CKANGroupTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(CKANGroupTest.class); private static Logger logger = LoggerFactory.getLogger(CKANGroupTest.class);
@Test
public void list() throws Exception {
CKANGroup ckanGroup = new CKANGroup();
String ret = ckanGroup.list(10,0);
logger.debug("{}", ret);
}
@Test @Test
public void read() throws Exception { public void read() throws Exception {
CKANGroup ckanGroup = new CKANGroup(); CKANGroup ckanGroup = new CKANGroup();
ckanGroup.setApiKey(CKANUtility.getSysAdminAPI()); ckanGroup.setApiKey(CKANUtility.getSysAdminAPI());
String name = ""; String name = "abundance-level";
ckanGroup.setName(name); ckanGroup.setName(name);
String ret = ckanGroup.read(); String ret = ckanGroup.read();
logger.debug("{}", ret); logger.debug("{}", ret);

View File

@ -75,7 +75,13 @@ public class CKANUserTest extends ContextTest {
@Test @Test
public void checkAndUpdateInformation() throws Exception { public void checkAndUpdateInformation() throws Exception {
CKANUser ckanUser = CKANUser.getCurrentCKANUser(); CKANUser ckanUser = CKANUserCache.getCurrrentCKANUser();
logger.debug("{}", ckanUser.result);
ckanUser = CKANUserCache.getCurrrentCKANUser();
logger.debug("{}", ckanUser.result);
ckanUser = CKANUserCache.getCurrrentCKANUser();
logger.debug("{}", ckanUser.result); logger.debug("{}", ckanUser.result);
} }
} }

View File

@ -0,0 +1,18 @@
package org.gcube.gcat.social;
import org.gcube.gcat.ContextTest;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SocialServiceTest {
private static Logger logger = LoggerFactory.getLogger(SocialServiceTest.class);
@Test
public void get() throws Exception {
ContextTest.setContextByName("/d4science.research-infrastructures.eu/D4Research/AGINFRAplusDev");
SocialService socialService = SocialService.getSocialService();
logger.debug(socialService.getServiceBasePath());
}
}