- added new tests

This commit is contained in:
lucio.lelii 2020-12-14 20:32:46 +01:00
parent e236ec83c7
commit b2bc132be2
27 changed files with 610 additions and 128 deletions

View File

@ -1,37 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="VREManager">
<wb-module deploy-name="context-manager">
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<dependent-module archiveName="context-manager-model-1.0.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/context-manager-model/context-manager-model">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="context-root" value="VREManager"/>
<property name="java-output-path" value="/ContextManager/target/classes"/>
<property name="context-root" value="context-manager"/>
</wb-module>
</project-modules>

83
pom.xml
View File

@ -3,9 +3,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.gcube.vremanagement</groupId>
<artifactId>VREManager</artifactId>
<artifactId>context-manager</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>VREManager</name>
<name>context-manager</name>
<packaging>war</packaging>
<parent>
<artifactId>maven-parent</artifactId>
@ -56,6 +56,34 @@
<artifactId>common-smartgears</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.32</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.persistence/eclipselink -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.7.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.18</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.interceptor/javax.interceptor-api -->
<dependency>
<groupId>javax.interceptor</groupId>
<artifactId>javax.interceptor-api</artifactId>
<version>1.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/xstream/xstream -->
<dependency>
<groupId>xstream</groupId>
@ -89,7 +117,7 @@
<artifactId>jersey-media-multipart</artifactId>
<version>2.32</version>
</dependency>
<!-- RabbitMQClient -->
<dependency>
<groupId>com.rabbitmq</groupId>
@ -119,13 +147,13 @@
<artifactId>jersey-cdi1x</artifactId>
<version>2.32</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.32</version>
</dependency>
<!-- <dependency> <groupId>org.glassfish.jersey.ext.cdi</groupId> <artifactId>jersey-cdi1x-servlet</artifactId>
<version>2.32</version> </dependency> -->
@ -133,15 +161,24 @@
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-core</artifactId>
<version>2.2.10.Final</version>
<version>3.1.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss/jandex -->
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jandex</artifactId>
<version>1.2.2.Final</version>
<version>2.2.2.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.bull.javamelody/javamelody-core -->
<dependency>
<groupId>net.bull.javamelody</groupId>
<artifactId>javamelody-core</artifactId>
<version>1.82.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
@ -158,7 +195,7 @@
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
<version>2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-common -->
@ -207,12 +244,7 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-weld2-se</artifactId>
<version>2.30.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
@ -221,13 +253,30 @@
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jglue.cdi-unit</groupId>
<artifactId>cdi-unit</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>3.1.0.Final</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -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<String, String, ScopeDescriptor> scopeDescriptorMap = new MultiKeysMap<String, String,ScopeDescriptor>();
@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<GenericResource> client = clientFor(GenericResource.class);
List<GenericResource> 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<String, String, ScopeDescriptor> getScopeDescriptorMap() {
return scopeDescriptorMap;
}
}

View File

@ -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());
}
}

View File

@ -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);

View File

@ -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<GenericResource> client = clientFor(GenericResource.class);
List<GenericResource> 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<String> 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;
}
}

View File

@ -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<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
// register resources and features
//classes.add(XMLQueryAccess.class);
//classes.add(XMLResources.class);
classes.add(ContextService.class);
classes.add(MultiPartFeature.class);
return classes;

View File

@ -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;
}
}

View File

@ -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<String> 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;
}
}

View File

@ -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
}
}

View File

@ -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<String> 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;

View File

@ -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<String> 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);

View File

@ -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<Type> 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);
}

View File

@ -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<String, List<String>> resourceMap = new HashMap<>();
private static ContextTree contextTree = new ContextTree() {
@Override
public void init() {
}
};
public List<String> 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<String> 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();
}
}

View File

@ -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();

View File

@ -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
}

View File

@ -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<BackendConnector> collectors;
Instance<BackendConnector> connectors;
@Override
public List<Type> 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
}

View File

@ -1,4 +1,4 @@
package org.gcube.vremanagement.contextmanager;
package org.gcube.vremanagement.contextmanager.handlers.impl;
import java.util.Collections;
import java.util.HashSet;

View File

@ -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;
}
}

View File

@ -1,6 +1,9 @@
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
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_1_1.xsd"
bean-discovery-mode="all">
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">
<interceptors>
<class>net.bull.javamelody.MonitoringCdiInterceptor</class>
<class>net.bull.javamelody.MonitoringAsynchronousCdiInterceptor</class>
</interceptors>
</beans>

View File

@ -0,0 +1,39 @@
<persistence
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="main"
transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>org.secondquadrant.javabook.eclipselink.LargeCities</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:postgresql://localhost:5432/postgres" />
<property name="javax.persistence.jdbc.user"
value="postgres" />
<property name="javax.persistence.jdbc.password" value="" />
</properties>
</persistence-unit>
<persistence-unit name="test"
transaction-type="RESOURCE_LOCAL">
<class>com.test.jpa.Student</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="eclipselink.ddl-generation"
value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
</properties>
</persistence-unit>
</persistence>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>org.gcube.vremanagement.contextmanager.Service</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>org.gcube.vremanagement.contextmanager.Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

View File

@ -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<String> resourceIds) {
public void createContext(Context context, String parentContextId, List<String> 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;
}

View File

@ -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<String, List<String>> 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();
}
*/
}

View File

@ -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<ExternalRequestScope>[] 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

View File

@ -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");
}
}

View File

@ -1,6 +1,6 @@
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
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_1_1.xsd"
bean-discovery-mode="all">
bean-discovery-mode="all">
</beans>