Monday, February 23, 2009

BPEL - Embedded Java - JAXB

A simple how-to on leveraging JAXB within your BPEL process - with help from my colleague Flavius!

Step 1 - create a project in JDev to hold the JAXB artifacts for your XSD e.g. order.xsd
Menu --> Tools--> JAXB Compilation

Jar up the classes e.g. orderjaxb.jar

Step 2 - create the BPEL project e.g. with input/output set to order from order.xsd
Add an Assign to copy input to output
After the Assign, add the Java embedding with the following code -

addAuditTrailEntry("Java Embedding JAXB");
try{

// get the output Order element
Element element = (Element)getVariableData("outputVariable", "payload", "/ns1:order");
// Create the JAXB Context for Order
JAXBContext jc = JAXBContext.newInstance("orderjaxb");
Unmarshaller u = jc.createUnmarshaller();
// Cast the output order element as an object of class Order
Order order = (Order)u.unmarshal((org.w3c.dom.Node)element);

// Display customer
addAuditTrailEntry("Order: " + order.getCustomer());
}



catch (Exception e){
addAuditTrailEntry("Java embedding Exception thrown...." + e.toString());
}

Add the imports -





Add the following 2 libraries to your BPEL project (via project properties)
Oracle XMLParser V2
orderJaxb (Create a new library entry pointing to the jar file created in step 1)

That's it!