Changing thread local paradigm

This commit is contained in:
Luca Frosini 2022-02-25 18:08:51 +01:00
parent e9493eec19
commit b9e78300a6
3 changed files with 42 additions and 15 deletions

View File

@ -11,19 +11,10 @@ import org.gcube.common.authorization.utils.user.User;
*/
public class SecretManager {
public static final InheritableThreadLocal<SecretManager> instance = new InheritableThreadLocal<SecretManager>() {
@Override
protected SecretManager initialValue() {
return new SecretManager();
}
};
private SecretHolder initialSecretHolder;
private SecretHolder currentSecretHolder;
private SecretManager() {
public SecretManager() {
initialSecretHolder = new SecretHolder();
currentSecretHolder = initialSecretHolder;
}
@ -90,7 +81,6 @@ public class SecretManager {
if (initialSecretHolder != currentSecretHolder) {
initialSecretHolder.reset();
}
instance.remove();
}
public synchronized String getContext() {

View File

@ -0,0 +1,34 @@
package org.gcube.common.authorization.utils.manager;
public class SecretManagerProvider {
public static SecretManagerProvider instance = new SecretManagerProvider();
// Thread local variable containing each thread's ID
private static final InheritableThreadLocal<SecretManager> thread = new InheritableThreadLocal<SecretManager>() {
@Override
protected SecretManager initialValue() {
return new SecretManager();
}
};
private SecretManagerProvider(){}
public SecretManager get(){
SecretManager secretManager = thread.get();
return secretManager;
}
public void set(SecretManager secretManager){
thread.set(secretManager);
}
public void reset(){
SecretManager secretManager = thread.get();
secretManager.reset();
thread.remove();
}
}

View File

@ -19,6 +19,7 @@ import javax.ws.rs.core.Response.Status;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.utils.user.GCubeUser;
import org.gcube.common.authorization.utils.user.User;
@ -50,7 +51,8 @@ public class SocialService {
}
public static SocialService getSocialService() throws Exception {
String context = SecretManager.instance.get().getContext();
SecretManager secretManager = SecretManagerProvider.instance.get();
String context = secretManager.getContext();
SocialService socialService = socialServicePerContext.get(context);
if(socialService == null) {
socialService = new SocialService();
@ -68,6 +70,7 @@ public class SocialService {
}
protected void getServiceBasePathViaGCoreEndpoint() throws Exception {
SecretManager secretManager = SecretManagerProvider.instance.get();
try {
SimpleQuery query = queryFor(GCoreEndpoint.class);
query.addCondition(String.format("$resource/Profile/ServiceClass/text() eq '%s'", SERVICE_CLASSE));
@ -81,20 +84,20 @@ public class SocialService {
List<String> endpoints = client.submit(query);
if(endpoints == null || endpoints.isEmpty()) {
throw new Exception("Cannot retrieve the GCoreEndpoint SERVICE_NAME: " + SERVICE_NAME
+ ", SERVICE_CLASSE: " + SERVICE_CLASSE + ", in scope: " + SecretManager.instance.get().getContext());
+ ", SERVICE_CLASSE: " + SERVICE_CLASSE + ", in scope: " + secretManager.getContext());
}
this.serviceBasePath = endpoints.get(0);
if(serviceBasePath == null)
throw new Exception("Endpoint:" + RESOURCE + ", is null for SERVICE_NAME: " + SERVICE_NAME
+ ", SERVICE_CLASSE: " + SERVICE_CLASSE + ", in scope: " + SecretManager.instance.get().getContext());
+ ", SERVICE_CLASSE: " + SERVICE_CLASSE + ", in scope: " + secretManager.getContext());
serviceBasePath = serviceBasePath.endsWith("/") ? serviceBasePath : serviceBasePath + "/";
} catch(Exception e) {
String error = "An error occurred during GCoreEndpoint discovery, SERVICE_NAME: " + SERVICE_NAME
+ ", SERVICE_CLASSE: " + SERVICE_CLASSE + ", in scope: " + SecretManager.instance.get().getContext() + ".";
+ ", SERVICE_CLASSE: " + SERVICE_CLASSE + ", in scope: " + secretManager.getContext() + ".";
logger.error(error, e);
throw new Exception(error);
}