Compare commits

...

7 Commits

Author SHA1 Message Date
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
22 changed files with 195 additions and 590 deletions

View File

@ -1,18 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
@ -27,6 +14,20 @@
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>

1
.gitignore vendored Normal file
View File

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

View File

@ -5,11 +5,21 @@
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
@ -17,7 +27,10 @@
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</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,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
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
## [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);

56
pom.xml
View File

@ -1,73 +1,76 @@
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>maven-parent</artifactId>
<groupId>org.gcube.tools</groupId>
<version>1.1.0</version>
<version>1.2.0</version>
<relativePath />
</parent>
<groupId>org.gcube.core</groupId>
<artifactId>common-scope</artifactId>
<version>1.3.1-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<name>Common Scope</name>
<description>Scope-related APIs</description>
<properties>
<distroDirectory>distro</distroDirectory>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<scm>
<connection>scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</connection>
<developerConnection>scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</developerConnection>
<url>https://code-repo.d4science.org/gCubeSystem/${project.artifactId}</url>
<connection>scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</connection>
<developerConnection>scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</developerConnection>
<url>https://code-repo.d4science.org/gCubeSystem/${project.artifactId}</url>
</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>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-configuration-scanner</artifactId>
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-scope-maps</artifactId>
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>common-security</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
@ -94,7 +97,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
@ -113,8 +115,6 @@
</execution>
</executions>
</plugin>
</plugins>
</build>
</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

@ -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;
import static org.gcube.common.scope.impl.ScopeBean.Type.*;
import java.util.Map;
import org.gcube.common.scope.api.ScopeProvider;
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
@ -47,14 +47,14 @@ public class ScopedServiceMap implements ServiceMap {
//helper
public ServiceMap currentMap() {
String currentScope = ScopeProvider.instance.get();
String currentScope = SecretManagerProvider.get().getContext();
if (currentScope==null)
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();
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

@ -1,19 +1,20 @@
package org.gcube.common.scope.impl;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
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.reflections.Reflections;
import org.reflections.scanners.ResourcesScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -37,19 +38,27 @@ class ServiceMapScanner {
*/
static Map<String, ServiceMap> maps() {
log.info("scanning form maps");
Map<String, ServiceMap> maps = new HashMap<String, ServiceMap>();
try {
Set<String> resources = getMapNames();
Collection<URL> urls = ClasspathHelper.forJavaClassPath();
ConfigurationBuilder reflectionConf = new ConfigurationBuilder().addUrls(ClasspathHelper.forJavaClassPath()).addUrls(ClasspathHelper.forClassLoader()).setScanners(new ResourcesScanner());
Reflections reflection = new Reflections(reflectionConf);
JAXBContext context = JAXBContext.newInstance(DefaultServiceMap.class);
Set<String> resources = reflection.getResources(Pattern.compile(".*\\.servicemap"));
log.info("loading {} ", resources);
Unmarshaller um = context.createUnmarshaller();
for (String resource : resources) {
for (String resource: resources) {
log.info("loading {} ", resource);
URL url = Thread.currentThread().getContextClassLoader().getResource(resource);
log.info("loading {} ", url);
DefaultServiceMap map = (DefaultServiceMap) um.unmarshal(url);
ServiceMap current = maps.get(map.scope());
@ -63,20 +72,15 @@ class ServiceMapScanner {
new Object[] { current.version(), map.version(), map.scope() });
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);
}
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;
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.scope.impl.ScopeBean.Type;
import org.gcube.common.security.ContextBean;
import org.gcube.common.security.ContextBean.Type;
import org.junit.Test;
public class BeanTest {
@ -13,22 +15,22 @@ public class BeanTest {
public void beansAreParsedCorrectly() {
String infra ="/infra";
ScopeBean infraBean = new ScopeBean(infra);
ContextBean infraBean = new ContextBean(infra);
assertEquals("infra",infraBean.name());
assertTrue(infraBean.is(Type.INFRASTRUCTURE));
assertNull(infraBean.enclosingScope());
assertEquals(infra,infraBean.toString());
assertEquals(infraBean,new ScopeBean(infra));
assertEquals(infraBean,new ContextBean(infra));
String vo =infra+"/vo";
ScopeBean vobean = new ScopeBean(vo);
ContextBean vobean = new ContextBean(vo);
assertEquals("vo",vobean.name());
assertTrue(vobean.is(Type.VO));
assertEquals(infraBean,vobean.enclosingScope());
assertEquals(vo,vobean.toString());
String vre = vo+"/vre";
ScopeBean vrebean = new ScopeBean(vre);
ContextBean vrebean = new ContextBean(vre);
assertEquals("vre",vrebean.name());
assertTrue(vrebean.is(Type.VRE));
assertEquals(vobean,vrebean.enclosingScope());

View File

@ -1,13 +1,14 @@
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.ExecutorService;
import java.util.concurrent.Executors;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopedTasks;
import org.gcube.common.security.AuthorizedTasks;
import org.gcube.common.security.providers.SecretManagerProvider;
import org.junit.Test;
public class BindingTest {
@ -20,36 +21,36 @@ public class BindingTest {
final String callScope = "somescope";
ScopeProvider.instance.set(callScope);
SecretManagerProvider.set(new TestSecret(callScope));
Callable<Void> unbound = new Callable<Void>() {
@Override
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;
}
};
Callable<Void> bound = ScopedTasks.bind(unbound);
Callable<Void> bound = AuthorizedTasks.bind(unbound);
String newScope = "newscope";
//scope in current thread changes
ScopeProvider.instance.set(newScope);
SecretManagerProvider.set(new TestSecret(newScope));
//task is submittted
executor.submit(bound).get();
//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
ScopeProvider.instance.reset();
SecretManagerProvider.reset();
Callable<Void> cleanupTest = new Callable<Void>() {
@Override
public Void call() throws Exception {
assertNull(ScopeProvider.instance.get());
assertNull(SecretManagerProvider.get());
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,15 +1,16 @@
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.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.impl.DefaultServiceMap;
import org.gcube.common.security.providers.SecretManagerProvider;
import org.junit.After;
import org.junit.Test;
@ -53,7 +54,7 @@ public class ServiceMapTest {
@Test
public void serviceMapsDiscoveredCorrectly() throws Exception {
ScopeProvider.instance.set("/infra/vo");
SecretManagerProvider.set(new TestSecret("/infra/vo"));
assertNotNull(ServiceMap.instance.endpoint("service1"));
@ -64,7 +65,7 @@ public class ServiceMapTest {
@Test
public void serviceMapsCanBeLookedupInVREScope() throws Exception {
ScopeProvider.instance.set("/infra/vo/vre");
SecretManagerProvider.set(new TestSecret("/infra/vo/vre"));
assertNotNull(ServiceMap.instance.endpoint("service1"));
@ -72,7 +73,7 @@ public class ServiceMapTest {
@After
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);
}
@Override
public String getContext() {
return this.context;
}
@Override
public Map<String, String> getHTTPAuthorizationHeaders() {
return Collections.emptyMap();
}
@Override
public boolean isExpired() {
return false;
}
}