From b2bc132be235e4b9f54fc51aeed1111efcbc3fd3 Mon Sep 17 00:00:00 2001 From: "lucio.lelii" Date: Mon, 14 Dec 2020 20:32:46 +0100 Subject: [PATCH] - added new tests --- .settings/org.eclipse.wst.common.component | 30 +++--- pom.xml | 83 ++++++++++++---- .../contextmanager/ContextAppManager.java | 40 +------- .../contextmanager/LogFactory.java | 15 +++ .../contextmanager/ResourceManager.java | 5 +- .../contextmanager/ScopeInitializer.java | 96 +++++++++++++++++++ ...xtManagerApplication.java => Service.java} | 4 +- .../vremanagement/contextmanager/Utils.java | 18 ++++ .../collector/LegacyISConnector.java | 86 +++++++++++++++++ .../ContextAlreadyExistsException.java | 36 +++++++ .../handlers/ContextContainer.java | 5 +- .../handlers/ContextHandler.java | 10 +- .../handlers/ResourceHandler.java | 9 +- .../handlers/impl/ContextContainerImpl.java | 81 ++++++++++++++++ .../handlers/impl}/ContextTree.java | 7 +- .../handlers/impl/DynamicResourceHandler.java | 10 +- .../handlers/impl/StaticResourceHandler.java | 31 ++++-- .../handlers/impl}/TreeItem.java | 2 +- .../services/ContextService.java | 16 +++- src/main/resources/META-INF/beans.xml | 11 ++- src/main/resources/META-INF/persistence.xml | 39 ++++++++ src/main/webapp/WEB-INF/web.xml | 13 +++ .../BackendConnectorTester.java | 10 +- .../ContextContainerTester.java | 30 +++--- .../ContextServiceIntegrationTest.java | 22 ++++- .../contextmanager/ScopeTester.java | 23 +++++ src/test/resources/META-INF/beans.xml | 6 +- 27 files changed, 610 insertions(+), 128 deletions(-) create mode 100644 src/main/java/org/gcube/vremanagement/contextmanager/LogFactory.java create mode 100644 src/main/java/org/gcube/vremanagement/contextmanager/ScopeInitializer.java rename src/main/java/org/gcube/vremanagement/contextmanager/{ContextManagerApplication.java => Service.java} (79%) create mode 100644 src/main/java/org/gcube/vremanagement/contextmanager/Utils.java create mode 100644 src/main/java/org/gcube/vremanagement/contextmanager/collector/LegacyISConnector.java create mode 100644 src/main/java/org/gcube/vremanagement/contextmanager/exceptions/ContextAlreadyExistsException.java create mode 100644 src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/ContextContainerImpl.java rename src/{test/java/org/gcube/vremanagement/contextmanager => main/java/org/gcube/vremanagement/contextmanager/handlers/impl}/ContextTree.java (94%) rename src/{test/java/org/gcube/vremanagement/contextmanager => main/java/org/gcube/vremanagement/contextmanager/handlers/impl}/TreeItem.java (95%) create mode 100644 src/main/resources/META-INF/persistence.xml create mode 100644 src/main/webapp/WEB-INF/web.xml create mode 100644 src/test/java/org/gcube/vremanagement/contextmanager/ScopeTester.java diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 206c60d..6587437 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -1,37 +1,45 @@ - + - - + + + + - + + - + + + + + uses + - + - - - - + + + - + + diff --git a/pom.xml b/pom.xml index 01c9f61..3acacf8 100644 --- a/pom.xml +++ b/pom.xml @@ -3,9 +3,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.gcube.vremanagement - VREManager + context-manager 1.0.0-SNAPSHOT - VREManager + context-manager war maven-parent @@ -56,6 +56,34 @@ common-smartgears + + org.glassfish.jersey.core + jersey-client + 2.32 + + + + + org.eclipse.persistence + eclipselink + 2.7.7 + + + + + org.postgresql + postgresql + 42.2.18 + + + + + javax.interceptor + javax.interceptor-api + 1.2.2 + + + xstream @@ -89,7 +117,7 @@ jersey-media-multipart 2.32 - + com.rabbitmq @@ -119,13 +147,13 @@ jersey-cdi1x 2.32 - + org.glassfish.jersey.media jersey-media-multipart 2.32 - + @@ -133,15 +161,24 @@ org.jboss.weld.servlet weld-servlet-core - 2.2.10.Final + 3.1.0.Final + org.jboss jandex - 1.2.2.Final + 2.2.2.Final + + + net.bull.javamelody + javamelody-core + 1.82.0 + + + com.fasterxml.jackson.jaxrs jackson-jaxrs-json-provider @@ -158,7 +195,7 @@ javax.enterprise cdi-api - 1.2 + 2.0 @@ -207,12 +244,7 @@ test - - org.glassfish.jersey.ext.cdi - jersey-weld2-se - 2.30.1 - test - + ch.qos.logback @@ -221,13 +253,30 @@ test + + - junit - junit - 4.11 + com.h2database + h2 + 1.4.200 test + + org.jglue.cdi-unit + cdi-unit + 4.1.0 + test + + + + org.jboss.weld.se + weld-se-core + 3.1.0.Final + test + + + diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/ContextAppManager.java b/src/main/java/org/gcube/vremanagement/contextmanager/ContextAppManager.java index b9a8827..a7a0c78 100644 --- a/src/main/java/org/gcube/vremanagement/contextmanager/ContextAppManager.java +++ b/src/main/java/org/gcube/vremanagement/contextmanager/ContextAppManager.java @@ -1,58 +1,26 @@ package org.gcube.vremanagement.contextmanager; -import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; -import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; +import javax.inject.Inject; -import java.util.List; - -import javax.xml.bind.JAXBException; - -import org.gcube.common.resources.gcore.GenericResource; import org.gcube.common.scope.api.ScopeProvider; -import org.gcube.resources.discovery.client.api.DiscoveryClient; -import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.gcube.smartgears.ApplicationManager; import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class ContextAppManager implements ApplicationManager { - private static Logger logger = LoggerFactory.getLogger(ContextAppManager.class); + @Inject Logger logger; - - MultiKeysMap scopeDescriptorMap = new MultiKeysMap(); + @Inject ScopeInitializer scopeInitializer; @Override public void onInit() { String currentContext = ScopeProvider.instance.get(); - logger.info("resource initialization started in {} ",currentContext); - - SimpleQuery query = queryFor(GenericResource.class); - query.addCondition("$resource/Profile/SecondaryType/text eq 'VRE' or $resource/Profile/SecondaryType/text eq 'VO'"); - - DiscoveryClient client = clientFor(GenericResource.class); - List resources = client.submit(query); - for (GenericResource resource : resources) { - try { - String body = resource.profile().bodyAsString(); - ScopeDescriptor scopeDescr = ResourceBinder.get().bind(body); - scopeDescriptorMap.put(scopeDescr.context, resource.id(), scopeDescr); - } catch (JAXBException e) { - logger.error("cannot unmarshal resource",e); - } - } - + scopeInitializer.initScope(currentContext); } @Override public void onShutdown() { } - - public MultiKeysMap getScopeDescriptorMap() { - return scopeDescriptorMap; - } - - } diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/LogFactory.java b/src/main/java/org/gcube/vremanagement/contextmanager/LogFactory.java new file mode 100644 index 0000000..2fc4a46 --- /dev/null +++ b/src/main/java/org/gcube/vremanagement/contextmanager/LogFactory.java @@ -0,0 +1,15 @@ +package org.gcube.vremanagement.contextmanager; + +import javax.enterprise.inject.Produces; +import javax.enterprise.inject.spi.InjectionPoint; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +class LogFactory { + + @Produces public Logger createLogger(InjectionPoint injectionPoint) { + return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass()); + } + +} diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/ResourceManager.java b/src/main/java/org/gcube/vremanagement/contextmanager/ResourceManager.java index 6d56187..e50e7ee 100644 --- a/src/main/java/org/gcube/vremanagement/contextmanager/ResourceManager.java +++ b/src/main/java/org/gcube/vremanagement/contextmanager/ResourceManager.java @@ -12,6 +12,7 @@ import org.gcube.common.resources.gcore.Resource; import org.gcube.common.resources.gcore.Resource.Type; import org.gcube.vremanagement.contextmanager.handlers.ResourceHandler; import org.gcube.vremanagement.contextmanager.model.collectors.BackendConnector; +import org.gcube.vremanagement.contextmanager.model.types.Context; @Singleton @@ -25,13 +26,13 @@ public class ResourceManager { @Default BackendConnector defaultCollector; - public ScopedResource addResourceToContext(String context, Resource resource) { + public ScopedResource addResourceToContext(Context context, Resource resource) { ResourceHandler handler = retrieveHandler(resource.type()); handler.addResource(context, resource); return getScopedResource(resource); } - public ScopedResource removeResourceFromContext(String context, String id ) { + public ScopedResource removeResourceFromContext(Context context, String id ) { Resource res = retrieveResource(id); ResourceHandler handler = retrieveHandler(res.type()); handler.removeResource(context, res); diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/ScopeInitializer.java b/src/main/java/org/gcube/vremanagement/contextmanager/ScopeInitializer.java new file mode 100644 index 0000000..195bf47 --- /dev/null +++ b/src/main/java/org/gcube/vremanagement/contextmanager/ScopeInitializer.java @@ -0,0 +1,96 @@ +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); + try { + createEntireContext(new ScopeBean(currentContext)); + } catch (InvalidContextException e) { + logger.error("invalid context {}", currentContext,e); + return; + } + + + logger.info("resource initialization started in {} ",currentContext); + + SimpleQuery query = queryFor(GenericResource.class); + query.addCondition("$resource/Profile/SecondaryType/text eq 'VRE' or $resource/Profile/SecondaryType/text eq 'VO'"); + + DiscoveryClient client = clientFor(GenericResource.class); + List resources = client.submit(query); + for (GenericResource resource : resources) { + try { + String body = resource.profile().bodyAsString(); + ScopeDescriptor scopeDescr = ResourceBinder.get().bind(body); + 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 contextList = new LinkedList<>(); + contextList.addFirst(originalContextBean.name()); + ScopeBean enclosingScope = originalContextBean.enclosingScope(); + while (enclosingScope!=null) { + logger.debug("adding scope {} to list",enclosingScope.name()); + String scope = enclosingScope.name(); + contextList.addFirst(scope); + enclosingScope = enclosingScope.enclosingScope(); + } + + Context parentContext = null; + for (String context : contextList) { + logger.debug("adding context {}",context); + int level = context.split("/").length; + String name = context.substring(context.lastIndexOf("/")); + try { + parentContext = new Context(parentContext,context, name, Type.values()[level]); + contextContainer.addContext(parentContext); + } catch (ContextAlreadyExistsException e) { + logger.warn("context {} already exists", context,e); + } + } + + return parentContext; + } +} diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/ContextManagerApplication.java b/src/main/java/org/gcube/vremanagement/contextmanager/Service.java similarity index 79% rename from src/main/java/org/gcube/vremanagement/contextmanager/ContextManagerApplication.java rename to src/main/java/org/gcube/vremanagement/contextmanager/Service.java index c6505d7..a0dc23c 100644 --- a/src/main/java/org/gcube/vremanagement/contextmanager/ContextManagerApplication.java +++ b/src/main/java/org/gcube/vremanagement/contextmanager/Service.java @@ -10,14 +10,12 @@ import org.gcube.vremanagement.contextmanager.services.ContextService; import org.glassfish.jersey.media.multipart.MultiPartFeature; @Path("") -public class ContextManagerApplication extends Application { +public class Service extends Application { @Override public Set> getClasses() { final Set> classes = new HashSet>(); // register resources and features - //classes.add(XMLQueryAccess.class); - //classes.add(XMLResources.class); classes.add(ContextService.class); classes.add(MultiPartFeature.class); return classes; diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/Utils.java b/src/main/java/org/gcube/vremanagement/contextmanager/Utils.java new file mode 100644 index 0000000..39a7da9 --- /dev/null +++ b/src/main/java/org/gcube/vremanagement/contextmanager/Utils.java @@ -0,0 +1,18 @@ +package org.gcube.vremanagement.contextmanager; + +import org.gcube.vremanagement.contextmanager.model.types.Context; + +public class Utils { + + + public static String getScopeFromContext(Context context) { + Context contextInUse = context; + String scope = String.format("/%s",contextInUse.getName()); + while (contextInUse.getParent()!= null) { + contextInUse = contextInUse.getParent(); + scope = String.format("/%s%s",contextInUse.getName(), scope); + } + return scope; + } + +} diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/collector/LegacyISConnector.java b/src/main/java/org/gcube/vremanagement/contextmanager/collector/LegacyISConnector.java new file mode 100644 index 0000000..f49854c --- /dev/null +++ b/src/main/java/org/gcube/vremanagement/contextmanager/collector/LegacyISConnector.java @@ -0,0 +1,86 @@ +package org.gcube.vremanagement.contextmanager.collector; + +import java.io.StringWriter; +import java.util.List; + +import javax.enterprise.inject.Default; +import javax.inject.Singleton; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; + +import org.gcube.common.resources.gcore.Resource; +import org.gcube.common.resources.gcore.Resources; +import org.gcube.common.scope.api.ServiceMap; +import org.gcube.common.scope.impl.ScopedServiceMap; +import org.gcube.vremanagement.contextmanager.Utils; +import org.gcube.vremanagement.contextmanager.model.collectors.BackendConnector; +import org.gcube.vremanagement.contextmanager.model.types.Context; + +@Singleton +@Default +public class LegacyISConnector implements BackendConnector { + + + private Client client = ClientBuilder.newClient(); + + private static final String RESOURCE_SERVICE = "XMLStoreService"; + + @Override + public Resource find(String resourceId) { + ServiceMap map = ScopedServiceMap.instance; + String service = map.endpoint(RESOURCE_SERVICE); + WebTarget webTarget = client.target(service).path(resourceId); + return webTarget.request(MediaType.APPLICATION_XML).get(Resource.class); + } + + @Override + public void createContext(Context context, String parentContextId, List resourceIds) { + } + + @Override + public boolean removeContext(Context context) { + return true; + } + + @Override + public boolean addResourceToContext(Context context, Resource resource) { + String completeContext = Utils.getScopeFromContext(context); + resource.scopes().asCollection().add(completeContext); + ServiceMap map = ScopedServiceMap.instance; + String service = map.endpoint(RESOURCE_SERVICE); + WebTarget webTarget = client.target(service).path(resource.type().name()).path(resource.id()); + StringWriter writer = new StringWriter(); + Resources.marshal(resource, writer); + String ret = webTarget.request(MediaType.TEXT_XML).put(Entity.text(writer.toString()), String.class); + return ret!=null; + } + + @Override + public boolean removeResourceFromContext(Context context, Resource resource) { + String completeContext = Utils.getScopeFromContext(context); + resource.scopes().asCollection().remove(completeContext); + ServiceMap map = ScopedServiceMap.instance; + String service = map.endpoint(RESOURCE_SERVICE); + WebTarget webTarget = client.target(service).path(resource.type().name()).path(resource.id()); + + String ret; + if (resource.scopes().size()>0) { + StringWriter writer = new StringWriter(); + Resources.marshal(resource, writer); + ret = webTarget.request(MediaType.TEXT_XML).put(Entity.text(writer.toString()), String.class); + } else { + ret = webTarget.request().delete(String.class); + } + return ret!=null; + } + + @Override + public boolean updateResource(Resource resource) { + // TODO Auto-generated method stub + return false; + } + +} diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/exceptions/ContextAlreadyExistsException.java b/src/main/java/org/gcube/vremanagement/contextmanager/exceptions/ContextAlreadyExistsException.java new file mode 100644 index 0000000..3edf240 --- /dev/null +++ b/src/main/java/org/gcube/vremanagement/contextmanager/exceptions/ContextAlreadyExistsException.java @@ -0,0 +1,36 @@ +package org.gcube.vremanagement.contextmanager.exceptions; + +public class ContextAlreadyExistsException extends Exception { + + /** + * + */ + private static final long serialVersionUID = -5847389374215009478L; + + public ContextAlreadyExistsException() { + super(); + // TODO Auto-generated constructor stub + } + + public ContextAlreadyExistsException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + // TODO Auto-generated constructor stub + } + + public ContextAlreadyExistsException(String message, Throwable cause) { + super(message, cause); + // TODO Auto-generated constructor stub + } + + public ContextAlreadyExistsException(String message) { + super(message); + // TODO Auto-generated constructor stub + } + + public ContextAlreadyExistsException(Throwable cause) { + super(cause); + // TODO Auto-generated constructor stub + } + +} diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ContextContainer.java b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ContextContainer.java index 7520be3..6993094 100644 --- a/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ContextContainer.java +++ b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ContextContainer.java @@ -4,17 +4,18 @@ import java.util.List; import javax.inject.Singleton; +import org.gcube.vremanagement.contextmanager.exceptions.ContextAlreadyExistsException; import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException; import org.gcube.vremanagement.contextmanager.model.types.Context; -@Singleton + public interface ContextContainer { List getAvailableContexts(); Context getContextById(String id) throws InvalidContextException; - void addContext(String parentId, Context toAdd) throws InvalidContextException; + void addContext(Context toAdd) throws InvalidContextException, ContextAlreadyExistsException; void removeContext(String contextId) throws InvalidContextException; diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ContextHandler.java b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ContextHandler.java index 5a053e6..c0aded5 100644 --- a/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ContextHandler.java +++ b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ContextHandler.java @@ -9,6 +9,7 @@ import javax.inject.Singleton; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.vremanagement.contextmanager.ContextAppManager; +import org.gcube.vremanagement.contextmanager.exceptions.ContextAlreadyExistsException; import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException; import org.gcube.vremanagement.contextmanager.model.operators.context.CustomContextOperator; import org.gcube.vremanagement.contextmanager.model.operators.context.MandatoryContextOperator; @@ -37,9 +38,12 @@ public class ContextHandler { public void createContext(String parentContextId, String vreName, List resourcesIds, ContextAppManager appContext) throws InvalidContextException { Context parentContext = contextContainer.getContextById(parentContextId); - //validateCall(fullContext); - Context newContext = new Context(vreName, vreName, Type.VRE); - contextContainer.addContext(parentContext.getId(), newContext ); + 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"); + } for (String resourceId: resourcesIds) { contextContainer.addResource(newContext.getId(), resourceId); log.debug("resource {} added", resourceId); diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ResourceHandler.java b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ResourceHandler.java index cf09b9d..57bc38f 100644 --- a/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ResourceHandler.java +++ b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ResourceHandler.java @@ -5,19 +5,20 @@ import java.util.List; import org.gcube.common.resources.gcore.Resource; import org.gcube.common.resources.gcore.Resource.Type; +import org.gcube.vremanagement.contextmanager.model.types.Context; public interface ResourceHandler { public List getManagedResources(); - public void addResource(String contextName, Resource resource); + public void addResource(Context context, Resource resource); - public void removeResource(String contextName, Resource resource); + public void removeResource(Context context, Resource resource); //in case of resource created directly by the publisher - public void createResourceNotified(String contextName, Resource resource); + public void createResourceNotified(Context context, Resource resource); //in case of resource deleted directly by the publisher - public void removeResourceNotified(String contextName, Resource resource); + public void removeResourceNotified(Context context, Resource resource); } diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/ContextContainerImpl.java b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/ContextContainerImpl.java new file mode 100644 index 0000000..8af6694 --- /dev/null +++ b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/ContextContainerImpl.java @@ -0,0 +1,81 @@ +package org.gcube.vremanagement.contextmanager.handlers.impl; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.enterprise.inject.Default; +import javax.inject.Singleton; + +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; + +@Singleton +@Default +public class ContextContainerImpl implements ContextContainer { + + + private Map> resourceMap = new HashMap<>(); + + private static ContextTree contextTree = new ContextTree() { + + @Override + public void init() { + + } + }; + + public List getAvailableContexts() { + return contextTree.getContexts(); + } + + public void addContext(Context toAdd) throws InvalidContextException, ContextAlreadyExistsException{ + contextTree.createItem(toAdd==null?null:toAdd.getId(), toAdd); + } + + public void removeContext(String contextId) throws InvalidContextException { + TreeItem item = contextTree.removeItem(contextId); + if (item==null) throw new InvalidContextException("context with id "+contextId+" not found"); + resourceMap.remove(item.getContext().getId()); + } + + public Context getContextById(String id) throws InvalidContextException { + Context context = contextTree.getContext(id); + if (context==null) throw new InvalidContextException("context with id "+id+" not found"); + return context; + } + + public synchronized void addResource(String contextId, String resourceId) throws InvalidContextException { + Context context = contextTree.getContext(contextId); + if (context==null) throw new InvalidContextException("context with id "+contextId+" not found"); + + if (!resourceMap.containsKey(contextId)) + resourceMap.put(contextId, new ArrayList<>()); + resourceMap.get(contextId).add(resourceId); + + } + + public synchronized void removeResource(String contextId, String resourceId) throws InvalidContextException { + Context context = contextTree.getContext(contextId); + if (context==null) throw new InvalidContextException("context with id "+contextId+" not found"); + + if (resourceMap.containsKey(contextId)) + resourceMap.get(contextId).remove(resourceId); + } + + public List getResources(String contextId) throws InvalidContextException { + Context context = contextTree.getContext(contextId); + if (context==null) throw new InvalidContextException("context with id "+contextId+" not found"); + if (resourceMap.containsKey(contextId)) + return resourceMap.get(contextId); + else return Collections.emptyList(); + } + +} + + + \ No newline at end of file diff --git a/src/test/java/org/gcube/vremanagement/contextmanager/ContextTree.java b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/ContextTree.java similarity index 94% rename from src/test/java/org/gcube/vremanagement/contextmanager/ContextTree.java rename to src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/ContextTree.java index 19a321a..00334fb 100644 --- a/src/test/java/org/gcube/vremanagement/contextmanager/ContextTree.java +++ b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/ContextTree.java @@ -1,4 +1,4 @@ -package org.gcube.vremanagement.contextmanager; +package org.gcube.vremanagement.contextmanager.handlers.impl; import java.util.ArrayList; import java.util.HashMap; @@ -6,15 +6,16 @@ import java.util.List; import java.util.Map; import java.util.Set; +import javax.inject.Inject; + 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; -import org.slf4j.LoggerFactory; public abstract class ContextTree { - private static Logger log = LoggerFactory.getLogger(ContextTree.class); + @Inject Logger log; public ContextTree() { init(); diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/DynamicResourceHandler.java b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/DynamicResourceHandler.java index 7997bae..af0a430 100644 --- a/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/DynamicResourceHandler.java +++ b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/DynamicResourceHandler.java @@ -10,6 +10,8 @@ import org.gcube.common.resources.gcore.Resource; import org.gcube.common.resources.gcore.Resource.Type; import org.gcube.vremanagement.contextmanager.handlers.ResourceHandler; import org.gcube.vremanagement.contextmanager.model.collectors.BackendConnector; +import org.gcube.vremanagement.contextmanager.model.types.Context; + public class DynamicResourceHandler implements ResourceHandler { @@ -23,24 +25,24 @@ public class DynamicResourceHandler implements ResourceHandler { } @Override - public void addResource(String contextName, Resource resource) { + public void addResource(Context context, Resource resource) { // TODO Auto-generated method stub } @Override - public void removeResource(String contextName, Resource resource) { + public void removeResource(Context context, Resource resource) { // TODO Auto-generated method stub } @Override - public void createResourceNotified(String contextName, Resource resource) { + public void createResourceNotified(Context context, Resource resource) { // TODO Auto-generated method stub } @Override - public void removeResourceNotified(String contextName, Resource resource) { + public void removeResourceNotified(Context context, Resource resource) { // TODO Auto-generated method stub } diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/StaticResourceHandler.java b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/StaticResourceHandler.java index 736c1c7..7ee5aac 100644 --- a/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/StaticResourceHandler.java +++ b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/StaticResourceHandler.java @@ -11,12 +11,17 @@ import org.gcube.common.resources.gcore.Resource; import org.gcube.common.resources.gcore.Resource.Type; import org.gcube.vremanagement.contextmanager.handlers.ResourceHandler; import org.gcube.vremanagement.contextmanager.model.collectors.BackendConnector; +import org.gcube.vremanagement.contextmanager.model.types.Context; +import org.slf4j.Logger; public class StaticResourceHandler implements ResourceHandler { + @Inject + Logger log; + @Inject @Any - Instance collectors; + Instance connectors; @Override public List getManagedResources() { @@ -24,25 +29,33 @@ public class StaticResourceHandler implements ResourceHandler { } @Override - public void addResource(String contextName, Resource resource) { - collectors.forEach(c-> c.addResourceToContext(contextName, resource)); - //ADD resource operator execution onAdd + public void addResource(Context context, Resource resource) { + for (BackendConnector connector : connectors) + try { + connector.addResourceToContext(context, resource); + }catch (Exception e) { + log.warn("error adding context {} in resource with id {} ",resource.id(), context.getName()); + } } @Override - public void removeResource(String contextName, Resource resource) { - collectors.forEach(c-> c.removeResourceFromContext(contextName, resource)); - //ADD resource operator execution onREmove + public void removeResource(Context context, Resource resource) { + for (BackendConnector connector : connectors) + try { + connector.removeResourceFromContext(context, resource); + }catch (Exception e) { + log.warn("error removing context {} in resource with id {} ",resource.id(), context.getName()); + } } @Override - public void createResourceNotified(String contextName, Resource resource) { + public void createResourceNotified(Context context, Resource resource) { // TODO Auto-generated method stub } @Override - public void removeResourceNotified(String contextName, Resource resource) { + public void removeResourceNotified(Context context, Resource resource) { // TODO Auto-generated method stub } diff --git a/src/test/java/org/gcube/vremanagement/contextmanager/TreeItem.java b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/TreeItem.java similarity index 95% rename from src/test/java/org/gcube/vremanagement/contextmanager/TreeItem.java rename to src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/TreeItem.java index 6bd78a4..144012e 100644 --- a/src/test/java/org/gcube/vremanagement/contextmanager/TreeItem.java +++ b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/TreeItem.java @@ -1,4 +1,4 @@ -package org.gcube.vremanagement.contextmanager; +package org.gcube.vremanagement.contextmanager.handlers.impl; import java.util.Collections; import java.util.HashSet; diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/services/ContextService.java b/src/main/java/org/gcube/vremanagement/contextmanager/services/ContextService.java index 6c0d776..09e9655 100644 --- a/src/main/java/org/gcube/vremanagement/contextmanager/services/ContextService.java +++ b/src/main/java/org/gcube/vremanagement/contextmanager/services/ContextService.java @@ -22,7 +22,10 @@ import org.gcube.smartgears.annotations.ManagedBy; import org.gcube.smartgears.context.application.ApplicationContext; import org.gcube.vremanagement.contextmanager.ContextAppManager; import org.gcube.vremanagement.contextmanager.ResourceManager; +import org.gcube.vremanagement.contextmanager.Utils; +import org.gcube.vremanagement.contextmanager.handlers.ContextContainer; import org.gcube.vremanagement.contextmanager.handlers.ContextHandler; +import org.gcube.vremanagement.contextmanager.model.types.Context; import org.gcube.vremanagement.contextmanager.model.types.StringList; import org.glassfish.jersey.media.multipart.FormDataParam; import org.slf4j.Logger; @@ -42,6 +45,9 @@ public class ContextService { @Inject ResourceManager resourceHandler; + @Inject + ContextContainer contextContainer; + @RequestScoped @PathParam("contextId") String contextId; @@ -95,15 +101,15 @@ public class ContextService { @DELETE @Path("/{contextId}/resources/{resourceId}") - public String removeResourceFromContext(@PathParam("resourceId") String resourceId) { - /*log.info("remove resource {} from context {}", resourceId, id); + public void removeResourceFromContext(@PathParam("resourceId") String resourceId) { + log.info("remove resource {} from context {}", resourceId, contextId); try { - validateCall(context); + Context context = contextContainer.getContextById(contextId); + validateCall(Utils.getScopeFromContext(context)); resourceHandler.removeResourceFromContext(context, resourceId); }catch (Exception e) { // TODO: handle exception - } */ - return null; + } } diff --git a/src/main/resources/META-INF/beans.xml b/src/main/resources/META-INF/beans.xml index f64554d..7feabd6 100644 --- a/src/main/resources/META-INF/beans.xml +++ b/src/main/resources/META-INF/beans.xml @@ -1,6 +1,9 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd" + bean-discovery-mode="all" version="2.0"> + + net.bull.javamelody.MonitoringCdiInterceptor + net.bull.javamelody.MonitoringAsynchronousCdiInterceptor + \ No newline at end of file diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..ca1ba46 --- /dev/null +++ b/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,39 @@ + + + + org.eclipse.persistence.jpa.PersistenceProvider + org.secondquadrant.javabook.eclipselink.LargeCities + + + + + + + + + + + com.test.jpa.Student + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..a0bec9d --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,13 @@ + + + + + org.gcube.vremanagement.contextmanager.Service + + + + org.gcube.vremanagement.contextmanager.Service + /* + + + diff --git a/src/test/java/org/gcube/vremanagement/contextmanager/BackendConnectorTester.java b/src/test/java/org/gcube/vremanagement/contextmanager/BackendConnectorTester.java index cf0d9bb..2f2f2f7 100644 --- a/src/test/java/org/gcube/vremanagement/contextmanager/BackendConnectorTester.java +++ b/src/test/java/org/gcube/vremanagement/contextmanager/BackendConnectorTester.java @@ -6,6 +6,7 @@ import javax.enterprise.inject.Default; import org.gcube.common.resources.gcore.Resource; import org.gcube.vremanagement.contextmanager.model.collectors.BackendConnector; +import org.gcube.vremanagement.contextmanager.model.types.Context; @Default public class BackendConnectorTester implements BackendConnector { @@ -16,25 +17,24 @@ public class BackendConnectorTester implements BackendConnector { } @Override - public String createContext(String contextName, String parentContextId, List resourceIds) { + public void createContext(Context context, String parentContextId, List resourceIds) { // TODO Auto-generated method stub - return null; } @Override - public boolean removeContext(String contextId) { + public boolean removeContext(Context context) { // TODO Auto-generated method stub return false; } @Override - public boolean addResourceToContext(String contextId, Resource resource) { + public boolean addResourceToContext(Context context, Resource resource) { // TODO Auto-generated method stub return false; } @Override - public boolean removeResourceFromContext(String contextId, Resource resource) { + public boolean removeResourceFromContext(Context context, Resource resource) { // TODO Auto-generated method stub return false; } diff --git a/src/test/java/org/gcube/vremanagement/contextmanager/ContextContainerTester.java b/src/test/java/org/gcube/vremanagement/contextmanager/ContextContainerTester.java index 1299662..f87a7ab 100644 --- a/src/test/java/org/gcube/vremanagement/contextmanager/ContextContainerTester.java +++ b/src/test/java/org/gcube/vremanagement/contextmanager/ContextContainerTester.java @@ -6,20 +6,22 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.enterprise.inject.Alternative; +import javax.enterprise.inject.Default; import javax.inject.Singleton; +import org.gcube.vremanagement.contextmanager.exceptions.ContextAlreadyExistsException; import org.gcube.vremanagement.contextmanager.handlers.ContextContainer; +import org.gcube.vremanagement.contextmanager.handlers.impl.ContextTree; +import org.gcube.vremanagement.contextmanager.handlers.impl.TreeItem; 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; -import org.slf4j.LoggerFactory; -@Singleton -public class ContextContainerTester implements ContextContainer { - private static Logger log = LoggerFactory.getLogger(ContextContainerTester.class); - +public class ContextContainerTester { + + /* private Map> resourceMap = new HashMap<>(); private static ContextTree contextTree = new ContextTree() { @@ -27,11 +29,11 @@ public class ContextContainerTester implements ContextContainer { @Override public void init() { try { - TreeItem root = this.createItem(null, new Context("gcube", "gcube", Type.INFRASTRUCTURE)); - TreeItem devsec = this.createItem(root.getContext().getId(), new Context("devsec", "devsec", Type.VO)); - TreeItem devNext = this.createItem(root.getContext().getId(), new Context("devNext", "devNext", Type.VO)); - this.createItem(devsec.getContext().getId(), new Context("devVRE", "devVRE", Type.VRE)); - this.createItem(devNext.getContext().getId(), new Context("nextNext", "nextNext", Type.VRE)); + TreeItem root = this.createItem(null, new Context(null, "gcube", "gcube", Type.INFRASTRUCTURE)); + TreeItem devsec = this.createItem(root.getContext().getId(), new Context(root.getContext(), "devsec", "devsec", Type.VO)); + TreeItem devNext = this.createItem(root.getContext().getId(), new Context(root.getContext(), "devNext", "devNext", Type.VO)); + this.createItem(devsec.getContext().getId(), new Context(devsec.getContext(),"devVRE", "devVRE", Type.VRE)); + this.createItem(devNext.getContext().getId(), new Context(devNext.getContext(),"nextNext", "nextNext", Type.VRE)); }catch (InvalidContextException e) {} } }; @@ -42,8 +44,8 @@ public class ContextContainerTester implements ContextContainer { } @Override - public void addContext(String parentId, Context toAdd) throws InvalidContextException{ - contextTree.createItem(parentId, toAdd); + public void addContext(Context toAdd) throws InvalidContextException, ContextAlreadyExistsException{ + contextTree.createItem(toAdd==null?null:toAdd.getId(), toAdd); } @Override @@ -88,5 +90,5 @@ public class ContextContainerTester implements ContextContainer { return resourceMap.get(contextId); else return Collections.emptyList(); } - + */ } diff --git a/src/test/java/org/gcube/vremanagement/contextmanager/ContextServiceIntegrationTest.java b/src/test/java/org/gcube/vremanagement/contextmanager/ContextServiceIntegrationTest.java index 5b769d2..112dac0 100644 --- a/src/test/java/org/gcube/vremanagement/contextmanager/ContextServiceIntegrationTest.java +++ b/src/test/java/org/gcube/vremanagement/contextmanager/ContextServiceIntegrationTest.java @@ -16,6 +16,7 @@ import javax.ws.rs.core.Response.Status; import org.gcube.vremanagement.contextmanager.model.types.StringList; import org.gcube.vremanagement.contextmanager.services.ContextService; +import org.glassfish.hk2.utilities.binding.AbstractBinder; import org.glassfish.jersey.client.ClientConfig; import org.glassfish.jersey.logging.LoggingFeature; import org.glassfish.jersey.media.multipart.FormDataMultiPart; @@ -24,9 +25,14 @@ import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; import org.jboss.weld.environment.se.Weld; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class ContextServiceIntegrationTest extends JerseyTest { + Logger log = LoggerFactory.getLogger(ContextServiceIntegrationTest.class); + @Override protected Application configure() { /*Class[] extScopes = ServiceFinder.find(ExternalRequestScope.class, true).toClassArray(); @@ -40,12 +46,24 @@ public class ContextServiceIntegrationTest extends JerseyTest { return new ResourceConfig(ContextService.class) .register(MultiPartFeature.class) - .register(LoggingFeature.class); + .register(LoggingFeature.class).register(new AbstractBinder() { + @Override + protected void configure() { + bind(log).to(Logger.class); + } + }); } @Override protected void configureClient(ClientConfig config) { - config.register(MultiPartFeature.class); + config.register(MultiPartFeature.class) + .register(LogFactory.class).register(new AbstractBinder() { + @Override + protected void configure() { + bind(log).to(Logger.class); + } + }); + } @Test diff --git a/src/test/java/org/gcube/vremanagement/contextmanager/ScopeTester.java b/src/test/java/org/gcube/vremanagement/contextmanager/ScopeTester.java new file mode 100644 index 0000000..5565a64 --- /dev/null +++ b/src/test/java/org/gcube/vremanagement/contextmanager/ScopeTester.java @@ -0,0 +1,23 @@ +package org.gcube.vremanagement.contextmanager; + +import javax.inject.Inject; + +import org.gcube.vremanagement.contextmanager.handlers.impl.ContextContainerImpl; +import org.jglue.cdiunit.AdditionalClasses; +import org.jglue.cdiunit.CdiRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(CdiRunner.class) +@AdditionalClasses({ContextContainerImpl.class, LogFactory.class }) +public class ScopeTester { + + @Inject ScopeInitializer scopeInitializer; + + + @Test + public void initTest() { + scopeInitializer.initScope("/gcube/devNext"); + } + +} diff --git a/src/test/resources/META-INF/beans.xml b/src/test/resources/META-INF/beans.xml index f64554d..77e45d1 100644 --- a/src/test/resources/META-INF/beans.xml +++ b/src/test/resources/META-INF/beans.xml @@ -1,6 +1,6 @@ + bean-discovery-mode="all"> \ No newline at end of file