Fixing code

This commit is contained in:
Luca Frosini 2022-03-18 14:45:19 +01:00
parent 9492350a61
commit b32a78ab7d
4 changed files with 54 additions and 71 deletions

View File

@ -1,57 +0,0 @@
package org.gcube.smartgears.handler.resourceregistry;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.UUID;
import org.gcube.common.authorization.client.proxy.AuthorizationProxy;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.smartgears.provider.ProviderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI-CNR)
*/
public class ContextUtility {
private static Logger logger = LoggerFactory.getLogger(ContextUtility.class);
private static AuthorizationProxy authorizationProxy;
static {
authorizationProxy = ProviderFactory.provider().authorizationProxy();
}
public static String getContextName(String token) {
try {
return authorizationProxy.get(token).getContext();
} catch (Exception e) {
logger.error("Error retrieving context form token {}, it should never happen", token, e);
return null;
}
}
public static SortedSet<String> getContextFullNamesFromTokens(Set<String> tokens){
SortedSet<String> contextFullNames = new TreeSet<>();
for(String token : tokens) {
String contextFullName = getContextName(token);
contextFullNames.add(contextFullName);
}
return contextFullNames;
}
public static SortedSet<UUID> getContextUUIDFromTokens(Set<String> tokens) throws ResourceRegistryException {
SortedSet<UUID> contextsUUID = new TreeSet<>();
ContextCache contextCache = ContextCache.getInstance();
for(String token : tokens) {
String contextFullName = getContextName(token);
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
contextsUUID.add(contextUUID);
}
return contextsUUID;
}
}

View File

@ -101,18 +101,32 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
private void init() {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
SecretManager secretManager = SecretManagerProvider.instance.get();
boolean secretManagerNull = false;
if(secretManager==null) {
secretManager = new SecretManager();
secretManagerNull = true;
SecretManagerProvider.instance.set(secretManager);
}
try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
boolean create = true;
Set<String> startTokens = applicationContext.configuration().startTokens();
Set<UUID> startContextsUUID = new HashSet<>();
ContextCache contextCache = ContextCache.getInstance();
ContextCache contextCache = null;
Secret firstSecret = null;
for (String token : startTokens) {
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
try {
String contextFullName = ContextUtility.getContextName(token);
Secret secret = SecretUtility.getSecretByTokenString(token);
if(firstSecret==null) {
firstSecret = secret;
}
secretManager.startSession(secret);
if(contextCache == null) {
contextCache = ContextCache.getInstance();
}
String contextFullName = secret.getContext();
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
startContextsUUID.add(contextUUID);
if (create) {
@ -124,22 +138,28 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
} else {
eServiceManager.addToContext();
}
} catch (Exception e) {
UUID uuid = UUID.fromString(applicationContext.id());
logger.error("Unable to add {} with UUID {} to current context ({})", EService.NAME, uuid,
ContextUtility.getContextName(token), e);
secretManager.getContext(), e);
}finally {
secretManager.endSession();
}
}
secretManager.startSession(firstSecret);
Set<UUID> resourceContextsUUID = eServiceManager.getContextsUUID().keySet();
removeResourceFromOldContexts(startContextsUUID, resourceContextsUUID);
secretManager.endSession();
} catch (Throwable e) {
rethrowUnchecked(e);
} finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
Thread.currentThread().setContextClassLoader(contextCL);
}
logger.info("{} init() terminated", this.getClass().getSimpleName());

View File

@ -100,46 +100,65 @@ public class HostingNodeHandler extends ContainerHandler {
private void init() {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
SecretManager secretManager = SecretManagerProvider.instance.get();
boolean secretManagerNull = false;
if(secretManager==null) {
secretManager = new SecretManager();
secretManagerNull = true;
SecretManagerProvider.instance.set(secretManager);
}
try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
boolean create = true;
List<String> startTokens = containerContext.configuration().startTokens();
ContextCache contextCache = null;
Secret firstSecret = null;
Set<UUID> startContextsUUID = new HashSet<>();
ContextCache contextCache = ContextCache.getInstance();
for (String token : startTokens) {
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
String contextFullName = ContextUtility.getContextName(token);
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
startContextsUUID.add(contextUUID);
try {
Secret secret = SecretUtility.getSecretByTokenString(token);
if(firstSecret==null) {
firstSecret = secret;
}
secretManager.startSession(secret);
if(contextCache == null) {
contextCache = ContextCache.getInstance();
}
String contextFullName = secret.getContext();
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
startContextsUUID.add(contextUUID);
if (create) {
hostingNodeManager = new HostingNodeManager(containerContext);
hostingNodeManager.createHostingNode();
containerContext.properties()
.add(new Property(Constants.HOSTING_NODE_MANAGER_PROPERTY, hostingNodeManager));
create = false;
} else {
hostingNodeManager.addToContext();
}
} catch (Exception e) {
UUID uuid = UUID.fromString(containerContext.id());
logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid,
ContextUtility.getContextName(token), e);
secretManager.getContext(), e);
} finally {
secretManager.endSession();
}
}
secretManager.startSession(firstSecret);
Set<UUID> resourceContextsUUID = hostingNodeManager.getContextsUUID().keySet();
removeResourceFromOldContexts(startContextsUUID, resourceContextsUUID);
secretManager.endSession();
} catch (Throwable e) {
rethrowUnchecked(e);
} finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
Thread.currentThread().setContextClassLoader(contextCL);
}
logger.info("{} init() terminated", this.getClass().getSimpleName());

View File

@ -191,6 +191,7 @@ public class EServiceManager {
EventFacet eventFacet = new EventFacetImpl();
eventFacet.setEvent(state);
eventFacet.setDate(date);
eService.addFacet(eventFacet);
return eService;
}