Reverting to version before authorization-utils

This commit is contained in:
Luca Frosini 2022-05-24 11:13:39 +02:00
parent 576a69e787
commit 519a5d79fe
6 changed files with 273 additions and 276 deletions

View File

@ -58,11 +58,6 @@
<groupId>org.gcube.resource-management</groupId>
<artifactId>gcube-model</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>authorization-utils</artifactId>
<version>[2.0.0, 3.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>

View File

@ -0,0 +1,80 @@
package org.gcube.smartgears.handler.resourceregistry;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.UUID;
import org.gcube.common.authorization.client.proxy.AuthorizationProxy;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.smartgears.provider.ProviderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI-CNR)
*/
public class ContextUtility {
private static Logger logger = LoggerFactory.getLogger(ContextUtility.class);
private static AuthorizationProxy authorizationProxy;
static {
authorizationProxy = ProviderFactory.provider().authorizationProxy();
}
public static void resetContex() {
SecurityTokenProvider.instance.reset();
ScopeProvider.instance.reset();
}
public static void setContextFromToken(String token) {
if (token == null || token.compareTo("") == 0) {
resetContex();
} else {
SecurityTokenProvider.instance.set(token);
String scope = getContextName(token);
ScopeProvider.instance.set(scope);
}
}
public static String getCurrentContextName() {
String token = SecurityTokenProvider.instance.get();
return getContextName(token);
}
public static String getContextName(String token) {
try {
return authorizationProxy.get(token).getContext();
} catch (Exception e) {
logger.error("Error retrieving context form token {}, it should never happen", token, e);
return null;
}
}
public static SortedSet<String> getContextFullNamesFromTokens(Set<String> tokens){
SortedSet<String> contextFullNames = new TreeSet<>();
for(String token : tokens) {
String contextFullName = getContextName(token);
contextFullNames.add(contextFullName);
}
return contextFullNames;
}
public static SortedSet<UUID> getContextUUIDFromTokens(Set<String> tokens) throws ResourceRegistryException {
SortedSet<UUID> contextsUUID = new TreeSet<>();
ContextCache contextCache = ContextCache.getInstance();
for(String token : tokens) {
String contextFullName = getContextName(token);
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
contextsUUID.add(contextUUID);
}
return contextsUUID;
}
}

View File

@ -10,17 +10,13 @@ import static org.gcube.smartgears.utils.Utils.rethrowUnchecked;
import java.util.HashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.UUID;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import javax.xml.bind.annotation.XmlRootElement;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.utils.secret.SecretUtility;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.events.Observes;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
@ -100,22 +96,20 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
private void init() {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
SecretManager secretManager = SecretManagerProvider.instance.get();
boolean secretManagerNull = false;
if(secretManager==null) {
secretManager = new SecretManager();
secretManagerNull = true;
SecretManagerProvider.instance.set(secretManager);
}
String previousToken = SecurityTokenProvider.instance.get();
try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
boolean create = true;
Set<String> startTokens = applicationContext.configuration().startTokens();
Set<UUID> startContextsUUID = new HashSet<>();
ContextCache contextCache = ContextCache.getInstance();
for (String token : startTokens) {
ContextUtility.setContextFromToken(token);
String contextFullName = ContextUtility.getContextName(token);
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
startContextsUUID.add(contextUUID);
try {
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
if (create) {
eServiceManager = new EServiceManager(applicationContext);
eServiceManager.createEService();
@ -125,23 +119,19 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
} else {
eServiceManager.addToContext();
}
} catch (Exception e) {
UUID uuid = UUID.fromString(applicationContext.id());
logger.error("Unable to add {} with UUID {} to current context ({})", EService.NAME, uuid,
secretManager.getContext(), e);
}finally {
secretManager.endSession();
logger.error("Unable to add {} to current context ({})", eServiceManager.getEService(),
ContextUtility.getContextName(token), e);
}
}
Set<UUID> resourceContextsUUID = eServiceManager.getContextsUUID().keySet();
removeResourceFromOldContexts(startContextsUUID, resourceContextsUUID);
} catch (Throwable e) {
rethrowUnchecked(e);
} finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
ContextUtility.setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL);
}
logger.info("{} init() terminated", this.getClass().getSimpleName());
@ -154,56 +144,61 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
@Observes({ activation, stop, failure })
void onChanged(ApplicationLifecycle lc) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
SecretManager secretManager = SecretManagerProvider.instance.get();
boolean secretManagerNull = false;
if(secretManager==null) {
secretManager = new SecretManager();
secretManagerNull = true;
SecretManagerProvider.instance.set(secretManager);
String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
ContextUtility
.setContextFromToken(applicationContext.configuration().startTokens().iterator().next());
}
try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
SortedSet<Secret> secrets = secretManager.getCurrentSecretHolder().getSecrets();
if (secrets==null || secrets.size()==0) {
String token = applicationContext.configuration().startTokens().iterator().next();
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
}
eServiceManager.updateServiceStateFacet();
} catch (Exception e) {
logger.error("Failed to update {} State", EService.NAME, e);
} finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
ContextUtility.setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL);
}
}
@Observes(value = addToContext)
void addTo(String token) {
/*
@Observes({ stop, failure })
void onStop(ApplicationLifecycle lc) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
SecretManager secretManager = SecretManagerProvider.instance.get();
boolean secretManagerNull = false;
if(secretManager==null) {
secretManager = new SecretManager();
secretManagerNull = true;
SecretManagerProvider.instance.set(secretManager);
String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
ContextUtility
.setContextFromToken(applicationContext.configuration().startTokens().iterator().next());
}
try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
eServiceManager.updateServiceStateFacet();
} catch (Exception e) {
logger.error("Failed to update {} State", EService.NAME, e);
} finally {
ContextUtility.setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL);
}
}
*/
@Observes(value = addToContext)
void addTo(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());
ContextUtility.setContextFromToken(token);
eServiceManager.addToContext();
} catch (Exception e) {
logger.error("Failed to add {} to current context ({})", EService.NAME,
secretManager.getContext(), e);
ContextUtility.getCurrentContextName(), e);
} finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
ContextUtility.setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL);
}
}
@ -211,26 +206,19 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
@Observes(value = removeFromContext)
void removeFrom(String token) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
SecretManager secretManager = SecretManagerProvider.instance.get();
boolean secretManagerNull = false;
if(secretManager==null) {
secretManager = new SecretManager();
secretManagerNull = true;
SecretManagerProvider.instance.set(secretManager);
String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
ContextUtility
.setContextFromToken(applicationContext.configuration().startTokens().iterator().next());
}
try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
eServiceManager.removeFromContext();
} catch (Exception e) {
logger.error("Failed to remove {} from current context ({})",
EService.NAME, secretManager.getContext(), e);
EService.NAME, ContextUtility.getCurrentContextName(), e);
} finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
ContextUtility.setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL);
}
@ -257,37 +245,26 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
}
if (lc.state() == ApplicationState.active) {
logger.info("Scheduling periodic updates of {} from application {}",
EService.NAME, applicationContext.name());
logger.info("scheduling periodic updates of application {} EService",
applicationContext.name());
} else {
logger.info("Resuming periodic updates of {} for application {}",
EService.NAME, applicationContext.name());
logger.info("resuming periodic updates of application {} EService",
applicationContext.name());
}
final Runnable updateTask = new Runnable() {
public void run() {
SecretManager secretManager = SecretManagerProvider.instance.get();
boolean secretManagerNull = false;
if(secretManager==null) {
secretManager = new SecretManager();
secretManagerNull = true;
SecretManagerProvider.instance.set(secretManager);
String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
ContextUtility.setContextFromToken(
applicationContext.configuration().startTokens().iterator().next());
}
try {
SortedSet<Secret> secrets = secretManager.getCurrentSecretHolder().getSecrets();
if (secrets==null || secrets.size()==0) {
String token = applicationContext.configuration().startTokens().iterator().next();
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
}
eServiceManager.updateServiceStateFacet();
} catch (Exception e) {
logger.error("Cannot complete periodic update of {}", EService.NAME, e);
logger.error("Cannot complete periodic update of EService", e);
} finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
ContextUtility.setContextFromToken(previousToken);
}
}
};
@ -302,15 +279,15 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
synchronized void cancelPeriodicUpdates(ContainerLifecycle ignore) {
if (periodicUpdates != null) {
logger.trace("Stopping periodic updates of {} for application {} ",
EService.NAME, applicationContext.name());
logger.trace("stopping periodic updates of EService for application {} ",
applicationContext.name());
try {
periodicUpdates.cancel(true);
periodicUpdates = null;
} catch (Exception e) {
logger.warn("Could not stop periodic updates of {} for application {}",
EService.NAME, applicationContext.name(), e);
logger.warn("could not stop periodic updates of EService for application {}",
applicationContext.name(), e);
}
}
}

View File

@ -12,8 +12,9 @@ import static org.gcube.smartgears.lifecycle.container.ContainerLifecycle.stop;
import static org.gcube.smartgears.lifecycle.container.ContainerState.active;
import static org.gcube.smartgears.utils.Utils.rethrowUnchecked;
import java.util.HashSet;
import java.util.List;
import java.util.SortedSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@ -21,14 +22,13 @@ import java.util.concurrent.ScheduledFuture;
import javax.xml.bind.annotation.XmlRootElement;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.utils.secret.SecretUtility;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.events.Observes;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.resourcemanagement.model.reference.entities.facets.StateFacet;
import org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode;
import org.gcube.smartgears.context.Property;
@ -84,52 +84,56 @@ public class HostingNodeHandler extends ContainerHandler {
logger.error("onStart failed", re);
}
}
protected void removeResourceFromOldContexts(Set<UUID> startContexts, Set<UUID> resourceContexts) throws ResourceRegistryException {
Set<UUID> contextsToRemove = new HashSet<>(resourceContexts);
contextsToRemove.removeAll(startContexts);
for(UUID contextToRemove : contextsToRemove) {
hostingNodeManager.removeFromContext(contextToRemove);
}
}
private void init() {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
SecretManager secretManager = SecretManagerProvider.instance.get();
boolean secretManagerNull = false;
if(secretManager==null) {
secretManager = new SecretManager();
secretManagerNull = true;
SecretManagerProvider.instance.set(secretManager);
}
String previousToken = SecurityTokenProvider.instance.get();
try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
boolean create = true;
List<String> startTokens = containerContext.configuration().startTokens();
Set<UUID> startContextsUUID = new HashSet<>();
ContextCache contextCache = ContextCache.getInstance();
for (String token : startTokens) {
ContextUtility.setContextFromToken(token);
String contextFullName = ContextUtility.getContextName(token);
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
startContextsUUID.add(contextUUID);
try {
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
if (create) {
hostingNodeManager = new HostingNodeManager(containerContext);
hostingNodeManager.createHostingNode();
containerContext.properties()
.add(new Property(Constants.HOSTING_NODE_MANAGER_PROPERTY, hostingNodeManager));
create = false;
} else {
hostingNodeManager.addToContext();
}
} catch (Exception e) {
UUID uuid = UUID.fromString(containerContext.id());
logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid,
secretManager.getContext(), e);
} finally {
secretManager.endSession();
logger.error("Unable to add {} to current context ({})", hostingNodeManager.getHostingNode(),
ContextUtility.getContextName(token), e);
}
}
Set<UUID> resourceContextsUUID = hostingNodeManager.getContextsUUID().keySet();
removeResourceFromOldContexts(startContextsUUID, resourceContextsUUID);
} catch (Throwable e) {
rethrowUnchecked(e);
} finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
ContextUtility.setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL);
}
logger.info("{} init() terminated", this.getClass().getSimpleName());
@ -139,24 +143,16 @@ public class HostingNodeHandler extends ContainerHandler {
containerContext.events().subscribe(new Object() {
@Observes({ activation, part_activation, shutdown, stop, failure})
//@Observes({ activation, part_activation, shutdown, stop, failure })
@Observes({ activation, part_activation})
void onChanged(ContainerLifecycle cl) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
SecretManager secretManager = SecretManagerProvider.instance.get();
boolean secretManagerNull = false;
if(secretManager==null) {
secretManager = new SecretManager();
secretManagerNull = true;
SecretManagerProvider.instance.set(secretManager);
String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
ContextUtility.setContextFromToken(containerContext.configuration().startTokens().iterator().next());
}
try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
SortedSet<Secret> secrets = secretManager.getCurrentSecretHolder().getSecrets();
if (secrets==null || secrets.size()==0) {
String token = containerContext.configuration().startTokens().iterator().next();
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
}
ContainerState containerState = cl.state();
switch (containerState) {
case active:
@ -170,10 +166,26 @@ public class HostingNodeHandler extends ContainerHandler {
} catch (Exception e) {
logger.error("Failed to update {} State", HostingNode.NAME, e);
} finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
ContextUtility.setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL);
}
}
@Observes({ shutdown, stop, failure })
void onShutdown(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());
hostingNodeManager.updateStatus();
} catch (Exception e) {
logger.error("cannot complete periodic update of {}", HostingNode.NAME, e);
} finally {
ContextUtility.setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL);
}
}
@ -181,25 +193,18 @@ public class HostingNodeHandler extends ContainerHandler {
@Observes(value = addToContext)
void addTo(String token) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
SecretManager secretManager = SecretManagerProvider.instance.get();
boolean secretManagerNull = false;
if(secretManager==null) {
secretManager = new SecretManager();
secretManagerNull = true;
SecretManagerProvider.instance.set(secretManager);
String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
ContextUtility
.setContextFromToken(containerContext.configuration().startTokens().iterator().next());
}
try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
hostingNodeManager.addToContext();
} catch (Exception e) {
logger.error("Failed to update Service State", e);
} finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
ContextUtility.setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL);
}
}
@ -207,25 +212,17 @@ public class HostingNodeHandler extends ContainerHandler {
@Observes(value = removeFromContext)
void removeFrom(String token) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
SecretManager secretManager = SecretManagerProvider.instance.get();
boolean secretManagerNull = false;
if(secretManager==null) {
secretManager = new SecretManager();
secretManagerNull = true;
SecretManagerProvider.instance.set(secretManager);
String previousToken = SecurityTokenProvider.instance.get();
if (previousToken == null) {
ContextUtility
.setContextFromToken(containerContext.configuration().startTokens().iterator().next());
}
try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
hostingNodeManager.removeFromContext();
} catch (Exception e) {
logger.error("Failed to update Service State", e);
} finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
Thread.currentThread().setContextClassLoader(contextCL);
}
}
@ -252,35 +249,22 @@ public class HostingNodeHandler extends ContainerHandler {
}
if (cl.state() == active) {
logger.info("Scheduling periodic updates of {}", HostingNode.NAME);
logger.info("scheduling periodic updates of {}", HostingNode.NAME);
} else {
logger.info("Resuming periodic updates of {}", HostingNode.NAME);
logger.info("resuming periodic updates of {}", HostingNode.NAME);
}
final Runnable updateTask = new Runnable() {
public void run() {
SecretManager secretManager = SecretManagerProvider.instance.get();
boolean secretManagerNull = false;
if(secretManager==null) {
secretManager = new SecretManager();
secretManagerNull = true;
SecretManagerProvider.instance.set(secretManager);
}
String currentToken = SecurityTokenProvider.instance.get();
if (currentToken == null)
currentToken = containerContext.configuration().startTokens().iterator().next();
ContextUtility.setContextFromToken(currentToken);
try {
SortedSet<Secret> secrets = secretManager.getCurrentSecretHolder().getSecrets();
if (secrets==null || secrets.size()==0) {
String token = containerContext.configuration().startTokens().iterator().next();
Secret secret = SecretUtility.getSecretByTokenString(token);
secretManager.startSession(secret);
}
hostingNodeManager.updateFacets();
} catch (Exception e) {
logger.error("Cannot complete periodic update of {}", HostingNode.NAME, e);
}finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
logger.error("cannot complete periodic update of {}", HostingNode.NAME, e);
}
}
};
@ -294,13 +278,13 @@ public class HostingNodeHandler extends ContainerHandler {
synchronized void cancelPeriodicUpdates(ContainerLifecycle cl) {
if (periodicUpdates != null) {
logger.trace("Stopping periodic updates of {}", HostingNode.NAME);
logger.trace("stopping periodic updates of {}", HostingNode.NAME);
try {
periodicUpdates.cancel(true);
service.shutdownNow();
periodicUpdates = null;
} catch (Exception e) {
logger.warn("Could not stop periodic updates of {}", HostingNode.NAME, e);
logger.warn("could not stop periodic updates of {}", HostingNode.NAME, e);
}
}
}

View File

@ -2,16 +2,12 @@ package org.gcube.smartgears.handler.resourceregistry.resourcemanager;
import java.net.URI;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.ServletRegistration;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.informationsystem.model.impl.properties.HeaderImpl;
import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl;
import org.gcube.informationsystem.model.reference.properties.Header;
@ -29,16 +25,13 @@ import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClien
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher;
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory;
import org.gcube.resourcemanagement.model.impl.entities.facets.AccessPointFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.EventFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.StateFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.resources.EServiceImpl;
import org.gcube.resourcemanagement.model.impl.entities.resources.HostingNodeImpl;
import org.gcube.resourcemanagement.model.impl.properties.ValueSchemaImpl;
import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentifiedByImpl;
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.SoftwareFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.StateFacet;
import org.gcube.resourcemanagement.model.reference.entities.resources.EService;
@ -49,6 +42,8 @@ import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Activa
import org.gcube.smartgears.configuration.application.ApplicationConfiguration;
import org.gcube.smartgears.configuration.container.ContainerConfiguration;
import org.gcube.smartgears.context.application.ApplicationContext;
import org.gcube.smartgears.handler.resourceregistry.Constants;
import org.gcube.smartgears.handler.resourceregistry.ContextUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -57,7 +52,7 @@ import org.slf4j.LoggerFactory;
*/
public class EServiceManager {
private static Logger logger = LoggerFactory.getLogger(EServiceManager.class);
private static Logger logger = LoggerFactory.getLogger(HostingNodeManager.class);
private static List<String> servletExcludes = Arrays.asList("default", "jsp");
@ -78,50 +73,37 @@ public class EServiceManager {
return eService;
}
public void addEServiceToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(applicationContext.id());
SecretManager secretManager = SecretManagerProvider.instance.get();
try {
resourceRegistryPublisher.addResourceToCurrentContext(EService.NAME, uuid, false);
logger.info("{} with UUID {} successfully added to current context ({})", EService.NAME, uuid, secretManager.getContext());
} catch (Exception e) {
logger.error("Unable to add {} with UUID {} to current context ({})", EService.NAME, uuid, secretManager.getContext(), e);
}
}
public void addToContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(applicationContext.container().id());
SecretManager secretManager = SecretManagerProvider.instance.get();
HostingNode hostingNode = applicationContext.container().properties()
.lookup(Constants.HOSTING_NODE_MANAGER_PROPERTY).value(HostingNodeManager.class).getHostingNode();
try {
resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false);
logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, secretManager.getContext());
resourceRegistryPublisher.addResourceToCurrentContext(hostingNode, false);
logger.info("{} successfully added to current context ({})", eService, ContextUtility.getCurrentContextName());
} catch (Exception e) {
logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, secretManager.getContext(), e);
logger.error("Unable to add {} to current context ({})", eService, ContextUtility.getCurrentContextName(), e);
}
}
public void removeFromContext()
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(applicationContext.container().id());
SecretManager secretManager = SecretManagerProvider.instance.get();
try {
resourceRegistryPublisher.removeResourceFromCurrentContext(HostingNode.NAME, uuid, false);
logger.info("{} with UUID {} successfully removed from current context ({})", HostingNode.NAME, uuid,
secretManager.getContext());
resourceRegistryPublisher.removeResourceFromCurrentContext(eService, false);
logger.info("{} successfully removed from current context ({})", eService,
ContextUtility.getCurrentContextName());
} catch (Exception e) {
logger.error("Unable to remove {} with UUID {} from current context ({})", HostingNode.NAME, uuid, secretManager.getContext(), e);
logger.error("Unable to remove {} from current context ({})", eService,
ContextUtility.getCurrentContextName(), e);
}
}
public void removeFromContext(UUID contextUUID)
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
String contextFullName = ContextCache.getInstance().getContextFullNameByUUID(contextUUID);
UUID uuid = UUID.fromString(applicationContext.container().id());
try {
resourceRegistryPublisher.removeResourceFromContext(HostingNode.NAME, uuid, contextUUID, false);
logger.info("{} with UUID {} successfully removed from context ({})", HostingNode.NAME, uuid, contextFullName);
resourceRegistryPublisher.removeResourceFromContext(eService, contextUUID, false);
logger.info("{} successfully removed from context ({})", eService, contextFullName);
} catch (Exception e) {
logger.error("Unable to remove {} from current context ({})", HostingNode.NAME, uuid, contextFullName, e);
logger.error("Unable to remove {} from current context ({})", eService, contextFullName, e);
}
}
@ -191,19 +173,11 @@ public class EServiceManager {
}
}
Date date = Calendar.getInstance().getTime();
stateFacet = new StateFacetImpl();
String state = getState();
stateFacet.setValue(state);
eService.addFacet(stateFacet);
stateFacet.setAdditionalProperty("date", date);
EventFacet eventFacet = new EventFacetImpl();
eventFacet.setEvent(state);
eventFacet.setDate(date);
eService.addFacet(eventFacet);
return eService;
}
@ -214,7 +188,15 @@ public class EServiceManager {
ResourceRegistryClientFactory.includeContextsInInstanceHeader(true);
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
stateFacet = eService.getFacets(StateFacet.class).get(0);
updateServiceStateFacet();
if (stateFacet == null) {
stateFacet = new StateFacetImpl();
String state = getState();
stateFacet.setValue(state);
eService.addFacet(stateFacet);
resourceRegistryPublisher.update(eService);
} else {
updateServiceStateFacet();
}
} catch (NotFoundException e) {
eService = instantiateEService();
createActivatesRelation(eService);
@ -224,8 +206,7 @@ public class EServiceManager {
try {
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
} catch (AvailableInAnotherContextException ex) {
addEServiceToCurrentContext();
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
resourceRegistryPublisher.addResourceToCurrentContext(eService, false);
// addToContext() is executed on HostingNode.
// If the EService is still not available we need to create activates
// relation because does not exists otherwise the EService should
@ -233,7 +214,15 @@ public class EServiceManager {
createActivatesRelation(eService);
}
stateFacet = eService.getFacets(StateFacet.class).get(0);
updateServiceStateFacet();
if (stateFacet == null) {
stateFacet = new StateFacetImpl();
String state = getState();
stateFacet.setValue(state);
eService.addFacet(stateFacet);
resourceRegistryPublisher.update(eService);
} else {
updateServiceStateFacet();
}
} catch (ResourceRegistryException e) {
throw e;
}
@ -241,38 +230,16 @@ public class EServiceManager {
}
public void updateServiceStateFacet() throws ResourceRegistryException {
Date date = Calendar.getInstance().getTime();
String state = getState();
stateFacet.setValue(state);
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;
}
}
if(!found) {
EventFacet eventFacet = new EventFacetImpl();
eventFacet.setDate(date);
eventFacet.setEvent(state);
eService.addFacet(eventFacet);
}
eService = resourceRegistryPublisher.updateResource(eService);
stateFacet = resourceRegistryPublisher.updateFacet(stateFacet);
}
private Activates<HostingNode, EService> createActivatesRelation(EService eService)
throws ResourceRegistryException {
UUID hostingNodeUUID = UUID.fromString(applicationContext.container().id());
HostingNode hostingNode = new HostingNodeImpl();
Header header = new HeaderImpl(hostingNodeUUID);
hostingNode.setHeader(header);
HostingNode hostingNode = applicationContext.container().properties()
.lookup(Constants.HOSTING_NODE_MANAGER_PROPERTY).value(HostingNodeManager.class).getHostingNode();
PropagationConstraint propagationConstraint = new PropagationConstraintImpl();
propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade);

View File

@ -32,8 +32,6 @@ import javax.management.ObjectName;
import javax.management.ReflectionException;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.informationsystem.model.impl.properties.HeaderImpl;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource;
@ -79,6 +77,7 @@ import org.gcube.smartgears.configuration.container.ContainerConfiguration;
import org.gcube.smartgears.configuration.container.Site;
import org.gcube.smartgears.configuration.library.SmartGearsConfiguration;
import org.gcube.smartgears.context.container.ContainerContext;
import org.gcube.smartgears.handler.resourceregistry.ContextUtility;
import org.gcube.smartgears.lifecycle.container.ContainerState;
import org.gcube.smartgears.provider.ProviderFactory;
import org.slf4j.Logger;
@ -112,38 +111,33 @@ public class HostingNodeManager {
}
public void addToContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(containerContext.id());
SecretManager secretManager = SecretManagerProvider.instance.get();
try {
resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false);
logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, secretManager.getContext());
resourceRegistryPublisher.addResourceToCurrentContext(hostingNode, false);
logger.info("{} successfully added to current context ({})", hostingNode, ContextUtility.getCurrentContextName());
} catch (Exception e) {
logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, secretManager.getContext(), e);
logger.error("Unable to add {} to current context ({})", hostingNode, ContextUtility.getCurrentContextName(), e);
}
}
public void removeFromContext()
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(containerContext.id());
SecretManager secretManager = SecretManagerProvider.instance.get();
try {
resourceRegistryPublisher.removeResourceFromCurrentContext(HostingNode.NAME, uuid, false);
logger.info("{} with UUID {} successfully removed from current context ({})", HostingNode.NAME, uuid,
secretManager.getContext());
resourceRegistryPublisher.removeResourceFromCurrentContext(hostingNode, false);
logger.info("{} successfully removed from current context ({})", hostingNode,
ContextUtility.getCurrentContextName());
} catch (Exception e) {
logger.error("Unable to remove {} with UUID {} from current context ({})", HostingNode.NAME, uuid, secretManager.getContext(), e);
logger.error("Unable to remove {} from current context ({})", hostingNode, ContextUtility.getCurrentContextName(), e);
}
}
public void removeFromContext(UUID contextUUID)
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
String contextFullName = ContextCache.getInstance().getContextFullNameByUUID(contextUUID);
UUID uuid = UUID.fromString(containerContext.id());
try {
resourceRegistryPublisher.removeResourceFromContext(HostingNode.NAME, uuid, contextUUID, false);
logger.info("{} with UUID {} successfully removed from context ({})", HostingNode.NAME, uuid, contextFullName);
resourceRegistryPublisher.removeResourceFromContext(hostingNode, contextUUID, false);
logger.info("{} successfully removed from context ({})", hostingNode, contextFullName);
} catch (Exception e) {
logger.error("Unable to remove {} from current context ({})", HostingNode.NAME, uuid, contextFullName, e);
logger.error("Unable to remove {} from current context ({})", hostingNode, contextFullName, e);
}
}
@ -399,7 +393,7 @@ public class HostingNodeManager {
locationFacet.setLongitude(site.longitude());
hostingNode.addFacet(locationFacet);
logger.info("{} instantiated", HostingNode.NAME);
logger.info("hostingNode instanciated");
return hostingNode;
}