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(); Set<String> startTokens = applicationContext.configuration().startTokens();
String firstToken = startTokens.iterator().next();
ContextUtility.setContextFromToken(firstToken);
eServiceManager = new EServiceManager(applicationContext);
Set<UUID> startContextsUUID = new HashSet<>(); Set<UUID> startContextsUUID = new HashSet<>();
for (String token : startTokens) { for (String token : startTokens) {
UUID contextUUID = ContextUtility.getContextUUID(token);
startContextsUUID.add(contextUUID);
if (create) { if (create) {
ContextUtility.setContextFromToken(token);
eServiceManager = new EServiceManager(applicationContext);
eServiceManager.createEService(); eServiceManager.createEService();
create = false; create = false;
} else { } else {
try { eServiceManager.addToContext(contextUUID);
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;
}
} }
/*
* 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(); Set<UUID> resourceContextsUUID = eServiceManager.getContextsUUID().keySet();
@ -152,12 +139,13 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
void onChanged(ApplicationLifecycle lc) { void onChanged(ApplicationLifecycle lc) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get(); String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
ContextUtility.setContextFromToken(applicationContext.configuration().startTokens().iterator().next());
}
try { try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); 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) { } catch (Exception e) {
logger.error("Failed to update {} State", EService.NAME, e); logger.error("Failed to update {} State", EService.NAME, e);
} finally { } finally {
@ -170,10 +158,6 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
void addTo(String token) { void addTo(String token) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get(); String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
previousToken = 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);
@ -192,10 +176,6 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
void removeFrom(String token) { void removeFrom(String token) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get(); String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
ContextUtility
.setContextFromToken(applicationContext.configuration().startTokens().iterator().next());
}
try { try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
eServiceManager.removeFromCurrentContext(); eServiceManager.removeFromCurrentContext();
@ -229,34 +209,38 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
return; return;
} }
String applicationName = applicationContext.name();
if (lc.state() == ApplicationState.active) { if (lc.state() == ApplicationState.active) {
logger.info("Scheduling periodic updates of {} from application {}", logger.info("Scheduling periodic updates of {} for application {}",
EService.NAME, applicationContext.name()); EService.NAME, applicationName);
} else { } else {
logger.info("Resuming periodic updates of {} for application {}", logger.info("Resuming periodic updates of {} for application {}",
EService.NAME, applicationContext.name()); EService.NAME, applicationName);
} }
final Runnable updateTask = new Runnable() { final Runnable updateTask = new Runnable() {
public void run() { public void run() {
String previousToken = SecurityTokenProvider.instance.get(); String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) { if (previousToken == null) {
ContextUtility.setContextFromToken( String token = applicationContext.configuration().startTokens().iterator().next();
applicationContext.configuration().startTokens().iterator().next()); ContextUtility.setContextFromToken(token);
} }
try { try {
eServiceManager.updateServiceStateFacet(); eServiceManager.updateFacets();
} catch (Exception e) { } 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 { } finally {
ContextUtility.setContextFromToken(previousToken); ContextUtility.setContextFromToken(previousToken);
} }
} }
}; };
periodicUpdates = Utils.scheduledServicePool.scheduleAtFixedRate(updateTask, // periodicUpdates = Utils.scheduledServicePool.scheduleAtFixedRate(updateTask,
Constants.application_republish_frequency_in_minutes, // Constants.application_republish_frequency_in_minutes,
Constants.application_republish_frequency_in_minutes, TimeUnit.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) { synchronized void cancelPeriodicUpdates(ContainerLifecycle ignore) {
if (periodicUpdates != null) { if (periodicUpdates != null) {
String applicationName = applicationContext.name();
logger.trace("Stopping periodic updates of {} for application {} ", logger.trace("Stopping periodic updates of {} for application {} ",
EService.NAME, applicationContext.name()); EService.NAME, applicationName);
try { try {
periodicUpdates.cancel(true); periodicUpdates.cancel(true);
periodicUpdates = null; periodicUpdates = null;
} catch (Exception e) { } catch (Exception e) {
logger.warn("Could not stop periodic updates of {} for application {}", 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(); List<String> startTokens = containerContext.configuration().startTokens();
String firstToken = startTokens.iterator().next();
ContextUtility.setContextFromToken(firstToken);
hostingNodeManager = new HostingNodeManager(containerContext);
Set<UUID> startContextsUUID = new HashSet<>(); Set<UUID> startContextsUUID = new HashSet<>();
for (String token : startTokens) { 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); UUID contextUUID = ContextUtility.getContextUUID(token);
startContextsUUID.add(contextUUID); 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(); Set<UUID> resourceContextsUUID = hostingNodeManager.getContextsUUID().keySet();
@ -157,21 +140,13 @@ public class HostingNodeHandler extends ContainerHandler {
void onChanged(ContainerLifecycle cl) { void onChanged(ContainerLifecycle cl) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get(); String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
ContextUtility.setContextFromToken(containerContext.configuration().startTokens().iterator().next());
}
try { try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
ContainerState containerState = cl.state(); if (previousToken == null) {
switch (containerState) { String token = containerContext.configuration().startTokens().iterator().next();
case active: ContextUtility.setContextFromToken(token);
hostingNodeManager.updateFacets();
break;
default:
hostingNodeManager.updateStatus();
break;
} }
hostingNodeManager.updateFacets();
} catch (Exception e) { } catch (Exception e) {
logger.error("Failed to update {} State", HostingNode.NAME, e); logger.error("Failed to update {} State", HostingNode.NAME, e);
} finally { } finally {
@ -184,10 +159,6 @@ public class HostingNodeHandler extends ContainerHandler {
void addTo(String token) { void addTo(String token) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get(); String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
previousToken = containerContext.configuration().startTokens().iterator().next();
// ContextUtility.setContextFromToken(previousToken);
}
try { try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
ContextUtility.setContextFromToken(token); ContextUtility.setContextFromToken(token);
@ -205,10 +176,6 @@ public class HostingNodeHandler extends ContainerHandler {
void removeFrom(String token) { void removeFrom(String token) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get(); String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
previousToken = containerContext.configuration().startTokens().iterator().next();
// ContextUtility.setContextFromToken(previousToken);
}
try { try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
ContextUtility.setContextFromToken(token); ContextUtility.setContextFromToken(token);
@ -217,6 +184,7 @@ public class HostingNodeHandler extends ContainerHandler {
} catch (Exception e) { } catch (Exception e) {
logger.error("Failed to update Service State", e); logger.error("Failed to update Service State", e);
} finally { } finally {
ContextUtility.setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL); Thread.currentThread().setContextClassLoader(contextCL);
} }
} }
@ -250,15 +218,17 @@ public class HostingNodeHandler extends ContainerHandler {
final Runnable updateTask = new Runnable() { final Runnable updateTask = new Runnable() {
public void run() { public void run() {
String currentToken = SecurityTokenProvider.instance.get(); String previousToken = SecurityTokenProvider.instance.get();
if (currentToken == null) if (previousToken == null) {
currentToken = containerContext.configuration().startTokens().iterator().next(); String token = containerContext.configuration().startTokens().iterator().next();
ContextUtility.setContextFromToken(token);
ContextUtility.setContextFromToken(currentToken); }
try { try {
hostingNodeManager.updateFacets(); hostingNodeManager.updateFacets();
} catch (Exception e) { } catch (Exception e) {
logger.error("Cannot complete periodic update of {}", HostingNode.NAME, 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; package org.gcube.smartgears.handler.resourceregistry.resourcemanager;
import java.net.URI; import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
@ -14,10 +15,13 @@ import javax.servlet.ServletRegistration;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider; 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.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.Header;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint; 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.AddConstraint;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint; 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.contexts.ContextCache;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; 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.impl.relations.isrelatedto.ActivatesImpl;
import org.gcube.resourcemanagement.model.reference.entities.facets.AccessPointFacet; 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.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.SoftwareFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.StateFacet; 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.EService;
import org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode; import org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode;
import org.gcube.resourcemanagement.model.reference.properties.ValueSchema; 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.consistsof.IsIdentifiedBy;
import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Activates; import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Activates;
import org.gcube.smartgears.configuration.application.ApplicationConfiguration; import org.gcube.smartgears.configuration.application.ApplicationConfiguration;
@ -64,10 +71,8 @@ public class EServiceManager {
private ResourceRegistryPublisher resourceRegistryPublisher; private ResourceRegistryPublisher resourceRegistryPublisher;
// private Activates<HostingNode, EService> activates;
private EService eService; private EService eService;
private StateFacet stateFacet;
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
public EServiceManager(ApplicationContext applicationContext) { public EServiceManager(ApplicationContext applicationContext) {
@ -75,10 +80,6 @@ public class EServiceManager {
this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
} }
public EService getEService() {
return eService;
}
public void addEServiceToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { public void addEServiceToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
String currentToken = SecurityTokenProvider.instance.get(); String currentToken = SecurityTokenProvider.instance.get();
UUID contextUUID = ContextUtility.getContextUUID(currentToken); UUID contextUUID = ContextUtility.getContextUUID(currentToken);
@ -102,9 +103,9 @@ public class EServiceManager {
}else { }else {
resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false); 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) { } 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 { }finally {
ContextUtility.setContextFromToken(currentToken); ContextUtility.setContextFromToken(currentToken);
} }
@ -133,9 +134,9 @@ public class EServiceManager {
}else { }else {
resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false); 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) { } 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 { }finally {
ContextUtility.setContextFromToken(currentToken); ContextUtility.setContextFromToken(currentToken);
} }
@ -145,9 +146,9 @@ public class EServiceManager {
UUID uuid = UUID.fromString(applicationContext.container().id()); UUID uuid = UUID.fromString(applicationContext.container().id());
try { try {
resourceRegistryPublisher.addToContext(HostingNode.NAME, uuid, contextUUID, false); 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) { } 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()); UUID uuid = UUID.fromString(applicationContext.container().id());
try { try {
resourceRegistryPublisher.removeResourceFromCurrentContext(HostingNode.NAME, uuid, false); 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()); ContextUtility.getCurrentContextName());
} catch (Exception e) { } 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); resourceRegistryPublisher.removeResourceFromContext(HostingNode.NAME, uuid, contextUUID, false);
logger.info("{} with UUID {} successfully removed from context ({})", HostingNode.NAME, uuid, contextFullName); logger.info("{} with UUID {} successfully removed from context ({})", HostingNode.NAME, uuid, contextFullName);
} catch (Exception e) { } 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(); 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() { private EService instantiateEService() {
logger.info("Creating {} for {}", EService.NAME, applicationContext.name()); logger.info("Creating {} for {}", EService.NAME, applicationContext.name());
@ -243,15 +262,10 @@ public class EServiceManager {
Date date = Calendar.getInstance().getTime(); Date date = Calendar.getInstance().getTime();
stateFacet = new StateFacetImpl(); StateFacet stateFacet = getStateFacet(null, date);
String state = getState();
stateFacet.setValue(state);
stateFacet.setAdditionalProperty("date", date);
eService.addFacet(stateFacet); eService.addFacet(stateFacet);
EventFacet eventFacet = new EventFacetImpl(); EventFacet eventFacet = getEventFacet(date);;
eventFacet.setEvent(state);
eventFacet.setDate(date);
eService.addFacet(eventFacet); eService.addFacet(eventFacet);
return eService; return eService;
@ -263,8 +277,7 @@ public class EServiceManager {
try { try {
ResourceRegistryClientFactory.includeContextsInInstanceHeader(true); ResourceRegistryClientFactory.includeContextsInInstanceHeader(true);
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
stateFacet = eService.getFacets(StateFacet.class).get(0); updateFacets();
updateServiceStateFacet();
} catch (NotFoundException e) { } catch (NotFoundException e) {
eService = instantiateEService(); eService = instantiateEService();
createActivatesRelation(eService); createActivatesRelation(eService);
@ -281,38 +294,66 @@ public class EServiceManager {
// 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); updateFacets();
updateServiceStateFacet();
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
throw e; throw e;
} }
return eService; 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(); Date date = Calendar.getInstance().getTime();
String state = getState(); List<ConsistsOf<? extends Resource, ? extends Facet>> consistsOfToRemove = new ArrayList<>();
stateFacet.setValue(state);
boolean found = false; List<ConsistsOf<? extends Resource, ? extends Facet>> consistsOfList = eService.getConsistsOf();
List<EventFacet> eventFacets = eService.getFacets(EventFacet.class); for (ConsistsOf<? extends Resource, ? extends Facet> c : consistsOfList) {
for(EventFacet eventFacet : eventFacets) { if (c.getTarget() instanceof StateFacet) {
if(eventFacet.getEvent().compareTo(state)==0) { stateFacet = (StateFacet) c.getTarget();
found = true; stateFacet = getStateFacet(stateFacet, date);
eventFacet.setDate(date); continue;
break;
} }
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(); if(eventFacet == null) {
eventFacet.setDate(date); eventFacet = getEventFacet(date);
eventFacet.setEvent(state);
eService.addFacet(eventFacet); 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) private Activates<HostingNode, EService> createActivatesRelation(EService eService)
@ -340,14 +381,14 @@ public class EServiceManager {
} }
public void removeEService() throws ResourceRegistryException { // public void removeEService() throws ResourceRegistryException {
try { // try {
resourceRegistryPublisher.delete(eService); // resourceRegistryPublisher.delete(eService);
} catch (ResourceRegistryException e) { // } catch (ResourceRegistryException e) {
logger.error("Unable to delete {}. Going to set the state to {}", applicationContext.name(), getState()); // logger.error("Unable to delete {}. Going to set the state to {}", applicationContext.name(), getState());
updateServiceStateFacet(); // updateFacets();
} // }
} // }
public Map<UUID,String> getContextsUUID() throws Exception { public Map<UUID,String> getContextsUUID() throws Exception {
return resourceRegistryPublisher.getResourceContexts(eService); return resourceRegistryPublisher.getResourceContexts(eService);

View File

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