From 61a5cf870095f97a3f680142c03dc866df9bd941 Mon Sep 17 00:00:00 2001 From: lucio Date: Tue, 7 Apr 2020 19:16:48 +0200 Subject: [PATCH] - Default validator Context control added --- .../publisher/RegistryPublisherImpl.java | 14 ++-- .../scope/DefaultScopeValidator.java | 30 ++------ .../publisher/scope/Validator.java | 10 ++- .../publisher/scope/ValidatorProvider.java | 19 +----- .../publisher/ConfigurationTest.java | 68 ------------------- .../publisher/DefaultConfigurationTest.java | 22 ------ .../publisher/DefaultScopeValidatorTest.java | 46 ------------- .../publisher/ScopeValidatorTest.java | 64 ----------------- .../validator/MyGenericResourceValidator.java | 33 --------- .../validator/MyServiceEndpointValidator.java | 35 ---------- .../scope/validator/ValidatorContextImpl.java | 18 ----- 11 files changed, 18 insertions(+), 341 deletions(-) delete mode 100644 src/test/java/org/gcube/informationsystem/publisher/ConfigurationTest.java delete mode 100644 src/test/java/org/gcube/informationsystem/publisher/DefaultConfigurationTest.java delete mode 100644 src/test/java/org/gcube/informationsystem/publisher/DefaultScopeValidatorTest.java delete mode 100644 src/test/java/org/gcube/informationsystem/publisher/ScopeValidatorTest.java delete mode 100644 src/test/java/org/gcube/informationsystem/scope/validator/MyGenericResourceValidator.java delete mode 100644 src/test/java/org/gcube/informationsystem/scope/validator/MyServiceEndpointValidator.java delete mode 100644 src/test/java/org/gcube/informationsystem/scope/validator/ValidatorContextImpl.java diff --git a/src/main/java/org/gcube/informationsystem/publisher/RegistryPublisherImpl.java b/src/main/java/org/gcube/informationsystem/publisher/RegistryPublisherImpl.java index b77fe6e..3b793f8 100644 --- a/src/main/java/org/gcube/informationsystem/publisher/RegistryPublisherImpl.java +++ b/src/main/java/org/gcube/informationsystem/publisher/RegistryPublisherImpl.java @@ -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; } diff --git a/src/main/java/org/gcube/informationsystem/publisher/scope/DefaultScopeValidator.java b/src/main/java/org/gcube/informationsystem/publisher/scope/DefaultScopeValidator.java index 52eaef1..41d5056 100644 --- a/src/main/java/org/gcube/informationsystem/publisher/scope/DefaultScopeValidator.java +++ b/src/main/java/org/gcube/informationsystem/publisher/scope/DefaultScopeValidator.java @@ -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 implements Validator{ - - private static Logger log = LoggerFactory.getLogger(DefaultScopeValidator.class); +public class DefaultScopeValidator implements Validator{ @Override - public void validate(R resource) { -// log.info("validate method of "+this.getClass()); -// String currentScope=ScopeProvider.instance.get(); -// ScopeGroup scopes=resource.scopes(); -// boolean founded= false; -// for(Iterator 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 void checkScopeCompatibility(R resource, + public void checkScopeCompatibility(Resource resource, List scopesList) { // for(String scope: scopesList){ // ScopeGroup scopes=resource.scopes(); diff --git a/src/main/java/org/gcube/informationsystem/publisher/scope/Validator.java b/src/main/java/org/gcube/informationsystem/publisher/scope/Validator.java index 0797eb7..d010ff0 100644 --- a/src/main/java/org/gcube/informationsystem/publisher/scope/Validator.java +++ b/src/main/java/org/gcube/informationsystem/publisher/scope/Validator.java @@ -4,11 +4,9 @@ import java.util.List; import org.gcube.common.resources.gcore.Resource; -public interface Validator { +public interface Validator { - void validate(R resource); - void checkScopeCompatibility(R resource, List scopes); - - public Class type(); - + void validate(Resource resource); + void checkScopeCompatibility(Resource resource, List scopes); + } diff --git a/src/main/java/org/gcube/informationsystem/publisher/scope/ValidatorProvider.java b/src/main/java/org/gcube/informationsystem/publisher/scope/ValidatorProvider.java index d8f9b84..f2e4436 100644 --- a/src/main/java/org/gcube/informationsystem/publisher/scope/ValidatorProvider.java +++ b/src/main/java/org/gcube/informationsystem/publisher/scope/ValidatorProvider.java @@ -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 validatorsMap= new LinkedHashMap(); - public static Validator getValidator(Resource resource){ - Validator validator=null; - if(validatorsMap.isEmpty()){ - IValidatorContext context= ScopeValidatorScanner.provider(); - List 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(); } diff --git a/src/test/java/org/gcube/informationsystem/publisher/ConfigurationTest.java b/src/test/java/org/gcube/informationsystem/publisher/ConfigurationTest.java deleted file mode 100644 index 3fc8ae9..0000000 --- a/src/test/java/org/gcube/informationsystem/publisher/ConfigurationTest.java +++ /dev/null @@ -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 impls=load(); - assertEquals(impls.size(), 1); - } - - private List load(){ - ServiceLoader loader = ServiceLoader.load(IValidatorContext.class); - Iterator iterator = loader.iterator(); - List impls = new ArrayList(); - 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 jarUrls = new ArrayList(); - 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); - } -} diff --git a/src/test/java/org/gcube/informationsystem/publisher/DefaultConfigurationTest.java b/src/test/java/org/gcube/informationsystem/publisher/DefaultConfigurationTest.java deleted file mode 100644 index ac3a6c4..0000000 --- a/src/test/java/org/gcube/informationsystem/publisher/DefaultConfigurationTest.java +++ /dev/null @@ -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 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"); - } - -} diff --git a/src/test/java/org/gcube/informationsystem/publisher/DefaultScopeValidatorTest.java b/src/test/java/org/gcube/informationsystem/publisher/DefaultScopeValidatorTest.java deleted file mode 100644 index aea19f2..0000000 --- a/src/test/java/org/gcube/informationsystem/publisher/DefaultScopeValidatorTest.java +++ /dev/null @@ -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 list= context.getValidators(); - for(Validator validator : list){ - System.out.println("validator founded: "+validator.type()); - } - } - -} diff --git a/src/test/java/org/gcube/informationsystem/publisher/ScopeValidatorTest.java b/src/test/java/org/gcube/informationsystem/publisher/ScopeValidatorTest.java deleted file mode 100644 index 083d40b..0000000 --- a/src/test/java/org/gcube/informationsystem/publisher/ScopeValidatorTest.java +++ /dev/null @@ -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 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); -// -// } -// -// } - -} diff --git a/src/test/java/org/gcube/informationsystem/scope/validator/MyGenericResourceValidator.java b/src/test/java/org/gcube/informationsystem/scope/validator/MyGenericResourceValidator.java deleted file mode 100644 index 0b06776..0000000 --- a/src/test/java/org/gcube/informationsystem/scope/validator/MyGenericResourceValidator.java +++ /dev/null @@ -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 implements Validator { - - private static Logger log = LoggerFactory.getLogger(MyGenericResourceValidator.class); - - @Override - public void validate(R resource) { - log.info("validate method of "+this.getClass()); - } - - @Override - public Class type() { - return GenericResource.class; - } - - @Override - public void checkScopeCompatibility(R resource, - List scopes) { - // TODO Auto-generated method stub - - } - - -} diff --git a/src/test/java/org/gcube/informationsystem/scope/validator/MyServiceEndpointValidator.java b/src/test/java/org/gcube/informationsystem/scope/validator/MyServiceEndpointValidator.java deleted file mode 100644 index e327e73..0000000 --- a/src/test/java/org/gcube/informationsystem/scope/validator/MyServiceEndpointValidator.java +++ /dev/null @@ -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 implements Validator{ - - private static Logger log = LoggerFactory.getLogger(MyServiceEndpointValidator.class); - - @Override - public void validate(R resource) { - log.info("validate method of "+this.getClass()); - - } - - @Override - public Class type() { - return ServiceEndpoint.class; - } - - @Override - public void checkScopeCompatibility(R resource, - List scopes) { - // TODO Auto-generated method stub - - } - - - -} diff --git a/src/test/java/org/gcube/informationsystem/scope/validator/ValidatorContextImpl.java b/src/test/java/org/gcube/informationsystem/scope/validator/ValidatorContextImpl.java deleted file mode 100644 index fb5f5bd..0000000 --- a/src/test/java/org/gcube/informationsystem/scope/validator/ValidatorContextImpl.java +++ /dev/null @@ -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 validators = Arrays.asList(new MyGenericResourceValidator(), new MyServiceEndpointValidator()); - - @Override - public List getValidators() { - return validators; - } - -}