diff --git a/src/main/java/org/gcube/common/authorization/utils/manager/SecretManager.java b/src/main/java/org/gcube/common/authorization/utils/manager/SecretManager.java index 5ff6575..314b47c 100644 --- a/src/main/java/org/gcube/common/authorization/utils/manager/SecretManager.java +++ b/src/main/java/org/gcube/common/authorization/utils/manager/SecretManager.java @@ -11,19 +11,10 @@ import org.gcube.common.authorization.utils.user.User; */ public class SecretManager { - public static final InheritableThreadLocal instance = new InheritableThreadLocal() { - - @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() { diff --git a/src/main/java/org/gcube/common/authorization/utils/manager/SecretManagerProvider.java b/src/main/java/org/gcube/common/authorization/utils/manager/SecretManagerProvider.java new file mode 100644 index 0000000..276e9a3 --- /dev/null +++ b/src/main/java/org/gcube/common/authorization/utils/manager/SecretManagerProvider.java @@ -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 thread = new InheritableThreadLocal() { + + @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(); + } + +} diff --git a/src/main/java/org/gcube/common/authorization/utils/socialservice/SocialService.java b/src/main/java/org/gcube/common/authorization/utils/socialservice/SocialService.java index 8e2ed00..1a8510d 100644 --- a/src/main/java/org/gcube/common/authorization/utils/socialservice/SocialService.java +++ b/src/main/java/org/gcube/common/authorization/utils/socialservice/SocialService.java @@ -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 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); }