cxf-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Liu, Jervis" <j...@iona.com>
Subject How to write a JSR-311 Aegis EntityProvider
Date Tue, 25 Dec 2007 07:30:59 GMT
Hi Benson, I guess this is a question for you :-). First of all, I have
to admit I don't know much about Aegis binding, spent some time this
morning, looked into unit/system tests and the wiki, but I don't think
I've gathered enough information to get myself started. Basically, what
I am looking for is sth similar to JAXB EntityProvider which I
implemented for JSR-311. See below:

public final class JAXBElementProvider implements EntityProvider<Object>
{

    static Map<Class, JAXBContext> jaxbContexts = new WeakHashMap<Class,
JAXBContext>();

    public boolean supports(Class<?> type) {
        return type.getAnnotation(XmlRootElement.class) != null;
    }

    public Object readFrom(Class<Object> type, MediaType m,
MultivaluedMap<String, String> headers,
                           InputStream is) {
        try {
            JAXBContext context = getJAXBContext(type);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            return unmarshaller.unmarshal(is);
        } catch (JAXBException e) {
            e.printStackTrace();         
        }

        return null;
    }

    public void writeTo(Object obj, MediaType m, MultivaluedMap<String,
Object> headers, OutputStream os) {
        try {
            JAXBContext context = getJAXBContext(obj.getClass());
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
            marshaller.marshal(obj, os);
            
            //TODO: support Calendar type
            //}
        } catch (JAXBException e) {
            //TODO: better exception handling
            e.printStackTrace();
        }
    }

    private JAXBContext getJAXBContext(Class type) throws JAXBException
{
        synchronized (jaxbContexts) {
            JAXBContext context = jaxbContexts.get(type);
            if (context == null) {
                context = JAXBContext.newInstance(type);
                jaxbContexts.put(type, context);
            }
            return context;
        }
    }
}

The AegisEntityProvider I want to write should look like below:

public final class AegisEntityProvider implements EntityProvider<Object>
{
    public boolean supports(Class<?> type) {
        return true;
    }

    public Object readFrom(Class<Object> type, MediaType m,
MultivaluedMap<String, String> headers,
                           InputStream is) {
    }

    public void writeTo(Object obj, MediaType m, MultivaluedMap<String,
Object> headers, OutputStream os) {
    }
}

So for example, the readFrom method is responsible for reading an
inputstream to an Aegis object. The input are 1). Inputstream 2). The
Java class type which we want to read to, for example, we want to read
an inputstream into a Customer object. Based on the information above,
how I can use Aegis binding to read an object? I thought I can do sth
like below:

    public Object readFrom(Class<Object> type, MediaType m,
MultivaluedMap<String, String> headers,
                           InputStream is) {
      AegisDatabinding binding = new AegisDatabinding();
      DataReader reader = binding.createReader(XMLStreamReader.class);
      XMLStreamReader input =
XMLInputFactory.newInstance().createXMLStreamReader(is, encoding);
      Object obj = reader.read(type, input);
      return obj;
    }

Unfortunately org.apache.cxf.aegis.databinding.XMLStreamDataReader does
not have a method whose signature is "public Object read(Class type,
XMLStreamReader input)". Maybe I should use "public Object read(QName
name, XMLStreamReader input, Class type) ", but it is not implemented.

Any suggestions? BTW, there is no hurry for this, do enjoy your
Christmas season.

Merry Christmas,

Jervis 

----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland

Mime
View raw message