Fixing handlers

This commit is contained in:
Luca Frosini 2022-06-09 16:07:22 +02:00
parent 604856b4d2
commit 615dcd90c8
4 changed files with 111 additions and 37 deletions

View File

@ -105,6 +105,7 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
Set<UUID> startContextsUUID = new HashSet<>(); Set<UUID> startContextsUUID = new HashSet<>();
for (String token : startTokens) { for (String token : startTokens) {
UUID contextUUID = ContextUtility.getContextUUID(token); UUID contextUUID = ContextUtility.getContextUUID(token);
startContextsUUID.add(contextUUID);
if (create) { if (create) {
ContextUtility.setContextFromToken(token); ContextUtility.setContextFromToken(token);
eServiceManager = new EServiceManager(applicationContext); eServiceManager = new EServiceManager(applicationContext);
@ -120,7 +121,6 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
throw e; throw e;
} }
} }
startContextsUUID.add(contextUUID);
} }
Set<UUID> resourceContextsUUID = eServiceManager.getContextsUUID().keySet(); Set<UUID> resourceContextsUUID = eServiceManager.getContextsUUID().keySet();
@ -162,13 +162,14 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get(); String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) { if (previousToken == null) {
ContextUtility previousToken = applicationContext.configuration().startTokens().iterator().next();
.setContextFromToken(applicationContext.configuration().startTokens().iterator().next()); // ContextUtility.setContextFromToken(previousToken);
} }
try { try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
ContextUtility.setContextFromToken(token); ContextUtility.setContextFromToken(token);
eServiceManager.addToCurrentContext(); UUID contextUUID = ContextUtility.getContextUUID(token);
eServiceManager.addToContext(contextUUID);
} catch (Exception e) { } catch (Exception e) {
logger.error("Failed to add {} to current context ({})", EService.NAME, logger.error("Failed to add {} to current context ({})", EService.NAME,
ContextUtility.getCurrentContextName(), e); ContextUtility.getCurrentContextName(), e);
@ -188,7 +189,7 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
} }
try { try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
eServiceManager.removeFromContext(); eServiceManager.removeFromCurrentContext();
} catch (Exception e) { } catch (Exception e) {
logger.error("Failed to remove {} from current context ({})", logger.error("Failed to remove {} from current context ({})",
EService.NAME, ContextUtility.getCurrentContextName(), e); EService.NAME, ContextUtility.getCurrentContextName(), e);

View File

@ -105,6 +105,7 @@ public class HostingNodeHandler extends ContainerHandler {
Set<UUID> startContextsUUID = new HashSet<>(); Set<UUID> startContextsUUID = new HashSet<>();
for (String token : startTokens) { for (String token : startTokens) {
UUID contextUUID = ContextUtility.getContextUUID(token); UUID contextUUID = ContextUtility.getContextUUID(token);
startContextsUUID.add(contextUUID);
if (create) { if (create) {
ContextUtility.setContextFromToken(token); ContextUtility.setContextFromToken(token);
hostingNodeManager = new HostingNodeManager(containerContext); hostingNodeManager = new HostingNodeManager(containerContext);
@ -120,7 +121,6 @@ public class HostingNodeHandler extends ContainerHandler {
throw e; throw e;
} }
} }
startContextsUUID.add(contextUUID);
} }
Set<UUID> resourceContextsUUID = hostingNodeManager.getContextsUUID().keySet(); Set<UUID> resourceContextsUUID = hostingNodeManager.getContextsUUID().keySet();
@ -171,12 +171,14 @@ public class HostingNodeHandler extends ContainerHandler {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get(); String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) { if (previousToken == null) {
ContextUtility previousToken = containerContext.configuration().startTokens().iterator().next();
.setContextFromToken(containerContext.configuration().startTokens().iterator().next()); // ContextUtility.setContextFromToken(previousToken);
} }
try { try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
hostingNodeManager.addToCurrentContext(); ContextUtility.setContextFromToken(token);
UUID contextUUID = ContextUtility.getContextUUID(token);
hostingNodeManager.addToContext(contextUUID);
} catch (Exception e) { } catch (Exception e) {
logger.error("Failed to update Service State", e); logger.error("Failed to update Service State", e);
} finally { } finally {
@ -190,12 +192,14 @@ public class HostingNodeHandler extends ContainerHandler {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get(); String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) { if (previousToken == null) {
ContextUtility previousToken = containerContext.configuration().startTokens().iterator().next();
.setContextFromToken(containerContext.configuration().startTokens().iterator().next()); // ContextUtility.setContextFromToken(previousToken);
} }
try { try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
hostingNodeManager.removeFromContext(); ContextUtility.setContextFromToken(token);
UUID contextUUID = ContextUtility.getContextUUID(token);
hostingNodeManager.removeFromContext(contextUUID);
} catch (Exception e) { } catch (Exception e) {
logger.error("Failed to update Service State", e); logger.error("Failed to update Service State", e);
} finally { } finally {

View File

@ -4,12 +4,15 @@ import java.net.URI;
import java.util.Arrays; import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import javax.servlet.ServletRegistration; import javax.servlet.ServletRegistration;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.informationsystem.model.impl.properties.HeaderImpl; import org.gcube.informationsystem.model.impl.properties.HeaderImpl;
import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl; import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl;
import org.gcube.informationsystem.model.reference.properties.Header; import org.gcube.informationsystem.model.reference.properties.Header;
@ -78,22 +81,64 @@ public class EServiceManager {
} }
public void addEServiceToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { public void addEServiceToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
String currentToken = SecurityTokenProvider.instance.get();
UUID contextUUID = ContextUtility.getContextUUID(currentToken);
/* Trying to get a context which is not the current to properly invoke the addToContext without forcing the operation using addResourceToCurrentContext */
boolean anotherContextSet = false;
Set<String> startTokens = applicationContext.configuration().startTokens();
for (String token : startTokens) {
UUID anotherContextUUID = ContextUtility.getContextUUID(token);
if(anotherContextUUID.compareTo(contextUUID)!=0) {
ContextUtility.setContextFromToken(token);
anotherContextSet = true;
break;
}
}
UUID uuid = UUID.fromString(applicationContext.id()); UUID uuid = UUID.fromString(applicationContext.id());
try { try {
resourceRegistryPublisher.addResourceToCurrentContext(EService.NAME, uuid, false); if(anotherContextSet) {
resourceRegistryPublisher.addResourceToContext(EService.NAME, uuid, contextUUID, false);
}else {
resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false);
}
logger.info("{} with UUID {} successfully added to current context ({})", EService.NAME, uuid, ContextUtility.getCurrentContextName()); logger.info("{} with UUID {} successfully added to current context ({})", EService.NAME, uuid, ContextUtility.getCurrentContextName());
} catch (Exception e) { } catch (Exception e) {
logger.error("Unable to add {} with UUID {} to current context ({})", EService.NAME, uuid, ContextUtility.getCurrentContextName(), e); logger.error("Unable to add {} with UUID {} to current context ({})", EService.NAME, uuid, ContextUtility.getCurrentContextName(), e);
}finally {
ContextUtility.setContextFromToken(currentToken);
} }
} }
public void addToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { public void addHostingNodeToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
String currentToken = SecurityTokenProvider.instance.get();
UUID contextUUID = ContextUtility.getContextUUID(currentToken);
/* Trying to get a context which is not the current to properly invoke the addToContext without forcing the operation using addResourceToCurrentContext */
boolean anotherContextSet = false;
Set<String> startTokens = applicationContext.configuration().startTokens();
for (String token : startTokens) {
UUID anotherContextUUID = ContextUtility.getContextUUID(token);
if(anotherContextUUID.compareTo(contextUUID)!=0) {
ContextUtility.setContextFromToken(token);
anotherContextSet = true;
break;
}
}
UUID uuid = UUID.fromString(applicationContext.container().id()); UUID uuid = UUID.fromString(applicationContext.container().id());
try { try {
resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false); if(anotherContextSet) {
resourceRegistryPublisher.addResourceToContext(HostingNode.NAME, uuid, contextUUID, false);
}else {
resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false);
}
logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName()); logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName());
} catch (Exception e) { } catch (Exception e) {
logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e); logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e);
}finally {
ContextUtility.setContextFromToken(currentToken);
} }
} }
@ -107,7 +152,7 @@ public class EServiceManager {
} }
} }
public void removeFromContext() public void removeFromCurrentContext()
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(applicationContext.container().id()); UUID uuid = UUID.fromString(applicationContext.container().id());
try { try {
@ -226,20 +271,20 @@ public class EServiceManager {
createActivatesRelation(eService); createActivatesRelation(eService);
stateFacet = eService.getFacets(StateFacet.class).get(0); stateFacet = eService.getFacets(StateFacet.class).get(0);
} catch (AvailableInAnotherContextException e) { } catch (AvailableInAnotherContextException e) {
// addToContext(); addHostingNodeToCurrentContext();
// try { try {
// eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
// } catch (AvailableInAnotherContextException ex) { } catch (AvailableInAnotherContextException ex) {
// addEServiceToCurrentContext(); addEServiceToCurrentContext();
// eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
// // addToContext() is executed on HostingNode. // addToContext() is executed on HostingNode.
// // If the EService is still not available we need to create activates // If the EService is still not available we need to create activates
// // relation because does not exists otherwise the EService should // relation because does not exists otherwise the EService should
// // already be in the context due to propagation constraint. // already be in the context due to propagation constraint.
// createActivatesRelation(eService); createActivatesRelation(eService);
// } }
// stateFacet = eService.getFacets(StateFacet.class).get(0); stateFacet = eService.getFacets(StateFacet.class).get(0);
// updateServiceStateFacet(); updateServiceStateFacet();
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
throw e; throw e;
} }
@ -289,7 +334,7 @@ public class EServiceManager {
activates = resourceRegistryPublisher.createIsRelatedTo(activates); activates = resourceRegistryPublisher.createIsRelatedTo(activates);
this.eService = activates.getTarget(); this.eService = activates.getTarget();
} catch (NotFoundException e) { } catch (NotFoundException e) {
logger.error("THIS IS REALLY STRANGE. YOU SHOULD NE BE HERE. Error while creating {}.", activates, e); logger.error("THIS IS REALLY STRANGE. YOU SHOULD NOT BE HERE. Error while creating {}.", activates, e);
throw e; throw e;
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
logger.error("Error while creating {}", activates, e); logger.error("Error while creating {}", activates, e);

View File

@ -32,6 +32,7 @@ import javax.management.ObjectName;
import javax.management.ReflectionException; import javax.management.ReflectionException;
import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.informationsystem.model.impl.properties.HeaderImpl; import org.gcube.informationsystem.model.impl.properties.HeaderImpl;
import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.entities.Resource;
@ -111,9 +112,28 @@ public class HostingNodeManager {
} }
public void addToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { public void addToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
String currentToken = SecurityTokenProvider.instance.get();
UUID contextUUID = ContextUtility.getContextUUID(currentToken);
/* Trying to get a context which is not the current to properly invoke the addToContext without forcing the operation using addResourceToCurrentContext */
boolean anotherContextSet = false;
List<String> startTokens = containerContext.configuration().startTokens();
for (String token : startTokens) {
UUID anotherContextUUID = ContextUtility.getContextUUID(token);
if(anotherContextUUID.compareTo(contextUUID)!=0) {
ContextUtility.setContextFromToken(token);
anotherContextSet = true;
break;
}
}
UUID uuid = UUID.fromString(containerContext.id()); UUID uuid = UUID.fromString(containerContext.id());
try { try {
resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false); if(anotherContextSet) {
resourceRegistryPublisher.addToContext(HostingNode.NAME, uuid, contextUUID, false);
}else {
resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false);
}
logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName()); logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName());
} catch (Exception e) { } catch (Exception e) {
logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e); logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e);
@ -130,7 +150,7 @@ public class HostingNodeManager {
} }
} }
public void removeFromContext() public void removeFromCurrentContext()
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(containerContext.id()); UUID uuid = UUID.fromString(containerContext.id());
try { try {
@ -210,8 +230,10 @@ public class HostingNodeManager {
consistsOfList.addAll(consistsOfToRemove); consistsOfList.addAll(consistsOfToRemove);
hostingNode = resourceRegistryPublisher.createResource(hostingNode); hostingNode = resourceRegistryPublisher.createResource(hostingNode);
} catch (AvailableInAnotherContextException e) { } catch (AvailableInAnotherContextException e) {
addToCurrentContext(); // addToCurrentContext();
hostingNode = resourceRegistryPublisher.updateResource(hostingNode); // hostingNode = resourceRegistryPublisher.updateResource(hostingNode);
logger.error("", e);
throw e;
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
logger.error("error trying to publish hosting node", e); logger.error("error trying to publish hosting node", e);
} }
@ -274,8 +296,10 @@ public class HostingNodeManager {
consistsOfList.addAll(consistsOfToRemove); consistsOfList.addAll(consistsOfToRemove);
hostingNode = resourceRegistryPublisher.createResource(hostingNode); hostingNode = resourceRegistryPublisher.createResource(hostingNode);
} catch (AvailableInAnotherContextException e) { } catch (AvailableInAnotherContextException e) {
addToCurrentContext(); // addToCurrentContext();
hostingNode = resourceRegistryPublisher.updateResource(hostingNode); // hostingNode = resourceRegistryPublisher.updateResource(hostingNode);
logger.error("", e);
throw e;
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
logger.error("error trying to publish hosting node", e); logger.error("error trying to publish hosting node", e);
} }