context-manager/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ContextHandler.java

68 lines
2.5 KiB
Java
Raw Normal View History

2020-12-03 14:24:34 +01:00
package org.gcube.vremanagement.contextmanager.handlers;
import java.util.List;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.vremanagement.contextmanager.ContextAppManager;
2020-12-14 20:32:46 +01:00
import org.gcube.vremanagement.contextmanager.exceptions.ContextAlreadyExistsException;
2020-12-04 19:57:56 +01:00
import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException;
2020-12-03 14:24:34 +01:00
import org.gcube.vremanagement.contextmanager.model.operators.context.CustomContextOperator;
import org.gcube.vremanagement.contextmanager.model.operators.context.MandatoryContextOperator;
2020-12-04 19:57:56 +01:00
import org.gcube.vremanagement.contextmanager.model.types.Context;
import org.gcube.vremanagement.contextmanager.model.types.Context.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2020-12-03 14:24:34 +01:00
@Singleton
public class ContextHandler {
2020-12-04 19:57:56 +01:00
private static Logger log = LoggerFactory.getLogger(ContextHandler.class);
2020-12-03 14:24:34 +01:00
@Inject
@Any
Instance<MandatoryContextOperator> mandatoryContextOperators;
@Inject
@Any
Instance<CustomContextOperator> customContextOperators;
2020-12-04 19:57:56 +01:00
@Inject
ContextContainer contextContainer;
public void createContext(String parentContextId, String vreName, List<String> resourcesIds, ContextAppManager appContext) throws InvalidContextException {
Context parentContext = contextContainer.getContextById(parentContextId);
2020-12-14 20:32:46 +01:00
Context newContext = new Context(parentContext, vreName, vreName, Type.VRE);
try {
contextContainer.addContext(newContext );
} catch (ContextAlreadyExistsException e) {
throw new InvalidContextException("the context with id "+vreName+" already exists");
}
2020-12-04 19:57:56 +01:00
for (String resourceId: resourcesIds) {
contextContainer.addResource(newContext.getId(), resourceId);
log.debug("resource {} added", resourceId);
}
mandatoryContextOperators.forEach(co -> co.onCreateContext(SecurityTokenProvider.instance.get(), newContext.getId(), null));
}
public void disposeContext(String contextId, ContextAppManager appContext) throws InvalidContextException {
contextContainer.removeContext(contextId);
mandatoryContextOperators.forEach(co -> co.onDisposeContext(SecurityTokenProvider.instance.get(), contextId, null));
}
public List<String> getAvailableContexts(){
return contextContainer.getAvailableContexts();
2020-12-03 14:24:34 +01:00
}
2020-12-04 19:57:56 +01:00
public List<String> getAssociatedResources(String contextId) throws InvalidContextException{
return contextContainer.getResources(contextId);
2020-12-03 14:24:34 +01:00
}
}