Monday, June 26, 2006
Rationale for orienting the API around JAXB.
In Java there are many ways of handling XML: SAX, DOM, StAX, JAXB, etc. This post attempts to outline a rationale for orienting the APIs within TReCX around JAXB in particular.
- standard mature API (due to become "core" in JDK 1.6 "Mustang").
- allows for symmetry i.e. reuse, in the implementation of the publish and store modules. Put another way, the inputs and outputs should be the same, as we're using the same classes on either end!
- allows for separation of XML and persistence layers. This makes unit testing easier.
- the JAXB generated classes form the POJOs that are then enhanced via JPOX to form the persistence-capable classes used in the persistence layer. (At runtime, the classes used by JAXB to perform unmarshalling / marshalling and the ones used by JPOX to handle persistence are one and the same. The persistence-capable nature of the classes is effectively "invisible" to the JAXB runtime).
Put another way, JAXB is more object-oriented and more closely integrated with Java.
Equally, however, it should be noted that if at some point in the future it would make more sense to implement the store module based on something like StAX, for performance or other reasons, then that should be perfectly sensible and possible. It just wouldn't be "symmetrical" with the publish module any more. However, by that stage, the JAXB implementation should have proved it's worth and done it's job!
Saturday, June 10, 2006
Comparing the outputs of JAXB version 1.0 and 2.0.
JAXB is the tool that one can use to generate classes that bind to your XML schema. Since version 2.0
, you can also use it to generate a schema from your classes. For various reasons I'd been happening to compare what you got from running the xjc
(schema to java) tool over our event XML schema between versions of 1.0
and 2.0
of JAXB. This is what you get with JAXB 1.0
...
JAXB 1.0
- " - /s2j/DomainType.java
- " - /s2j/Event.java
- " - /s2j/EventList.java
- " - /s2j/EventListType.java
- " - /s2j/Message.java
- " - /s2j/ObjectFactory.java
- " - /s2j/OperationType.java
- " - /s2j/UserID.java
- " - /s2j/UserIdType.java
- " - /s2j/bgm.ser
- " - /s2j/jaxb.properties
- " - /s2j/impl/EventImpl.java
- " - /s2j/impl/EventListImpl.java
- " - /s2j/impl/EventListTypeImpl.java
- " - /s2j/impl/JAXBVersion.java
- " - /s2j/impl/MessageImpl.java
- " - /s2j/impl/UserIDImpl.java
- " - /s2j/impl/runtime/IdentityHashSet.java
- " - /s2j/impl/runtime/PrefixCallback.java
- " - /s2j/impl/runtime/NamespaceContext2.java
- " - /s2j/impl/runtime/Util.java
- " - /s2j/impl/runtime/XMLSerializable.java
- " - /s2j/impl/runtime/UnmarshallerImpl.java
- " - /s2j/impl/runtime/UnmarshallingEventHandlerAdaptor.java
- " - /s2j/impl/runtime/XMLSerializer.java
- " - /s2j/impl/runtime/UnmarshallableObject.java
- " - /s2j/impl/runtime/AbstractUnmarshallingEventHandlerImpl.java
- " - /s2j/impl/runtime/MSVValidator.java
- " - /s2j/impl/runtime/UnmarshallingContext.java
- " - /s2j/impl/runtime/ValidatorImpl.java
- " - /s2j/impl/runtime/MarshallerImpl.java
- " - /s2j/impl/runtime/GrammarInfoImpl.java
- " - /s2j/impl/runtime/SAXUnmarshallerHandlerImpl.java
- " - /s2j/impl/runtime/ValidatableObject.java
- " - /s2j/impl/runtime/SAXMarshaller.java
- " - /s2j/impl/runtime/NamespaceContextImpl.java
- " - /s2j/impl/runtime/ContentHandlerAdaptor.java
- " - /s2j/impl/runtime/UnmarshallingEventHandler.java
- " - /s2j/impl/runtime/ErrorHandlerAdaptor.java
- " - /s2j/impl/runtime/InterningUnmarshallerHandler.java
- " - /s2j/impl/runtime/GrammarInfoFacade.java
- " - /s2j/impl/runtime/ValidationContext.java
- " - /s2j/impl/runtime/ValidatingUnmarshaller.java
- " - /s2j/impl/runtime/DefaultJAXBContextImpl.java
- " - /s2j/impl/runtime/SAXUnmarshallerHandler.java
- " - /s2j/impl/runtime/Discarder.java
- " - /s2j/impl/runtime/GrammarInfo.java
... and this is what you get with JAXB
2.0
...JAXB 2.0
- " - /s2j/DomainType.java
- " - /s2j/Event.java
- " - /s2j/EventList.java
- " - /s2j/Message.java
- " - /s2j/ObjectFactory.java
- " - /s2j/OperationType.java
- " - /s2j/UserID.java
- " - /s2j/UserIdType.java
... can you spot the difference!!! Now, I'm assuming that all the stuff that was in version
1.0
hasn't disapeared, it's just been packaged up a bit more nicely! JAXB 2.0
makes good use of annotations and thus has a dependancy on JDK 1.5
. I realize that one should just treat the generated outputs as a "black box", but if you do look at the outputs (like I did!) some of the JAXB 1.0
stuff is ghastly to look at (for example, outputting the serialized binary form of an object as a string!). The JAXB 2.0
stuff looks much closer to what you expect, with little added cruft. No prizes for guessing which I prefer!