The Role of Zeus Data Binding
in Lutris EAS 4.1

This article presents a high level view of XML data binding and how it is used in Lutris Technologies' application server, Lutris EAS. This is not an in-depth article by any means, so I will point you to various resources at the bottom of this page. We will, however, discuss:

  1. "What is XML data binding?"
  2. Zeus, an open source XML data binding tool
  3. How XML data binding is used in Lutris EAS.

What is XML Data Binding?

In a nutshell, XML data binding is the process of converting XML constraints, like DTDs and XML Schema, to "some other" representation. In terms of the Java platform and the Java language, the "some other" representation is a set of Java classes. These classes (and possibly some support class) allow you to unmarshal XML data to Java objects, manipulate those objects through accessors and mutators, and then marshal the objects back to XML.

To summarize, the key functions involved in XML data binding are:

  1. Binding constraints to Java classes.
  2. Unmarshaling XML data to Java objects.
  3. Marshaling Java objects to XML data.

So if you find yourself writing classes that use SAX, DOM, or JDOM to read information from some XML input, manipulate those values, and then output the updated data to XML, then XML data binding could be a valuable time saving tool for you.

Note that Sun Microsystems has defined a specification for XML data binding called JAXB (one of many XML related specification offered by Sun). A reference implementation is also provided. We'll discuss the relationship of Zeus and JAXB later in this article.

XML Data Binding with Zeus

Though there are many XML data binding tools available, we will quickly discuss Zeus in particular because it is open source and because it is the tool used in Lutris EAS. The Zeus project is hosted on Enhydra.org, and it was founded by Brett McLaughlin (of JDOM fame). Zeus currently has very good support for DTDs, and early support for XML Schema. We will examine Zeus by looking at how one performs the above mentioned fuctions involved in XML data binding using the Zeus tool.

Note that setup (e.g. CLASSPATH setting, etc.) and many other options provided by Zeus are not covered here. See the Resources section at the end for a pointer to a much more detailed Zeus user's guide. We'll assume we are working with a DTD called my-dtd.dtd.

Binding Constraints

Zeus converts DTDs to Java source through the DTDSourceGenerator class. Simply invoke the class

java org.enhydra.zeus.util.DTDSourceGenerator -constraints=my-dtd.dtd

The -constraints argument specifies the location of the XML constraints you are using.

The output from running this command is a set of Java source files that represent your DTD. After compiling the source, you can use the generated classes as you would any other Java code.

Unmarshaling

To umarshal some input stream of XML data to the Java objects you created, simply use the generated unmarshaller class. For example, the following code snippet:

MyObject o = MyObjectUnmarshaller.unmarshal(new File("my-xml.xml"), false);

unmarshals the input XML stream (located in the file 'my-xml.xml') to the object MyObject. MyObject would have various generated accessor and mutator methods that lets you access and manipulate the data in my-xml.xml.

Marshaling

To marshal your object back to XML (possibly after having changed various values), simply use the marshal method available on each generated class. For example, using the MyObject object 'o' from above:

o.marshal.marshal(new FileOutputStream(new File("output.xml")));

This line of code will output the XML representation of 'o' (in this case, to a file named 'output.xml').

JAXB and Zeus

As a side note, one of the goals of Zeus is to be compatible with JAXB. The JAXB specification is not really a specification in the sense of stating a required API. Instead, it is more of a specification of functionality. Zeus seeks to provide this functionality and, at the same time, still offer unique, value-add characteristics (e.g. open source, more extensible, ease of use, no requirement for any particular built-in framework, etc.).

Data Binding in Lutris EAS

In the context of a J2EE application server, data binding can make handling deployment descriptor information fairly straightforward. All application servers will read in the various standard deployment descriptor information (web.xml, ejb-jar.xml) and application server specific deployment descriptor information (e.g., eas-web.xml and eas-ejb-jar.xml n the case of Lutris EAS) when deploying J2EE components.

Instead of manually creating classes to handle extracting the data from these descriptors, EAS takes advantage of XML data binding by using Zeus. As part of the build process, Zeus is used to generate a set of classes that provide a Java object view of the data contained in the various descriptors. The EAS deployers (e.g. EJB deployer, Web deployer) then use these classes to extract the relevant data needed by using simple accessor methods.

This set of classes provides a simple layer of abstraction that hides the fact that the data came from an XML file, and effectively shields the development team from having to use lower level APIs like SAX or DOM. While these APIs are far from impossible to use, XML data binding still speeds up development time, and automatically generated code is often preferable to hand crafted code.

Conclusion

Hopefully this article has peaked your interest in XML data binding. We've described its role and provided an example of its use in Lutris EAS. This integration with EAS is also a good example of how an open source tool can be leveraged in a commercial product.

For further details on XML data binding and the Java platform, the Zeus project, and Lutris EAS, see the Resources section below.

Resources

1. Sun's JAXB site. Read the JAXB specification and download the reference implementation.

http://java.sun.com/xml/jaxb/index.html

2. The Zeus project home page. Read the mailing list archives, download the latest binary version, and find out how to participate in the project.

http://zeus.enhydra.org

3. Lutris Technologies home page. Find out more about the company and Lutris EAS.

http://www.lutris.com