Updating Resource Registry Handler

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/resource-registry-handlers@147155 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-04-26 14:48:26 +00:00
parent 3407a40547
commit 9632747a0f
2 changed files with 85 additions and 59 deletions

View File

@ -40,7 +40,6 @@ import org.gcube.informationsystem.model.embedded.PropagationConstraint;
import org.gcube.informationsystem.model.embedded.PropagationConstraint.AddConstraint;
import org.gcube.informationsystem.model.embedded.PropagationConstraint.RemoveConstraint;
import org.gcube.informationsystem.model.embedded.ValueSchema;
import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.Resource;
import org.gcube.informationsystem.model.entity.facet.AccessPointFacet;
import org.gcube.informationsystem.model.entity.facet.ServiceStateFacet;
@ -55,6 +54,8 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
import org.gcube.informationsystem.resourceregistry.client.Direction;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
@ -142,11 +143,14 @@ public class EServiceManager extends ApplicationLifecycleHandler {
@Observes({ activation, stop, failure })
void onChanged(ApplicationLifecycle lc) {
String state = lc.state().remoteForm().toLowerCase();
String state = getState(lc);
logger.debug("Moving app {} to {}", applicationContext.name(),
state);
EService eService = applicationContext.properties().lookup(Constants.ESERVICE_PROPERTY).value(EService.class);
createOrUpdateServiceStateFacet(eService, state);
try {
createOrUpdateServiceStateFacet(getEService(), state);
} catch (ResourceRegistryException e) {
rethrowUnchecked(e);
}
}
@Observes(value = addToContext)
@ -163,9 +167,15 @@ public class EServiceManager extends ApplicationLifecycleHandler {
removeHostingNodeFromContext(token);
}
});
}
private String getState(ApplicationLifecycle lc){
return lc.state().remoteForm().toLowerCase();
}
private void schedulePeriodicUpdates() {
// register to cancel updates
@ -196,11 +206,9 @@ public class EServiceManager extends ApplicationLifecycleHandler {
final Runnable updateTask = new Runnable() {
public void run() {
EService eService = applicationContext.properties().lookup(Constants.ESERVICE_PROPERTY).value(EService.class);
try {
String state = lc.state().remoteForm()
.toLowerCase();
createOrUpdateServiceStateFacet(eService, state);
String state = getState(lc);
createOrUpdateServiceStateFacet(getEService(), state);
} catch (Exception e) {
logger.error(
"cannot complete periodic update of EService",
@ -295,6 +303,29 @@ public class EServiceManager extends ApplicationLifecycleHandler {
}
}
}
private EService getEService() throws ResourceRegistryException {
EService eService = null;
ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create();
UUID eServiceUUID = UUID.fromString(this.applicationContext.id());
try {
resourceRegistryClient.exists(EService.class, eServiceUUID);
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
} catch (ERNotFoundException e) {
eService = instantiateEService();
eService = publishEservice(eService);
} catch (ERAvailableInAnotherContextException e) {
addHostingNodeToCurrentContext();
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
} catch (ResourceRegistryException e) {
throw e;
}
return eService;
}
private EService createEServiceAndHosts(
ResourceRegistryPublisher resourceRegistryPublisher,
@ -544,57 +575,58 @@ public class EServiceManager extends ApplicationLifecycleHandler {
}
*/
private void createOrUpdateServiceStateFacet(EService eService, String state) {
@SuppressWarnings("unchecked")
private void createOrUpdateServiceStateFacet(EService eService, String state) throws ResourceRegistryException {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get();
Thread.currentThread().setContextClassLoader(EServiceManager.class.getClassLoader());
ResourceRegistryClient resourceRegistryClient= ResourceRegistryClientFactory
.create();
ResourceRegistryPublisher resourceRegistryPublisher = ResourceRegistryPublisherFactory
.create();
UUID eServiceUUID = eService.getHeader().getUUID();
try {
if (previousToken == null) {
setContextFromToken((String) applicationContext.configuration()
.startTokens().toArray()[0]);
}
Thread.currentThread().setContextClassLoader(
EServiceManager.class.getClassLoader());
ServiceStateFacet serviceStateFacet = null;
List<ConsistsOf<? extends Resource, ? extends Facet>> consistsOfList = eService
.getConsistsOf();
for (ConsistsOf<? extends Resource, ? extends Facet> c : consistsOfList) {
if (c.getTarget() instanceof ServiceStateFacet) {
serviceStateFacet = (ServiceStateFacet) c.getTarget();
break;
}
}
ResourceRegistryPublisher resourceRegistryPublisher = ResourceRegistryPublisherFactory
.create();
try {
List<ServiceStateFacet> serviceStateFacets = eService.getFacets(ServiceStateFacet.class);
if(serviceStateFacets !=null && serviceStateFacets.size()>=1){
serviceStateFacet = serviceStateFacets.get(0);
serviceStateFacet.setValue(state);
serviceStateFacet = resourceRegistryPublisher.updateFacet(
ServiceStateFacet.class, serviceStateFacet);
} catch (Exception e) {
try {
ConsistsOf<EService, ServiceStateFacet> consistsOf = new ConsistsOfImpl<EService, ServiceStateFacet>(
eService, serviceStateFacet, null);
@SuppressWarnings("unchecked")
ConsistsOf<EService, ServiceStateFacet> createdConsistsOf = resourceRegistryPublisher.createConsistsOf(ConsistsOf.class, consistsOf);
eService.addFacet(createdConsistsOf);
} catch (Exception ex) {
eService = createEServiceAndHosts(
resourceRegistryPublisher, eService);
serviceStateFacet = resourceRegistryPublisher.updateFacet(ServiceStateFacet.class, serviceStateFacet);
for(int i=1; i<serviceStateFacets.size(); i++){
try {
logger.warn("You should not be here. There are more than one {}. Anyway deleting it : {}", ServiceStateFacet.class.getSimpleName(), serviceStateFacets.get(i));
resourceRegistryPublisher.deleteFacet(serviceStateFacets.get(i));
}catch (Exception e) {
logger.warn("Unable to delete {} which should not exists : {}", ServiceStateFacet.class.getSimpleName(), serviceStateFacets.get(i));
}
}
}else {
serviceStateFacet = new ServiceStateFacetImpl();
serviceStateFacet.setValue(state);
serviceStateFacet = resourceRegistryPublisher.createFacet(ServiceStateFacet.class, serviceStateFacet);
ConsistsOf<EService, ServiceStateFacet> consistsOf = new ConsistsOfImpl<EService, ServiceStateFacet>(
eService, serviceStateFacet, null);
consistsOf = resourceRegistryPublisher.createConsistsOf(ConsistsOf.class, consistsOf);
}
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
share(eService);
} catch (Exception e) {
rethrowUnchecked(e);
} finally {
setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL);
}
@ -645,10 +677,6 @@ public class EServiceManager extends ApplicationLifecycleHandler {
if (!servletExcludes.contains(servlet.getName())) {
for (String mapping : servlet.getMappings()) {
/*String address = ProfileBuilder.getAddressFromBaseAddress(
baseAddress, mapping);
*/
String address = baseAddress+(mapping.endsWith("*")?mapping.substring(0,mapping.length()-2):mapping);
AccessPointFacet accessPointFacet = new AccessPointFacetImpl();
@ -665,8 +693,7 @@ public class EServiceManager extends ApplicationLifecycleHandler {
}
ServiceStateFacet serviceStateFacet = new ServiceStateFacetImpl();
String state = applicationContext.lifecycle().state().remoteForm()
.toLowerCase();
String state = getState(applicationContext.lifecycle());
serviceStateFacet.setValue(state.toLowerCase());
eService.addFacet(serviceStateFacet);

View File

@ -410,10 +410,7 @@ public class HostingNodeManager extends ContainerHandler {
Thread.currentThread().setContextClassLoader(
HostingNodeManager.class.getClassLoader());
ResourceRegistryPublisher resourceRegistryPublisher = ResourceRegistryPublisherFactory
.create();
hostingNode = updateFacets(hostingNode, resourceRegistryPublisher);
hostingNode = updateFacets(hostingNode);
share(hostingNode);
@ -427,10 +424,12 @@ public class HostingNodeManager extends ContainerHandler {
}
private HostingNode updateFacets(HostingNode hostingNode,
ResourceRegistryPublisher resourceRegistryPublisher) throws ResourceRegistryException {
private HostingNode updateFacets(HostingNode hostingNode) throws ResourceRegistryException {
logger.debug("Updating HostingNode");
ResourceRegistryPublisher resourceRegistryPublisher = ResourceRegistryPublisherFactory
.create();
ContainerStateFacet containerStateFacet = null;
MemoryFacet ramFacet = null;
MemoryFacet jvmMemoryFacet = null;
@ -474,7 +473,7 @@ public class HostingNodeManager extends ContainerHandler {
}
// Resource Update has affect an all specified facet.
// Resource Update has effect only on specified facet.
// Removing the one that have not to be changed
consistsOfList.removeAll(consistsOfToRemove);