branch for 1.0.x releases (first created for gCube 2.10.0)
git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/branches/common/common-scope/1.0@57622 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
commit
8610d6f0ec
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
|
||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
|
@ -0,0 +1,23 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>common-scope</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -0,0 +1 @@
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
gCube System - License
|
||||
------------------------------------------------------------
|
||||
|
||||
The gCube/gCore software is licensed as Free Open Source software conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
|
||||
The software and documentation is provided by its authors/distributors "as is" and no expressed or
|
||||
implied warranty is given for its use, quality or fitness for a particular case.
|
|
@ -0,0 +1 @@
|
|||
* Fabio Simeoni (fabio.simeoni@fao.org), FAO of the UN, Italy
|
|
@ -0,0 +1,38 @@
|
|||
The gCube System - ${name}
|
||||
----------------------
|
||||
|
||||
This work has been partially supported by the following European projects: DILIGENT (FP6-2003-IST-2), D4Science (FP7-INFRA-2007-1.2.2),
|
||||
D4Science-II (FP7-INFRA-2008-1.2.2), iMarine (FP7-INFRASTRUCTURES-2011-2), and EUBrazilOpenBio (FP7-ICT-2011-EU-Brazil).
|
||||
|
||||
Authors
|
||||
-------
|
||||
|
||||
* Fabio Simeoni (fabio.simeoni@fao.org), FAO of the UN, Italy.
|
||||
|
||||
Version and Release Date
|
||||
------------------------
|
||||
${version}
|
||||
|
||||
Description
|
||||
-----------
|
||||
${description}
|
||||
|
||||
Download information
|
||||
--------------------
|
||||
|
||||
Source code is available from SVN:
|
||||
${scm.url}
|
||||
|
||||
Binaries can be downloaded from:
|
||||
|
||||
|
||||
Documentation
|
||||
-------------
|
||||
Documentation is available on-line from the Projects Documentation Wiki:
|
||||
https://gcube.wiki.gcube-system.org/gcube/index.php/Integration_and_Interoperability_Facilities_Framework:_Client_Libraries_Design_Model#Scope_Management
|
||||
|
||||
|
||||
Licensing
|
||||
---------
|
||||
|
||||
This software is licensed under the terms you may find in the file named "LICENSE" in this directory.
|
|
@ -0,0 +1,5 @@
|
|||
<ReleaseNotes>
|
||||
<Changeset component="${build.finalName}" date="">
|
||||
<Change>First Release</Change>
|
||||
</Changeset>
|
||||
</ReleaseNotes>
|
|
@ -0,0 +1,42 @@
|
|||
<assembly
|
||||
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
|
||||
<id>servicearchive</id>
|
||||
<formats>
|
||||
<format>tar.gz</format>
|
||||
</formats>
|
||||
<baseDirectory>/</baseDirectory>
|
||||
<fileSets>
|
||||
<fileSet>
|
||||
<directory>${distroDirectory}</directory>
|
||||
<outputDirectory>/</outputDirectory>
|
||||
<useDefaultExcludes>true</useDefaultExcludes>
|
||||
<includes>
|
||||
<include>README</include>
|
||||
<include>LICENSE</include>
|
||||
<include>INSTALL</include>
|
||||
<include>MAINTAINERS</include>
|
||||
<include>changelog.xml</include>
|
||||
</includes>
|
||||
<fileMode>755</fileMode>
|
||||
<filtered>true</filtered>
|
||||
</fileSet>
|
||||
</fileSets>
|
||||
<files>
|
||||
<file>
|
||||
<source>${distroDirectory}/profile.xml</source>
|
||||
<outputDirectory>/etc</outputDirectory>
|
||||
<filtered>true</filtered>
|
||||
</file>
|
||||
<file>
|
||||
<source>target/${build.finalName}.jar</source>
|
||||
<outputDirectory>/${artifactId}</outputDirectory>
|
||||
</file>
|
||||
<file>
|
||||
<source>${distroDirectory}/svnpath.txt</source>
|
||||
<outputDirectory>/${artifactId}</outputDirectory>
|
||||
<filtered>true</filtered>
|
||||
</file>
|
||||
</files>
|
||||
</assembly>
|
|
@ -0,0 +1,26 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<ID />
|
||||
<Type>Service</Type>
|
||||
<Profile>
|
||||
<Description>${description}</Description>
|
||||
<Class>Common</Class>
|
||||
<Name>${artifactId}</Name>
|
||||
<Version>1.0.0</Version>
|
||||
<Packages>
|
||||
<Software>
|
||||
<Name>${artifactId}</Name>
|
||||
<Version>${version}</Version>
|
||||
<MavenCoordinates>
|
||||
<groupId>${groupId}</groupId>
|
||||
<artifactId>${artifactId}</artifactId>
|
||||
<version>${version}</version>
|
||||
</MavenCoordinates>
|
||||
<Files>
|
||||
<File>${build.finalName}.jar</File>
|
||||
</Files>
|
||||
</Software>
|
||||
</Packages>
|
||||
</Profile>
|
||||
</Resource>
|
||||
|
|
@ -0,0 +1 @@
|
|||
${scm.url}
|
|
@ -0,0 +1,113 @@
|
|||
<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.0.0</version>
|
||||
<relativePath />
|
||||
</parent>
|
||||
|
||||
<groupId>org.gcube.core</groupId>
|
||||
<artifactId>common-scope</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>Common Scope</name>
|
||||
<description>Scope-related APIs</description>
|
||||
|
||||
<properties>
|
||||
<distroDirectory>distro</distroDirectory>
|
||||
</properties>
|
||||
|
||||
<scm>
|
||||
<connection>scm:svn:http://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/${project.artifactId}</connection>
|
||||
<developerConnection>scm:svn:https://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/${project.artifactId}</developerConnection>
|
||||
<url>http://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/${project.artifactId}</url>
|
||||
</scm>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.reflections</groupId>
|
||||
<artifactId>reflections</artifactId>
|
||||
<version>0.9.6</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.10</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.6.4</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>1.6.4</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
<plugins>
|
||||
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>2.5</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-profile</id>
|
||||
<phase>install</phase>
|
||||
<goals>
|
||||
<goal>copy-resources</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>target</outputDirectory>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>${distroDirectory}</directory>
|
||||
<filtering>true</filtering>
|
||||
<includes>
|
||||
<include>profile.xml</include>
|
||||
</includes>
|
||||
</resource>
|
||||
</resources>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<configuration>
|
||||
<descriptors>
|
||||
<descriptor>${distroDirectory}/descriptor.xml</descriptor>
|
||||
</descriptors>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>servicearchive</id>
|
||||
<phase>install</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,21 @@
|
|||
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 {}
|
|
@ -0,0 +1,34 @@
|
|||
package org.gcube.common.scope.api;
|
||||
|
||||
import org.gcube.common.scope.impl.ScopeProviderScanner;
|
||||
|
||||
/**
|
||||
* Provides a scope in the caller's context.
|
||||
*
|
||||
* @author Fabio Simeoni
|
||||
*
|
||||
*/
|
||||
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();
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package org.gcube.common.scope.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.gcube.common.scope.impl.ScopedServiceMap;
|
||||
|
||||
/**
|
||||
* Resolves service endpoints statically configured for a given scope.
|
||||
*
|
||||
* @author Fabio Simeoni
|
||||
*
|
||||
*/
|
||||
public interface ServiceMap {
|
||||
|
||||
/**
|
||||
* Shared {@link ServiceMap}.
|
||||
*/
|
||||
public static final ServiceMap instance = new ScopedServiceMap();
|
||||
|
||||
/**
|
||||
* Returns the endpoints of a given service.
|
||||
* @param service the service
|
||||
* @return the endpoints, or <code>null</code> if the service is unknown.
|
||||
*/
|
||||
List<String> endpoint(String service);
|
||||
|
||||
/**
|
||||
* Returns the associated scope.
|
||||
* @return the scope
|
||||
*/
|
||||
String scope();
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
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 DefaultScopeProviderImpl implements ScopeProvider {
|
||||
|
||||
/** System property for scope */
|
||||
public static final String SCOPE_PROPERTY = "gcube.scope";
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(DefaultScopeProviderImpl.class);
|
||||
|
||||
private InheritableThreadLocal<String> scopes = new InheritableThreadLocal<String>();
|
||||
|
||||
protected DefaultScopeProviderImpl() {};
|
||||
|
||||
@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.info("setting scope {} in thread {}",scope,Thread.currentThread().getId());
|
||||
scopes.set(scope);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
log.info("resetting scope in thread {}",Thread.currentThread().getId());
|
||||
scopes.remove();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package org.gcube.common.scope.impl;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* A {@link ServiceMap} with a standard XML binding.
|
||||
*
|
||||
* @author Fabio Simeoni
|
||||
*
|
||||
*/
|
||||
@XmlRootElement(name="service-map")
|
||||
public class DefaultServiceMap implements ServiceMap {
|
||||
|
||||
@XmlAttribute
|
||||
private String scope;
|
||||
|
||||
@XmlJavaTypeAdapter(ServiceMapAdapter.class)
|
||||
Map<String,List<String>> services = new LinkedHashMap<String,List<String>>();
|
||||
|
||||
@Override
|
||||
public String scope() {
|
||||
return scope;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> endpoint(String service) {
|
||||
return services.get(service);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package org.gcube.common.scope.impl;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.gcube.common.scope.api.DefaultScopeProvider;
|
||||
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.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 {
|
||||
|
||||
// we include urls specified in manifest files, which is required
|
||||
// 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);
|
||||
|
||||
Set<Class<? extends ScopeProvider>> impls =
|
||||
reflections.getSubTypesOf(ScopeProvider.class);
|
||||
|
||||
ScopeProvider defaultProvider = null;
|
||||
|
||||
for (Class<? extends ScopeProvider> impl : impls)
|
||||
if (impl.isAnnotationPresent(DefaultScopeProvider.class))
|
||||
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();
|
||||
}
|
||||
|
||||
return defaultProvider;
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("could not configure scope provider", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package org.gcube.common.scope.impl;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.common.scope.api.ServiceMap;
|
||||
|
||||
/**
|
||||
* A {@link ServiceMap} that forwards requests to {@link ServiceMap}s associated
|
||||
* with the scope of callers.
|
||||
* <p>
|
||||
* At construction time, it configures itself with all the service maps found
|
||||
* the classpath (excluding URLs available to primordial and extension
|
||||
* classloader). Recognises service maps as resources whose names match a
|
||||
* {@link ServiceMapScanner#mapConfigPattern}.
|
||||
*
|
||||
* @author Fabio Simeoni
|
||||
*
|
||||
*/
|
||||
public class ScopedServiceMap implements ServiceMap {
|
||||
|
||||
private final Map<String, ServiceMap> maps;
|
||||
|
||||
|
||||
public ScopedServiceMap() {
|
||||
maps = ServiceMapScanner.maps();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String scope() {
|
||||
return ScopeProvider.instance.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> endpoint(String service) {
|
||||
|
||||
String currentScope = scope();
|
||||
|
||||
ServiceMap map = maps.get(currentScope);
|
||||
|
||||
return map == null ? null : map.endpoint(service);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package org.gcube.common.scope.impl;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAttribute;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.bind.annotation.adapters.XmlAdapter;
|
||||
|
||||
|
||||
/**
|
||||
* Adapts the JAXB-binding for {@link DefaultServiceMap}.
|
||||
*
|
||||
* @author Fabio Simeoni
|
||||
*
|
||||
*/
|
||||
public class ServiceMapAdapter extends XmlAdapter<ServiceMapAdapter.ValueServiceMap,Map<String,List<String>>> {
|
||||
|
||||
@XmlRootElement(name="services")
|
||||
static class ValueServiceMap {
|
||||
|
||||
@XmlElement(name="service")
|
||||
Set<ServiceEntry> services;
|
||||
|
||||
}
|
||||
|
||||
static class ServiceEntry {
|
||||
|
||||
@XmlAttribute
|
||||
private String name;
|
||||
|
||||
@XmlElement(name="endpoint")
|
||||
private List<String> endpoints;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, List<String>> unmarshal(ValueServiceMap valueMap) throws Exception {
|
||||
Map<String, List<String>> map = new LinkedHashMap<String,List<String>>();
|
||||
for (ServiceEntry service : valueMap.services)
|
||||
map.put(service.name,service.endpoints);
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValueServiceMap marshal(Map<String, List<String>> map) throws Exception {
|
||||
ValueServiceMap valueMap = new ValueServiceMap();
|
||||
for (Map.Entry<String,List<String>> e : map.entrySet()) {
|
||||
ServiceEntry entry = new ServiceEntry();
|
||||
entry.name=e.getKey();
|
||||
entry.endpoints = e.getValue();
|
||||
}
|
||||
return valueMap;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package org.gcube.common.scope.impl;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Scans the classpath for {@link ServiceMap}s.
|
||||
* @author Fabio Simeoni
|
||||
*
|
||||
*/
|
||||
class ServiceMapScanner {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(ServiceMapScanner.class);
|
||||
|
||||
/**
|
||||
* The path used to find service map configuration files.
|
||||
*/
|
||||
static final String mapConfigPattern = ".*\\.servicemap";
|
||||
|
||||
/**
|
||||
* Scans the classpath for {@link ServiceMap}s.
|
||||
*/
|
||||
static Map<String, ServiceMap> maps() {
|
||||
|
||||
Map<String, ServiceMap> maps = new HashMap<String, ServiceMap>();
|
||||
|
||||
try {
|
||||
|
||||
JAXBContext context = JAXBContext
|
||||
.newInstance(DefaultServiceMap.class);
|
||||
Unmarshaller um = context.createUnmarshaller();
|
||||
|
||||
// we include urls specified in manifest files, which is required
|
||||
// when we run tests in surefire's forked-mode
|
||||
ConfigurationBuilder builder = new ConfigurationBuilder().setUrls(
|
||||
ClasspathHelper.forManifest(Utils.urlsToScan())).setScanners(
|
||||
new ResourcesScanner());
|
||||
|
||||
Reflections reflections = new Reflections(builder);
|
||||
|
||||
for (String resource : reflections.getResources(Pattern
|
||||
.compile(mapConfigPattern))) {
|
||||
URL url = Thread.currentThread().getContextClassLoader()
|
||||
.getResource(resource);
|
||||
log.trace("loading {} ", url);
|
||||
DefaultServiceMap map = (DefaultServiceMap) um.unmarshal(url);
|
||||
maps.put(map.scope(), map);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("could not load service maps", e);
|
||||
}
|
||||
|
||||
return maps;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package org.gcube.common.scope.impl;
|
||||
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Set;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
/**
|
||||
* Classpath discovery utils.
|
||||
*
|
||||
* @author Fabio Simeoni
|
||||
*
|
||||
*/
|
||||
public class Utils {
|
||||
|
||||
// helper: we use reflections' code but exclude extension and primordial
|
||||
// classloaders
|
||||
// whose URLs we do not want to include. especially because these may have
|
||||
// non-standard URLs
|
||||
// that would need to be excluded individually from standard scanning,
|
||||
// or reflections will show (but ignore) a horrible error in the logs. and
|
||||
// we do no know how to predict what we will
|
||||
// find on any given machine
|
||||
static Set<URL> urlsToScan() {
|
||||
final Set<URL> result = Sets.newHashSet();
|
||||
|
||||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
||||
while (classLoader != null && classLoader.getParent() != null) {
|
||||
if (classLoader instanceof URLClassLoader) {
|
||||
URL[] urls = ((URLClassLoader) classLoader).getURLs();
|
||||
if (urls != null) {
|
||||
result.addAll(Sets.<URL> newHashSet(urls));
|
||||
}
|
||||
}
|
||||
classLoader = classLoader.getParent();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package org.gcube.common.scope;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ConfigurationTest {
|
||||
|
||||
private ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||
|
||||
@Test
|
||||
public void alternativeProvidersCanBeConfigured() {
|
||||
|
||||
addJarToClasspath("alternativeprovider.jar");
|
||||
|
||||
assertEquals("AlternativeScopeProvider",ScopeProvider.instance.getClass().getSimpleName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleAlternativeProvidersCannotBeConfigured() {
|
||||
|
||||
try {
|
||||
addJarToClasspath("multiplealternativeproviders.jar");
|
||||
ScopeProvider.instance.set("shouldfail");
|
||||
fail();
|
||||
}
|
||||
catch(Error e) {}
|
||||
|
||||
}
|
||||
|
||||
private void addJarToClasspath(String jar) {
|
||||
|
||||
URL jarURL = loader.getResource(jar);
|
||||
|
||||
URLClassLoader urlClassLoader
|
||||
= new URLClassLoader(new URL[]{jarURL},loader);
|
||||
|
||||
Thread.currentThread().setContextClassLoader(urlClassLoader);
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void teardown() {
|
||||
Thread.currentThread().setContextClassLoader(loader);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
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");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package org.gcube.common.scope;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
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.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ServiceMapTest {
|
||||
|
||||
private static String map = "<service-map scope='scope'>" + "<services>"
|
||||
+ "<service name='service1'>"
|
||||
+ "<endpoint>http://acme.org:8000/service1</endpoint>"
|
||||
+ "<endpoint>http://acme2.org:8000/service1</endpoint>"
|
||||
+ "</service>" + "<service name='service2'>"
|
||||
+ "<endpoint>http://acme3.org:8000/service2</endpoint>"
|
||||
+ "<endpoint>http://acme4.org:8000/service2</endpoint>"
|
||||
+ "</service>" + "</services>" + "</service-map>";
|
||||
|
||||
@Test
|
||||
public void serviceMapsBindCorrectly() throws Exception {
|
||||
|
||||
JAXBContext context = JAXBContext.newInstance(DefaultServiceMap.class);
|
||||
|
||||
DefaultServiceMap serviceMap = (DefaultServiceMap) context
|
||||
.createUnmarshaller().unmarshal(new StringReader(map));
|
||||
|
||||
assertEquals("scope", serviceMap.scope());
|
||||
|
||||
List<String> expected = Arrays.asList("http://acme.org:8000/service1","http://acme2.org:8000/service1");
|
||||
assertEquals(expected, serviceMap.endpoint("service1"));
|
||||
|
||||
expected = Arrays.asList("http://acme3.org:8000/service2","http://acme4.org:8000/service2");
|
||||
assertEquals(expected, serviceMap.endpoint("service2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serviceMapsDiscoveredCorrectly() throws Exception {
|
||||
|
||||
ScopeProvider.instance.set("scope");
|
||||
|
||||
assertNotNull(ServiceMap.instance.endpoint("service1"));
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
ScopeProvider.instance.reset();
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1 @@
|
|||
<service-map scope='scope'><services><service name='service1'><endpoint>http://acme.org:8000/service1</endpoint><endpoint>http://acme2.org:8000/service1</endpoint></service><service name='service2'><endpoint>http://acme3.org:8000/service2</endpoint><endpoint>http://acme4.org:8000/service2</endpoint></service></services></service-map>
|
Loading…
Reference in New Issue