merged from trunk after classpath scanning fix
git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/branches/common/common-scope/1.0@58260 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
8610d6f0ec
commit
39ba616ccb
|
@ -1,21 +0,0 @@
|
||||||
package org.gcube.common.scope.api;
|
|
||||||
|
|
||||||
import java.lang.annotation.Documented;
|
|
||||||
import java.lang.annotation.ElementType;
|
|
||||||
import java.lang.annotation.Inherited;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Marks a {@link ScopeProvider} implementations to use in place of {@link DefaultScopeProvider}
|
|
||||||
*
|
|
||||||
* @author Fabio Simeoni
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@Target({ElementType.TYPE})
|
|
||||||
@Inherited
|
|
||||||
@Documented
|
|
||||||
public @interface DefaultScopeProvider {}
|
|
|
@ -13,16 +13,16 @@ import org.slf4j.LoggerFactory;
|
||||||
* @see ScopeProviderScanner
|
* @see ScopeProviderScanner
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class DefaultScopeProviderImpl implements ScopeProvider {
|
public class DefaultScopeProvider implements ScopeProvider {
|
||||||
|
|
||||||
/** System property for scope */
|
/** System property for scope */
|
||||||
public static final String SCOPE_PROPERTY = "gcube.scope";
|
public static final String SCOPE_PROPERTY = "gcube.scope";
|
||||||
|
|
||||||
private static Logger log = LoggerFactory.getLogger(DefaultScopeProviderImpl.class);
|
private static Logger log = LoggerFactory.getLogger(DefaultScopeProvider.class);
|
||||||
|
|
||||||
private InheritableThreadLocal<String> scopes = new InheritableThreadLocal<String>();
|
private InheritableThreadLocal<String> scopes = new InheritableThreadLocal<String>();
|
||||||
|
|
||||||
protected DefaultScopeProviderImpl() {};
|
protected DefaultScopeProvider() {};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String get() {
|
public String get() {
|
|
@ -1,13 +1,11 @@
|
||||||
package org.gcube.common.scope.impl;
|
package org.gcube.common.scope.impl;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ServiceLoader;
|
||||||
|
|
||||||
import org.gcube.common.scope.api.DefaultScopeProvider;
|
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
import org.reflections.Reflections;
|
|
||||||
import org.reflections.scanners.SubTypesScanner;
|
|
||||||
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;
|
||||||
|
|
||||||
|
@ -31,31 +29,27 @@ public class ScopeProviderScanner {
|
||||||
public static ScopeProvider provider() {
|
public static ScopeProvider provider() {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// we include urls specified in manifest files, which is required
|
ScopeProvider impl = null;
|
||||||
// when we run tests in surefire's forked-mode
|
|
||||||
ConfigurationBuilder builder = new ConfigurationBuilder().setUrls(
|
|
||||||
ClasspathHelper.forManifest(Utils.urlsToScan())).setScanners(new SubTypesScanner());
|
|
||||||
|
|
||||||
Reflections reflections = new Reflections(builder);
|
ServiceLoader<ScopeProvider> loader = ServiceLoader.load(ScopeProvider.class);
|
||||||
|
|
||||||
Set<Class<? extends ScopeProvider>> impls =
|
Iterator<ScopeProvider> iterator = loader.iterator();
|
||||||
reflections.getSubTypesOf(ScopeProvider.class);
|
List<ScopeProvider> impls = new ArrayList<ScopeProvider>();
|
||||||
|
|
||||||
ScopeProvider defaultProvider = null;
|
while(iterator.hasNext())
|
||||||
|
impls.add(iterator.next());
|
||||||
|
|
||||||
for (Class<? extends ScopeProvider> impl : impls)
|
if (impls.size()==0) {
|
||||||
if (impl.isAnnotationPresent(DefaultScopeProvider.class))
|
impl = new DefaultScopeProvider();
|
||||||
if (defaultProvider==null)
|
|
||||||
defaultProvider = impl.newInstance();
|
|
||||||
else
|
|
||||||
throw new Exception("mis-configured environment: detected multiple deafult providers among "+impls);
|
|
||||||
|
|
||||||
if (defaultProvider==null) {
|
|
||||||
log.info("using default scope provider");
|
|
||||||
defaultProvider = new DefaultScopeProviderImpl();
|
|
||||||
}
|
}
|
||||||
|
else if (impls.size()>1)
|
||||||
|
throw new Exception("mis-configured environment: detected multiple default providers "+impls);
|
||||||
|
else
|
||||||
|
impl=impls.get(0);
|
||||||
|
|
||||||
return defaultProvider;
|
log.info("using scope provider "+impl);
|
||||||
|
|
||||||
|
return impl;
|
||||||
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
org.acme.AlternativeProvider
|
|
@ -0,0 +1,26 @@
|
||||||
|
package org.acme;
|
||||||
|
|
||||||
|
|
||||||
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
|
|
||||||
|
public class AlternativeProvider implements ScopeProvider {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String get() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void set(String scope) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reset() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
org.acme2.YetAnotherAlternativeProvider
|
|
@ -0,0 +1,25 @@
|
||||||
|
package org.acme2;
|
||||||
|
|
||||||
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
|
|
||||||
|
public class YetAnotherAlternativeProvider implements ScopeProvider {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String get() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void set(String scope) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reset() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,44 +4,44 @@ import static org.junit.Assert.*;
|
||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class ConfigurationTest {
|
public class ConfigurationTest {
|
||||||
|
|
||||||
private ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
private ClassLoader loader;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
loader = Thread.currentThread().getContextClassLoader();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void alternativeProvidersCanBeConfigured() {
|
public void alternativeProvidersCanBeConfigured() {
|
||||||
|
|
||||||
addJarToClasspath("alternativeprovider.jar");
|
addJarsToClasspath("alternativeprovider.jar");
|
||||||
|
|
||||||
assertEquals("AlternativeScopeProvider",ScopeProvider.instance.getClass().getSimpleName());
|
assertEquals("AlternativeProvider",ScopeProvider.instance.getClass().getSimpleName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void multipleAlternativeProvidersCannotBeConfigured() {
|
|
||||||
|
|
||||||
try {
|
|
||||||
addJarToClasspath("multiplealternativeproviders.jar");
|
|
||||||
ScopeProvider.instance.set("shouldfail");
|
|
||||||
fail();
|
|
||||||
}
|
|
||||||
catch(Error e) {}
|
|
||||||
|
|
||||||
}
|
private void addJarsToClasspath(String ... jars) {
|
||||||
|
|
||||||
private void addJarToClasspath(String jar) {
|
List<URL> jarUrls = new ArrayList<URL>();
|
||||||
|
for (String jar : jars)
|
||||||
URL jarURL = loader.getResource(jar);
|
jarUrls.add(loader.getResource(jar));
|
||||||
|
|
||||||
URLClassLoader urlClassLoader
|
URLClassLoader urlClassLoader
|
||||||
= new URLClassLoader(new URL[]{jarURL},loader);
|
= new URLClassLoader(jarUrls.toArray(new URL[0]),loader);
|
||||||
|
|
||||||
|
|
||||||
Thread.currentThread().setContextClassLoader(urlClassLoader);
|
Thread.currentThread().setContextClassLoader(urlClassLoader);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue