From 9632747a0f2d7546fd0219f2ebce96db4465eb7a Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 26 Apr 2017 14:48:26 +0000 Subject: [PATCH] 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 --- .../resourceregistry/EServiceManager.java | 131 +++++++++++------- .../resourceregistry/HostingNodeManager.java | 13 +- 2 files changed, 85 insertions(+), 59 deletions(-) diff --git a/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceManager.java b/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceManager.java index b37d094..460e733 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceManager.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceManager.java @@ -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> consistsOfList = eService - .getConsistsOf(); - for (ConsistsOf c : consistsOfList) { - if (c.getTarget() instanceof ServiceStateFacet) { - serviceStateFacet = (ServiceStateFacet) c.getTarget(); - break; - } - } - - ResourceRegistryPublisher resourceRegistryPublisher = ResourceRegistryPublisherFactory - .create(); - try { + + + List 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 consistsOf = new ConsistsOfImpl( - eService, serviceStateFacet, null); - @SuppressWarnings("unchecked") - ConsistsOf 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 consistsOf = new ConsistsOfImpl( + 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); diff --git a/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeManager.java b/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeManager.java index 0ba29c8..1c642e2 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeManager.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeManager.java @@ -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);