Jaxb Win
Published:
A JAXB Success Story
Our team recently achieved a significant milestone with JAXB, and I wanted to share the journey. Previously, we had been writing rules against Java classes generated from XSDs. However, instead of working directly with these generated classes, we relied on an additional layer of models built on top of them. While functional, this approach introduced a major challenge:
- Challenge: When a rule was triggered, it needed to return the object that caused the trigger. From this object, we also had to determine the fully qualified path to the corresponding XML element.
- Problem: The second-layer models lacked a direct connection to the original XML elements.
- Initial Attempt: We tried using a
Map<Object, Object>
to link the second-layer models to the original XML elements, but this was far from ideal.
The Solution: Simplify and Streamline
After evaluating the situation, I realized the best solution was to eliminate the second layer of models entirely and write rules directly against the generated classes. This transition was straightforward due to the similarities between the original and second-layer models. The benefits were immediate:
- Improved Clarity: Rules became more representative of the actual XML data.
- Effortless Path Retrieval: With rules returning the original model objects, obtaining the fully qualified path to XML elements became trivial.
- Team Buy-In: Convincing the team and leadership to adopt this change was surprisingly easy. The simplicity and clarity of the new approach made it an obvious improvement.
Formalizing the JAXB Model Generation Process
To further enhance the workflow, I formalized the JAXB model generation process. Initially, I was handed a zip file containing scripts and jars for a bare-minimum xjc
generation of classes, accompanied by sparse documentation. To address this:
- I created a Maven project using the HighSource JAXB Tools plugin.
- This setup allowed us to generate JAXB classes from XSDs in a robust and customizable way.
- The plugin provided invaluable features, such as auto-generated builders, which significantly streamlined development.
Results and Impact
Once the team approved the changes, it took just one day to integrate everything into the development branch using the original model. Key outcomes included:
- Backward Compatibility: Old rules continued to function as before.
- Enhanced Workflow: New rules could now be written directly against the original model.
- Developer Support: I worked closely with the rules developers to optimize their workflow and improve their experience when writing and testing rules.
The team has been thrilled with the improvements. The new rules are easier to write, maintain, and understand.