This recipe explains how to generate XML facade with the use of the Apache ANT utility. We use the ANT scripts when we want to build or rebuild the XML facade in many iterations, for example, every time during nightly builds. Using ANT to build XML façade is very useful when XML definition changes are constantly in phases of development. With ANT, we can ensure continuous synchronization between XML and generated Java code.
The official ANT homepage along with detailed information on how to use it can be found at the following URL: http://ant.apache.org/.
By completing our previous recipe, we built up a JDeveloper project ready to create XML facade out of XML schema. To complete this recipe, we need to add ANT project technology to the project. We achieve this through the Project Properties dialog:
Here are the steps we need to take to create a project in JDeveloper for building XML façade with ANT:
build.xml
file with the code to build XML facade. <property name="schema_file" location="../Banking_BPEL/xsd/Derivative_Cashflow.xsd"/> <property name="dest_dir" location="./src"/> <property name="package" value="org.packt.cashflow.facade"/>
<target name="xjc"> <delete dir="src"/> <mkdir dir="src"/> <echo message="Compiling the schema..." /> <exec executable="xjc"> <arg value="-xmlschema"/> <arg value="${schema_file}"/> <arg value="-d"/> <arg value="${dest_dir}"/> <arg value="-p"/> <arg value="${package}"/> </exec> </target>
ANT is used as a build tool and performs various tasks. As such, we can easily use it to build XML facade. Java Architecture for XML Binding provides the xjc
utility, which can help us in building XML facade.
We have provided the following parameters to the xjc
utility:
Xmlschema
: This is the threat input schema as XML schemad
: This specifies the destination directory of the generated classesp
: This specifies the package name of the generated classesThere are a number of other parameters, however we will not go into detail about them here. Based on the parameters we provided to the xjc
utility, the Java representation of the XML schema is generated. If we examine the generated classes, we can see that there exists a Java class for every type defined in the XML schema. Also, we can see that the ObjectFactory
class is generated, which eases the generation of Java class instances.
There is a difference in creating XML facade between Versions 10g and 11g of Oracle SOA Suite. In Oracle SOA Suite 10g, there was a convenient utility named schema, which is used for building XML facade. However, in Oracle SOA Suite 11g, the schema utility is not available anymore.
To provide a similar solution, we create a template class, which is later copied to a real code package when needed to provide functionality for XML facade. We create a new class Facade
in the called facade
package. The only method in the class is static and serves as a creation point of facade
:
public static Object createFacade(String context, XMLElement doc) throws Exception { JAXBContext jaxbContext; Object zz= null; try { jaxbContext = JAXBContext.newInstance(context); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); zz = unmarshaller.unmarshal(doc); return zz; } catch (JAXBException e) { throw new Exception("Cannot create facade from the XML content. " + e.getMessage()); } }
The class code implementation is simple and consists of creating the JAXB context. Further, we un-marshall the context and return the resulting class to the client. In case of problems, we either throw an exception or return a null object.
Now the calling code is trivial. For example, to create XML facade for the XML content, we call as follows:
Object zz = facade.Facade.createFacade("org.packt.cashflow.facade", document.getSrcRoot());