git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/branches/common/common-gcore-resources/1.3@132321 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
5385c20032
commit
973b95aa99
|
@ -1,2 +1,6 @@
|
||||||
${gcube.license}
|
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.
|
||||||
|
|
BIN
distro/README
BIN
distro/README
Binary file not shown.
4
pom.xml
4
pom.xml
|
@ -8,7 +8,7 @@
|
||||||
</parent>
|
</parent>
|
||||||
<groupId>org.gcube.resources</groupId>
|
<groupId>org.gcube.resources</groupId>
|
||||||
<artifactId>common-gcore-resources</artifactId>
|
<artifactId>common-gcore-resources</artifactId>
|
||||||
<version>1.3.1-SNAPSHOT</version>
|
<version>1.3.2-SNAPSHOT</version>
|
||||||
|
|
||||||
<scm>
|
<scm>
|
||||||
<connection>scm:svn:http://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/${project.artifactId}</connection>
|
<connection>scm:svn:http://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/${project.artifactId}</connection>
|
||||||
|
@ -19,7 +19,7 @@
|
||||||
<properties>
|
<properties>
|
||||||
<distroDirectory>distro</distroDirectory>
|
<distroDirectory>distro</distroDirectory>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
|
|
|
@ -20,6 +20,11 @@ public class GenericResource extends Resource {
|
||||||
this.type(Type.GENERIC);
|
this.type(Type.GENERIC);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GenericResource(String id) {
|
||||||
|
this();
|
||||||
|
this.setId(id);
|
||||||
|
}
|
||||||
|
|
||||||
@XmlElementRef
|
@XmlElementRef
|
||||||
private Profile profile;
|
private Profile profile;
|
||||||
|
|
||||||
|
|
|
@ -96,7 +96,7 @@ public abstract class Resource {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setId(String id){
|
public void setId(String id){
|
||||||
this.id=id;
|
this.id=id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,10 +41,10 @@ public class Resources {
|
||||||
|
|
||||||
//cached schemas
|
//cached schemas
|
||||||
private static Map<Class<?>,Schema> schemas = new HashMap<Class<?>, Schema>();
|
private static Map<Class<?>,Schema> schemas = new HashMap<Class<?>, Schema>();
|
||||||
|
|
||||||
private static Map<Class<?>, JAXBContext> contexts = new HashMap<Class<?>, JAXBContext>();
|
private static Map<Class<?>, JAXBContext> contexts = new HashMap<Class<?>, JAXBContext>();
|
||||||
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
|
|
||||||
schemaFactory.setResourceResolver(new SchemaResolver());
|
schemaFactory.setResourceResolver(new SchemaResolver());
|
||||||
|
@ -92,22 +92,23 @@ public class Resources {
|
||||||
public static void validate(Resource resource) throws IllegalArgumentException, Exception {
|
public static void validate(Resource resource) throws IllegalArgumentException, Exception {
|
||||||
|
|
||||||
Schema schema = schema(resource.getClass());
|
Schema schema = schema(resource.getClass());
|
||||||
|
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(marshal(resource,out).toByteArray());
|
ByteArrayInputStream in = new ByteArrayInputStream(marshal(resource,out).toByteArray());
|
||||||
|
|
||||||
schema.newValidator().validate(new StreamSource(in));
|
schema.newValidator().validate(new StreamSource(in));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//helper
|
//helper
|
||||||
private static synchronized Schema schema(Class<?> resourceClass) throws Exception {
|
private static synchronized Schema schema(Class<?> resourceClass) throws Exception {
|
||||||
|
|
||||||
String schemaResource = schemaResources.get(resourceClass);
|
String schemaResource = schemaResources.get(resourceClass);
|
||||||
|
|
||||||
if (schemaResource==null)
|
if (schemaResource==null)
|
||||||
throw new IllegalArgumentException("no known schema for:\n "+resourceClass);
|
throw new IllegalArgumentException("no known schema for:\n "+resourceClass);
|
||||||
|
|
||||||
Schema schema = schemas.get(resourceClass);
|
Schema schema = schemas.get(resourceClass);
|
||||||
if (schema==null) {
|
if (schema==null) {
|
||||||
InputStream stream = Resources.class.getClassLoader().getResourceAsStream(schemaResource);
|
InputStream stream = Resources.class.getClassLoader().getResourceAsStream(schemaResource);
|
||||||
|
@ -123,22 +124,22 @@ public class Resources {
|
||||||
* @param stream the stream in input
|
* @param stream the stream in input
|
||||||
*/
|
*/
|
||||||
public static <T extends OutputStream> T marshal(Object resource,T stream) {
|
public static <T extends OutputStream> T marshal(Object resource,T stream) {
|
||||||
|
|
||||||
marshal(resource, new StreamResult(stream));
|
marshal(resource, new StreamResult(stream));
|
||||||
return stream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write the serialisation of a given resource to a given character stream.
|
* Write the serialisation of a given resource to a given character stream.
|
||||||
* @param resource the resource
|
* @param resource the resource
|
||||||
* @param stream the stream in input
|
* @param stream the stream in input
|
||||||
*/
|
*/
|
||||||
public static <T extends Writer> T marshal(Object resource,T stream) {
|
public static <T extends Writer> T marshal(Object resource,T stream) {
|
||||||
|
|
||||||
marshal(resource,new StreamResult(stream));
|
marshal(resource,new StreamResult(stream));
|
||||||
return stream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write the serialisation of a given resource to a {@link Result}.
|
* Write the serialisation of a given resource to a {@link Result}.
|
||||||
* @param resource the resource
|
* @param resource the resource
|
||||||
|
@ -146,16 +147,16 @@ public class Resources {
|
||||||
* @return the result in input
|
* @return the result in input
|
||||||
*/
|
*/
|
||||||
public static <T extends Result> T marshal(Object resource,T result) {
|
public static <T extends Result> T marshal(Object resource,T result) {
|
||||||
|
|
||||||
if (resource instanceof Resource)
|
if (resource instanceof Resource)
|
||||||
((Resource) resource).lock.lock();
|
((Resource) resource).lock.lock();
|
||||||
try {
|
try {
|
||||||
JAXBContext context = context(resource.getClass());
|
JAXBContext context = context(resource.getClass());
|
||||||
Marshaller m = context.createMarshaller();
|
Marshaller m = context.createMarshaller();
|
||||||
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||||
|
|
||||||
m.marshal(resource,result);
|
m.marshal(resource,result);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
catch(Exception e) {
|
catch(Exception e) {
|
||||||
|
@ -164,7 +165,7 @@ public class Resources {
|
||||||
if (resource instanceof Resource)
|
if (resource instanceof Resource)
|
||||||
((Resource) resource).lock.unlock();
|
((Resource) resource).lock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -195,7 +196,7 @@ public class Resources {
|
||||||
public static <T> T unmarshal(Class<T> resourceClass, InputStream stream) {
|
public static <T> T unmarshal(Class<T> resourceClass, InputStream stream) {
|
||||||
return unmarshal(resourceClass,new StreamSource(stream));
|
return unmarshal(resourceClass,new StreamSource(stream));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a resource of given class from its serialisation in a given {@link Source}.
|
* Creates a resource of given class from its serialisation in a given {@link Source}.
|
||||||
* @param resourceClass the class of the resource
|
* @param resourceClass the class of the resource
|
||||||
|
@ -212,16 +213,16 @@ public class Resources {
|
||||||
throw new RuntimeException("deserialisation error",e);
|
throw new RuntimeException("deserialisation error",e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//helper
|
//helper
|
||||||
private static synchronized JAXBContext context(Class<?> resourceClass) throws Exception {
|
private static synchronized JAXBContext context(Class<?> resourceClass) throws Exception {
|
||||||
|
|
||||||
JAXBContext ctx = contexts.get(resourceClass);
|
JAXBContext ctx = contexts.get(resourceClass);
|
||||||
if (ctx==null) {
|
if (ctx==null) {
|
||||||
ctx = JAXBContext.newInstance(resourceClass);
|
ctx = JAXBContext.newInstance(resourceClass);
|
||||||
contexts.put(resourceClass,ctx);
|
contexts.put(resourceClass,ctx);
|
||||||
}
|
}
|
||||||
return ctx;
|
return ctx;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,6 +54,8 @@ public class GCoreEndpointTest {
|
||||||
endpoint.profile().newDeploymentData().activationTime(Calendar.getInstance());
|
endpoint.profile().newDeploymentData().activationTime(Calendar.getInstance());
|
||||||
endpoint.profile().endpoints().add().nameAndAddress("name",URI.create("http://acme.org"));
|
endpoint.profile().endpoints().add().nameAndAddress("name",URI.create("http://acme.org"));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return endpoint;
|
return endpoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue