branched for 1.x releases
git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/branches/common/common-gcube-calls/1.0@83987 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
commit
50835f635d
|
@ -0,0 +1,36 @@
|
|||
<?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 excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<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"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
|
@ -0,0 +1,23 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>common-gcube-calls</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,7 @@
|
|||
#Fri Oct 11 15:08:46 CEST 2013
|
||||
eclipse.preferences.version=1
|
||||
encoding//src/main/java=UTF-8
|
||||
encoding//src/main/resources=UTF-8
|
||||
encoding//src/test/java=UTF-8
|
||||
encoding//src/test/resources=UTF-8
|
||||
encoding/<project>=UTF-8
|
|
@ -0,0 +1,13 @@
|
|||
#Fri Oct 11 15:08:46 CEST 2013
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
|
@ -0,0 +1,5 @@
|
|||
#Wed Oct 09 11:35:36 CEST 2013
|
||||
activeProfiles=
|
||||
eclipse.preferences.version=1
|
||||
resolveWorkspaceProjects=true
|
||||
version=1
|
|
@ -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,2 @@
|
|||
* Lucio Lelii (lucio.lelii@isti.cnr.it), CNR, Italy
|
||||
* Fabio Simeoni (fabio.simeoni@fao.org), FAO of the UN, Italy
|
|
@ -0,0 +1,39 @@
|
|||
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
|
||||
-------
|
||||
|
||||
* Lucio Lelii (lucio.lelii@isti.cnr.it), CNR, Italy
|
||||
* 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/....
|
||||
|
||||
|
||||
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="2013-10-24">
|
||||
<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,91 @@
|
|||
<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>
|
||||
<groupId>org.gcube.tools</groupId>
|
||||
<artifactId>maven-parent</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
<groupId>org.gcube.core</groupId>
|
||||
<artifactId>common-gcube-calls</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
|
||||
<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.gcube.core</groupId>
|
||||
<artifactId>common-scope</artifactId>
|
||||
<version>[1.1.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.5</version>
|
||||
</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,13 @@
|
|||
package org.gcube.common.calls;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Call {
|
||||
|
||||
private Map<String, Object> properties = new HashMap<String, Object>();
|
||||
|
||||
public Map<String, Object> properties() {
|
||||
return properties;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package org.gcube.common.calls;
|
||||
|
||||
public interface Interceptor {
|
||||
|
||||
void handleRequest(Request context, Call callContext);
|
||||
|
||||
void handleResponse(Response context, Call callContext);
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package org.gcube.common.calls;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
public class Interceptors {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(Interceptors.class);
|
||||
|
||||
private final static List<Interceptor> interceptors = new ArrayList<Interceptor>();
|
||||
|
||||
static {
|
||||
ServiceLoader<Interceptor> loader = ServiceLoader.load(Interceptor.class);
|
||||
Iterator<Interceptor> it = loader.iterator();
|
||||
while (it.hasNext())
|
||||
try {
|
||||
Interceptor handler = it.next();
|
||||
log.info("loaded interceptor {}",handler);
|
||||
interceptors.add(handler);
|
||||
}
|
||||
catch(Error e) {
|
||||
log.error("could not load interceptors",e);
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Interceptor> getInterceptors(){
|
||||
return interceptors;
|
||||
}
|
||||
|
||||
public static Request executeRequestChain(Call call){
|
||||
log.trace("executing request chain");
|
||||
Request context = new Request();
|
||||
for (Interceptor interceptor : interceptors)
|
||||
interceptor.handleRequest(context, call);
|
||||
return context;
|
||||
}
|
||||
|
||||
public static Response executeResponseChain(Call call){
|
||||
Response context = new Response();
|
||||
for (Interceptor interceptor : interceptors)
|
||||
interceptor.handleResponse(context, call);
|
||||
return context;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package org.gcube.common.calls;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public abstract class Message {
|
||||
|
||||
private Map<String, String> headers = new HashMap<String, String>();
|
||||
|
||||
public void addHeader(String key, String value){
|
||||
headers.put(key, value);
|
||||
}
|
||||
|
||||
public Set<Entry<String, String>> getHeaders(){
|
||||
return Collections.unmodifiableSet(headers.entrySet());
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package org.gcube.common.calls;
|
||||
|
||||
public class Request extends Message {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package org.gcube.common.calls;
|
||||
|
||||
public class Response extends Message{
|
||||
|
||||
}
|
|
@ -0,0 +1,120 @@
|
|||
package org.gcube.common.calls;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Privately used by a {@link StubFactory}, caches {@link Service} instances for services with given names.
|
||||
* <p>
|
||||
* The cache is LRU and bounded at a maximum size of {@link LRUCache#max}. It is also thread-safe, though synchronisation
|
||||
* occurs on a per-key basis.
|
||||
*
|
||||
* @author Fabio Simeoni, Lucio Lelii
|
||||
* @see ServiceCache
|
||||
*/
|
||||
class ServiceCache<T> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ServiceCache.class);
|
||||
|
||||
private volatile LRUCache cache = new LRUCache();
|
||||
|
||||
//holds key locks for LRU map
|
||||
private ConcurrentHashMap<Call,Lock> nameLocks = new ConcurrentHashMap<Call, Lock>();
|
||||
|
||||
void clear(Call name) {
|
||||
|
||||
//obtain a lock for current key
|
||||
Lock nameLock = lockFor(name);
|
||||
|
||||
nameLock.lock();
|
||||
|
||||
try {
|
||||
cache.remove(name);
|
||||
}
|
||||
finally {
|
||||
nameLock.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
T get(Call name,Callable<T> task) {
|
||||
|
||||
//obtain a lock for current key
|
||||
Lock nameLock = lockFor(name);
|
||||
|
||||
nameLock.lock();
|
||||
|
||||
try {
|
||||
|
||||
T service = cache.get(name);
|
||||
|
||||
if (service==null)
|
||||
try {
|
||||
service= task.call();
|
||||
log.trace("caching stub for "+name);
|
||||
cache.put(name,service);
|
||||
}
|
||||
catch(Exception e) {
|
||||
throw new RuntimeException("could not build service",e);
|
||||
}
|
||||
else {
|
||||
log.trace("using cached stub for "+name);
|
||||
}
|
||||
|
||||
return service;
|
||||
}
|
||||
finally {
|
||||
nameLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
//helper
|
||||
private Lock lockFor(Call name) {
|
||||
|
||||
Lock nameLock = nameLocks.get(name);
|
||||
|
||||
//no need to create a new lock a priori
|
||||
if (nameLock==null) {
|
||||
|
||||
Lock newLock = new ReentrantLock();
|
||||
|
||||
//get name lock, creating it if it doesn't exist
|
||||
//this is where we first synchronise: second-come thread waits for new one to have put a lock
|
||||
//then it gets that same shared lock
|
||||
nameLock = nameLocks.putIfAbsent(name,newLock);
|
||||
|
||||
nameLock = nameLock == null? newLock : nameLock;
|
||||
}
|
||||
|
||||
return nameLock;
|
||||
}
|
||||
|
||||
private class LRUCache extends LinkedHashMap<Call,T> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static final int max = 50;
|
||||
|
||||
public LRUCache() {
|
||||
//use defaults, but indicate accessor-order as 3rd parameter (rather than default insertion order)
|
||||
super(16,.75f,true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean removeEldestEntry(java.util.Map.Entry<Call, T> eldest) {
|
||||
if (size()>=max) {
|
||||
nameLocks.remove(eldest.getKey());
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package org.gcube.common.calls.interceptors;
|
||||
|
||||
import org.gcube.common.calls.Call;
|
||||
import org.gcube.common.calls.Interceptor;
|
||||
import org.gcube.common.calls.Request;
|
||||
import org.gcube.common.calls.Response;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ScopeInterceptor implements Interceptor {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(ScopeInterceptor.class);
|
||||
|
||||
public static final String scope_header="gcube-scope";
|
||||
|
||||
public void handleRequest(Request request, Call call) {
|
||||
|
||||
String scope = ScopeProvider.instance.get();
|
||||
|
||||
if (scope==null)
|
||||
throw new RuntimeException("call is unscoped");
|
||||
|
||||
request.addHeader(scope_header, scope);
|
||||
|
||||
logger.trace("scope set in the header is "+scope);
|
||||
|
||||
}
|
||||
|
||||
public void handleResponse(Response context, Call callContext) {}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
org.gcube.common.calls.interceptors.ScopeInterceptor
|
Loading…
Reference in New Issue