Improving code

This commit is contained in:
Luca Frosini 2022-06-09 19:20:10 +02:00
parent c09eba8a57
commit 16da31d117
4 changed files with 182 additions and 228 deletions

View File

@ -102,34 +102,21 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
Set<String> startTokens = applicationContext.configuration().startTokens();
String firstToken = startTokens.iterator().next();
ContextUtility.setContextFromToken(firstToken);
eServiceManager = new EServiceManager(applicationContext);
Set<UUID> startContextsUUID = new HashSet<>();
for (String token : startTokens) {
UUID contextUUID = ContextUtility.getContextUUID(token);
startContextsUUID.add(contextUUID);
if (create) {
ContextUtility.setContextFromToken(token);
eServiceManager = new EServiceManager(applicationContext);
eServiceManager.createEService();
create = false;
} else {
try {
UUID contextUUID = ContextUtility.getContextUUID(token);
eServiceManager.addToContext(contextUUID);
} 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);
throw e;
}
eServiceManager.addToContext(contextUUID);
}
/*
* Do not move this code before having instantiated HostingNodeManager
* which in turn instantiate the ResourceRegistryPublisher
* which in turn initialize ContextCacheRenewal in ContextCache.
* Please note that to properly instantiate ResourceRegistryPublisher
* it is necessary having set a token.
*/
UUID contextUUID = ContextUtility.getContextUUID(token);
startContextsUUID.add(contextUUID);
}
Set<UUID> resourceContextsUUID = eServiceManager.getContextsUUID().keySet();
@ -152,12 +139,13 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
void onChanged(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();
if(previousToken==null) {
String token = applicationContext.configuration().startTokens().iterator().next();
ContextUtility.setContextFromToken(token);
}
eServiceManager.updateFacets();
} catch (Exception e) {
logger.error("Failed to update {} State", EService.NAME, e);
} finally {
@ -170,10 +158,6 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
void addTo(String token) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
previousToken = applicationContext.configuration().startTokens().iterator().next();
// ContextUtility.setContextFromToken(previousToken);
}
try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
ContextUtility.setContextFromToken(token);
@ -192,10 +176,6 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
void removeFrom(String token) {
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.removeFromCurrentContext();
@ -229,34 +209,38 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
return;
}
String applicationName = applicationContext.name();
if (lc.state() == ApplicationState.active) {
logger.info("Scheduling periodic updates of {} from application {}",
EService.NAME, applicationContext.name());
logger.info("Scheduling periodic updates of {} for application {}",
EService.NAME, applicationName);
} else {
logger.info("Resuming periodic updates of {} for application {}",
EService.NAME, applicationContext.name());
EService.NAME, applicationName);
}
final Runnable updateTask = new Runnable() {
public void run() {
String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
ContextUtility.setContextFromToken(
applicationContext.configuration().startTokens().iterator().next());
String token = applicationContext.configuration().startTokens().iterator().next();
ContextUtility.setContextFromToken(token);
}
try {
eServiceManager.updateServiceStateFacet();
eServiceManager.updateFacets();
} catch (Exception e) {
logger.error("Cannot complete periodic update of {}", EService.NAME, e);
logger.error("Cannot complete periodic update of {} for application {}", EService.NAME, applicationName, e);
} finally {
ContextUtility.setContextFromToken(previousToken);
}
}
};
periodicUpdates = Utils.scheduledServicePool.scheduleAtFixedRate(updateTask,
Constants.application_republish_frequency_in_minutes,
Constants.application_republish_frequency_in_minutes, TimeUnit.MINUTES);
// periodicUpdates = Utils.scheduledServicePool.scheduleAtFixedRate(updateTask,
// Constants.application_republish_frequency_in_minutes,
// Constants.application_republish_frequency_in_minutes, TimeUnit.MINUTES);
periodicUpdates = Utils.scheduledServicePool.scheduleAtFixedRate(updateTask, 120, 120, TimeUnit.SECONDS);
}
@ -264,15 +248,18 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
synchronized void cancelPeriodicUpdates(ContainerLifecycle ignore) {
if (periodicUpdates != null) {
String applicationName = applicationContext.name();
logger.trace("Stopping periodic updates of {} for application {} ",
EService.NAME, applicationContext.name());
EService.NAME, applicationName);
try {
periodicUpdates.cancel(true);
periodicUpdates = null;
} catch (Exception e) {
logger.warn("Could not stop periodic updates of {} for application {}",
EService.NAME, applicationContext.name(), e);
EService.NAME, applicationName, e);
}
}
}

View File

@ -103,38 +103,21 @@ public class HostingNodeHandler extends ContainerHandler {
List<String> startTokens = containerContext.configuration().startTokens();
String firstToken = startTokens.iterator().next();
ContextUtility.setContextFromToken(firstToken);
hostingNodeManager = new HostingNodeManager(containerContext);
Set<UUID> startContextsUUID = new HashSet<>();
for (String token : startTokens) {
if (create) {
ContextUtility.setContextFromToken(token);
hostingNodeManager = new HostingNodeManager(containerContext);
hostingNodeManager.createHostingNode();
containerContext.properties().add(new Property(Constants.HOSTING_NODE_MANAGER_PROPERTY, hostingNodeManager));
create = false;
} else {
try {
UUID contextUUID = ContextUtility.getContextUUID(token);
hostingNodeManager.addToContext(contextUUID);
} 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);
throw e;
}
}
/*
* Do not move this code before having instantiated HostingNodeManager
* which in turn instantiate the ResourceRegistryPublisher
* which in turn initialize ContextCacheRenewal in ContextCache.
* Please note that to properly instantiate ResourceRegistryPublisher
* it is necessary having set a token.
*/
UUID contextUUID = ContextUtility.getContextUUID(token);
startContextsUUID.add(contextUUID);
if (create) {
hostingNodeManager.createHostingNode();
containerContext.properties().add(new Property(Constants.HOSTING_NODE_MANAGER_PROPERTY, hostingNodeManager));
create = false;
} else {
hostingNodeManager.addToContext(contextUUID);
}
}
Set<UUID> resourceContextsUUID = hostingNodeManager.getContextsUUID().keySet();
@ -157,21 +140,13 @@ public class HostingNodeHandler extends ContainerHandler {
void onChanged(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());
ContainerState containerState = cl.state();
switch (containerState) {
case active:
hostingNodeManager.updateFacets();
break;
default:
hostingNodeManager.updateStatus();
break;
if (previousToken == null) {
String token = containerContext.configuration().startTokens().iterator().next();
ContextUtility.setContextFromToken(token);
}
hostingNodeManager.updateFacets();
} catch (Exception e) {
logger.error("Failed to update {} State", HostingNode.NAME, e);
} finally {
@ -184,10 +159,6 @@ public class HostingNodeHandler extends ContainerHandler {
void addTo(String token) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
previousToken = containerContext.configuration().startTokens().iterator().next();
// ContextUtility.setContextFromToken(previousToken);
}
try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
ContextUtility.setContextFromToken(token);
@ -205,10 +176,6 @@ public class HostingNodeHandler extends ContainerHandler {
void removeFrom(String token) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
previousToken = containerContext.configuration().startTokens().iterator().next();
// ContextUtility.setContextFromToken(previousToken);
}
try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
ContextUtility.setContextFromToken(token);
@ -217,6 +184,7 @@ public class HostingNodeHandler extends ContainerHandler {
} catch (Exception e) {
logger.error("Failed to update Service State", e);
} finally {
ContextUtility.setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL);
}
}
@ -250,15 +218,17 @@ public class HostingNodeHandler extends ContainerHandler {
final Runnable updateTask = new Runnable() {
public void run() {
String currentToken = SecurityTokenProvider.instance.get();
if (currentToken == null)
currentToken = containerContext.configuration().startTokens().iterator().next();
ContextUtility.setContextFromToken(currentToken);
String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
String token = containerContext.configuration().startTokens().iterator().next();
ContextUtility.setContextFromToken(token);
}
try {
hostingNodeManager.updateFacets();
} catch (Exception e) {
logger.error("Cannot complete periodic update of {}", HostingNode.NAME, e);
}finally {
ContextUtility.setContextFromToken(previousToken);
}
}
};

View File

@ -1,6 +1,7 @@
package org.gcube.smartgears.handler.resourceregistry.resourcemanager;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
@ -14,10 +15,13 @@ 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.PropagationConstraintImpl;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.AddConstraint;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
@ -38,11 +42,14 @@ import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentified
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.MemoryFacet;
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;
import org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode;
import org.gcube.resourcemanagement.model.reference.properties.ValueSchema;
import org.gcube.resourcemanagement.model.reference.relations.consistsof.HasPersistentMemory;
import org.gcube.resourcemanagement.model.reference.relations.consistsof.HasVolatileMemory;
import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy;
import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Activates;
import org.gcube.smartgears.configuration.application.ApplicationConfiguration;
@ -64,10 +71,8 @@ public class EServiceManager {
private ResourceRegistryPublisher resourceRegistryPublisher;
// private Activates<HostingNode, EService> activates;
private EService eService;
private StateFacet stateFacet;
private ApplicationContext applicationContext;
public EServiceManager(ApplicationContext applicationContext) {
@ -75,10 +80,6 @@ public class EServiceManager {
this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
}
public EService getEService() {
return eService;
}
public void addEServiceToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
String currentToken = SecurityTokenProvider.instance.get();
UUID contextUUID = ContextUtility.getContextUUID(currentToken);
@ -102,9 +103,9 @@ public class EServiceManager {
}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 context ({})", EService.NAME, uuid, ContextUtility.getCurrentContextName());
} 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 context ({})", EService.NAME, uuid, ContextUtility.getCurrentContextName(), e);
}finally {
ContextUtility.setContextFromToken(currentToken);
}
@ -133,9 +134,9 @@ public class EServiceManager {
}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 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);
logger.error("Unable to add {} with UUID {} to context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e);
}finally {
ContextUtility.setContextFromToken(currentToken);
}
@ -145,9 +146,9 @@ public class EServiceManager {
UUID uuid = UUID.fromString(applicationContext.container().id());
try {
resourceRegistryPublisher.addToContext(HostingNode.NAME, uuid, contextUUID, false);
logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName());
logger.info("{} with UUID {} successfully added to 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);
logger.error("Unable to add {} with UUID {} to context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e);
}
}
@ -156,10 +157,10 @@ public class EServiceManager {
UUID uuid = UUID.fromString(applicationContext.container().id());
try {
resourceRegistryPublisher.removeResourceFromCurrentContext(HostingNode.NAME, uuid, false);
logger.info("{} with UUID {} successfully removed from current context ({})", HostingNode.NAME, uuid,
logger.info("{} with UUID {} successfully removed from context ({})", HostingNode.NAME, uuid,
ContextUtility.getCurrentContextName());
} catch (Exception e) {
logger.error("Unable to remove {} with UUID {} from current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e);
logger.error("Unable to remove {} with UUID {} from context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e);
}
}
@ -171,7 +172,7 @@ public class EServiceManager {
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.NAME, uuid, contextFullName, e);
logger.error("Unable to remove {} from context ({})", HostingNode.NAME, uuid, contextFullName, e);
}
}
@ -199,6 +200,24 @@ public class EServiceManager {
return applicationContext.lifecycle().state().remoteForm().toLowerCase();
}
private StateFacet getStateFacet(StateFacet stateFacet, Date date) {
if (stateFacet == null) {
stateFacet = new StateFacetImpl();
}
String state = getState();
stateFacet.setValue(state);
stateFacet.setAdditionalProperty("date", date);
return stateFacet;
}
private EventFacet getEventFacet(Date date) {
EventFacet eventFacet = new EventFacetImpl();
eventFacet.setDate(date);
String state = getState();
eventFacet.setEvent(state);
return eventFacet;
}
private EService instantiateEService() {
logger.info("Creating {} for {}", EService.NAME, applicationContext.name());
@ -243,15 +262,10 @@ public class EServiceManager {
Date date = Calendar.getInstance().getTime();
stateFacet = new StateFacetImpl();
String state = getState();
stateFacet.setValue(state);
stateFacet.setAdditionalProperty("date", date);
StateFacet stateFacet = getStateFacet(null, date);
eService.addFacet(stateFacet);
EventFacet eventFacet = new EventFacetImpl();
eventFacet.setEvent(state);
eventFacet.setDate(date);
EventFacet eventFacet = getEventFacet(date);;
eService.addFacet(eventFacet);
return eService;
@ -263,8 +277,7 @@ public class EServiceManager {
try {
ResourceRegistryClientFactory.includeContextsInInstanceHeader(true);
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
stateFacet = eService.getFacets(StateFacet.class).get(0);
updateServiceStateFacet();
updateFacets();
} catch (NotFoundException e) {
eService = instantiateEService();
createActivatesRelation(eService);
@ -281,38 +294,66 @@ public class EServiceManager {
// already be in the context due to propagation constraint.
createActivatesRelation(eService);
}
stateFacet = eService.getFacets(StateFacet.class).get(0);
updateServiceStateFacet();
updateFacets();
} catch (ResourceRegistryException e) {
throw e;
}
return eService;
}
public void updateServiceStateFacet() throws ResourceRegistryException {
public EService updateFacets() throws ResourceRegistryException {
logger.debug("Updating {} for {}", EService.NAME, applicationContext.configuration().name());
StateFacet stateFacet = null;
EventFacet eventFacet = null;
Date date = Calendar.getInstance().getTime();
String state = getState();
stateFacet.setValue(state);
List<ConsistsOf<? extends Resource, ? extends Facet>> consistsOfToRemove = new ArrayList<>();
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;
List<ConsistsOf<? extends Resource, ? extends Facet>> consistsOfList = eService.getConsistsOf();
for (ConsistsOf<? extends Resource, ? extends Facet> c : consistsOfList) {
if (c.getTarget() instanceof StateFacet) {
stateFacet = (StateFacet) c.getTarget();
stateFacet = getStateFacet(stateFacet, date);
continue;
}
if(c.getTarget() instanceof EventFacet) {
eventFacet = (EventFacet) c.getTarget();
String value = eventFacet.getEvent();
if(value.compareTo(getState())==0) {
// This facet must be updated (the date must be updated) so it must not be removed from udpate
eventFacet.setDate(date);
continue;
}else {
// This is not the event facet to be updated
// Setting the variable to null so it will be created if the event does not already exists.
eventFacet = null;
}
}
consistsOfToRemove.add(c);
}
// Resource Update has effect only on specified facets.
// Removing the ones that have not to be changed.
consistsOfList.removeAll(consistsOfToRemove);
if(!found) {
EventFacet eventFacet = new EventFacetImpl();
eventFacet.setDate(date);
eventFacet.setEvent(state);
if(eventFacet == null) {
eventFacet = getEventFacet(date);
eService.addFacet(eventFacet);
}
eService = resourceRegistryPublisher.updateResource(eService);
try {
eService = resourceRegistryPublisher.updateResource(eService);
}catch (ResourceRegistryException e) {
logger.error("error trying to publish hosting node", e);
}
return eService;
}
private Activates<HostingNode, EService> createActivatesRelation(EService eService)
@ -340,14 +381,14 @@ public class EServiceManager {
}
public void removeEService() throws ResourceRegistryException {
try {
resourceRegistryPublisher.delete(eService);
} catch (ResourceRegistryException e) {
logger.error("Unable to delete {}. Going to set the state to {}", applicationContext.name(), getState());
updateServiceStateFacet();
}
}
// public void removeEService() throws ResourceRegistryException {
// try {
// resourceRegistryPublisher.delete(eService);
// } catch (ResourceRegistryException e) {
// logger.error("Unable to delete {}. Going to set the state to {}", applicationContext.name(), getState());
// updateFacets();
// }
// }
public Map<UUID,String> getContextsUUID() throws Exception {
return resourceRegistryPublisher.getResourceContexts(eService);

View File

@ -13,6 +13,7 @@ import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@ -134,9 +135,9 @@ public class HostingNodeManager {
}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 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);
logger.error("Unable to add {} with UUID {} to context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e);
}
}
@ -144,9 +145,9 @@ public class HostingNodeManager {
UUID uuid = UUID.fromString(containerContext.id());
try {
resourceRegistryPublisher.addToContext(HostingNode.NAME, uuid, contextUUID, false);
logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName());
logger.info("{} with UUID {} successfully added to 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);
logger.error("Unable to add {} with UUID {} to context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e);
}
}
@ -155,10 +156,10 @@ public class HostingNodeManager {
UUID uuid = UUID.fromString(containerContext.id());
try {
resourceRegistryPublisher.removeResourceFromCurrentContext(HostingNode.NAME, uuid, false);
logger.info("{} with UUID {} successfully removed from current context ({})", HostingNode.NAME, uuid,
logger.info("{} with UUID {} successfully removed from context ({})", HostingNode.NAME, uuid,
ContextUtility.getCurrentContextName());
} catch (Exception e) {
logger.error("Unable to remove {} with UUID {} from current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e);
logger.error("Unable to remove {} with UUID {} from context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e);
}
}
@ -170,25 +171,28 @@ public class HostingNodeManager {
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.NAME, uuid, contextFullName, e);
logger.error("Unable to remove {} from context ({})", HostingNode.NAME, uuid, contextFullName, e);
}
}
public HostingNode updateFacets() throws ResourceRegistryException {
logger.debug("Updating {}", HostingNode.NAME);
StateFacet stateFacet = null;
MemoryFacet ramFacet = null;
MemoryFacet jvmMemoryFacet = null;
MemoryFacet disk = null;
StateFacet stateFacet = null;
EventFacet eventFacet = null;
Date date = Calendar.getInstance().getTime();
List<ConsistsOf<? extends Resource, ? extends Facet>> consistsOfToRemove = new ArrayList<>();
List<ConsistsOf<? extends Resource, ? extends Facet>> consistsOfList = hostingNode.getConsistsOf();
for (ConsistsOf<? extends Resource, ? extends Facet> c : consistsOfList) {
if (c.getTarget() instanceof StateFacet) {
stateFacet = (StateFacet) c.getTarget();
stateFacet = getStateFacet(stateFacet);
stateFacet = getStateFacet(stateFacet, date);
continue;
}
@ -214,60 +218,12 @@ public class HostingNodeManager {
continue;
}
consistsOfToRemove.add(c);
}
// Resource Update has effect only on specified facet.
// Removing the one that have not to be changed
consistsOfList.removeAll(consistsOfToRemove);
try {
hostingNode = resourceRegistryPublisher.updateResource(hostingNode);
} catch (NotFoundException e) {
/* Update failed trying to recreate it */
// ReAdding the removed relations to recreate all
consistsOfList.addAll(consistsOfToRemove);
hostingNode = resourceRegistryPublisher.createResource(hostingNode);
} catch (AvailableInAnotherContextException e) {
// addToCurrentContext();
// hostingNode = resourceRegistryPublisher.updateResource(hostingNode);
logger.error("", e);
throw e;
} catch (ResourceRegistryException e) {
logger.error("error trying to publish hosting node", e);
}
return hostingNode;
}
public HostingNode updateStatus() throws ResourceRegistryException {
ContainerState containerState = containerContext.lifecycle().state();
logger.debug("Setting {} {}", HostingNode.NAME, containerState.remoteForm().toLowerCase());
EventFacet eventFacet = null;
StateFacet stateFacet = null;
List<ConsistsOf<? extends Resource, ? extends Facet>> consistsOfToRemove = new ArrayList<>();
List<ConsistsOf<? extends Resource, ? extends Facet>> consistsOfList = hostingNode.getConsistsOf();
for (ConsistsOf<? extends Resource, ? extends Facet> c : consistsOfList) {
if (c.getTarget() instanceof StateFacet) {
stateFacet = (StateFacet) c.getTarget();
stateFacet = getStateFacet(stateFacet);
continue;
}
if(c.getTarget() instanceof EventFacet) {
eventFacet = (EventFacet) c.getTarget();
String value = eventFacet.getEvent();
if(value.compareTo(containerState.remoteForm().toLowerCase())==0) {
if(value.compareTo(getState())==0) {
// This facet must be updated (the date must be updated) so it must not be removed from udpate
eventFacet.setDate(Calendar.getInstance().getTime());
eventFacet.setDate(date);
continue;
}else {
// This is not the event facet to be updated
@ -277,32 +233,24 @@ public class HostingNodeManager {
}
consistsOfToRemove.add(c);
}
// Resource Update has effect only on specified facet.
// Removing the one that have not to be changed
// Resource Update has effect only on specified facets.
// Removing the ones that have not to be changed.
consistsOfList.removeAll(consistsOfToRemove);
if(eventFacet == null) {
eventFacet = getEventFacet();
eventFacet = getEventFacet(date);
hostingNode.addFacet(eventFacet);
}
try {
hostingNode = resourceRegistryPublisher.updateResource(hostingNode);
} catch (NotFoundException e) {
/* Update failed trying to recreate it */
// ReAdding the removed relations to recreate all
consistsOfList.addAll(consistsOfToRemove);
hostingNode = resourceRegistryPublisher.createResource(hostingNode);
} catch (AvailableInAnotherContextException e) {
// addToCurrentContext();
// hostingNode = resourceRegistryPublisher.updateResource(hostingNode);
logger.error("", e);
throw e;
} catch (ResourceRegistryException e) {
logger.error("error trying to publish hosting node", e);
}
return hostingNode;
}
@ -399,10 +347,12 @@ public class HostingNodeManager {
SimplePropertyFacet simplePropertyFacet = addEnvironmentVariables(containerConfiguration);
hostingNode.addFacet(simplePropertyFacet);
StateFacet stateFacet = getStateFacet(null);
Date date = Calendar.getInstance().getTime();
StateFacet stateFacet = getStateFacet(null, date);
hostingNode.addFacet(stateFacet);
EventFacet eventFacet = getEventFacet();
EventFacet eventFacet = getEventFacet(date);
hostingNode.addFacet(eventFacet);
MemoryFacet ramFacet = getRamInfo(null);
@ -452,19 +402,25 @@ public class HostingNodeManager {
return hostingNode;
}
private StateFacet getStateFacet(StateFacet stateFacet) {
public String getState() {
return containerContext.lifecycle().state().remoteForm().toLowerCase();
}
private StateFacet getStateFacet(StateFacet stateFacet, Date date) {
if (stateFacet == null) {
stateFacet = new StateFacetImpl();
}
String state = containerContext.lifecycle().state().remoteForm().toLowerCase();
String state = getState();
stateFacet.setValue(state);
stateFacet.setAdditionalProperty("date", date);
return stateFacet;
}
private EventFacet getEventFacet() {
private EventFacet getEventFacet(Date date) {
EventFacet eventFacet = new EventFacetImpl();
String state = containerContext.lifecycle().state().remoteForm().toLowerCase();
eventFacet.setDate(Calendar.getInstance().getTime());
eventFacet.setDate(date);
String state = getState();
eventFacet.setEvent(state);
return eventFacet;
}