Use the JAXB object classes generated by a Java™ Architecture for XML Binding (JAXB) compiler
by using the JAXB class template.
About this task
You can use Java Architecture
for XML Binding (JAXB) with a JavaCompute node to process
your messages by accessing, creating, and manipulating JAXB Java object classes that you
generate from your message model schema files. JAXB Java object classes are a Java object representation of your message,
and can be used with Java code
completion. The Process via JAXB class template
in the New Java Compute Node Class wizard
generates template code for processing your messages by using
JAXB Java object classes. If
you also used the New Java Compute Node Class wizard
either to generate your JAXB Java object
classes, or to reference your existing JAXB Java object classes, step 1 might be completed
for you.
To process
message body data by using JAXB Java object
classes, complete the following steps:
Procedure
- Optional: Modify the
onInitialize () method
to reference the package or packages that contain your Java object binding classes. public void onInitialize() throws MbException {
try {
// TODO Update context path "com.example.jaxb" to be the package of your
// Java object classes that were generated by a Java Architecture for XML
// Binding (JAXB) binding compiler
jxbcntxt = JAXBContext.newInstance("com.example.jaxb");
Replace com.example.jaxb with
your JAXB Java object bindings
classes package.
Note: When you
deploy or restart your message flow, the
onInitialize () method
is called only once, and initializes JAXB context for all processing
threads.
If an error is thrown by the generation of the JAXB context,
the code that is provided in the "Process via JAXB class" template
throws an MbUserException, and the deployment operation
fails. If the deployment operation fails, inspect the error message
that is recorded in the host system event log to determine the cause.
The most likely cause is that one or more package names cannot be
resolved, or that one or more of the packages you specified do not
contain the required JAXB object factory or bindings properties.
- Modify the
evaluate() method to process
each message that is passed in a MbMessageAssembly object.
The MbMessageAssembly object is defined in the Java user-defined node API.
- The first section of the
evaluate() method
extracts the message data from the assembly, creates an output assembly
and copies message headers. For more information, see Creating a message processing or output node
in Java. public void onInitialize() throws MbException {
try {
// TODO Update context path "com.example.jaxb" to be the package of your
// Java object classes that were generated by a Java Architecture for XML
// Binding (JAXB) binding compiler
jaxbContext = JAXBContext.newInstance("com.example.jaxb");
} catch (JAXBException e) {
// This exception will cause the deploy of this Java compute node to fail
// Typical cause is the JAXB package above is not available
MbUserException mbue = new MbUserException(this, "onInitialize()",
"", "", e.toString(), null);
throw mbue;
}
}
- The next part in the
evaluate() method unmarshals the
input message data into the set of Java objects
that are bound from the message schema. Unmarshalling creates a Java object model copy of the message
data that is in memory. try {
// unmarshal the input message data from the message tree into your Java object classes
Object inMsgJavaObj = jaxbContext.createUnmarshaller().unmarshal(inMessage.getDOMDocument());
- The next part in the
evaluate() method
is where you insert your own JAXB code to create or update the output
message by processing message data. See external resources for details
of how to program transformations by using JAXB Java object classes. For information about debugging
in Java, see Java Debugger.
// ----------------------------------------------------------
// Add user code below to build the new output data by updating
// your Java objects or building new Java objects
Object outMsgJavaObj = inMsgJavaObj;
// End of user Java object processing
// ----------------------------------------------------------
The following example shows some JAXB
code that adds a record to a CSV message, and sets the value of the
three fields in that record:
// Example: add a record of fixed data
CsvMsg outMsgJavaObj = (CsvMsg) inMsgJavaObj;
Record additionalCsvRecord = new Record();
additionalCsvRecord.setField1("My new field 1 text");
additionalCsvRecord.setField2("My new field 2 text");
additionalCsvRecord.setField3("My new field 3 text");
outMsgJavaObj.getRecord().add(additionalCsvRecord);
// end example
- The next part in the
evaluate() method marshals the
processed Java object classes
back into the Integration node message tree. Modify this code to pass
the relevant Java object in
place of the template default. If your JAXB transformation code updated
the input message, pass the Java object
class that was created during unmarshalling into the marshalling code.
If you are creating an output message, pass the Java object class for your new output message
into the marshalling code. The template sets the output
domain to XMLNSC; if you require the output message to be in the DFDL
or SOAP domains, you must modify this code.
// TODO set the required
domain for the output message, eg XMLNSC
Document outDocument = outMessage.createDOMDocument(MbXMLNSC.PARSER_NAME);
// marshal the new or updated output Java object class into the message tree
jaxbContext.createMarshaller().marshal(outMsgJavaObj, outDocument);