Java Tutorial - Java Scipt : CMP 2.x Entity Bean Tests and Sample Code

Java Tutorial - Java Scipt :

CMP 2.x Entity Bean Tests and Sample Code

Rather than go through the tedium of creating all the files in the last section by hand again, we’ll show you how XDoclet can simplify your life. The idea behind XDoclet is the automatic generation of support files, such as deployment descriptors, based on Javadoc comments within the source code. As we shall see, this is an eminently practical concept. Also, we will start using Ant build scripts to simplify the build process, because this is fast becoming the standard for building Java projects in any capacity, and it works like a charm. The following examples will assume that you have already installed Ant. Please refer to Chapter 4 for more information on installing these tools. There is a multitude of really good Ant build scripts, projects layouts, and other configuration-related material available. This project layout, and the Ant build script associated with it, is a stripped down version of one Stan normally uses for new projects. It has roots in the original XDoclet samples, but has evolved over the course of usage in real-world projects. As always, it is only a starting point, and each project tends to have unique requirements that warrant new tweaks and changes. The Ant build script we’ll be using will only generate the EJB .jar file. Often, the build will generate .war and .ear files. Automatic deployment, JUnit tests, and source control management round out the other favorites. You’ll find an extensive list of extra capabilities in Ant. For more details, check out their Web site at Okay, let’s get started with the basic directory structure for this simple project. The layout is as follows, where the $PROJECT_ROOT is the base directory
for this application:


Our source files will go into the src directory. The gen-src and gen-meta directories are where the Xdoclet-generated code and descriptors go, respectively. The build directory just holds temporary build classes and other miscellaneous files. The dist directory is where the final EJB .jar file is placed when the build script completes its work. Finally, the lib directory houses the .jar files that are referenced in building the code. For this project, the only file needed is jboss-j2ee.jar, which can be copied from %JBOSS_HOME%\
client\. In the project’s root directory, we have two files: build.xml and The build.xml file is a standard Ant build script. If you run Ant from the project root directory, this file will be detected and used to build the project.

 You can use the file to hold properties that can change from machine to machine. This way configuration changes are limited to the file, and the build.xml file can be used by all.

Here are the contents of the build.xml file:

<?xml version=”1.0”?>
<project name=”j2ee-app” default=”clean-build” basedir=”.”>
<!-- Grab overrides for any properties. -->
<property file=”” />
<property file=”${user.home}/” />
<!-- Default property values. -->
<property name=”project.ejb.file” value=”${}-ejb.jar” />
<property name=”project.dir” value=”.” />
<property name=”project.dist.dir” value=”${project.dir}/dist” />
<property name=”project.lib.dir” value=”${project.dir}/lib” />
<property name=”project.src.dir” value=”${project.dir}/src” />
<property name=””
value=”${project.src.dir}/java” />
<property name=”project.gen-src.dir”
value=”${project.dir}/gen-src” />
<property name=”project.gen-meta.dir”
value=”${project.dir}/gen-meta” />
<property name=”” value=”${project.dir}/build” />
<property name=””
value=”${}/classes” />
<property name=”xdoclet.lib.dir”
value=”${project.lib.dir}” />
<property name=”project.xdoclet.force” value=”false” />
<!-- These are here to test the use of properties in @tags. -->
<!-- WARNING: Allowing jBOSS to create or destroy
tables may lead to a loss of data. -->
<property name=”jboss.create_table” value=”true” />
<property name=”jboss.remove_table” value=”false” />
<!-- Set up the classpath to all jars, including those in
the project and affliated tools like xdoclet, etc. -->
<path id=”project.class.path”>
<!-- Add all jars in the lib directory. -->
<fileset dir=”${project.lib.dir}”>
<include name=”**/*.jar” />
<!-- Add all jars in the xdoclet lib directory. -->
<fileset dir=”${xdoclet.lib.dir}”>
<include name=”**/*.jar” />
<!-- Append the external classpath last. -->
<pathelement path=”${java.class.path}” />
Implementing an EJB Container 383
<!-- Init -->
<target name=”init”>
<format property=”TODAY” pattern=”yyyy-MM-dd”/>
<target name=”ejbdoclet” depends=”init”>
addedtags=”@xdoclet-generated at ${TODAY},@copyright
holder,@author author”
<!-- Includes EJBs in test.ejb package only -->
<fileset dir=”${}”>
<include name=”test/ejb/*” />
<exclude name=”test/ejb/Base*.java” />
packages=”ejb” substituteWith=”interfaces”/>
<utilobject cacheHomes=”true” includeGUID=”true”/>
<configParam name=”clientjar” value=”blah.jar”/>