Compare commits

...

10 Commits

@ -6,19 +6,16 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
@ -28,6 +25,12 @@
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry combineaccessrules="false" kind="src" path="/ic-client"/>
<classpathentry combineaccessrules="false" kind="src" path="/common-scope-maps"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

1
.gitignore vendored

@ -0,0 +1 @@
/target/

@ -1,5 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.7
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8

@ -21,7 +21,8 @@
<groupId>org.gcube.resources</groupId>
<artifactId>registry-publisher</artifactId>
<version>1.3.0</version>
<version>1.4.0-SNAPSHOT</version>
<dependencies>

@ -19,8 +19,9 @@ public class ResourceMediator {
public static void setScope(Resource resource, String scope){
resource.scopes().add(scope);
}
public static void removeScope(Resource resource, String scope){
resource.scopes().remove(scope);
public static void removeScope(Resource resource, String ... scopes){
for (String scope : scopes)
resource.scopes().remove(scope);
}
public static Resource cleanAllScopes(Resource resource){

@ -48,6 +48,11 @@ public class AdvancedPublisher extends AdvancedPublisherCommonUtils implements R
@Override
public <T extends Resource> T vosUpdate(T resource) {
return publisher.vosUpdate(resource);
}
@Override
public <T extends Resource> T vosRemove(T resource, List<String> scopes) {
return publisher.vosRemove(resource, scopes);
}

@ -48,5 +48,16 @@ public interface RegistryPublisher {
* @return the resource without the current scope if the remove operation has been successfully completed
*/
<T extends Resource> T remove(T resource);
/**
* The resource will be removed from the list of scopes.
* if the scope is the last scope in the resource, the profile will be deleted from IS else:
* if it is a VRE scope then the profile will be updated without the VRE scope;
* if it is a VO scope but there is another VRE scope, belong to the VO, defined in the resource then throw IllegalArgumentException;
* if it is a INFRA scope but there is another VRE or VO scope , belong to the INFRA, defined in the resource then throw IllegalArgumentException.
* @throws IllegalArgumentException if the current scope is not defined in the resource or if there is another VRE scope defined in the resource
* @return the resource without the current scope if the remove operation has been successfully completed
*/
<T extends Resource> T vosRemove(T resource, List<String> scopes);
}

@ -1,12 +1,12 @@
package org.gcube.informationsystem.publisher;
import java.io.StringWriter;
import java.util.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.xml.ws.soap.SOAPFaultException;
@ -24,6 +24,7 @@ import org.gcube.informationsystem.publisher.scope.ValidatorProvider;
import org.gcube.informationsystem.publisher.stubs.registry.RegistryStub;
import org.gcube.informationsystem.publisher.stubs.registry.faults.CreateException;
import org.gcube.informationsystem.publisher.stubs.registry.faults.InvalidResourceException;
import org.gcube.informationsystem.publisher.stubs.registry.faults.ResourceDoesNotExistException;
import org.gcube.informationsystem.publisher.stubs.registry.faults.ResourceNotAcceptedException;
import org.gcube.informationsystem.publisher.stubs.registry.faults.UpdateException;
import org.gcube.informationsystem.publisher.utils.RegistryStubs;
@ -88,7 +89,6 @@ public class RegistryPublisherImpl implements RegistryPublisher {
log.debug("id generated: "+id);
ResourceMediator.setId(resource, id);
}
HashSet<String> vosScopes = updateResourceScopes(resource);
// add the new scopes
for(String scope: scopes){
log.debug("[VOCREATE] new scope added {}",scope);
@ -101,19 +101,20 @@ public class RegistryPublisherImpl implements RegistryPublisher {
log.error("the resource is not valid", e);
throw new IllegalArgumentException("the resource is not valid ", e.getCause());
}
HashSet<String> vosScopes = updateResourceScopes(resource);
try{
if(currentScope != null){
// checking the current scope: if the operation fails in the current VO it will give an exception, if it fails in another VO no exception will be given
String currentVO = Utils.getCurrentVO(currentScope);
if (currentVO != null){
RegistryStub stub = getRegistryStub();
createResource(resource, currentVO, stub);
vosScopes.remove(currentVO);
//in this case it is a root-vo scope so we need to create the resource only at root-vo level
createResource(resource, currentVO, stub);
vosScopes.remove(currentVO);
//in this case it is a root-vo scope so we need to create the resource only at root-vo level
}else{
RegistryStub stub = getRegistryStub();
createResource(resource, currentVO, stub);
return resource;
createResource(resource, currentVO, stub);
return resource;
}
}
// update the resource for each VO
@ -232,17 +233,17 @@ public class RegistryPublisherImpl implements RegistryPublisher {
HashSet<String> vosScopes = Utils.getInternalVOScopes(resource);
try{
if(currentScope != null){
// checking the current scope: if the operation fail in the current VO it will give an exception, if it fails in another VO no exception will be given
// checking the current scope: if the operation fail in the current VO it will raise an exception, if it fails in another VO no exception will be raised
String currentVO = Utils.getCurrentVO(currentScope);
if (currentVO != null){
ScopeProvider.instance.set(currentVO);
registryUpdate(resource, 0);
vosScopes.remove(currentVO);
// in this case it is a root-vo scope so we need to update the resource only at root-vo level
// in this case it is a root-vo scope
}else{
ScopeProvider.instance.set(currentScope);
registryUpdate(resource, 0);
return resource;
vosScopes.remove(currentScope);
}
}
@ -274,6 +275,66 @@ public class RegistryPublisherImpl implements RegistryPublisher {
return resource;
}
/**
* The resource will be removed from all the scopes
* if the scope is the last scope in the resource, the profile will be deleted from IS else
* if it is a VRE scope then the profile will be updated without the VRE scope,
* if it is a VO scope but there is another VRE scope, belong to the VO, defined in the resource then throw IllegalArgumentException
*
* @throws IllegalArgumentException if no service endpoints can be discovered or if there is another VRE scope defined in the resource
*/
public <T extends Resource> T vosRemove(T resource, List<String> scopes){
String currentScope=ScopeProvider.instance.get();
log.info(" remove resource with id : {} from scope: {}",resource.id(),currentScope);
ValidationUtils.valid("resource", resource);// helper that throws an IllegalArgumentException if resource are null
ValidationUtils.valid("scopes", currentScope);// helper that throws an IllegalArgumentException if scopes are null
validateScope(resource);
// the returned voScopes list is not used yet. it should be used if the update/remove operation will be done at VO level
// HashSet<String> vosScopes = updateResourceScopes(resource);
updateResourceScopes(resource);
try {
Resources.validate(resource);
} catch (Exception e) {
log.error("the resource is not valid", e);
throw new IllegalArgumentException("the resource is not valid", e);
}
log.info(" remove {} scope from resource {}",scopes,resource.id());
Set<String> voScopesBeforeRemove =Utils.getInternalVOScopes(resource);
ResourceMediator.removeScope(resource, scopes.toArray(new String[scopes.size()]));
Set<String> voScopesAfterRemove =Utils.getInternalVOScopes(resource);
voScopesBeforeRemove.removeAll(voScopesAfterRemove);
for (String scope: voScopesBeforeRemove)
try {
ScopeProvider.instance.set(scope);
log.info("remove from IS scope {}",currentScope);
registry.getStubs().remove(resource.id(), resource.type().toString());
} catch (ResourceDoesNotExistException e) {
log.warn("resource not found in the current scope, continue");
} catch (Exception e) {
log.error("the resource can't be removed ", e);
throw new IllegalArgumentException("the resource can't be removed from scope "+currentScope, e);
} finally {
ScopeProvider.instance.set(currentScope);
}
// retrieves the scopes on resource and update it
// updateResource(resource, currentScope);
if (!voScopesAfterRemove.isEmpty())
try{
vosUpdate(resource);
}catch(Exception e){
log.error("exception message: "+e.getMessage());
throw new RuntimeException(e.getMessage());
}
return resource;
}
/**
* The resource will be removed from current scope
* if the scope is the last scope in the resource, the profile will be deleted from IS else
@ -284,12 +345,12 @@ public class RegistryPublisherImpl implements RegistryPublisher {
*/
public <T extends Resource> T remove(T resource){
String currentScope=ScopeProvider.instance.get();
log.info(" remove resource with id : "+resource.id()+" from scope: "+currentScope);
log.info(" remove resource with id : {} from scope: {}",resource.id(),currentScope);
ValidationUtils.valid("resource", resource);// helper that throws an IllegalArgumentException if resource are null
ValidationUtils.valid("scopes", currentScope);// helper that throws an IllegalArgumentException if scopes are null
validateScope(resource);
// the returned voScopes list is not used yet. it should be used if the update/remove operation will be done at VO level
// HashSet<String> vosScopes = updateResourceScopes(resource);
// the returned voScopes list is not used yet. it should be used if the update/remove operation will be done at VO level
// HashSet<String> vosScopes = updateResourceScopes(resource);
updateResourceScopes(resource);
try {
Resources.validate(resource);
@ -297,7 +358,7 @@ public class RegistryPublisherImpl implements RegistryPublisher {
log.error("the resource is not valid", e);
throw new IllegalArgumentException("the resource is not valid", e);
}
log.info(" remove "+currentScope+" scope from resource "+resource.id());
log.info(" remove {} scope from resource {}",currentScope,resource.id());
ResourceMediator.removeScope(resource, currentScope);
// retrieves the scopes on resource and update it
// updateResource(resource, currentScope);
@ -312,6 +373,7 @@ public class RegistryPublisherImpl implements RegistryPublisher {
}
private void registryUpdate(Resource resource, int tries){
log.trace("try to update resource with id: "+resource.id()+" times "+(tries+1)+" on scope: "+ScopeProvider.instance.get());
try{
@ -358,14 +420,14 @@ public class RegistryPublisherImpl implements RegistryPublisher {
*/
private <T extends Resource> void updateResourceRemoveOperation(T resource, String currentScope) {
if(!isRemoveNeeded(resource, currentScope)){
// updateResource(resource, currentScope);
// v 1.3 (20190902) try to update the resource just at vo level
// updateResource(resource, currentScope);
// v 1.3 (20190902) try to update the resource just at vo level
vosUpdate(resource);
}else{ // remove the profile from IC
log.info("the resource have only the "+currentScope+" scope defined. Remove the resource "+resource.id()+" from IC");
log.info("the resource have only the {} scope defined. Remove the resource {} from IC", currentScope,resource.id());
// if the resource hasn't any scope, the resource will be removed
try {
log.debug("remove from IS scope "+currentScope);
log.info("remove from IS scope {}",currentScope);
registry.getStubs().remove(resource.id(), resource.type().toString());
} catch (Exception e) {
log.error("the resource can't be removed ", e);
@ -429,8 +491,7 @@ public class RegistryPublisherImpl implements RegistryPublisher {
ScopeProvider.instance.set(currentScope);
}
}
/*
private <T extends Resource> void updateResourceVOLevel(T resource, List <String> voScopes, String currentScope) {
int tries=0;
try{
@ -443,14 +504,17 @@ public class RegistryPublisherImpl implements RegistryPublisher {
ScopeProvider.instance.set(currentScope);
}
}
*/
private <T extends Resource> HashSet<String> updateResourceScopes(T resource) {
HashSet<String> vosScopes = Utils.getInternalVOScopes(resource);
// if the resource type is a RunningInstance or a HostingNode the check on the most recent resource is bypassed
if((resource.type().toString().equalsIgnoreCase("RunningInstance")) || (resource.type().toString().equalsIgnoreCase("GHN")) || (resource.type().toString().equalsIgnoreCase("GCoreEndpoint")) || (resource.type().toString().equalsIgnoreCase("HostingNode")))
return vosScopes;
//extract the scopes from the more recent resource found at vo level
List <String> latestScopesFound= Utils.setLatestInternalScopes(resource, vosScopes);
log.debug("latest scope found are "+latestScopesFound);
if((latestScopesFound == null) || (latestScopesFound.isEmpty())){
// if it is a new resource the latestScopesFound should be null, in this case the more recent scopes are the new scopes
// if it is a new resource the latestScopesFound should be null, in this case the more recent scopes are the new scopes
latestScopesFound= new ArrayList<String>(vosScopes.size());
for (String scope: vosScopes){
latestScopesFound.add(scope);
@ -463,7 +527,7 @@ public class RegistryPublisherImpl implements RegistryPublisher {
log.debug("[VOCREATE] scope added {}",scope);
ResourceMediator.setScope(resource, scope);
ScopeBean scopeBean = new ScopeBean(scope);
// check if the scope was already present inside the resource, if not, it will be added to the voScopes
// check if the scope was already present inside the resource, if not, it will be added to the voScopes
if((!scopeBean.is(Type.VRE)) && (!vosScopes.contains(scope))){
vosScopes.add(scope);
}

@ -47,13 +47,7 @@ public class ScopedPublisherImpl implements ScopedPublisher{
@Override
public <T extends Resource> T remove(T resource, List<String> scopes) throws RegistryNotFoundException{
String currentScope=ScopeProvider.instance.get();
for(String scope : scopes){
ScopeProvider.instance.set(scope);
registryPublisher.remove(resource);
}
ScopeProvider.instance.set(currentScope);
return resource;
return registryPublisher.vosRemove(resource, scopes);
}
}

@ -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");
}
@Override
public Class type() {
return Resource.class;
public void validate(Resource resource) {
if (resource.scopes().isEmpty())
throw new IllegalArgumentException("scopes in the resource are empty");
}
@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();

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

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

@ -0,0 +1,121 @@
package org.gcube.informationsystem.publisher.stubs.registry;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.publisher.stubs.registry.faults.CreateException;
import org.gcube.informationsystem.publisher.stubs.registry.faults.InvalidResourceException;
import org.gcube.informationsystem.publisher.stubs.registry.faults.RemoveException;
import org.gcube.informationsystem.publisher.stubs.registry.faults.ResourceDoesNotExistException;
import org.gcube.informationsystem.publisher.stubs.registry.faults.ResourceNotAcceptedException;
import org.gcube.informationsystem.publisher.stubs.registry.faults.UpdateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CollectorStubs implements RegistryStub {
private static Logger log = LoggerFactory.getLogger(CollectorStubs.class);
public static final String TOKEN_HEADER_ENTRY = "gcube-token";
public static final String SCOPE_HEADER_ENTRY = "gcube-scope";
private String endopoint;
public CollectorStubs(String endopoint) {
super();
this.endopoint = endopoint;
}
@Override
public void create(String profile, String type)
throws InvalidResourceException, ResourceNotAcceptedException, CreateException {
StringBuilder callUrl = new StringBuilder(endopoint).append("/").append(type);
try {
URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-type", "text/xml");
try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){
os.write(profile.getBytes());
}
if (connection.getResponseCode()!=200) throw new CreateException("error creating resource "+connection.getResponseCode());
}catch (Exception e) {
log.error("error on create",e);
throw new CreateException(e.getMessage());
}
}
@Override
public void update(String id, String type, String profile)
throws InvalidResourceException, ResourceNotAcceptedException, UpdateException {
StringBuilder callUrl = new StringBuilder(endopoint).append("/").append(type).append("/").append(id);
try {
URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "PUT");
connection.setDoOutput(true);
connection.setRequestProperty("Content-type", "text/xml");
try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){
os.write(profile.getBytes());
}
if (connection.getResponseCode()!=200) throw new UpdateException("error updating resource "+connection.getResponseCode());
}catch (Exception e) {
log.error("error on update",e);
throw new UpdateException(e.getMessage());
}
}
@Override
public void remove(String id, String type) throws ResourceDoesNotExistException, RemoveException {
StringBuilder callUrl = new StringBuilder(endopoint).append("/").append(type).append("/").append(id);
try {
URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "DELETE");
connection.setDoInput(true);
if (connection.getResponseCode()!=200){
log.info("response code is not 200");
if (connection.getResponseCode()==404)
throw new ResourceDoesNotExistException("resource with id "+id+" not found");
else throw new RemoveException("generic error removing resource "+id);
}
}catch (ResourceDoesNotExistException | RemoveException e) {
throw e;
}catch (Exception e1) {
throw new RemoveException("connection error removing resource");
}
}
private HttpURLConnection makeRequest(URL url, String method) throws Exception{
HttpURLConnection connection;
if (url.toString().startsWith("https://"))
connection = (HttpsURLConnection)url.openConnection();
else connection = (HttpURLConnection)url.openConnection();
if (ScopeProvider.instance.get()!=null)
connection.setRequestProperty(SCOPE_HEADER_ENTRY,ScopeProvider.instance.get());
else if (SecurityTokenProvider.instance.get()!=null)
connection.setRequestProperty(TOKEN_HEADER_ENTRY,SecurityTokenProvider.instance.get());
else throw new RuntimeException("Collector requires authorization (via token or scope)");
connection.setRequestMethod(method);
return connection;
}
}

@ -2,11 +2,15 @@ package org.gcube.informationsystem.publisher.stubs.registry;
import static org.gcube.informationsystem.publisher.stubs.registry.RegistryConstants.*;
import java.io.StringWriter;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import org.gcube.common.resources.gcore.Resource;
import org.gcube.common.resources.gcore.Resources;
import org.gcube.informationsystem.publisher.stubs.registry.faults.CreateException;
import org.gcube.informationsystem.publisher.stubs.registry.faults.InvalidResourceException;
import org.gcube.informationsystem.publisher.stubs.registry.faults.RemoveException;
@ -35,6 +39,7 @@ public interface RegistryStub {
@WebResult()
void create(@WebParam(name="profile") String profile, @WebParam(name="type") String type ) throws InvalidResourceException,
ResourceNotAcceptedException, CreateException;
/**
*
* @param id the id of the resource to update
@ -48,7 +53,8 @@ public interface RegistryStub {
@WebMethod(operationName="update")
@WebResult()
void update(@WebParam(name="uniqueID") String id, @WebParam(name="type") String type, @WebParam(name="xmlProfile") String profile ) throws InvalidResourceException,
ResourceNotAcceptedException, UpdateException;
ResourceNotAcceptedException, UpdateException;
/**
*
* @param id the id of the resource to remove
@ -60,7 +66,6 @@ public interface RegistryStub {
@WebMethod(operationName="remove")
@WebResult()
void remove(@WebParam(name="uniqueID") String id, @WebParam(name="type") String type) throws ResourceDoesNotExistException,
RemoveException;
RemoveException;
}

@ -8,8 +8,10 @@ import java.util.List;
import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.api.ServiceMap;
import org.gcube.informationsystem.publisher.cache.RegistryCache;
import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException;
import org.gcube.informationsystem.publisher.stubs.registry.CollectorStubs;
import org.gcube.informationsystem.publisher.stubs.registry.RegistryConstants;
import org.gcube.informationsystem.publisher.stubs.registry.RegistryStub;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
@ -21,16 +23,17 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RegistryStubs {
private RegistryCache cache = new RegistryCache(10);
private List<URI> endpoints;
private static final Logger log = LoggerFactory.getLogger(RegistryStubs.class);
private static final String XMLSTOREACCESS_SERVICE ="XMLStoreService";
public List<URI> getEndPoints(){
String scope=ScopeProvider.instance.get();
// able/disable cache
// able/disable cache
endpoints=(List<URI>)cache.get(scope);
if(endpoints==null){
SimpleQuery query = queryFor(GCoreEndpoint.class);
@ -40,32 +43,47 @@ public class RegistryStubs {
public URI parse(String result) throws Exception {
return new URI(result.replaceAll("\n", ""));
}
};
DiscoveryClient<URI> client = new DelegateClient<URI>(uriParser, new ICClient());
query.addCondition("$resource/Profile/ServiceClass/text() eq '"+RegistryConstants.service_class+"'")
.addCondition("$resource/Profile/ServiceName/text() eq '"+RegistryConstants.service_name+"'")
.setResult("$resource/Profile/AccessPoint/RunningInstanceInterfaces/Endpoint[string(@EntryName) eq '"+RegistryConstants.service_entrypoint+"']/string()");
.addCondition("$resource/Profile/ServiceName/text() eq '"+RegistryConstants.service_name+"'")
.setResult("$resource/Profile/AccessPoint/RunningInstanceInterfaces/Endpoint[string(@EntryName) eq '"+RegistryConstants.service_entrypoint+"']/string()");
endpoints = client.submit(query);
if (endpoints.size()==0){
throw new IllegalArgumentException("No registry endpoint founded");
throw new IllegalArgumentException("No registry endpoint found");
}
// able/disable cache
// able/disable cache
cache.put(scope, endpoints);
}
return endpoints;
}
public RegistryStub getStubs() throws RegistryNotFoundException{
URI endpoint=null;
//use another method to cache epr
endpoint = getEndPoints().get(0);
log.debug("get stubs from endpoint: "+ endpoint);
return stubFor(RegistryConstants.registry).at(endpoint);
ServiceMap serviceMap = ServiceMap.instance;
if (serviceMap!=null && serviceMap.version().equals("2.0.0")) {
try {
@SuppressWarnings("unchecked")
Class<? extends RegistryStub> localClientclass =(Class<? extends RegistryStub>) Class.forName("org.gcube.informationsystem.collector.client.LocalPublisherClient", true, Thread.currentThread().getContextClassLoader());
log.info("using LocalPublisherClient, information collector specific publisher");
return localClientclass.newInstance();
}catch (Exception e) {
String endpoint = serviceMap.endpoint(XMLSTOREACCESS_SERVICE);
log.info("using REST COLLECTOR");
return new CollectorStubs(endpoint);
}
} else {
//use another method to cache epr
URI endpoint = getEndPoints().get(0);
log.info("getting REGISTRY STUBS stubs from endpoint: {}",endpoint);
return stubFor(RegistryConstants.registry).at(endpoint);
}
}
public RegistryStub getStubs(URI endpoint) throws RegistryNotFoundException{
log.debug("get stubs from endpoint: "+ endpoint);
log.debug("get stubs from endpoint: {}", endpoint);
return stubFor(RegistryConstants.registry).at(endpoint);
}

@ -42,13 +42,11 @@ public class Utils {
*/
public static String getCurrentVO(String currentScope) {
ScopeBean scopeBean = new ScopeBean(currentScope);
String currentVO=null;
if(scopeBean.is(Type.VRE))
currentVO=scopeBean.enclosingScope().toString();
return scopeBean.enclosingScope().toString();
else if (scopeBean.is(Type.VO))
currentVO= currentScope;
return currentScope;
else return null;
return currentVO;
}
/**
@ -65,6 +63,7 @@ public class Utils {
ScopeBean scopeBean = new ScopeBean(scope);
if(scopeBean.is(Type.VRE))
vosScopes.add(scopeBean.enclosingScope().toString());
else
// if the scope is a root-vo scope, it will be added to the vosScope array
vosScopes.add(scope);
}
@ -154,6 +153,7 @@ public class Utils {
private static <T extends Resource> List<String> extractInternalScopes(T resource, String latestVO) {
T extractedResource=null;
log.debug("checking resource "+resource.id()+" type: "+resource.type());
if(resource.type().toString().equalsIgnoreCase("RuntimeResource")){
extractedResource= (T)getServiceEndpointByID(resource.id(), latestVO);
}else if(resource.type().toString().equalsIgnoreCase("GenericResource")){

@ -74,7 +74,7 @@ public class ValidationUtils {
}
public static <T extends Resource> boolean isCompatibleScopeForRemove(T resource, String scope){
log.info("scope: "+scope+" check if update is needed inr resource: "+resource.id());
log.info("scope: {} check if update is needed inr resource: {}", scope,resource.id());
if(resource.scopes().size() == 0)
return true;
if(new ScopeBean(scope).is(Type.VRE)){
@ -86,11 +86,11 @@ public class ValidationUtils {
}else if(new ScopeBean(scope).is(Type.VO)){
log.debug(" "+scope+" is a VO scope");
if(anotherSonVREOnResource(resource, scope)){
throw new IllegalArgumentException("the resource "+resource.id()+" have another scope defined in the same VO. The VO is "+scope);
return false; //throw new IllegalArgumentException("the resource "+resource.id()+" have another scope defined in the same VO. The VO is "+scope);
}else return true;
}else{ // is a INFRA scope
if(anotherInfraScopeOnResource(resource, scope)){
throw new IllegalArgumentException("the resource "+resource.id()+" have another scope defined in the same INFRA. The INFRA is "+scope);
return false; //throw new IllegalArgumentException("the resource "+resource.id()+" have another scope defined in the same INFRA. The INFRA is "+scope);
}else return true;
}
}

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

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

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

@ -2,6 +2,8 @@ package org.gcube.informationsystem.publisher;
import static org.gcube.common.resources.gcore.Resources.print;
import static org.junit.Assert.*;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.common.resources.gcore.Resource;
import org.gcube.common.resources.gcore.Resource.Type;
@ -14,38 +16,39 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class GCoreEndpointPublisherTests {
private static final Logger log = LoggerFactory.getLogger(RegistryPublisherTests.class);
static GCoreEndpoint running;
static RegistryPublisher rp;
static Resource r;
@BeforeClass
public static void init(){
// ScopeProvider.instance.set("/d4science.research-infrastructures.eu/EUBrazilOpenBio");
ScopeProvider.instance.set("/gcube/devsec");
running = Resources.unmarshal(GCoreEndpoint.class, PublisherTest.class.getClassLoader().getResourceAsStream("gCoreEndpoint.xml"));
// ScopeProvider.instance.set("/d4science.research-infrastructures.eu/EUBrazilOpenBio");
ScopeProvider.instance.set("/gcube/devNext");
SecurityTokenProvider.instance.set("52b59669-ccde-46d2-a4da-108b9e941f7c-98187548");
rp=RegistryPublisherFactory.create();
}
@Test
public void printTest(){
print(running);
//resource-specific tests
//print(running);
//resource-specific tests
assertEquals(Type.GCOREENDPOINT,running.type());
}
@Test
public void registerCreate(){
r=rp.create(running);
System.out.println("new resource created: ");
if(r!=null)
print(r);
assertEquals(running,r);
for (int i = 0; i<10; i++) {
running = Resources.unmarshal(GCoreEndpoint.class, PublisherTest.class.getClassLoader().getResourceAsStream("gCoreEndpoint.xml"));
r=rp.create(running);
System.out.println("new resource created: "+r.id());
}
}
@AfterClass
/*@AfterClass
public static void forceDeleteResource(){
try {
Thread.sleep(3000);
@ -59,8 +62,8 @@ public class GCoreEndpointPublisherTests {
advancedPublisher.forceRemove(r);
ScopeProvider.instance.set(currentScope);
}
}*/
}

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

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

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

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

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Resource version="0.4.x">
<ID>5d244ab0-9d79-11e3-af18-dd5904b11dd6</ID>
<ID></ID>
<Type>RunningInstance</Type>
<Scopes>
</Scopes>

Loading…
Cancel
Save