porting to smartgears 4

This commit is contained in:
Lucio Lelii 2022-06-10 17:10:26 +02:00
parent d7999b1339
commit 830ed744e3
18 changed files with 109 additions and 531 deletions

1
.gitignore vendored Normal file
View File

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

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,7 @@ 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
- removed ScopePorvider and ScopeBean

22
pom.xml
View File

@ -11,7 +11,7 @@
<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>
@ -25,6 +25,19 @@
<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.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
@ -36,10 +49,14 @@
<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>junit</groupId>
<artifactId>junit</artifactId>
@ -50,7 +67,6 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
<scope>compile</scope>
</dependency>

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.instance.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,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.instance.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.instance.get().getContext(),callScope,SecretManagerProvider.instance.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.instance.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.instance.get().getContext());
//reset call scope
ScopeProvider.instance.reset();
SecretManagerProvider.instance.reset();
Callable<Void> cleanupTest = new Callable<Void>() {
@Override
public Void call() throws Exception {
assertNull(ScopeProvider.instance.get());
assertNull(SecretManagerProvider.instance.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.instance.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.instance.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.instance.reset();
}
}

View File

@ -0,0 +1,42 @@
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;
}
@Override
public boolean isRefreshable() {
return false;
}
}