- Default validator Context control added

This commit is contained in:
lucio 2020-04-07 19:16:48 +02:00
parent a00b163f3f
commit 61a5cf8700
11 changed files with 18 additions and 341 deletions

View File

@ -318,16 +318,16 @@ public class RegistryPublisherImpl implements RegistryPublisher {
ScopeProvider.instance.set(currentScope);
}
Utils.getInternalVOScopes(resource);
// retrieves the scopes on resource and update it
// updateResource(resource, currentScope);
try{
vosUpdate(resource);
}catch(Exception e){
log.error("exception message: "+e.getMessage());
throw new RuntimeException(e.getMessage());
}
if (!voScopesAfterRemove.isEmpty())
try{
vosUpdate(resource);
}catch(Exception e){
log.error("exception message: "+e.getMessage());
throw new RuntimeException(e.getMessage());
}
return resource;
}

View File

@ -3,36 +3,18 @@ package org.gcube.informationsystem.publisher.scope;
import java.util.List;
import org.gcube.common.resources.gcore.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DefaultScopeValidator<R> implements Validator<Resource>{
private static Logger log = LoggerFactory.getLogger(DefaultScopeValidator.class);
public class DefaultScopeValidator implements Validator{
@Override
public <R extends Resource> void validate(R resource) {
// log.info("validate method of "+this.getClass());
// String currentScope=ScopeProvider.instance.get();
// ScopeGroup<String> scopes=resource.scopes();
// boolean founded= false;
// for(Iterator<String> it=scopes.iterator(); it.hasNext();){
// String scope=it.next();
// if(scope.equals(currentScope))
// founded=true;
// }
// if(!founded)
// throw new IllegalStateException(" scope "+currentScope+" not present in resource");
public void validate(Resource resource) {
if (resource.scopes().isEmpty())
throw new IllegalArgumentException("scopes in the resource are empty");
}
@Override
public Class type() {
return Resource.class;
}
@Override
public <R extends Resource> void checkScopeCompatibility(R resource,
public void checkScopeCompatibility(Resource resource,
List<String> scopesList) {
// for(String scope: scopesList){
// ScopeGroup<String> scopes=resource.scopes();

View File

@ -4,11 +4,9 @@ import java.util.List;
import org.gcube.common.resources.gcore.Resource;
public interface Validator <R extends Resource>{
public interface Validator {
<R extends Resource> void validate(R resource);
<R extends Resource> void checkScopeCompatibility(R resource, List<String> scopes);
public Class<R> type();
void validate(Resource resource);
void checkScopeCompatibility(Resource resource, List<String> scopes);
}

View File

@ -1,29 +1,12 @@
package org.gcube.informationsystem.publisher.scope;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.gcube.common.resources.gcore.Resource;
public class ValidatorProvider {
private static Map<Class, Validator> validatorsMap= new LinkedHashMap<Class, Validator>();
public static Validator getValidator(Resource resource){
Validator validator=null;
if(validatorsMap.isEmpty()){
IValidatorContext context= ScopeValidatorScanner.provider();
List<Validator> validators=context.getValidators();
for(Validator v :validators){
validatorsMap.put(v.type(), v);
}
}
validator=validatorsMap.get(resource.getClass());
if (validator==null)
validator=new DefaultScopeValidator();
return validator;
return new DefaultScopeValidator();
}

View File

@ -1,68 +0,0 @@
package org.gcube.informationsystem.publisher;
import static org.junit.Assert.*;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;
import org.gcube.informationsystem.publisher.scope.IValidatorContext;
import org.gcube.informationsystem.publisher.scope.Validator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class ConfigurationTest {
private ClassLoader loader;
// static final String relativePath="src/test/java/META-INF/services/org.gcube.informationsystem.publisher.scope.IValidatorContext";
@Before
public void init() {
loader = Thread.currentThread().getContextClassLoader();
}
@Test
public void alternativeProvidersCanBeConfigured() {
addJarsToClasspath("customValidator.jar");
List<IValidatorContext> impls=load();
assertEquals(impls.size(), 1);
}
private List<IValidatorContext> load(){
ServiceLoader<IValidatorContext> loader = ServiceLoader.load(IValidatorContext.class);
Iterator<IValidatorContext> iterator = loader.iterator();
List<IValidatorContext> impls = new ArrayList<IValidatorContext>();
while(iterator.hasNext())
impls.add(iterator.next());
System.out.println("size: "+impls.size());
if(impls.size()==1){
IValidatorContext context = impls.get(0);
for( Validator validator : context.getValidators()){
System.out.println("implementation found: "+ validator.type());
}
}
return impls;
}
private void addJarsToClasspath(String ... jars) {
List<URL> jarUrls = new ArrayList<URL>();
for (String jar : jars)
jarUrls.add(loader.getResource(jar));
URLClassLoader urlClassLoader
= new URLClassLoader(jarUrls.toArray(new URL[0]),loader);
Thread.currentThread().setContextClassLoader(urlClassLoader);
}
@After
public void teardown() {
Thread.currentThread().setContextClassLoader(loader);
}
}

View File

@ -1,22 +0,0 @@
package org.gcube.informationsystem.publisher;
import java.util.List;
import static org.junit.Assert.*;
import org.gcube.informationsystem.publisher.scope.IValidatorContext;
import org.gcube.informationsystem.publisher.scope.ScopeValidatorScanner;
import org.gcube.informationsystem.publisher.scope.Validator;
import org.junit.Test;
public class DefaultConfigurationTest {
@Test
public void testDefaultValidator(){
IValidatorContext context=ScopeValidatorScanner.provider();
List<Validator> list= context.getValidators();
assertNotNull(list);
Validator validator =list.get(0);
System.out.println("found validator: "+validator.type());
assertEquals(validator.type().toString().trim(),"class org.gcube.common.resources.gcore.Resource");
}
}

View File

@ -1,46 +0,0 @@
package org.gcube.informationsystem.publisher;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import org.apache.log4j.helpers.Loader;
import org.gcube.informationsystem.publisher.scope.IValidatorContext;
import org.gcube.informationsystem.publisher.scope.ScopeValidatorScanner;
import org.gcube.informationsystem.publisher.scope.Validator;
import org.junit.BeforeClass;
import org.junit.Test;
public class DefaultScopeValidatorTest {
static final String relativePath="/src/test/java/META-INF/services/org.gcube.informationsystem.publisher.scope.IValidatorContext";
// @BeforeClass
public static void deleteServiceInfo(){
URL url=Thread.currentThread().getClass().getResource("/");
if(url != null){
File f =new File(url.getPath());
String rootPath=f.getParentFile().getParentFile().getAbsolutePath();
System.out.println(" "+f.exists()+" path "+rootPath);
File service=new File(rootPath+relativePath);
if(service.exists()){
boolean del=service.delete();
System.out.println("deleted? "+del);
}
}
System.out.println("url founded "+url);
}
// @Test
public void testDefaultValidator(){
IValidatorContext context=ScopeValidatorScanner.provider();
List<Validator> list= context.getValidators();
for(Validator validator : list){
System.out.println("validator founded: "+validator.type());
}
}
}

View File

@ -1,64 +0,0 @@
package org.gcube.informationsystem.publisher;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import javax.annotation.Resource;
import org.gcube.informationsystem.publisher.scope.IValidatorContext;
import org.gcube.informationsystem.publisher.scope.ScopeValidatorScanner;
import org.gcube.informationsystem.publisher.scope.Validator;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
public class ScopeValidatorTest {
// static IValidatorContext context;
static final String IMPL_CLASS="org.gcube.informationsystem.scope.validator.ValidatorContextImpl";
static final String relativePath="src/test/java/META-INF/services/org.gcube.informationsystem.publisher.scope.IValidatorContext";
// @Rule
// public static TemporaryFolder testFolder = new TemporaryFolder();
public static File service;
// @BeforeClass
public static void writeServiceInfo() throws IOException{
service=new File(relativePath);
FileOutputStream file = new FileOutputStream(service);
PrintStream output = new PrintStream(file);
output.print(IMPL_CLASS);
output.flush();
output.close();
System.out.println("file writed ");
}
// @Test
public void test(){
IValidatorContext context=ScopeValidatorScanner.provider();
List<Validator> list= context.getValidators();
for(Validator validator : list){
System.out.println("validator founded: "+validator.type());
}
}
// @AfterClass
// public static void deleteServiceInfo() throws IOException{
// if(service.exists()){
// boolean del=service.delete();
// System.out.println("deleted? "+del);
//
// }
//
// }
}

View File

@ -1,33 +0,0 @@
package org.gcube.informationsystem.scope.validator;
import java.util.List;
import org.gcube.common.resources.gcore.GenericResource;
import org.gcube.common.resources.gcore.Resource;
import org.gcube.informationsystem.publisher.scope.Validator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyGenericResourceValidator<R> implements Validator<GenericResource> {
private static Logger log = LoggerFactory.getLogger(MyGenericResourceValidator.class);
@Override
public <R extends Resource> void validate(R resource) {
log.info("validate method of "+this.getClass());
}
@Override
public Class type() {
return GenericResource.class;
}
@Override
public <R extends Resource> void checkScopeCompatibility(R resource,
List<String> scopes) {
// TODO Auto-generated method stub
}
}

View File

@ -1,35 +0,0 @@
package org.gcube.informationsystem.scope.validator;
import java.util.List;
import org.gcube.common.resources.gcore.Resource;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.informationsystem.publisher.scope.Validator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyServiceEndpointValidator<R> implements Validator<ServiceEndpoint>{
private static Logger log = LoggerFactory.getLogger(MyServiceEndpointValidator.class);
@Override
public <R extends Resource> void validate(R resource) {
log.info("validate method of "+this.getClass());
}
@Override
public Class type() {
return ServiceEndpoint.class;
}
@Override
public <R extends Resource> void checkScopeCompatibility(R resource,
List<String> scopes) {
// TODO Auto-generated method stub
}
}

View File

@ -1,18 +0,0 @@
package org.gcube.informationsystem.scope.validator;
import java.util.Arrays;
import java.util.List;
import org.gcube.informationsystem.publisher.scope.IValidatorContext;
import org.gcube.informationsystem.publisher.scope.Validator;
public class ValidatorContextImpl implements IValidatorContext{
final static List<Validator> validators = Arrays.asList(new MyGenericResourceValidator(), new MyServiceEndpointValidator());
@Override
public List<Validator> getValidators() {
return validators;
}
}