merged from trunk

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/branches/common/common-scope/1.0@73666 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
fabio.simeoni 2013-04-19 11:42:35 +00:00
parent d989de4e64
commit 62c5427cca
6 changed files with 63 additions and 108 deletions

View File

@ -29,8 +29,7 @@ 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
https://gcube.wiki.gcube-system.org/gcube/index.php/Common-scope
Licensing
---------

View File

@ -2,10 +2,14 @@
<Changeset component="common-scope-1.0.0" date="2012-1-27">
<Change>First Release</Change>
</Changeset>
<Changeset component="${build.finalName}" date="2013-01-11">
<Changeset component="common-scope-1.1.0" date="2013-01-11">
<Change>Scope can now be propagated in Callable and Runnable tasks via ScopedTasks</Change>
<Change>Scopes can now be manipulated through ScopeBean objects</Change>
<Change>Scope maps are now versioned and newer versions take precedence at startup</Change>
<Change>Scope maps can now be successfully looked up in VRE scopes (bug fix)</Change>
</Changeset>
<Changeset component="${build.finalName}" date="2013-01-27">
<Change>Replaced Google's reflections with common-configuration-scanner</Change>
<Change>Lowered log level on scope settings to DEBUG</Change>
</Changeset>
</ReleaseNotes>

29
pom.xml
View File

@ -11,7 +11,7 @@
<groupId>org.gcube.core</groupId>
<artifactId>common-scope</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>
<name>Common Scope</name>
<description>Scope-related APIs</description>
@ -25,50 +25,39 @@
<url>http://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/${project.artifactId}</url>
</scm>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>maven-bom</artifactId>
<version>[1.0.0-SNAPSHOT,)</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.8</version>
<groupId>org.gcube.core</groupId>
<artifactId>common-configuration-scanner</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-scope-maps</artifactId>
<version>[1.0.0-SNAPSHOT,)</version>
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
<version>1.7.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
<version>1.7.2</version>
<scope>test</scope>
</dependency>

View File

@ -35,14 +35,14 @@ public class DefaultScopeProvider implements ScopeProvider {
@Override
public void set(String scope) {
if (scope!=null)
log.info("setting scope {} in thread {}",scope,Thread.currentThread().getId());
log.debug("setting scope {} in thread {}",scope,Thread.currentThread().getId());
scopes.set(scope);
}
@Override
public void reset() {
log.info("resetting scope in thread {}",Thread.currentThread().getId());
log.debug("resetting scope in thread {}",Thread.currentThread().getId());
scopes.remove();
}

View File

@ -2,77 +2,81 @@ package org.gcube.common.scope.impl;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.Set;
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;
/**
* 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();
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 ResourcesScanner());
Set<String> resources = getMapNames();
Reflections reflections = new Reflections(builder);
JAXBContext context = JAXBContext.newInstance(DefaultServiceMap.class);
Unmarshaller um = context.createUnmarshaller();
for (String resource : reflections.getResources(Pattern
.compile(mapConfigPattern))) {
URL url = Thread.currentThread().getContextClassLoader()
.getResource(resource);
log.info("loading {} ", url);
DefaultServiceMap map = (DefaultServiceMap) um.unmarshal(url);
ServiceMap current = maps.get(map.scope());
if (current!=null && current.version()!=null)
if (current.version().compareToIgnoreCase(map.version())==1) {
log.warn("discarding {} because older (v.{}) than one previously loaded (v.{}) for {} ", new Object[]{url, map.version(), current.version(), map.scope()});
continue;
}
else
log.info("overwriting older map (v.{}) with newer map (v.{}) for {} ", new Object[]{current.version(), map.version(), map.scope()});
maps.put(map.scope(), map);
}
} catch (Exception e) {
throw new RuntimeException("could not load service maps", e);
for (String resource : resources) {
URL url = Thread.currentThread().getContextClassLoader().getResource(resource);
log.info("loading {} ", url);
DefaultServiceMap map = (DefaultServiceMap) um.unmarshal(url);
ServiceMap current = maps.get(map.scope());
if (current != null && current.version() != null)
if (current.version().compareToIgnoreCase(map.version()) == 1) {
log.warn("discarding {} because older (v.{}) than one previously loaded (v.{}) for {} ",
new Object[] { url, map.version(), current.version(), map.scope() });
continue;
} else
log.info("overwriting older map (v.{}) with newer map (v.{}) for {} ",
new Object[] { current.version(), map.version(), map.scope() });
maps.put(map.scope(), map);
}
return maps;
} catch (Exception 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,41 +0,0 @@
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;
}
}