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:
parent
3407a40547
commit
9632747a0f
|
@ -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.AddConstraint;
|
||||||
import org.gcube.informationsystem.model.embedded.PropagationConstraint.RemoveConstraint;
|
import org.gcube.informationsystem.model.embedded.PropagationConstraint.RemoveConstraint;
|
||||||
import org.gcube.informationsystem.model.embedded.ValueSchema;
|
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.Resource;
|
||||||
import org.gcube.informationsystem.model.entity.facet.AccessPointFacet;
|
import org.gcube.informationsystem.model.entity.facet.AccessPointFacet;
|
||||||
import org.gcube.informationsystem.model.entity.facet.ServiceStateFacet;
|
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.ResourceAlreadyPresentException;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException;
|
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.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.Direction;
|
||||||
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
|
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
|
||||||
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
|
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
|
||||||
|
@ -142,11 +143,14 @@ public class EServiceManager extends ApplicationLifecycleHandler {
|
||||||
|
|
||||||
@Observes({ activation, stop, failure })
|
@Observes({ activation, stop, failure })
|
||||||
void onChanged(ApplicationLifecycle lc) {
|
void onChanged(ApplicationLifecycle lc) {
|
||||||
String state = lc.state().remoteForm().toLowerCase();
|
String state = getState(lc);
|
||||||
logger.debug("Moving app {} to {}", applicationContext.name(),
|
logger.debug("Moving app {} to {}", applicationContext.name(),
|
||||||
state);
|
state);
|
||||||
EService eService = applicationContext.properties().lookup(Constants.ESERVICE_PROPERTY).value(EService.class);
|
try {
|
||||||
createOrUpdateServiceStateFacet(eService, state);
|
createOrUpdateServiceStateFacet(getEService(), state);
|
||||||
|
} catch (ResourceRegistryException e) {
|
||||||
|
rethrowUnchecked(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Observes(value = addToContext)
|
@Observes(value = addToContext)
|
||||||
|
@ -163,9 +167,15 @@ public class EServiceManager extends ApplicationLifecycleHandler {
|
||||||
removeHostingNodeFromContext(token);
|
removeHostingNodeFromContext(token);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String getState(ApplicationLifecycle lc){
|
||||||
|
return lc.state().remoteForm().toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
private void schedulePeriodicUpdates() {
|
private void schedulePeriodicUpdates() {
|
||||||
|
|
||||||
// register to cancel updates
|
// register to cancel updates
|
||||||
|
@ -196,11 +206,9 @@ public class EServiceManager extends ApplicationLifecycleHandler {
|
||||||
|
|
||||||
final Runnable updateTask = new Runnable() {
|
final Runnable updateTask = new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
EService eService = applicationContext.properties().lookup(Constants.ESERVICE_PROPERTY).value(EService.class);
|
|
||||||
try {
|
try {
|
||||||
String state = lc.state().remoteForm()
|
String state = getState(lc);
|
||||||
.toLowerCase();
|
createOrUpdateServiceStateFacet(getEService(), state);
|
||||||
createOrUpdateServiceStateFacet(eService, state);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error(
|
logger.error(
|
||||||
"cannot complete periodic update of EService",
|
"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(
|
private EService createEServiceAndHosts(
|
||||||
ResourceRegistryPublisher resourceRegistryPublisher,
|
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();
|
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 {
|
try {
|
||||||
|
|
||||||
if (previousToken == null) {
|
|
||||||
setContextFromToken((String) applicationContext.configuration()
|
|
||||||
.startTokens().toArray()[0]);
|
|
||||||
}
|
|
||||||
Thread.currentThread().setContextClassLoader(
|
|
||||||
EServiceManager.class.getClassLoader());
|
|
||||||
|
|
||||||
ServiceStateFacet serviceStateFacet = null;
|
ServiceStateFacet serviceStateFacet = null;
|
||||||
|
|
||||||
List<ConsistsOf<? extends Resource, ? extends Facet>> consistsOfList = eService
|
|
||||||
.getConsistsOf();
|
List<ServiceStateFacet> serviceStateFacets = eService.getFacets(ServiceStateFacet.class);
|
||||||
for (ConsistsOf<? extends Resource, ? extends Facet> c : consistsOfList) {
|
if(serviceStateFacets !=null && serviceStateFacets.size()>=1){
|
||||||
if (c.getTarget() instanceof ServiceStateFacet) {
|
serviceStateFacet = serviceStateFacets.get(0);
|
||||||
serviceStateFacet = (ServiceStateFacet) c.getTarget();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ResourceRegistryPublisher resourceRegistryPublisher = ResourceRegistryPublisherFactory
|
|
||||||
.create();
|
|
||||||
try {
|
|
||||||
serviceStateFacet.setValue(state);
|
serviceStateFacet.setValue(state);
|
||||||
serviceStateFacet = resourceRegistryPublisher.updateFacet(
|
serviceStateFacet = resourceRegistryPublisher.updateFacet(ServiceStateFacet.class, serviceStateFacet);
|
||||||
ServiceStateFacet.class, serviceStateFacet);
|
|
||||||
|
for(int i=1; i<serviceStateFacets.size(); i++){
|
||||||
} catch (Exception e) {
|
try {
|
||||||
try {
|
logger.warn("You should not be here. There are more than one {}. Anyway deleting it : {}", ServiceStateFacet.class.getSimpleName(), serviceStateFacets.get(i));
|
||||||
ConsistsOf<EService, ServiceStateFacet> consistsOf = new ConsistsOfImpl<EService, ServiceStateFacet>(
|
resourceRegistryPublisher.deleteFacet(serviceStateFacets.get(i));
|
||||||
eService, serviceStateFacet, null);
|
}catch (Exception e) {
|
||||||
@SuppressWarnings("unchecked")
|
logger.warn("Unable to delete {} which should not exists : {}", ServiceStateFacet.class.getSimpleName(), serviceStateFacets.get(i));
|
||||||
ConsistsOf<EService, ServiceStateFacet> createdConsistsOf = resourceRegistryPublisher.createConsistsOf(ConsistsOf.class, consistsOf);
|
}
|
||||||
|
|
||||||
eService.addFacet(createdConsistsOf);
|
|
||||||
} catch (Exception ex) {
|
|
||||||
eService = createEServiceAndHosts(
|
|
||||||
resourceRegistryPublisher, eService);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}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);
|
share(eService);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
rethrowUnchecked(e);
|
rethrowUnchecked(e);
|
||||||
} finally {
|
} finally {
|
||||||
setContextFromToken(previousToken);
|
|
||||||
Thread.currentThread().setContextClassLoader(contextCL);
|
Thread.currentThread().setContextClassLoader(contextCL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -645,10 +677,6 @@ public class EServiceManager extends ApplicationLifecycleHandler {
|
||||||
if (!servletExcludes.contains(servlet.getName())) {
|
if (!servletExcludes.contains(servlet.getName())) {
|
||||||
for (String mapping : servlet.getMappings()) {
|
for (String mapping : servlet.getMappings()) {
|
||||||
|
|
||||||
|
|
||||||
/*String address = ProfileBuilder.getAddressFromBaseAddress(
|
|
||||||
baseAddress, mapping);
|
|
||||||
*/
|
|
||||||
String address = baseAddress+(mapping.endsWith("*")?mapping.substring(0,mapping.length()-2):mapping);
|
String address = baseAddress+(mapping.endsWith("*")?mapping.substring(0,mapping.length()-2):mapping);
|
||||||
|
|
||||||
AccessPointFacet accessPointFacet = new AccessPointFacetImpl();
|
AccessPointFacet accessPointFacet = new AccessPointFacetImpl();
|
||||||
|
@ -665,8 +693,7 @@ public class EServiceManager extends ApplicationLifecycleHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
ServiceStateFacet serviceStateFacet = new ServiceStateFacetImpl();
|
ServiceStateFacet serviceStateFacet = new ServiceStateFacetImpl();
|
||||||
String state = applicationContext.lifecycle().state().remoteForm()
|
String state = getState(applicationContext.lifecycle());
|
||||||
.toLowerCase();
|
|
||||||
serviceStateFacet.setValue(state.toLowerCase());
|
serviceStateFacet.setValue(state.toLowerCase());
|
||||||
eService.addFacet(serviceStateFacet);
|
eService.addFacet(serviceStateFacet);
|
||||||
|
|
||||||
|
|
|
@ -410,10 +410,7 @@ public class HostingNodeManager extends ContainerHandler {
|
||||||
Thread.currentThread().setContextClassLoader(
|
Thread.currentThread().setContextClassLoader(
|
||||||
HostingNodeManager.class.getClassLoader());
|
HostingNodeManager.class.getClassLoader());
|
||||||
|
|
||||||
ResourceRegistryPublisher resourceRegistryPublisher = ResourceRegistryPublisherFactory
|
hostingNode = updateFacets(hostingNode);
|
||||||
.create();
|
|
||||||
|
|
||||||
hostingNode = updateFacets(hostingNode, resourceRegistryPublisher);
|
|
||||||
|
|
||||||
share(hostingNode);
|
share(hostingNode);
|
||||||
|
|
||||||
|
@ -427,10 +424,12 @@ public class HostingNodeManager extends ContainerHandler {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private HostingNode updateFacets(HostingNode hostingNode,
|
private HostingNode updateFacets(HostingNode hostingNode) throws ResourceRegistryException {
|
||||||
ResourceRegistryPublisher resourceRegistryPublisher) throws ResourceRegistryException {
|
|
||||||
logger.debug("Updating HostingNode");
|
logger.debug("Updating HostingNode");
|
||||||
|
|
||||||
|
ResourceRegistryPublisher resourceRegistryPublisher = ResourceRegistryPublisherFactory
|
||||||
|
.create();
|
||||||
|
|
||||||
ContainerStateFacet containerStateFacet = null;
|
ContainerStateFacet containerStateFacet = null;
|
||||||
MemoryFacet ramFacet = null;
|
MemoryFacet ramFacet = null;
|
||||||
MemoryFacet jvmMemoryFacet = 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
|
// Removing the one that have not to be changed
|
||||||
consistsOfList.removeAll(consistsOfToRemove);
|
consistsOfList.removeAll(consistsOfToRemove);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue