On Aug 8, 2009, at 7:27 PM, Morten wrote:
> --- Den lør 8/8/09 skrev Paul Sandoz <Paul.Sandoz_at_Sun.COM>:
>> To configure validation you will need to use a
>> ContextResolver [1] using either JAXBContext or a
>> Unmarshaller, that returns an Unmarshaller for the types you
>> want to validate with validation enabled.
>
> As a JAXBContext is expensive and needs configuration that should
> not duplicate Jersey's configuration of JAXB enabled objects, how do
> a resolver of Unmarshaller reuse the existing JAXBContext instance
> used by Jersey?
>
Currently it cannot.
> Besides this use case, I can see a lot of benefits of being able to
> access and manipulate the concrete JAXBContext used by Jersey.
>
Note that the context created by Jersey may be too restrictive. The
code in AbstractJAXBProvider does the following:
protected JAXBContext getStoredJAXBContext(Class type) throws
JAXBException {
synchronized (jaxbContexts) {
JAXBContext c = jaxbContexts.get(type);
if (c == null) {
c = JAXBContext.newInstance(type);
jaxbContexts.put(type, c);
}
return c;
}
}
It creates a JAXB context from the type. JAXBContext is immutable so
can only be manipulated by wrapping.
In certain cases you may want to create your own context that utilizes
the package name or a set of classes such that XML can be correctly
marshaled or unmarshaled, as well as setting certain properties on the
created JAXB context.
In addition, if you always use a ContextResolver for your JAXB types
there will be no JAXBContext instances created by Jersey. So there
will be no duplication.
Given the above points i do not think there is much value exposing
Jersey's set of JAXBContext's it has internally created for certain
types.
Paul.