Generating XML facade using ANT

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/.

Getting ready

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:

Getting ready

How to do it...

Here are the steps we need to take to create a project in JDeveloper for building XML façade with ANT:

  1. Create a new ANT build file by right-clicking on the CashflowFacade project node, select New, and choose Buildfile from Project (Ant):
    How to do it...
  2. The ANT build file is generated and added into the project under the Resources folder. Now we need to amend the build.xml file with the code to build XML facade.
  3. We will first define the properties for our 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"/>
  4. We define the location of the source XML schema (it is located in the BPEL process). Next, we define the destination of the generated Java files and the name of the package.
  5. Now, we define the ANT target in order to build XML facade classes. The ANT target presents one closed unit of ANT work. We define the build task for the XML façade as follows:
    <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>
  6. Now we have XML facade packaged and ready to be used in BPEL processes.

How it works…

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 schema
  • d: This specifies the destination directory of the generated classes
  • p: This specifies the package name of the generated classes

There 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's more...

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());

See also

To learn how to create facade using JDeveloper from XSD (XML Schema Definition Language), refer to the next recipe—Creating XML facade from XSD.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset