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