Backported fixes from version using authorization-utils

This commit is contained in:
Luca Frosini 2022-05-24 12:20:55 +02:00
parent 519a5d79fe
commit fea428d51e
4 changed files with 98 additions and 105 deletions

View File

@ -120,7 +120,8 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
eServiceManager.addToContext();
}
} catch (Exception e) {
logger.error("Unable to add {} to current context ({})", eServiceManager.getEService(),
UUID uuid = UUID.fromString(applicationContext.id());
logger.error("Unable to add {} with UUID {} to current context ({})", EService.NAME, uuid,
ContextUtility.getContextName(token), e);
}
}
@ -160,28 +161,6 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
}
}
/*
@Observes({ stop, failure })
void onStop(ApplicationLifecycle lc) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
ContextUtility
.setContextFromToken(applicationContext.configuration().startTokens().iterator().next());
}
try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
eServiceManager.updateServiceStateFacet();
} catch (Exception e) {
logger.error("Failed to update {} State", EService.NAME, e);
} finally {
ContextUtility.setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL);
}
}
*/
@Observes(value = addToContext)
void addTo(String token) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
@ -245,11 +224,11 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
}
if (lc.state() == ApplicationState.active) {
logger.info("scheduling periodic updates of application {} EService",
applicationContext.name());
logger.info("Scheduling periodic updates of {} from application {}",
EService.NAME, applicationContext.name());
} else {
logger.info("resuming periodic updates of application {} EService",
applicationContext.name());
logger.info("Resuming periodic updates of {} for application {}",
EService.NAME, applicationContext.name());
}
final Runnable updateTask = new Runnable() {
@ -262,7 +241,7 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
try {
eServiceManager.updateServiceStateFacet();
} catch (Exception e) {
logger.error("Cannot complete periodic update of EService", e);
logger.error("Cannot complete periodic update of {}", EService.NAME, e);
} finally {
ContextUtility.setContextFromToken(previousToken);
}
@ -279,15 +258,15 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
synchronized void cancelPeriodicUpdates(ContainerLifecycle ignore) {
if (periodicUpdates != null) {
logger.trace("stopping periodic updates of EService for application {} ",
applicationContext.name());
logger.trace("Stopping periodic updates of {} for application {} ",
EService.NAME, applicationContext.name());
try {
periodicUpdates.cancel(true);
periodicUpdates = null;
} catch (Exception e) {
logger.warn("could not stop periodic updates of EService for application {}",
applicationContext.name(), e);
logger.warn("Could not stop periodic updates of {} for application {}",
EService.NAME, applicationContext.name(), e);
}
}
}

View File

@ -122,7 +122,8 @@ public class HostingNodeHandler extends ContainerHandler {
hostingNodeManager.addToContext();
}
} catch (Exception e) {
logger.error("Unable to add {} to current context ({})", hostingNodeManager.getHostingNode(),
UUID uuid = UUID.fromString(containerContext.id());
logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid,
ContextUtility.getContextName(token), e);
}
}
@ -143,8 +144,7 @@ public class HostingNodeHandler extends ContainerHandler {
containerContext.events().subscribe(new Object() {
//@Observes({ activation, part_activation, shutdown, stop, failure })
@Observes({ activation, part_activation})
@Observes({ activation, part_activation, shutdown, stop, failure })
void onChanged(ContainerLifecycle cl) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get();
@ -171,25 +171,6 @@ public class HostingNodeHandler extends ContainerHandler {
}
}
@Observes({ shutdown, stop, failure })
void onShutdown(ContainerLifecycle cl) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
ContextUtility
.setContextFromToken(containerContext.configuration().startTokens().iterator().next());
}
try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
hostingNodeManager.updateStatus();
} catch (Exception e) {
logger.error("cannot complete periodic update of {}", HostingNode.NAME, e);
} finally {
ContextUtility.setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL);
}
}
@Observes(value = addToContext)
void addTo(String token) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
@ -249,9 +230,9 @@ public class HostingNodeHandler extends ContainerHandler {
}
if (cl.state() == active) {
logger.info("scheduling periodic updates of {}", HostingNode.NAME);
logger.info("Scheduling periodic updates of {}", HostingNode.NAME);
} else {
logger.info("resuming periodic updates of {}", HostingNode.NAME);
logger.info("Resuming periodic updates of {}", HostingNode.NAME);
}
final Runnable updateTask = new Runnable() {
@ -264,7 +245,7 @@ public class HostingNodeHandler extends ContainerHandler {
try {
hostingNodeManager.updateFacets();
} catch (Exception e) {
logger.error("cannot complete periodic update of {}", HostingNode.NAME, e);
logger.error("Cannot complete periodic update of {}", HostingNode.NAME, e);
}
}
};
@ -278,13 +259,13 @@ public class HostingNodeHandler extends ContainerHandler {
synchronized void cancelPeriodicUpdates(ContainerLifecycle cl) {
if (periodicUpdates != null) {
logger.trace("stopping periodic updates of {}", HostingNode.NAME);
logger.trace("Stopping periodic updates of {}", HostingNode.NAME);
try {
periodicUpdates.cancel(true);
service.shutdownNow();
periodicUpdates = null;
} catch (Exception e) {
logger.warn("could not stop periodic updates of {}", HostingNode.NAME, e);
logger.warn("Could not stop periodic updates of {}", HostingNode.NAME, e);
}
}
}

View File

@ -2,6 +2,8 @@ package org.gcube.smartgears.handler.resourceregistry.resourcemanager;
import java.net.URI;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ -25,13 +27,16 @@ import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClien
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher;
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory;
import org.gcube.resourcemanagement.model.impl.entities.facets.AccessPointFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.EventFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.StateFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.resources.EServiceImpl;
import org.gcube.resourcemanagement.model.impl.entities.resources.HostingNodeImpl;
import org.gcube.resourcemanagement.model.impl.properties.ValueSchemaImpl;
import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentifiedByImpl;
import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.ActivatesImpl;
import org.gcube.resourcemanagement.model.reference.entities.facets.AccessPointFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.EventFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.StateFacet;
import org.gcube.resourcemanagement.model.reference.entities.resources.EService;
@ -73,37 +78,47 @@ public class EServiceManager {
return eService;
}
public void addToContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
HostingNode hostingNode = applicationContext.container().properties()
.lookup(Constants.HOSTING_NODE_MANAGER_PROPERTY).value(HostingNodeManager.class).getHostingNode();
public void addEServiceToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(applicationContext.id());
try {
resourceRegistryPublisher.addResourceToCurrentContext(hostingNode, false);
logger.info("{} successfully added to current context ({})", eService, ContextUtility.getCurrentContextName());
resourceRegistryPublisher.addResourceToCurrentContext(EService.NAME, uuid, false);
logger.info("{} with UUID {} successfully added to current context ({})", EService.NAME, uuid, ContextUtility.getCurrentContextName());
} catch (Exception e) {
logger.error("Unable to add {} to current context ({})", eService, ContextUtility.getCurrentContextName(), e);
logger.error("Unable to add {} with UUID {} to current context ({})", EService.NAME, uuid, ContextUtility.getCurrentContextName(), e);
}
}
public void addToContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(applicationContext.container().id());
try {
resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false);
logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName());
} catch (Exception e) {
logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e);
}
}
public void removeFromContext()
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(applicationContext.container().id());
try {
resourceRegistryPublisher.removeResourceFromCurrentContext(eService, false);
logger.info("{} successfully removed from current context ({})", eService,
resourceRegistryPublisher.removeResourceFromCurrentContext(HostingNode.NAME, uuid, false);
logger.info("{} with UUID {} successfully removed from current context ({})", HostingNode.NAME, uuid,
ContextUtility.getCurrentContextName());
} catch (Exception e) {
logger.error("Unable to remove {} from current context ({})", eService,
ContextUtility.getCurrentContextName(), e);
logger.error("Unable to remove {} with UUID {} from current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e);
}
}
public void removeFromContext(UUID contextUUID)
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
String contextFullName = ContextCache.getInstance().getContextFullNameByUUID(contextUUID);
UUID uuid = UUID.fromString(applicationContext.container().id());
try {
resourceRegistryPublisher.removeResourceFromContext(eService, contextUUID, false);
logger.info("{} successfully removed from context ({})", eService, contextFullName);
resourceRegistryPublisher.removeResourceFromContext(HostingNode.NAME, uuid, contextUUID, false);
logger.info("{} with UUID {} successfully removed from context ({})", HostingNode.NAME, uuid, contextFullName);
} catch (Exception e) {
logger.error("Unable to remove {} from current context ({})", eService, contextFullName, e);
logger.error("Unable to remove {} from current context ({})", HostingNode.NAME, uuid, contextFullName, e);
}
}
@ -173,11 +188,19 @@ public class EServiceManager {
}
}
Date date = Calendar.getInstance().getTime();
stateFacet = new StateFacetImpl();
String state = getState();
stateFacet.setValue(state);
eService.addFacet(stateFacet);
stateFacet.setAdditionalProperty("date", date);
EventFacet eventFacet = new EventFacetImpl();
eventFacet.setEvent(state);
eventFacet.setDate(date);
eService.addFacet(eventFacet);
return eService;
}
@ -188,15 +211,7 @@ public class EServiceManager {
ResourceRegistryClientFactory.includeContextsInInstanceHeader(true);
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
stateFacet = eService.getFacets(StateFacet.class).get(0);
if (stateFacet == null) {
stateFacet = new StateFacetImpl();
String state = getState();
stateFacet.setValue(state);
eService.addFacet(stateFacet);
resourceRegistryPublisher.update(eService);
} else {
updateServiceStateFacet();
}
updateServiceStateFacet();
} catch (NotFoundException e) {
eService = instantiateEService();
createActivatesRelation(eService);
@ -206,7 +221,8 @@ public class EServiceManager {
try {
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
} catch (AvailableInAnotherContextException ex) {
resourceRegistryPublisher.addResourceToCurrentContext(eService, false);
addEServiceToCurrentContext();
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
// addToContext() is executed on HostingNode.
// If the EService is still not available we need to create activates
// relation because does not exists otherwise the EService should
@ -214,15 +230,7 @@ public class EServiceManager {
createActivatesRelation(eService);
}
stateFacet = eService.getFacets(StateFacet.class).get(0);
if (stateFacet == null) {
stateFacet = new StateFacetImpl();
String state = getState();
stateFacet.setValue(state);
eService.addFacet(stateFacet);
resourceRegistryPublisher.update(eService);
} else {
updateServiceStateFacet();
}
updateServiceStateFacet();
} catch (ResourceRegistryException e) {
throw e;
}
@ -230,16 +238,38 @@ public class EServiceManager {
}
public void updateServiceStateFacet() throws ResourceRegistryException {
Date date = Calendar.getInstance().getTime();
String state = getState();
stateFacet.setValue(state);
stateFacet = resourceRegistryPublisher.updateFacet(stateFacet);
boolean found = false;
List<EventFacet> eventFacets = eService.getFacets(EventFacet.class);
for(EventFacet eventFacet : eventFacets) {
if(eventFacet.getEvent().compareTo(state)==0) {
found = true;
eventFacet.setDate(date);
break;
}
}
if(!found) {
EventFacet eventFacet = new EventFacetImpl();
eventFacet.setDate(date);
eventFacet.setEvent(state);
eService.addFacet(eventFacet);
}
eService = resourceRegistryPublisher.updateResource(eService);
}
private Activates<HostingNode, EService> createActivatesRelation(EService eService)
throws ResourceRegistryException {
HostingNode hostingNode = applicationContext.container().properties()
.lookup(Constants.HOSTING_NODE_MANAGER_PROPERTY).value(HostingNodeManager.class).getHostingNode();
UUID hostingNodeUUID = UUID.fromString(applicationContext.container().id());
HostingNode hostingNode = new HostingNodeImpl();
Header header = new HeaderImpl(hostingNodeUUID);
hostingNode.setHeader(header);
PropagationConstraint propagationConstraint = new PropagationConstraintImpl();
propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade);

View File

@ -111,33 +111,36 @@ public class HostingNodeManager {
}
public void addToContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(containerContext.id());
try {
resourceRegistryPublisher.addResourceToCurrentContext(hostingNode, false);
logger.info("{} successfully added to current context ({})", hostingNode, ContextUtility.getCurrentContextName());
resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false);
logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName());
} catch (Exception e) {
logger.error("Unable to add {} to current context ({})", hostingNode, ContextUtility.getCurrentContextName(), e);
logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e);
}
}
public void removeFromContext()
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(containerContext.id());
try {
resourceRegistryPublisher.removeResourceFromCurrentContext(hostingNode, false);
logger.info("{} successfully removed from current context ({})", hostingNode,
resourceRegistryPublisher.removeResourceFromCurrentContext(HostingNode.NAME, uuid, false);
logger.info("{} with UUID {} successfully removed from current context ({})", HostingNode.NAME, uuid,
ContextUtility.getCurrentContextName());
} catch (Exception e) {
logger.error("Unable to remove {} from current context ({})", hostingNode, ContextUtility.getCurrentContextName(), e);
logger.error("Unable to remove {} with UUID {} from current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e);
}
}
public void removeFromContext(UUID contextUUID)
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
String contextFullName = ContextCache.getInstance().getContextFullNameByUUID(contextUUID);
UUID uuid = UUID.fromString(containerContext.id());
try {
resourceRegistryPublisher.removeResourceFromContext(hostingNode, contextUUID, false);
logger.info("{} successfully removed from context ({})", hostingNode, contextFullName);
resourceRegistryPublisher.removeResourceFromContext(HostingNode.NAME, uuid, contextUUID, false);
logger.info("{} with UUID {} successfully removed from context ({})", HostingNode.NAME, uuid, contextFullName);
} catch (Exception e) {
logger.error("Unable to remove {} from current context ({})", hostingNode, contextFullName, e);
logger.error("Unable to remove {} from current context ({})", HostingNode.NAME, uuid, contextFullName, e);
}
}
@ -393,7 +396,7 @@ public class HostingNodeManager {
locationFacet.setLongitude(site.longitude());
hostingNode.addFacet(locationFacet);
logger.info("hostingNode instanciated");
logger.info("{} with UUID {} instantiated", HostingNode.NAME, uuid);
return hostingNode;
}