Doclet is a code generation engine that enables a concept known as Attribute-Oriented Programming. To use XDoclet, a programmer adds metadata (attributes) for classes and methods within the JavaDoc for their source. XDoclet can then scour your source files for these attributes and generates artifacts that are necessary for your application. XDoclet is executed from within Jakarta Ant, using a set of custom Ant tasks.
XDoclet can be used to generate files for Web applications, EJBs, Castor, Hibernate, JDO, Struts and a number of other tools.
In an earlier article, you learned about a plugin to the Eclipse environment that lets you configure and deploy to your JBoss application server. The same plugin will invoke the Xdoclet standard to let you automatically generate Java code for enterprise applications. (Please refer to the first article for instructions on how to install the plugin: http://www.devx.com/opensource/Article/20242.)
XDoclet ships with modules for nearly all the leading Java application servers, including:
- IBM WebSphere Application Server
- BEA WebLogic
- Oracle IAS
- Macromedia JRun
What are some of the benefits of code generation with XDoclet? Here are a few:
Minimizes the number of files that a developer has to maintain.
Reduced development time because developers write less code and maintain code in fewer places
Deployment files can be generated for multiple application servers, allowing for easier vendor independence
Developers can view deployment information in the source code, while the deployment team can still use the deployment descriptor
Metadata can be regenerated and integrated with the source code
(See more DevX articles and resources about code generation in the left column.)
Big String, Small Code
Stateless session EJBs are a great way of introducing a service layer into an application, but we all know how tedious it can be to generate five or six separate files just to implement an EJB with a single method. The wonderful thing about XDoclet is that the only file that you have to manually create is the EJB "bean" class. After providing attributes in the JavaDoc, XDoclet can generate all the interfaces and deployment descriptors necessary for the EJB to run in an application server.
We wrote a stateless session EJB that does the extremely difficult task of (drum roll) capitalizing a string! We chose to invoke this EJB via a servlet so that we could showcase both the Web and EJB generation functionality provided by XDoclet. XDoclet will be used to generate a Web deployment descriptor, the EJB interfaces, and the EJB deployment descriptors.
We will not cover the packaging features of the plugin in this article, but you can refer to the plugin's documentation for more details. (See http://heanet.dl.sourceforge.net/sourceforge/jboss/Tutorial-1.2.2.pdf. Editor's Note: If the preceding link fails to load the PDF, please copy and paste the URL into a new browser instance.) You should also download our source code, which includes the XDoclet configurations, the Packaging configurations, and all the files that XDoclet generates (see the download link in the left column).
The two major classes you need to be concerned with are shown in listings 1 and 2. Listing 1 is the JavaDoc for the EJB, which has a method that capitalizes an incoming string. Listing 2 is the JavaDoc for the servlet, which simply performs a JNDI lookup of the EJB and makes a remote call to the EJB's capitalization method.
For those familiar with EJBs, you will notice that the @ejb.bean tag contains information that is usually specified in the ejb-jar.xml file (i.e., our EJB deployment descriptor).
Similar to our tag for our EJB, the XDoclet tags incorporated in the servlet help define the data that goes into the web.xml file, the Web deployment descriptor.