Fixing lib

This commit is contained in:
Luca Frosini 2022-05-24 11:01:25 +02:00
parent b32a78ab7d
commit 576a69e787
4 changed files with 94 additions and 72 deletions

View File

@ -58,6 +58,11 @@
<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

@ -112,23 +112,10 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
boolean create = true;
Set<String> startTokens = applicationContext.configuration().startTokens();
Set<UUID> startContextsUUID = new HashSet<>();
ContextCache contextCache = null;
Secret firstSecret = null;
for (String token : startTokens) {
try {
Secret secret = SecretUtility.getSecretByTokenString(token);
if(firstSecret==null) {
firstSecret = secret;
}
secretManager.startSession(secret);
if(contextCache == null) {
contextCache = ContextCache.getInstance();
}
String contextFullName = secret.getContext();
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
startContextsUUID.add(contextUUID);
if (create) {
eServiceManager = new EServiceManager(applicationContext);
eServiceManager.createEService();
@ -148,11 +135,6 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
}
}
secretManager.startSession(firstSecret);
Set<UUID> resourceContextsUUID = eServiceManager.getContextsUUID().keySet();
removeResourceFromOldContexts(startContextsUUID, resourceContextsUUID);
secretManager.endSession();
} catch (Throwable e) {
rethrowUnchecked(e);
} finally {
@ -173,6 +155,12 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
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);
}
try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
SortedSet<Secret> secrets = secretManager.getCurrentSecretHolder().getSecrets();
@ -186,6 +174,9 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
logger.error("Failed to update {} State", EService.NAME, e);
} finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
Thread.currentThread().setContextClassLoader(contextCL);
}
}
@ -194,6 +185,12 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
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);
}
try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
Secret secret = SecretUtility.getSecretByTokenString(token);
@ -204,6 +201,9 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
secretManager.getContext(), e);
} finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
Thread.currentThread().setContextClassLoader(contextCL);
}
}
@ -212,6 +212,12 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
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);
}
try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
Secret secret = SecretUtility.getSecretByTokenString(token);
@ -222,6 +228,9 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
EService.NAME, secretManager.getContext(), e);
} finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
Thread.currentThread().setContextClassLoader(contextCL);
}
@ -258,6 +267,12 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
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);
}
try {
SortedSet<Secret> secrets = secretManager.getCurrentSecretHolder().getSecrets();
if (secrets==null || secrets.size()==0) {
@ -270,6 +285,9 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
logger.error("Cannot complete periodic update of {}", EService.NAME, e);
} finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
}
}
};

View File

@ -12,9 +12,7 @@ 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.Set;
import java.util.SortedSet;
import java.util.UUID;
import java.util.concurrent.Executors;
@ -31,8 +29,6 @@ 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;
@ -89,14 +85,6 @@ public class HostingNodeHandler extends ContainerHandler {
}
}
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();
@ -112,23 +100,11 @@ public class HostingNodeHandler extends ContainerHandler {
List<String> startTokens = containerContext.configuration().startTokens();
ContextCache contextCache = null;
Secret firstSecret = null;
Set<UUID> startContextsUUID = new HashSet<>();
for (String token : startTokens) {
try {
Secret secret = SecretUtility.getSecretByTokenString(token);
if(firstSecret==null) {
firstSecret = secret;
}
secretManager.startSession(secret);
if(contextCache == null) {
contextCache = ContextCache.getInstance();
}
String contextFullName = secret.getContext();
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
startContextsUUID.add(contextUUID);
if (create) {
hostingNodeManager = new HostingNodeManager(containerContext);
hostingNodeManager.createHostingNode();
@ -147,11 +123,6 @@ public class HostingNodeHandler extends ContainerHandler {
}
}
secretManager.startSession(firstSecret);
Set<UUID> resourceContextsUUID = hostingNodeManager.getContextsUUID().keySet();
removeResourceFromOldContexts(startContextsUUID, resourceContextsUUID);
secretManager.endSession();
} catch (Throwable e) {
rethrowUnchecked(e);
} finally {
@ -172,6 +143,12 @@ public class HostingNodeHandler extends ContainerHandler {
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);
}
try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
SortedSet<Secret> secrets = secretManager.getCurrentSecretHolder().getSecrets();
@ -194,6 +171,9 @@ public class HostingNodeHandler extends ContainerHandler {
logger.error("Failed to update {} State", HostingNode.NAME, e);
} finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
Thread.currentThread().setContextClassLoader(contextCL);
}
}
@ -202,6 +182,12 @@ public class HostingNodeHandler extends ContainerHandler {
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);
}
try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
Secret secret = SecretUtility.getSecretByTokenString(token);
@ -211,6 +197,9 @@ public class HostingNodeHandler extends ContainerHandler {
logger.error("Failed to update Service State", e);
} finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
Thread.currentThread().setContextClassLoader(contextCL);
}
}
@ -219,6 +208,12 @@ public class HostingNodeHandler extends ContainerHandler {
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);
}
try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
Secret secret = SecretUtility.getSecretByTokenString(token);
@ -228,6 +223,9 @@ public class HostingNodeHandler extends ContainerHandler {
logger.error("Failed to update Service State", e);
} finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
Thread.currentThread().setContextClassLoader(contextCL);
}
}
@ -262,6 +260,12 @@ public class HostingNodeHandler extends ContainerHandler {
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);
}
try {
SortedSet<Secret> secrets = secretManager.getCurrentSecretHolder().getSecrets();
if (secrets==null || secrets.size()==0) {
@ -274,6 +278,9 @@ public class HostingNodeHandler extends ContainerHandler {
logger.error("Cannot complete periodic update of {}", HostingNode.NAME, e);
}finally {
secretManager.endSession();
if(secretManagerNull) {
SecretManagerProvider.instance.reset();
}
}
}
};

View File

@ -57,7 +57,7 @@ import org.slf4j.LoggerFactory;
*/
public class EServiceManager {
private static Logger logger = LoggerFactory.getLogger(HostingNodeManager.class);
private static Logger logger = LoggerFactory.getLogger(EServiceManager.class);
private static List<String> servletExcludes = Arrays.asList("default", "jsp");
@ -78,6 +78,17 @@ 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();
@ -203,17 +214,7 @@ public class EServiceManager {
ResourceRegistryClientFactory.includeContextsInInstanceHeader(true);
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
stateFacet = eService.getFacets(StateFacet.class).get(0);
if (stateFacet == null) {
stateFacet = new StateFacetImpl();
String state = getState();
stateFacet.setValue(state);
Date date = Calendar.getInstance().getTime();
stateFacet.setAdditionalProperty("date", date);
eService.addFacet(stateFacet);
resourceRegistryPublisher.update(eService);
} else {
updateServiceStateFacet();
}
} catch (NotFoundException e) {
eService = instantiateEService();
createActivatesRelation(eService);
@ -223,7 +224,8 @@ public class EServiceManager {
try {
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
} catch (AvailableInAnotherContextException ex) {
resourceRegistryPublisher.addResourceToCurrentContext(eService, false);
addEServiceToCurrentContext();
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
// 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
@ -231,17 +233,7 @@ public class EServiceManager {
createActivatesRelation(eService);
}
stateFacet = eService.getFacets(StateFacet.class).get(0);
if (stateFacet == null) {
stateFacet = new StateFacetImpl();
String state = getState();
stateFacet.setValue(state);
Date date = Calendar.getInstance().getTime();
stateFacet.setAdditionalProperty("date", date);
eService.addFacet(stateFacet);
resourceRegistryPublisher.update(eService);
} else {
updateServiceStateFacet();
}
} catch (ResourceRegistryException e) {
throw e;
}