Compare commits

...

9 Commits

Author SHA1 Message Date
lucio 6164f5ef4b added dep to jaxb-impl 2024-05-15 16:53:45 +02:00
lucio 04eac59249 moving to jakarta 2024-05-15 16:48:41 +02:00
lucio 47f22985a0 returning to java8 2024-02-23 12:34:09 +01:00
lucio fba448a7f2 moved to new gcube-security 2024-02-21 20:29:54 +01:00
lucio f9f535d252 changelog updated 2024-01-19 22:47:08 +01:00
lucio 2d1e81c50e maps scanned with reflection 2024-01-19 22:46:03 +01:00
Lucio Lelii ba2f8eb081 update 2022-07-26 15:13:08 +02:00
Lucio Lelii 005ab73a33 funding file added 2022-06-15 17:55:44 +02:00
Lucio Lelii 830ed744e3 porting to smartgears 4 2022-06-10 17:10:26 +02:00
24 changed files with 202 additions and 594 deletions

View File

@ -6,6 +6,12 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> <classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes> <attributes>
<attribute name="optional" value="true"/> <attribute name="optional" value="true"/>
@ -17,6 +23,7 @@
<attributes> <attributes>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/> <attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">

1
.gitignore vendored Normal file
View File

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

View File

@ -10,6 +10,16 @@
<arguments> <arguments>
</arguments> </arguments>
</buildCommand> </buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand> <buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name> <name>org.eclipse.m2e.core.maven2Builder</name>
<arguments> <arguments>
@ -19,5 +29,6 @@
<natures> <natures>
<nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature> <nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures> </natures>
</projectDescription> </projectDescription>

View File

@ -0,0 +1,5 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8

View File

@ -0,0 +1,8 @@
eclipse.preferences.version=1
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.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8

View File

@ -2,7 +2,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
# Changelog for common scope # Changelog for common scope
## [v1.3.1-SNAPSHOT] - 2022-06-06 ## [v2.0.0-SNAPSHOT] - 2022-06-06
- deprecated ScopePorvider and ScopeBean - maps scanned with reflection
- dependency to common-configuration-scanner removed
- removed ScopePorvider and ScopeBean

26
FUNDING.md Normal file
View File

@ -0,0 +1,26 @@
# Acknowledgments
The projects leading to this software have received funding from a series of European Union programmes including:
- the Sixth Framework Programme for Research and Technological Development
- [DILIGENT](https://cordis.europa.eu/project/id/004260) (grant no. 004260).
- the Seventh Framework Programme for research, technological development and demonstration
- [D4Science](https://cordis.europa.eu/project/id/212488) (grant no. 212488);
- [D4Science-II](https://cordis.europa.eu/project/id/239019) (grant no.239019);
- [ENVRI](https://cordis.europa.eu/project/id/283465) (grant no. 283465);
- [iMarine](https://cordis.europa.eu/project/id/283644) (grant no. 283644);
- [EUBrazilOpenBio](https://cordis.europa.eu/project/id/288754) (grant no. 288754).
- the H2020 research and innovation programme
- [SoBigData](https://cordis.europa.eu/project/id/654024) (grant no. 654024);
- [PARTHENOS](https://cordis.europa.eu/project/id/654119) (grant no. 654119);
- [EGI-Engage](https://cordis.europa.eu/project/id/654142) (grant no. 654142);
- [ENVRI PLUS](https://cordis.europa.eu/project/id/654182) (grant no. 654182);
- [BlueBRIDGE](https://cordis.europa.eu/project/id/675680) (grant no. 675680);
- [PerformFISH](https://cordis.europa.eu/project/id/727610) (grant no. 727610);
- [AGINFRA PLUS](https://cordis.europa.eu/project/id/731001) (grant no. 731001);
- [DESIRA](https://cordis.europa.eu/project/id/818194) (grant no. 818194);
- [ARIADNEplus](https://cordis.europa.eu/project/id/823914) (grant no. 823914);
- [RISIS 2](https://cordis.europa.eu/project/id/824091) (grant no. 824091);
- [EOSC-Pillar](https://cordis.europa.eu/project/id/857650) (grant no. 857650);
- [Blue Cloud](https://cordis.europa.eu/project/id/862409) (grant no. 862409);
- [SoBigData-PlusPlus](https://cordis.europa.eu/project/id/871042) (grant no. 871042);

63
pom.xml
View File

@ -1,73 +1,81 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<artifactId>maven-parent</artifactId> <artifactId>maven-parent</artifactId>
<groupId>org.gcube.tools</groupId> <groupId>org.gcube.tools</groupId>
<version>1.1.0</version> <version>1.2.0</version>
<relativePath /> <relativePath />
</parent> </parent>
<groupId>org.gcube.core</groupId> <groupId>org.gcube.core</groupId>
<artifactId>common-scope</artifactId> <artifactId>common-scope</artifactId>
<version>1.3.1-SNAPSHOT</version> <version>2.0.0-SNAPSHOT</version>
<name>Common Scope</name> <name>Common Scope</name>
<description>Scope-related APIs</description> <description>Scope-related APIs</description>
<properties> <properties>
<distroDirectory>distro</distroDirectory> <distroDirectory>distro</distroDirectory>
</properties> </properties>
<scm> <scm>
<connection>scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</connection> <connection>
<developerConnection>scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</developerConnection> scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</connection>
<url>https://code-repo.d4science.org/gCubeSystem/${project.artifactId}</url> <developerConnection>
scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</developerConnection>
<url>https://code-repo.d4science.org/gCubeSystem/${project.artifactId}</url>
</scm> </scm>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-bom</artifactId>
<version>3.0.1-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.gcube.core</groupId> <groupId>org.reflections</groupId>
<artifactId>common-configuration-scanner</artifactId> <artifactId>reflections</artifactId>
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.core</groupId> <groupId>org.gcube.core</groupId>
<artifactId>common-scope-maps</artifactId> <artifactId>common-scope-maps</artifactId>
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>common-security</artifactId>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.11</version> <version>4.11</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId> <artifactId>slf4j-simple</artifactId>
<version>1.7.2</version> <version>1.7.2</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId> <artifactId>maven-resources-plugin</artifactId>
@ -94,7 +102,6 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId> <artifactId>maven-assembly-plugin</artifactId>
@ -113,8 +120,6 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@ -1,35 +0,0 @@
package org.gcube.common.scope.api;
import org.gcube.common.scope.impl.ScopeProviderScanner;
/**
* Provides a scope in the caller's context.
*
* @author Fabio Simeoni
*
*/
@Deprecated
public interface ScopeProvider {
/**
* Shared {@link ScopeProvider}.
*/
public static final ScopeProvider instance = ScopeProviderScanner.provider();
/**
* Returns the scope in the caller's context.
* @return the scope
*/
String get();
/**
* Sets the scope in the caller's context.
* @param scope the scope
*/
void set(String scope);
/**
* Resets the scope in the caller's context.
*/
void reset();
}

View File

@ -1,51 +0,0 @@
package org.gcube.common.scope.impl;
import org.gcube.common.scope.api.ScopeProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A {@link ScopeProvider} that uses threads as contexts.
* <p>
* Relies an an internal {@link InheritableThreadLocal}.
*
* @author Fabio Simeoni
* @see ScopeProviderScanner
*
*/
public class DefaultScopeProvider implements ScopeProvider {
/** System property for scope */
public static final String SCOPE_PROPERTY = "gcube.scope";
private static Logger log = LoggerFactory.getLogger(DefaultScopeProvider.class);
private InheritableThreadLocal<String> scopes = new InheritableThreadLocal<String>();
protected DefaultScopeProvider() {};
@Override
public String get() {
String scope = scopes.get();
if (scope==null)
scope = System.getProperty(SCOPE_PROPERTY);
return scope;
}
@Override
public void set(String scope) {
if (scope!=null)
log.debug("setting scope {} in thread {}",scope,Thread.currentThread().getId());
scopes.set(scope);
}
@Override
public void reset() {
log.debug("resetting scope in thread {}",Thread.currentThread().getId());
scopes.remove();
}
}

View File

@ -3,14 +3,14 @@ package org.gcube.common.scope.impl;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.gcube.common.scope.api.ServiceMap; import org.gcube.common.scope.api.ServiceMap;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
/** /**
* A {@link ServiceMap} with a standard XML binding. * A {@link ServiceMap} with a standard XML binding.
* *

View File

@ -1,140 +0,0 @@
package org.gcube.common.scope.impl;
/**
* A object model of a scope.
*
* @author Lucio Lelii
*
*/
@Deprecated
public class ScopeBean {
/**
* Scope separators used in linear syntax.
*/
protected static String separator = "/";
/**
* Scope types *
*/
public static enum Type implements Comparable<Type> {VRE,VO,INFRASTRUCTURE}
/**
* The name of the scope.
*/
private String name;
/**
* The type of the scope.
*/
private Type type;
/**
* The enclosing scope, if any.
*/
private ScopeBean enclosingScope;
/**
* Returns the name of the scope.
* @return the name
*/
public String name() {
return name;
}
/**
* Returns <code>true</code> if the scope has a given {@link Type}.
* @param type the type
* @return <code>true</code> if the scope has the given type, <code>false</code> otherwise
*/
public boolean is(Type type) {
return this.type.equals(type);
}
/**
* Returns the {@link Type} of the scope.
* @return the type
*/
public Type type() {
return type;
}
/**
* Returns the enclosing scope, if any.
* @return the enclosing scope, or <code>null</code> if the scope is top-level
*/
public ScopeBean enclosingScope() {
return enclosingScope;
}
public ScopeBean(String scope) throws IllegalArgumentException {
String[] components=scope.split(separator);
if (components.length<2 || components.length>4)
throw new IllegalArgumentException("scope "+scope+" is malformed");
if(components.length>3) {
this.name=components[3];
this.enclosingScope = new ScopeBean(separator+components[1]+separator+components[2]);
this.type=Type.VRE;
}
else if (components.length>2) {
this.name=components[2];
this.enclosingScope=new ScopeBean(separator+components[1]);
this.type=Type.VO;
}
else {
this.name=components[1];
this.type=Type.INFRASTRUCTURE;
}
}
/**
* Returns the linear expression of the scope.
*/
public String toString() {
return is(Type.INFRASTRUCTURE)?separator+name():
enclosingScope().toString()+separator+name();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((enclosingScope == null) ? 0 : enclosingScope.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ScopeBean other = (ScopeBean) obj;
if (enclosingScope == null) {
if (other.enclosingScope != null)
return false;
} else if (!enclosingScope.equals(other.enclosingScope))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (type != other.type)
return false;
return true;
}
}

View File

@ -1,62 +0,0 @@
package org.gcube.common.scope.impl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;
import org.gcube.common.scope.api.ScopeProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Scans the classpath for a {@link ScopeProvider} implementation.
* <p>
* Returns a shared {@link DefaultScopeProvider} by default.
*
* @author Fabio Simeoni
* @see ScopeProvider
* @see ScopeProvider#instance
*/
public class ScopeProviderScanner {
private static Logger log = LoggerFactory.getLogger(ScopeProviderScanner.class);
/**
* Returns the configured provider.
* @return the provider
*/
public static ScopeProvider provider() {
try {
ScopeProvider impl = null;
ServiceLoader<ScopeProvider> loader = ServiceLoader.load(ScopeProvider.class);
Iterator<ScopeProvider> iterator = loader.iterator();
List<ScopeProvider> impls = new ArrayList<ScopeProvider>();
while(iterator.hasNext())
impls.add(iterator.next());
if (impls.size()==0) {
impl = new DefaultScopeProvider();
}
else if (impls.size()>1)
throw new Exception("mis-configured environment: detected multiple default providers "+impls);
else
impl=impls.get(0);
log.info("using scope provider "+impl);
return impl;
} catch (Exception e) {
throw new RuntimeException("could not configure scope provider", e);
}
}
}

View File

@ -1,11 +1,11 @@
package org.gcube.common.scope.impl; package org.gcube.common.scope.impl;
import static org.gcube.common.scope.impl.ScopeBean.Type.*;
import java.util.Map; import java.util.Map;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.api.ServiceMap; import org.gcube.common.scope.api.ServiceMap;
import org.gcube.common.security.ContextBean;
import org.gcube.common.security.ContextBean.Type;
import org.gcube.common.security.providers.SecretManagerProvider;
/** /**
* A {@link ServiceMap} that forwards requests to {@link ServiceMap}s associated * A {@link ServiceMap} that forwards requests to {@link ServiceMap}s associated
@ -47,14 +47,14 @@ public class ScopedServiceMap implements ServiceMap {
//helper //helper
public ServiceMap currentMap() { public ServiceMap currentMap() {
String currentScope = ScopeProvider.instance.get(); String currentScope = SecretManagerProvider.get().getContext();
if (currentScope==null) if (currentScope==null)
throw new IllegalStateException("current scope is undefined"); throw new IllegalStateException("current scope is undefined");
ScopeBean bean = new ScopeBean(currentScope); ContextBean bean = new ContextBean(currentScope);
if(bean.is(VRE)) if(bean.is(Type.VRE))
currentScope = bean.enclosingScope().toString(); currentScope = bean.enclosingScope().toString();
ServiceMap map = maps.get(currentScope); ServiceMap map = maps.get(currentScope);

View File

@ -1,70 +0,0 @@
package org.gcube.common.scope.impl;
import java.util.concurrent.Callable;
import org.gcube.common.scope.api.ScopeProvider;
/**
* Utility to bind the execution of standard tasks to the current scope.
*
* @author Fabio Simeoni
*
*/
public class ScopedTasks {
/**
* Binds a {@link Callable} task to the current scope.
* @param task the task
* @return an equivalent {@link Callable} task bound to the current scope
*/
static public <V> Callable<V> bind(final Callable<V> task) {
final String callScope = ScopeProvider.instance.get();
return new Callable<V>() {
@Override
public V call() throws Exception {
//bind underlying thread to callscope
ScopeProvider.instance.set(callScope);
try {
return task.call();
}
finally {
ScopeProvider.instance.reset();
}
}
};
}
/**
* Binds a {@link Runnable} task to the current scope.
* @param task the task
* @return an equivalent {@link Runnable} task bound to the current scope
*/
static public <V> Runnable bind(final Runnable task) {
final String callScope = ScopeProvider.instance.get();
return new Runnable() {
@Override
public void run() {
//bind underlying thread to callscope
ScopeProvider.instance.set(callScope);
try {
task.run();
}
finally {
ScopeProvider.instance.reset();
}
}
};
}
}

View File

@ -5,10 +5,10 @@ import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.xml.bind.annotation.XmlAttribute; import jakarta.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement; import jakarta.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import jakarta.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter; import jakarta.xml.bind.annotation.adapters.XmlAdapter;
/** /**

View File

@ -2,21 +2,21 @@ package org.gcube.common.scope.impl;
import java.net.URL; import java.net.URL;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import org.gcube.common.scan.ClasspathScanner;
import org.gcube.common.scan.ClasspathScannerFactory;
import org.gcube.common.scan.matchers.NameMatcher;
import org.gcube.common.scan.resources.ClasspathResource;
import org.gcube.common.scope.api.ServiceMap; import org.gcube.common.scope.api.ServiceMap;
import org.reflections.Reflections;
import org.reflections.scanners.ResourcesScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.Unmarshaller;
/** /**
* Scans the classpath for {@link ServiceMap}s. * Scans the classpath for {@link ServiceMap}s.
* *
@ -37,19 +37,25 @@ class ServiceMapScanner {
*/ */
static Map<String, ServiceMap> maps() { static Map<String, ServiceMap> maps() {
log.info("scanning form maps");
Map<String, ServiceMap> maps = new HashMap<String, ServiceMap>(); Map<String, ServiceMap> maps = new HashMap<String, ServiceMap>();
try { try {
Set<String> resources = getMapNames(); ConfigurationBuilder reflectionConf = new ConfigurationBuilder().addUrls(ClasspathHelper.forJavaClassPath()).addUrls(ClasspathHelper.forClassLoader()).setScanners(new ResourcesScanner());
Reflections reflection = new Reflections(reflectionConf);
JAXBContext context = JAXBContext.newInstance(DefaultServiceMap.class); JAXBContext context = JAXBContext.newInstance(DefaultServiceMap.class);
Set<String> resources = reflection.getResources(Pattern.compile(".*\\.servicemap"));
log.info("loading {} ", resources);
Unmarshaller um = context.createUnmarshaller(); Unmarshaller um = context.createUnmarshaller();
for (String resource : resources) { for (String resource: resources) {
log.info("loading {} ", resource);
URL url = Thread.currentThread().getContextClassLoader().getResource(resource); URL url = Thread.currentThread().getContextClassLoader().getResource(resource);
log.info("loading {} ", url);
DefaultServiceMap map = (DefaultServiceMap) um.unmarshal(url); DefaultServiceMap map = (DefaultServiceMap) um.unmarshal(url);
ServiceMap current = maps.get(map.scope()); ServiceMap current = maps.get(map.scope());
@ -63,20 +69,15 @@ class ServiceMapScanner {
new Object[] { current.version(), map.version(), map.scope() }); new Object[] { current.version(), map.version(), map.scope() });
maps.put(map.scope(), map); maps.put(map.scope(), map);
}
} catch (Exception e) { }
} catch (Throwable e) {
log.error("errror scanning service amps",e);
throw new RuntimeException("could not load service maps", e); throw new RuntimeException("could not load service maps", e);
} }
return maps; return maps;
} }
private static Set<String> getMapNames() {
ClasspathScanner scanner = ClasspathScannerFactory.scanner();
Set<String> names = new HashSet<String>();
for (ClasspathResource r : scanner.scan(new NameMatcher(mapConfigPattern)))
names.add(r.name());
return names;
}
} }

View File

@ -1,56 +0,0 @@
package org.gcube.common.scope;
import static org.junit.Assert.*;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import org.gcube.common.scope.api.ScopeProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class BadConfigurationTest {
private ClassLoader loader;
@Before
public void init() {
loader = Thread.currentThread().getContextClassLoader();
}
@Test
public void multipleAlternativeProvidersCannotBeConfigured() {
try {
addJarsToClasspath("alternativeprovider.jar","yetanotheralternativeprovider.jar");
ScopeProvider.instance.set("shouldfail");
}
catch(Error e) {
return;
}
fail();
}
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,9 +1,11 @@
package org.gcube.common.scope; package org.gcube.common.scope;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.gcube.common.scope.impl.ScopeBean; import org.gcube.common.security.ContextBean;
import org.gcube.common.scope.impl.ScopeBean.Type; import org.gcube.common.security.ContextBean.Type;
import org.junit.Test; import org.junit.Test;
public class BeanTest { public class BeanTest {
@ -13,22 +15,22 @@ public class BeanTest {
public void beansAreParsedCorrectly() { public void beansAreParsedCorrectly() {
String infra ="/infra"; String infra ="/infra";
ScopeBean infraBean = new ScopeBean(infra); ContextBean infraBean = new ContextBean(infra);
assertEquals("infra",infraBean.name()); assertEquals("infra",infraBean.name());
assertTrue(infraBean.is(Type.INFRASTRUCTURE)); assertTrue(infraBean.is(Type.INFRASTRUCTURE));
assertNull(infraBean.enclosingScope()); assertNull(infraBean.enclosingScope());
assertEquals(infra,infraBean.toString()); assertEquals(infra,infraBean.toString());
assertEquals(infraBean,new ScopeBean(infra)); assertEquals(infraBean,new ContextBean(infra));
String vo =infra+"/vo"; String vo =infra+"/vo";
ScopeBean vobean = new ScopeBean(vo); ContextBean vobean = new ContextBean(vo);
assertEquals("vo",vobean.name()); assertEquals("vo",vobean.name());
assertTrue(vobean.is(Type.VO)); assertTrue(vobean.is(Type.VO));
assertEquals(infraBean,vobean.enclosingScope()); assertEquals(infraBean,vobean.enclosingScope());
assertEquals(vo,vobean.toString()); assertEquals(vo,vobean.toString());
String vre = vo+"/vre"; String vre = vo+"/vre";
ScopeBean vrebean = new ScopeBean(vre); ContextBean vrebean = new ContextBean(vre);
assertEquals("vre",vrebean.name()); assertEquals("vre",vrebean.name());
assertTrue(vrebean.is(Type.VRE)); assertTrue(vrebean.is(Type.VRE));
assertEquals(vobean,vrebean.enclosingScope()); assertEquals(vobean,vrebean.enclosingScope());

View File

@ -1,13 +1,14 @@
package org.gcube.common.scope; package org.gcube.common.scope;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.security.AuthorizedTasks;
import org.gcube.common.scope.impl.ScopedTasks; import org.gcube.common.security.providers.SecretManagerProvider;
import org.junit.Test; import org.junit.Test;
public class BindingTest { public class BindingTest {
@ -20,36 +21,36 @@ public class BindingTest {
final String callScope = "somescope"; final String callScope = "somescope";
ScopeProvider.instance.set(callScope); SecretManagerProvider.set(new TestSecret(callScope));
Callable<Void> unbound = new Callable<Void>() { Callable<Void> unbound = new Callable<Void>() {
@Override @Override
public Void call() throws Exception { public Void call() throws Exception {
assertEquals("task thread is not bound to call scope, but to "+ScopeProvider.instance.get(),callScope,ScopeProvider.instance.get()); assertEquals("task thread is not bound to call scope, but to "+SecretManagerProvider.get().getContext(),callScope,SecretManagerProvider.get().getContext());
return null; return null;
} }
}; };
Callable<Void> bound = ScopedTasks.bind(unbound); Callable<Void> bound = AuthorizedTasks.bind(unbound);
String newScope = "newscope"; String newScope = "newscope";
//scope in current thread changes //scope in current thread changes
ScopeProvider.instance.set(newScope); SecretManagerProvider.set(new TestSecret(newScope));
//task is submittted //task is submittted
executor.submit(bound).get(); executor.submit(bound).get();
//resetting task //resetting task
assertEquals("call thread does not retain its latest scope",newScope,ScopeProvider.instance.get()); assertEquals("call thread does not retain its latest scope",newScope,SecretManagerProvider.get().getContext());
//reset call scope //reset call scope
ScopeProvider.instance.reset(); SecretManagerProvider.reset();
Callable<Void> cleanupTest = new Callable<Void>() { Callable<Void> cleanupTest = new Callable<Void>() {
@Override @Override
public Void call() throws Exception { public Void call() throws Exception {
assertNull(ScopeProvider.instance.get()); assertNull(SecretManagerProvider.get());
return null; return null;
} }
}; };

View File

@ -1,51 +0,0 @@
package org.gcube.common.scope;
import static org.junit.Assert.*;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import org.gcube.common.scope.api.ScopeProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class ConfigurationTest {
private ClassLoader loader;
@Before
public void init() {
loader = Thread.currentThread().getContextClassLoader();
}
@Test
public void alternativeProvidersCanBeConfigured() {
addJarsToClasspath("alternativeprovider.jar");
assertEquals("AlternativeProvider",ScopeProvider.instance.getClass().getSimpleName());
}
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,33 +0,0 @@
package org.gcube.common.scope;
import static org.junit.Assert.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.gcube.common.scope.api.ScopeProvider;
import org.junit.Test;
public class ProviderTest {
@Test
public void scopesAreThreadInherited() throws Exception {
final ScopeProvider provider = ScopeProvider.instance;
provider.set("scope");
final CountDownLatch latch = new CountDownLatch(1);
new Thread() {
public void run() {
assertNotNull(provider.get());
latch.countDown();
};
}.start();
if (!latch.await(100, TimeUnit.MILLISECONDS))
fail("scope was null in testing thread");
}
}

View File

@ -1,18 +1,19 @@
package org.gcube.common.scope; package org.gcube.common.scope;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.StringReader; import java.io.StringReader;
import java.io.StringWriter; import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.api.ServiceMap; import org.gcube.common.scope.api.ServiceMap;
import org.gcube.common.scope.impl.DefaultServiceMap; import org.gcube.common.scope.impl.DefaultServiceMap;
import org.gcube.common.security.providers.SecretManagerProvider;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import jakarta.xml.bind.JAXBContext;
public class ServiceMapTest { public class ServiceMapTest {
@ -53,7 +54,7 @@ public class ServiceMapTest {
@Test @Test
public void serviceMapsDiscoveredCorrectly() throws Exception { public void serviceMapsDiscoveredCorrectly() throws Exception {
ScopeProvider.instance.set("/infra/vo"); SecretManagerProvider.set(new TestSecret("/infra/vo"));
assertNotNull(ServiceMap.instance.endpoint("service1")); assertNotNull(ServiceMap.instance.endpoint("service1"));
@ -64,7 +65,7 @@ public class ServiceMapTest {
@Test @Test
public void serviceMapsCanBeLookedupInVREScope() throws Exception { public void serviceMapsCanBeLookedupInVREScope() throws Exception {
ScopeProvider.instance.set("/infra/vo/vre"); SecretManagerProvider.set(new TestSecret("/infra/vo/vre"));
assertNotNull(ServiceMap.instance.endpoint("service1")); assertNotNull(ServiceMap.instance.endpoint("service1"));
@ -72,7 +73,7 @@ public class ServiceMapTest {
@After @After
public void cleanup() { public void cleanup() {
ScopeProvider.instance.reset(); SecretManagerProvider.reset();
} }
} }

View File

@ -0,0 +1,36 @@
package org.gcube.common.scope;
import java.util.Collections;
import java.util.Map;
import org.gcube.common.security.Owner;
import org.gcube.common.security.secrets.Secret;
public class TestSecret extends Secret {
String context;
public TestSecret(String context){
this.context = context;
}
@Override
public Owner getOwner() {
return new Owner("test", Collections.emptyList(), false, false);
}
@Override
public String getContext() {
return this.context;
}
@Override
public Map<String, String> getHTTPAuthorizationHeaders() {
return Collections.emptyMap();
}
@Override
public boolean isValid() {
return true;
}
}