2020-12-14 20:32:46 +01:00
|
|
|
package org.gcube.vremanagement.contextmanager;
|
|
|
|
|
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.enterprise.inject.Default;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
import javax.inject.Singleton;
|
|
|
|
import javax.xml.bind.JAXBException;
|
|
|
|
|
|
|
|
import org.gcube.common.resources.gcore.GenericResource;
|
|
|
|
import org.gcube.common.scope.impl.ScopeBean;
|
|
|
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
|
|
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
|
|
|
import org.gcube.vremanagement.contextmanager.exceptions.ContextAlreadyExistsException;
|
|
|
|
import org.gcube.vremanagement.contextmanager.handlers.ContextContainer;
|
|
|
|
import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException;
|
|
|
|
import org.gcube.vremanagement.contextmanager.model.types.Context;
|
|
|
|
import org.gcube.vremanagement.contextmanager.model.types.Context.Type;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
|
|
|
@Singleton
|
|
|
|
public class ScopeInitializer {
|
|
|
|
|
|
|
|
@Inject Logger logger;
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
@Default
|
|
|
|
ContextContainer contextContainer;
|
|
|
|
|
|
|
|
public void initScope(String currentContext) {
|
|
|
|
logger.debug("current context is {}",currentContext);
|
2020-12-23 10:42:07 +01:00
|
|
|
ScopeBean currentScopeBean = new ScopeBean(currentContext);
|
|
|
|
if (currentScopeBean.is(ScopeBean.Type.VRE)) return;
|
2020-12-14 20:32:46 +01:00
|
|
|
try {
|
2020-12-23 10:42:07 +01:00
|
|
|
createEntireContext(currentScopeBean);
|
2020-12-14 20:32:46 +01:00
|
|
|
} catch (InvalidContextException e) {
|
|
|
|
logger.error("invalid context {}", currentContext,e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logger.info("resource initialization started in {} ",currentContext);
|
|
|
|
|
|
|
|
SimpleQuery query = queryFor(GenericResource.class);
|
2020-12-23 10:42:07 +01:00
|
|
|
query.addCondition("$resource/Profile/SecondaryType/text() eq 'VRE' or $resource/Profile/SecondaryType/text() eq 'VO'");
|
2020-12-14 20:32:46 +01:00
|
|
|
|
|
|
|
DiscoveryClient<GenericResource> client = clientFor(GenericResource.class);
|
|
|
|
List<GenericResource> resources = client.submit(query);
|
|
|
|
for (GenericResource resource : resources) {
|
|
|
|
try {
|
|
|
|
String body = resource.profile().bodyAsString();
|
2020-12-23 10:42:07 +01:00
|
|
|
ScopeDescriptor scopeDescr = ResourceBinder.get().bind("<Body>"+body+"</Body>");
|
2020-12-14 20:32:46 +01:00
|
|
|
String scope = scopeDescr.context;
|
|
|
|
|
|
|
|
try {
|
|
|
|
Context context = createEntireContext(new ScopeBean(scope));
|
|
|
|
for (ScopedResource res : scopeDescr.scopedResources)
|
|
|
|
contextContainer.addResource(context.getId(), res.id);
|
|
|
|
} catch (InvalidContextException e) {
|
|
|
|
logger.error("invalid context {}", scope,e);
|
|
|
|
}
|
|
|
|
} catch (JAXBException e) {
|
|
|
|
logger.error("cannot unmarshal resource",e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Context createEntireContext(ScopeBean originalContextBean) throws InvalidContextException{
|
|
|
|
LinkedList<String> contextList = new LinkedList<>();
|
2020-12-23 10:42:07 +01:00
|
|
|
contextList.addFirst(originalContextBean.toString());
|
2020-12-14 20:32:46 +01:00
|
|
|
ScopeBean enclosingScope = originalContextBean.enclosingScope();
|
|
|
|
while (enclosingScope!=null) {
|
|
|
|
logger.debug("adding scope {} to list",enclosingScope.name());
|
2020-12-23 10:42:07 +01:00
|
|
|
String scope = enclosingScope.toString();
|
2020-12-14 20:32:46 +01:00
|
|
|
contextList.addFirst(scope);
|
|
|
|
enclosingScope = enclosingScope.enclosingScope();
|
|
|
|
}
|
|
|
|
|
2020-12-23 10:42:07 +01:00
|
|
|
logger.debug("context list is {}",contextList);
|
|
|
|
|
|
|
|
Context actual = null;
|
2020-12-14 20:32:46 +01:00
|
|
|
for (String context : contextList) {
|
|
|
|
logger.debug("adding context {}",context);
|
2020-12-23 10:42:07 +01:00
|
|
|
int level = context.split("/").length-2;
|
|
|
|
String name = context.substring(context.lastIndexOf("/")+1);
|
|
|
|
logger.debug("adding context name {} and level is {}",name,level);
|
2020-12-14 20:32:46 +01:00
|
|
|
try {
|
2020-12-23 10:42:07 +01:00
|
|
|
Context parentContext = actual;
|
|
|
|
actual = new Context(parentContext,context, name, Type.values()[level]);
|
|
|
|
contextContainer.addContext(actual);
|
2020-12-14 20:32:46 +01:00
|
|
|
} catch (ContextAlreadyExistsException e) {
|
|
|
|
logger.warn("context {} already exists", context,e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 10:42:07 +01:00
|
|
|
return actual;
|
2020-12-14 20:32:46 +01:00
|
|
|
}
|
|
|
|
}
|